sdm: Add Sys::* types for system call wrappers.

- Add Sys class which encapsulates all system calls.
- Fix crash if display initialization failed.

Change-Id: I622aa7f415bf10a6c65a730cbe0dcf2426bae963
diff --git a/sdm/libs/utils/Android.mk b/sdm/libs/utils/Android.mk
index 237b48d..dd3a0d1 100644
--- a/sdm/libs/utils/Android.mk
+++ b/sdm/libs/utils/Android.mk
@@ -8,6 +8,8 @@
                                  -Wall -Werror -std=c++11 -fcolor-diagnostics\
                                  -DLOG_TAG=\"SDM\"
 LOCAL_CLANG                   := true
-LOCAL_SRC_FILES               := debug.cpp rect.cpp
+LOCAL_SRC_FILES               := debug.cpp \
+                                 rect.cpp \
+                                 sys.cpp
 
 include $(BUILD_SHARED_LIBRARY)
diff --git a/sdm/libs/utils/sys.cpp b/sdm/libs/utils/sys.cpp
new file mode 100644
index 0000000..e54436d
--- /dev/null
+++ b/sdm/libs/utils/sys.cpp
@@ -0,0 +1,77 @@
+/*
+* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following conditions are
+* met:
+*     * Redistributions of source code must retain the above copyright
+*       notice, this list of conditions and the following disclaimer.
+*     * Redistributions in binary form must reproduce the above
+*       copyright notice, this list of conditions and the following
+*       disclaimer in the documentation and/or other materials provided
+*       with the distribution.
+*     * Neither the name of The Linux Foundation nor the names of its
+*       contributors may be used to endorse or promote products derived
+*       from this software without specific prior written permission.
+*
+* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+* ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#include <utils/sys.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+
+#define __CLASS__ "Sys"
+
+namespace sdm {
+
+#ifndef SDM_VIRTUAL_DRIVER
+
+// Pointer to actual driver interfaces.
+Sys::ioctl Sys::ioctl_ = ::ioctl;
+Sys::open Sys::open_ = ::open;
+Sys::close Sys::close_ = ::close;
+Sys::poll Sys::poll_ = ::poll;
+Sys::pread Sys::pread_ = ::pread;
+Sys::pwrite Sys::pwrite_ = ::pwrite;
+Sys::fopen Sys::fopen_ = ::fopen;
+Sys::fclose Sys::fclose_ = ::fclose;
+Sys::getline Sys::getline_ = ::getline;
+
+#else
+
+// Point to virtual driver interfaces.
+extern int virtual_ioctl(int fd, int cmd, ...);
+extern int virtual_open(const char *file_name, int access, ...);
+extern int virtual_close(int fd);
+extern int virtual_poll(struct pollfd *fds,  nfds_t num, int timeout);
+extern ssize_t virtual_pread(int fd, void *data, size_t count, off_t offset);
+extern ssize_t virtual_pwrite(int fd, const void *data, size_t count, off_t offset);
+extern FILE* virtual_fopen(const char *fname, const char *mode);
+extern int virtual_fclose(FILE* fileptr);
+extern ssize_t virtual_getline(char **lineptr, size_t *linelen, FILE *stream);
+
+Sys::ioctl Sys::ioctl_ = virtual_ioctl;
+Sys::open Sys::open_ = virtual_open;
+Sys::close Sys::close_ = virtual_close;
+Sys::poll Sys::poll_ = virtual_poll;
+Sys::pread Sys::pread_ = virtual_pread;
+Sys::pwrite Sys::pwrite_ = virtual_pwrite;
+Sys::fopen Sys::fopen_ = virtual_fopen;
+Sys::fclose Sys::fclose_ = virtual_fclose;
+Sys::getline Sys::getline_ = virtual_getline;
+
+#endif  // SDM_VIRTUAL_DRIVER
+
+}  // namespace sdm
+