Modify CloseAllFds method 09/294109/2
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 13 Jun 2023 07:50:16 +0000 (07:50 +0000)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 13 Jun 2023 08:07:52 +0000 (08:07 +0000)
While searching the fd directory using std::filesystem::directory_iterator,
we can close the file descriptor of the directory_iterator. In this case,
the std::filesystem::filesystem_error exception will be thrown.
To avoid throwing the exception, the method uses std::vector to store
file descriptors. And then, the method closes all file descriptors using
the vector.

Change-Id: I281ba065ec1a8b90d90791a31f4abe3e4724fad3
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/lib/launchpad-glib/util.cc

index 3db226a0ef6e250b2ba6bda4d8caa98c03a2c2c3..1ba7970fac7b8c15ebfec892595a72e259cc28db 100644 (file)
@@ -468,6 +468,7 @@ void Util::CloseAllFds() {
   if (aul_listen_fd != nullptr)
     aul_fd = atoi(aul_listen_fd);
 
+  std::vector<int> fds;
   try {
     fs::path proc_path("/proc/self/fd");
     for (const auto& entry : fs::directory_iterator(proc_path)) {
@@ -478,8 +479,11 @@ void Util::CloseAllFds() {
       if (fd < 3 || fd == aul_fd)
         continue;
 
-      close(fd);
+      fds.push_back(fd);
     }
+
+    for (auto fd : fds)
+      close(fd);
   } catch (const fs::filesystem_error& e) {
     _E("Execption occurs. error(%s)", e.what());
   }