[Tools] Added stdout protection during dlopen
[platform/core/api/webapi-plugins.git] / src / tool / desc_gentool.cc
index 3b58e00..6166872 100644 (file)
@@ -135,6 +135,27 @@ const void* get_interface(const char* name) {
   return NULL;
 }
 
+struct stream_info {
+  int fd;
+  fpos_t pos;
+};
+struct stream_info s_info;
+
+void stdout_to_null() {
+  fflush(stdout);
+  fgetpos(stdout, &(s_info.pos));
+  s_info.fd = dup(fileno(stdout));
+  freopen("/dev/null", "w", stdout);
+}
+
+void reset_stdout() {
+  fflush(stdout);
+  dup2(s_info.fd, fileno(stdout));
+  close(s_info.fd);
+  clearerr(stdout);
+  fsetpos(stdout, &(s_info.pos));
+}
+
 int main(int argc, char* argv[]) {
   if (argc < 3) {
     std::cerr << "Need tizen crosswalk path" << std::endl;
@@ -155,6 +176,9 @@ int main(int argc, char* argv[]) {
   struct dirent** namelist;
   int num_entries = scandir(tec_path.c_str(), &namelist, NULL, alphasort);
   if (num_entries >= 0) {
+    // protect stdout for any unexpected text which can be printed by other
+    // libraries during dlopen
+    stdout_to_null();
     for (int i = 0; i < num_entries; ++i) {
       std::string fname = namelist[i]->d_name;
 
@@ -196,6 +220,8 @@ int main(int argc, char* argv[]) {
       free(namelist[i]);
     }
     free(namelist);
+    // reset stdout to previous values
+    reset_stdout();
     print_json();
   } else {
     perror("scandir");