From: Piotr Kosko/Tizen API (PLT) /SRPOL/Engineer/Samsung Electronics Date: Fri, 25 Aug 2023 11:32:02 +0000 (+0200) Subject: [Tools] Added stdout protection during dlopen X-Git-Tag: accepted/tizen/unified/20231004.100248~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F47%2F297847%2F2;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Tools] Added stdout protection during dlopen [Issue] During dlopen libraries loads other libraries. If any library in the chain generates some output to stdout, it corrupts the json file. To prevent this problem, we add stdout redirect during dlopen operation. [Verification] Code compiles without errors; plugins.json file is properly generated. Change-Id: Ic46e5f1b8d90b2de0ffe1065f2fffaa75127de19 --- diff --git a/src/tool/desc_gentool.cc b/src/tool/desc_gentool.cc index 3b58e003..61668721 100644 --- a/src/tool/desc_gentool.cc +++ b/src/tool/desc_gentool.cc @@ -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");