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/core/core_impl.cpp b/sdm/libs/core/core_impl.cpp
index 1dc6fe3..7d5e7b3 100644
--- a/sdm/libs/core/core_impl.cpp
+++ b/sdm/libs/core/core_impl.cpp
@@ -186,9 +186,7 @@
 
   DisplayError error = display_base->Init();
   if (error != kErrorNone) {
-    display_base->Deinit();
     delete display_base;
-    display_base = NULL;
     return error;
   }
 
@@ -206,7 +204,6 @@
   DisplayBase *display_base = static_cast<DisplayBase *>(intf);
   display_base->Deinit();
   delete display_base;
-  display_base = NULL;
 
   return kErrorNone;
 }
diff --git a/sdm/libs/core/fb/hw_device.cpp b/sdm/libs/core/fb/hw_device.cpp
index a292835..9b6f19e 100644
--- a/sdm/libs/core/fb/hw_device.cpp
+++ b/sdm/libs/core/fb/hw_device.cpp
@@ -28,6 +28,8 @@
 */
 
 #define __STDC_FORMAT_MACROS
+
+#include <stdio.h>
 #include <ctype.h>
 #include <math.h>
 #include <fcntl.h>
@@ -40,6 +42,7 @@
 #include <linux/fb.h>
 #include <utils/constants.h>
 #include <utils/debug.h>
+#include <utils/sys.h>
 
 #include "hw_device.h"
 
@@ -50,39 +53,6 @@
 HWDevice::HWDevice(BufferSyncHandler *buffer_sync_handler)
   : fb_node_index_(-1), fb_path_("/sys/devices/virtual/graphics/fb"), hotplug_enabled_(false),
     buffer_sync_handler_(buffer_sync_handler), synchronous_commit_(false) {
-#ifndef SDM_VIRTUAL_DRIVER
-  // Pointer to actual driver interfaces.
-  ioctl_ = ::ioctl;
-  open_ = ::open;
-  close_ = ::close;
-  poll_ = ::poll;
-  pread_ = ::pread;
-  pwrite_ = ::pwrite;
-  fopen_ = ::fopen;
-  fclose_ = ::fclose;
-  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);
-
-  ioctl_ = virtual_ioctl;
-  open_ = virtual_open;
-  close_ = virtual_close;
-  poll_ = virtual_poll;
-  pread_ = virtual_pread;
-  pwrite_ = virtual_pwrite;
-  fopen_ = virtual_fopen;
-  fclose_ = virtual_fclose;
-  getline_ = virtual_getline;
-#endif
 }
 
 DisplayError HWDevice::Init() {
@@ -114,7 +84,7 @@
   event_handler_ = eventhandler;
   snprintf(device_name, sizeof(device_name), "%s%d", "/dev/graphics/fb", fb_node_index_);
 
-  device_fd_ = open_(device_name, O_RDWR);
+  device_fd_ = Sys::open_(device_name, O_RDWR);
   if (device_fd_ < 0) {
     DLOGE("open %s failed err = %d errstr = %s", device_name, errno,  strerror(errno));
     return kErrorResources;
@@ -125,7 +95,7 @@
 
 DisplayError HWDevice::Close() {
   if (device_fd_ > 0) {
-    close_(device_fd_);
+    Sys::close_(device_fd_);
   }
 
   return kErrorNone;
@@ -158,7 +128,7 @@
 DisplayError HWDevice::PowerOn() {
   DTRACE_SCOPED();
 
-  if (ioctl_(device_fd_, FBIOBLANK, FB_BLANK_UNBLANK) < 0) {
+  if (Sys::ioctl_(device_fd_, FBIOBLANK, FB_BLANK_UNBLANK) < 0) {
     IOCTL_LOGE(FB_BLANK_UNBLANK, device_type_);
     return kErrorHardware;
   }
@@ -311,7 +281,7 @@
   }
 
   mdp_commit.flags |= MDP_VALIDATE_LAYER;
-  if (ioctl_(device_fd_, MSMFB_ATOMIC_COMMIT, &mdp_disp_commit_) < 0) {
+  if (Sys::ioctl_(device_fd_, MSMFB_ATOMIC_COMMIT, &mdp_disp_commit_) < 0) {
     IOCTL_LOGE(MSMFB_ATOMIC_COMMIT, device_type_);
     DumpLayerCommit(mdp_disp_commit_);
     return kErrorHardware;
@@ -427,7 +397,7 @@
   if (synchronous_commit_) {
     mdp_commit.flags |= MDP_COMMIT_WAIT_FOR_FINISH;
   }
-  if (ioctl_(device_fd_, MSMFB_ATOMIC_COMMIT, &mdp_disp_commit_) < 0) {
+  if (Sys::ioctl_(device_fd_, MSMFB_ATOMIC_COMMIT, &mdp_disp_commit_) < 0) {
     IOCTL_LOGE(MSMFB_ATOMIC_COMMIT, device_type_);
     DumpLayerCommit(mdp_disp_commit_);
     synchronous_commit_ = false;
@@ -454,7 +424,7 @@
   DLOGI_IF(kTagDriverConfig, "retire_fence_fd %d", stack->retire_fence_fd);
   DLOGI_IF(kTagDriverConfig, "*******************************************************************");
 
-  close_(mdp_commit.release_fence);
+  Sys::close_(mdp_commit.release_fence);
 
   if (synchronous_commit_) {
     // A synchronous commit can be requested when changing the display mode so we need to update
@@ -473,7 +443,7 @@
   mdp_commit.output_layer = NULL;
 
   mdp_commit.flags &= ~MDP_VALIDATE_LAYER;
-  if (ioctl_(device_fd_, MSMFB_ATOMIC_COMMIT, &mdp_disp_commit_) < 0) {
+  if (Sys::ioctl_(device_fd_, MSMFB_ATOMIC_COMMIT, &mdp_disp_commit_) < 0) {
     IOCTL_LOGE(MSMFB_ATOMIC_COMMIT, device_type_);
     DumpLayerCommit(mdp_disp_commit_);
     return kErrorHardware;
@@ -677,16 +647,17 @@
   char stringbuffer[kMaxStringLength];
   FILE *fileptr = NULL;
   snprintf(stringbuffer, sizeof(stringbuffer), "%s%d/msm_fb_panel_info", fb_path_, device_node);
-  fileptr = fopen(stringbuffer, "r");
+  fileptr = Sys::fopen_(stringbuffer, "r");
   if (!fileptr) {
     DLOGW("Failed to open msm_fb_panel_info node device node %d", device_node);
     return;
   }
 
+  char *line = stringbuffer;
   size_t len = kMaxStringLength;
   ssize_t read;
-  char *line = NULL;
-  while ((read = getline(&line, &len, fileptr)) != -1) {
+
+  while ((read = Sys::getline_(&line, &len, fileptr)) != -1) {
     uint32_t token_count = 0;
     const uint32_t max_count = 10;
     char *tokens[max_count] = { NULL };
@@ -718,8 +689,7 @@
       }
     }
   }
-  fclose(fileptr);
-  free(line);
+  Sys::fclose_(fileptr);
   panel_info->port = GetHWDisplayPort(device_node);
   panel_info->mode = GetHWDisplayMode(device_node);
   GetSplitInfo(device_node, panel_info);
@@ -727,20 +697,22 @@
 
 HWDisplayPort HWDevice::GetHWDisplayPort(int device_node) {
   char stringbuffer[kMaxStringLength];
-  char *line = NULL;
-  size_t len = kMaxStringLength;
-  ssize_t read;
   HWDisplayPort port = kPortDefault;
 
   snprintf(stringbuffer, sizeof(stringbuffer), "%s%d/msm_fb_type", fb_path_, device_node);
-  FILE *fileptr = fopen(stringbuffer, "r");
+  FILE *fileptr = Sys::fopen_(stringbuffer, "r");
   if (!fileptr) {
     DLOGW("File not found %s", stringbuffer);
     return port;
   }
-  read = getline(&line, &len, fileptr);
+
+  char *line = stringbuffer;
+  size_t len = kMaxStringLength;
+  ssize_t read;
+
+  read = Sys::getline_(&line, &len, fileptr);
   if (read == -1) {
-    fclose(fileptr);
+    Sys::fclose_(fileptr);
     return port;
   }
   if ((strncmp(line, "mipi dsi cmd panel", strlen("mipi dsi cmd panel")) == 0)) {
@@ -758,27 +730,28 @@
   } else {
     port = kPortDefault;
   }
-  fclose(fileptr);
-  free(line);
+  Sys::fclose_(fileptr);
   return port;
 }
 
 HWDisplayMode HWDevice::GetHWDisplayMode(int device_node) {
   char stringbuffer[kMaxStringLength];
-  char *line = NULL;
-  size_t len = kMaxStringLength;
-  ssize_t read;
   HWDisplayMode mode = kModeDefault;
 
   snprintf(stringbuffer, sizeof(stringbuffer), "%s%d/msm_fb_type", fb_path_, device_node);
-  FILE *fileptr = fopen(stringbuffer, "r");
+  FILE *fileptr = Sys::fopen_(stringbuffer, "r");
   if (!fileptr) {
     DLOGW("File not found %s", stringbuffer);
     return mode;
   }
-  read = getline(&line, &len, fileptr);
+
+  char *line = stringbuffer;
+  size_t len = kMaxStringLength;
+  ssize_t read;
+
+  read = Sys::getline_(&line, &len, fileptr);
   if (read == -1) {
-    fclose(fileptr);
+    Sys::fclose_(fileptr);
     return mode;
   }
   if ((strncmp(line, "mipi dsi cmd panel", strlen("mipi dsi cmd panel")) == 0)) {
@@ -788,56 +761,58 @@
   } else {
     mode = kModeDefault;
   }
-  fclose(fileptr);
-  free(line);
+  Sys::fclose_(fileptr);
+
   return mode;
 }
 
 void HWDevice::GetSplitInfo(int device_node, HWPanelInfo *panel_info) {
   char stringbuffer[kMaxStringLength];
   FILE *fileptr = NULL;
-  size_t len = kMaxStringLength;
-  ssize_t read;
-  char *line = NULL;
   uint32_t token_count = 0;
   const uint32_t max_count = 10;
   char *tokens[max_count] = { NULL };
 
   // Split info - for MDSS Version 5 - No need to check version here
   snprintf(stringbuffer , sizeof(stringbuffer), "%s%d/msm_fb_split", fb_path_, device_node);
-  fileptr = fopen(stringbuffer, "r");
+  fileptr = Sys::fopen_(stringbuffer, "r");
   if (!fileptr) {
     DLOGW("File not found %s", stringbuffer);
     return;
   }
 
+  char *line = stringbuffer;
+  size_t len = kMaxStringLength;
+  ssize_t read;
+
   // Format "left right" space as delimiter
-  read = getline(&line, &len, fileptr);
+  read = Sys::getline_(&line, &len, fileptr);
   if (read != -1) {
     if (!ParseLine(line, tokens, max_count, &token_count)) {
       panel_info->split_info.left_split = atoi(tokens[0]);
       panel_info->split_info.right_split = atoi(tokens[1]);
     }
   }
-  fclose(fileptr);
+
+  Sys::fclose_(fileptr);
 
   // SourceSplit enabled - Get More information
   snprintf(stringbuffer , sizeof(stringbuffer), "%s%d/msm_fb_src_split_info", fb_path_,
            device_node);
-  fileptr = fopen(stringbuffer, "r");
+  fileptr = Sys::fopen_(stringbuffer, "r");
   if (!fileptr) {
     DLOGW("File not found %s", stringbuffer);
     return;
   }
 
-  read = getline(&line, &len, fileptr);
+  read = Sys::getline_(&line, &len, fileptr);
   if (read != -1) {
     if (!strncmp(line, "src_split_always", strlen("src_split_always"))) {
       panel_info->split_info.always_src_split = true;
     }
   }
-  fclose(fileptr);
-  free(line);
+
+  Sys::fclose_(fileptr);
 }
 
 int HWDevice::ParseLine(char *input, char *tokens[], const uint32_t max_token, uint32_t *count) {
@@ -863,18 +838,18 @@
   char hpdpath[kMaxStringLength];
   int hdmi_node_index = GetFBNodeIndex(kDeviceHDMI);
   snprintf(hpdpath , sizeof(hpdpath), "%s%d/hpd", fb_path_, hdmi_node_index);
-  int hpdfd = open_(hpdpath, O_RDWR, 0);
+  int hpdfd = Sys::open_(hpdpath, O_RDWR, 0);
   if (hpdfd < 0) {
     DLOGE("Open failed = %s", hpdpath);
     return kErrorHardware;
   }
   char value = enable ? '1' : '0';
-  ssize_t length = pwrite_(hpdfd, &value, 1, 0);
+  ssize_t length = Sys::pwrite_(hpdfd, &value, 1, 0);
   if (length <= 0) {
     DLOGE("Write failed 'hpd' = %d", enable);
     ret_value = false;
   }
-  close_(hpdfd);
+  Sys::close_(hpdfd);
 
   return ret_value;
 }
diff --git a/sdm/libs/core/fb/hw_device.h b/sdm/libs/core/fb/hw_device.h
index 35a1c1d..4bd0556 100644
--- a/sdm/libs/core/fb/hw_device.h
+++ b/sdm/libs/core/fb/hw_device.h
@@ -26,11 +26,8 @@
 #define __HW_DEVICE_H__
 
 #include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
 #include <linux/msm_mdp_ext.h>
 #include <linux/mdss_rotator.h>
-#include <poll.h>
 #include <pthread.h>
 
 #include "hw_interface.h"
@@ -96,17 +93,6 @@
 
   bool EnableHotPlugDetection(int enable);
 
-  // Pointers to system calls which are either mapped to actual system call or virtual driver.
-  int (*ioctl_)(int, int, ...);
-  int (*open_)(const char *, int, ...);
-  int (*close_)(int);
-  int (*poll_)(struct pollfd *, nfds_t, int);
-  ssize_t (*pread_)(int, void *, size_t, off_t);
-  ssize_t (*pwrite_)(int, const void *, size_t, off_t);
-  FILE* (*fopen_)( const char *fname, const char *mode);
-  int (*fclose_)(FILE* fileptr);
-  ssize_t (*getline_)(char **lineptr, size_t *linelen, FILE *stream);
-
   // Store the Device EventHandler - used for callback
   HWEventHandler *event_handler_;
   HWResourceInfo hw_resource_;
diff --git a/sdm/libs/core/fb/hw_hdmi.cpp b/sdm/libs/core/fb/hw_hdmi.cpp
index 6ebd392..1c2ed17 100644
--- a/sdm/libs/core/fb/hw_hdmi.cpp
+++ b/sdm/libs/core/fb/hw_hdmi.cpp
@@ -27,12 +27,15 @@
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
+#include <stdio.h>
 #include <unistd.h>
 #include <string.h>
 #include <sys/ioctl.h>
 #include <ctype.h>
-#include <utils/debug.h>
 #include <fcntl.h>
+#include <utils/debug.h>
+#include <utils/sys.h>
+
 #include "hw_hdmi.h"
 
 #define __CLASS__ "HWHDMI"
@@ -155,13 +158,13 @@
   char edid_str[256] = {'\0'};
   char edid_path[kMaxStringLength] = {'\0'};
   snprintf(edid_path, sizeof(edid_path), "%s%d/edid_modes", fb_path_, fb_node_index_);
-  int edid_file = open_(edid_path, O_RDONLY);
+  int edid_file = Sys::open_(edid_path, O_RDONLY);
   if (edid_file < 0) {
     DLOGE("EDID file open failed.");
     return -1;
   }
 
-  length = pread_(edid_file, edid_str, sizeof(edid_str)-1, 0);
+  length = Sys::pread_(edid_file, edid_str, sizeof(edid_str)-1, 0);
   if (length <= 0) {
     DLOGE("%s: edid_modes file empty");
     edid_str[0] = '\0';
@@ -172,7 +175,7 @@
     }
     edid_str[length] = '\0';
   }
-  close_(edid_file);
+  Sys::close_(edid_file);
 
   if (length > 0) {
     // Get EDID modes from the EDID string
@@ -229,7 +232,7 @@
 
   // Variable screen info
   STRUCT_VAR(fb_var_screeninfo, vscreeninfo);
-  if (ioctl_(device_fd_, FBIOGET_VSCREENINFO, &vscreeninfo) < 0) {
+  if (Sys::ioctl_(device_fd_, FBIOGET_VSCREENINFO, &vscreeninfo) < 0) {
     IOCTL_LOGE(FBIOGET_VSCREENINFO, device_type_);
     return kErrorHardware;
   }
@@ -266,7 +269,7 @@
         vscreeninfo.upper_margin, vscreeninfo.pixclock/1000000);
 
   vscreeninfo.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_ALL | FB_ACTIVATE_FORCE;
-  if (ioctl_(device_fd_, FBIOPUT_VSCREENINFO, &vscreeninfo) < 0) {
+  if (Sys::ioctl_(device_fd_, FBIOPUT_VSCREENINFO, &vscreeninfo) < 0) {
     IOCTL_LOGE(FBIOGET_VSCREENINFO, device_type_);
     return kErrorHardware;
   }
@@ -368,7 +371,7 @@
   char data[4096] = {'\0'};
 
   snprintf(data, sizeof(data), "%s%d/scan_info", fb_path_, fb_node_index_);
-  scan_info_file = open_(data, O_RDONLY);
+  scan_info_file = Sys::open_(data, O_RDONLY);
   if (scan_info_file < 0) {
     DLOGW("File '%s' not found.", data);
     return;
@@ -377,12 +380,12 @@
   memset(&data[0], 0, sizeof(data));
   len = read(scan_info_file, data, sizeof(data) - 1);
   if (len <= 0) {
-    close_(scan_info_file);
+    Sys::close_(scan_info_file);
     DLOGW("File %s%d/scan_info is empty.", fb_path_, fb_node_index_);
     return;
   }
   data[len] = '\0';
-  close_(scan_info_file);
+  Sys::close_(scan_info_file);
 
   const uint32_t scan_info_max_count = 3;
   uint32_t scan_info_count = 0;
diff --git a/sdm/libs/core/fb/hw_info.cpp b/sdm/libs/core/fb/hw_info.cpp
index 28d798a..5fcfd70 100644
--- a/sdm/libs/core/fb/hw_info.cpp
+++ b/sdm/libs/core/fb/hw_info.cpp
@@ -31,6 +31,7 @@
 #include <sys/types.h>
 #include <utils/constants.h>
 #include <utils/debug.h>
+#include <utils/sys.h>
 
 #define __CLASS__ "HWInfo"
 
@@ -88,7 +89,7 @@
   char *tokens[max_count] = { NULL };
   snprintf(stringbuffer , sizeof(stringbuffer), "%s%d/mdp/caps",
            kHWCapabilitiesPath, kHWCapabilitiesNode);
-  fileptr = fopen(stringbuffer, "rb");
+  fileptr = Sys::fopen_(stringbuffer, "r");
 
   if (!fileptr) {
     DLOGE("File '%s' not found", stringbuffer);
@@ -99,7 +100,7 @@
   ssize_t read;
   char *line = stringbuffer;
   hw_resource->hw_version = kHWMdssVersion5;
-  while ((read = getline(&line, &len, fileptr)) != -1) {
+  while ((read = Sys::getline_(&line, &len, fileptr)) != -1) {
     // parse the line and update information accordingly
     if (!ParseLine(line, tokens, max_count, &token_count)) {
       if (!strncmp(tokens[0], "hw_rev", strlen("hw_rev"))) {
@@ -161,7 +162,8 @@
       }
     }
   }
-  fclose(fileptr);
+
+  Sys::fclose_(fileptr);
 
   DLOGI("SDE Version = %d, SDE Revision = %x, RGB = %d, VIG = %d, DMA = %d, Cursor = %d",
         hw_resource->hw_version, hw_resource->hw_revision, hw_resource->num_rgb_pipe,
diff --git a/sdm/libs/core/fb/hw_primary.cpp b/sdm/libs/core/fb/hw_primary.cpp
index 5c79475..639e5d3 100644
--- a/sdm/libs/core/fb/hw_primary.cpp
+++ b/sdm/libs/core/fb/hw_primary.cpp
@@ -27,6 +27,7 @@
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
+#include <stdio.h>
 #include <unistd.h>
 #include <string.h>
 #include <pthread.h>
@@ -36,6 +37,8 @@
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <utils/debug.h>
+#include <utils/sys.h>
+
 #include "hw_primary.h"
 #include "hw_color_manager.h"
 
@@ -104,7 +107,7 @@
       snprintf(node_path, sizeof(node_path), "%s%d/%s", fb_path_, fb_node_index_,
                event_name[event]);
 
-      poll_fd.fd = open_(node_path, O_RDONLY);
+      poll_fd.fd = Sys::open_(node_path, O_RDONLY);
       if (poll_fd.fd < 0) {
         DLOGE("open failed for event=%d, error=%s", event, strerror(errno));
         error = kErrorHardware;
@@ -112,7 +115,7 @@
       }
 
       // Read once on all fds to clear data on all fds.
-      pread_(poll_fd.fd, data , kMaxStringLength, 0);
+      Sys::pread_(poll_fd.fd, data , kMaxStringLength, 0);
       poll_fd.events = POLLPRI | POLLERR;
     }
   }
@@ -135,7 +138,7 @@
   for (int event = 0; event < kNumDisplayEvents; event++) {
     int &fd = poll_fds_[event].fd;
     if (fd >= 0) {
-      close_(fd);
+      Sys::close_(fd);
     }
   }
 
@@ -147,7 +150,7 @@
   pthread_join(event_thread_, NULL);
 
   for (int event = 0; event < kNumDisplayEvents; event++) {
-    close_(poll_fds_[event].fd);
+    Sys::close_(poll_fds_[event].fd);
   }
 
   return kErrorNone;
@@ -187,7 +190,7 @@
   // Variable screen info
   STRUCT_VAR(fb_var_screeninfo, var_screeninfo);
 
-  if (ioctl_(device_fd_, FBIOGET_VSCREENINFO, &var_screeninfo) < 0) {
+  if (Sys::ioctl_(device_fd_, FBIOGET_VSCREENINFO, &var_screeninfo) < 0) {
     IOCTL_LOGE(FBIOGET_VSCREENINFO, device_type_);
     return kErrorHardware;
   }
@@ -195,7 +198,7 @@
   // Frame rate
   STRUCT_VAR(msmfb_metadata, meta_data);
   meta_data.op = metadata_op_frame_rate;
-  if (ioctl_(device_fd_, MSMFB_METADATA_GET, &meta_data) < 0) {
+  if (Sys::ioctl_(device_fd_, MSMFB_METADATA_GET, &meta_data) < 0) {
     IOCTL_LOGE(MSMFB_METADATA_GET, device_type_);
     return kErrorHardware;
   }
@@ -241,7 +244,7 @@
 
   snprintf(node_path, sizeof(node_path), "%s%d/dynamic_fps", fb_path_, fb_node_index_);
 
-  int fd = open_(node_path, O_WRONLY);
+  int fd = Sys::open_(node_path, O_WRONLY);
   if (fd < 0) {
     DLOGE("Failed to open %s with error %s", node_path, strerror(errno));
     return kErrorFileDescriptor;
@@ -249,13 +252,13 @@
 
   char refresh_rate_string[kMaxStringLength];
   snprintf(refresh_rate_string, sizeof(refresh_rate_string), "%d", refresh_rate);
-  ssize_t len = pwrite_(fd, refresh_rate_string, strlen(refresh_rate_string), 0);
+  ssize_t len = Sys::pwrite_(fd, refresh_rate_string, strlen(refresh_rate_string), 0);
   if (len < 0) {
     DLOGE("Failed to write %d with error %s", refresh_rate, strerror(errno));
-    close_(fd);
+    Sys::close_(fd);
     return kErrorUndefined;
   }
-  close_(fd);
+  Sys::close_(fd);
 
   config_changed_ = true;
   synchronous_commit_ = true;
@@ -272,7 +275,7 @@
 }
 
 DisplayError HWPrimary::PowerOff() {
-  if (ioctl_(device_fd_, FBIOBLANK, FB_BLANK_POWERDOWN) < 0) {
+  if (Sys::ioctl_(device_fd_, FBIOBLANK, FB_BLANK_POWERDOWN) < 0) {
     IOCTL_LOGE(FB_BLANK_POWERDOWN, device_type_);
     return kErrorHardware;
   }
@@ -281,7 +284,7 @@
 }
 
 DisplayError HWPrimary::Doze() {
-  if (ioctl_(device_fd_, FBIOBLANK, FB_BLANK_NORMAL) < 0) {
+  if (Sys::ioctl_(device_fd_, FBIOBLANK, FB_BLANK_NORMAL) < 0) {
     IOCTL_LOGE(FB_BLANK_NORMAL, device_type_);
     return kErrorHardware;
   }
@@ -290,7 +293,7 @@
 }
 
 DisplayError HWPrimary::DozeSuspend() {
-  if (ioctl_(device_fd_, FBIOBLANK, FB_BLANK_VSYNC_SUSPEND) < 0) {
+  if (Sys::ioctl_(device_fd_, FBIOBLANK, FB_BLANK_VSYNC_SUSPEND) < 0) {
     IOCTL_LOGE(FB_BLANK_VSYNC_SUSPEND, device_type_);
     return kErrorHardware;
   }
@@ -379,7 +382,7 @@
                                                     &HWPrimary::HandleThermal };
 
   while (!exit_threads_) {
-    int error = poll_(poll_fds_, kNumDisplayEvents, -1);
+    int error = Sys::poll_(poll_fds_, kNumDisplayEvents, -1);
     if (error < 0) {
       DLOGW("poll failed. error = %s", strerror(errno));
       continue;
@@ -388,7 +391,7 @@
       pollfd &poll_fd = poll_fds_[event];
 
       if (poll_fd.revents & POLLPRI) {
-        ssize_t length = pread_(poll_fd.fd, data, kMaxStringLength, 0);
+        ssize_t length = Sys::pread_(poll_fd.fd, data, kMaxStringLength, 0);
         if (length < 0) {
           // If the read was interrupted - it is not a fatal error, just continue.
           DLOGW("pread failed. event = %d, error = %s", event, strerror(errno));
@@ -440,7 +443,7 @@
   snprintf(node_path, sizeof(node_path), "%s%d/idle_time", fb_path_, fb_node_index_);
 
   // Open a sysfs node to send the timeout value to driver.
-  int fd = open_(node_path, O_WRONLY);
+  int fd = Sys::open_(node_path, O_WRONLY);
   if (fd < 0) {
     DLOGE("Unable to open %s, node %s", node_path, strerror(errno));
     return;
@@ -450,19 +453,19 @@
   snprintf(timeout_string, sizeof(timeout_string), "%d", timeout_ms);
 
   // Notify driver about the timeout value
-  ssize_t length = pwrite_(fd, timeout_string, strlen(timeout_string), 0);
+  ssize_t length = Sys::pwrite_(fd, timeout_string, strlen(timeout_string), 0);
   if (length < -1) {
     DLOGE("Unable to write into %s, node %s", node_path, strerror(errno));
   }
 
-  close_(fd);
+  Sys::close_(fd);
 }
 
 DisplayError HWPrimary::SetVSyncState(bool enable) {
   DTRACE_SCOPED();
 
   int vsync_on = enable ? 1 : 0;
-  if (ioctl_(device_fd_, MSMFB_OVERLAY_VSYNC_CTRL, &vsync_on) < 0) {
+  if (Sys::ioctl_(device_fd_, MSMFB_OVERLAY_VSYNC_CTRL, &vsync_on) < 0) {
     IOCTL_LOGE(MSMFB_OVERLAY_VSYNC_CTRL, device_type_);
     return kErrorHardware;
   }
@@ -486,7 +489,7 @@
     return kErrorParameters;
   }
 
-  if (ioctl_(device_fd_, MSMFB_LPM_ENABLE, &mode) < 0) {
+  if (Sys::ioctl_(device_fd_, MSMFB_LPM_ENABLE, &mode) < 0) {
     IOCTL_LOGE(MSMFB_LPM_ENABLE, device_type_);
     return kErrorHardware;
   }
@@ -506,7 +509,7 @@
   for (int i(0); i < kMaxNumPPFeatures; i++) {
     version.pp_feature = feature_id_mapping[i];
 
-    if (ioctl_(device_fd_,  MSMFB_MDP_PP_GET_FEATURE_VERSION, &version) < 0) {
+    if (Sys::ioctl_(device_fd_,  MSMFB_MDP_PP_GET_FEATURE_VERSION, &version) < 0) {
       IOCTL_LOGE(MSMFB_MDP_PP_GET_FEATURE_VERSION, device_type_);
       return kErrorHardware;
     }
@@ -533,7 +536,7 @@
       if ((feature->feature_id_ < kMaxNumPPFeatures)) {
 
         HWColorManager::SetFeature[feature->feature_id_](*feature, &kernel_params);
-        if (ioctl_(device_fd_, MSMFB_MDP_PP, &kernel_params) < 0) {
+        if (Sys::ioctl_(device_fd_, MSMFB_MDP_PP, &kernel_params) < 0) {
           IOCTL_LOGE(MSMFB_MDP_PP, device_type_);
 
           feature_list.Reset();
diff --git a/sdm/libs/core/fb/hw_primary.h b/sdm/libs/core/fb/hw_primary.h
index 928f88b..f4f1521 100644
--- a/sdm/libs/core/fb/hw_primary.h
+++ b/sdm/libs/core/fb/hw_primary.h
@@ -25,6 +25,8 @@
 #ifndef __HW_PRIMARY_H__
 #define __HW_PRIMARY_H__
 
+#include <sys/poll.h>
+
 #include "hw_device.h"
 #include "hw_primary_interface.h"
 
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
+