[glib: 8/12] glib/win32: replace reopen_noninherited()




commit c43472060bc1a64715f1d2f90a40afb8f31756ba
Author: Marc-André Lureau <marcandre lureau redhat com>
Date:   Thu Apr 7 17:56:01 2022 +0400

    glib/win32: replace reopen_noninherited()
    
    Signed-off-by: Marc-André Lureau <marcandre lureau redhat com>

 glib/gspawn-win32-helper.c | 10 +++++++---
 glib/gspawn-win32.c        | 23 ++++++++---------------
 2 files changed, 15 insertions(+), 18 deletions(-)
---
diff --git a/glib/gspawn-win32-helper.c b/glib/gspawn-win32-helper.c
index 4729ee46ec..ee68f8eb74 100644
--- a/glib/gspawn-win32-helper.c
+++ b/glib/gspawn-win32-helper.c
@@ -39,6 +39,7 @@
 #include "glib.h"
 #define GSPAWN_HELPER
 #include "gspawn-win32.c"      /* For shared definitions */
+#include "glib/glib-private.h"
 
 
 static void
@@ -295,7 +296,8 @@ main (int ignored_argc, char **ignored_argv)
   /* GUI application do not necessarily have a stderr */
   if (_fileno (stderr) == 2)
     {
-      saved_stderr_fd = reopen_noninherited (dup (2), _O_WRONLY);
+      saved_stderr_fd = GLIB_PRIVATE_CALL (g_win32_reopen_noninherited) (
+        dup (2), _O_WRONLY, NULL);
       if (saved_stderr_fd == -1)
         write_err_and_exit (child_err_report_fd, CHILD_DUP_FAILED);
     }
@@ -403,8 +405,10 @@ main (int ignored_argc, char **ignored_argv)
   /* We don't want our child to inherit the error report and
    * helper sync fds.
    */
-  child_err_report_fd = reopen_noninherited (child_err_report_fd, _O_WRONLY);
-  helper_sync_fd = reopen_noninherited (helper_sync_fd, _O_RDONLY);
+  child_err_report_fd = GLIB_PRIVATE_CALL (g_win32_reopen_noninherited) (
+    child_err_report_fd, _O_WRONLY, NULL);
+  helper_sync_fd = GLIB_PRIVATE_CALL (g_win32_reopen_noninherited) (
+    helper_sync_fd, _O_RDONLY, NULL);
   if (helper_sync_fd == -1)
     write_err_and_exit (child_err_report_fd, CHILD_DUP_FAILED);
 
diff --git a/glib/gspawn-win32.c b/glib/gspawn-win32.c
index 29bdf4d79b..ffda37f539 100644
--- a/glib/gspawn-win32.c
+++ b/glib/gspawn-win32.c
@@ -107,19 +107,6 @@ enum {
   ARG_COUNT = ARG_PROGRAM
 };
 
-static int
-reopen_noninherited (int fd,
-                    int mode)
-{
-  HANDLE filehandle;
-
-  DuplicateHandle (GetCurrentProcess (), (LPHANDLE) _get_osfhandle (fd),
-                  GetCurrentProcess (), &filehandle,
-                  0, FALSE, DUPLICATE_SAME_ACCESS);
-  close (fd);
-  return _open_osfhandle ((gintptr) filehandle, mode | _O_NOINHERIT);
-}
-
 #ifndef GSPAWN_HELPER
 
 #ifdef _WIN64
@@ -684,7 +671,10 @@ fork_exec (gint                  *exit_status,
    * helper process, and the started actual user process. As such that
    * shouldn't harm, but it is unnecessary.
    */
-  child_err_report_pipe[0] = reopen_noninherited (child_err_report_pipe[0], _O_RDONLY);
+  child_err_report_pipe[0] = g_win32_reopen_noninherited (
+    child_err_report_pipe[0], _O_RDONLY, error);
+  if (child_err_report_pipe[0] == -1)
+      goto cleanup_and_fail;
 
   if (flags & G_SPAWN_FILE_AND_ARGV_ZERO)
     {
@@ -703,7 +693,10 @@ fork_exec (gint                  *exit_status,
    * process won't read but won't get any EOF either, as it has the
    * write end open itself.
    */
-  helper_sync_pipe[1] = reopen_noninherited (helper_sync_pipe[1], _O_WRONLY);
+  helper_sync_pipe[1] = g_win32_reopen_noninherited (
+    helper_sync_pipe[1], _O_WRONLY, error);
+  if (helper_sync_pipe[1] == -1)
+      goto cleanup_and_fail;
 
   if (stdin_fd != -1)
     {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]