• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1From d9ba6150909818beb05573f54f26232063492c5b Mon Sep 17 00:00:00 2001
2From: Emmanuel Fleury <emmanuel.fleury@gmail.com>
3Date: Mon, 1 Aug 2022 19:05:14 +0200
4Subject: [PATCH] Handling collision between standard i/o file descriptors and
5 newly created ones
6
7Though unlikely to happen, it may happen that newly created file
8descriptor take the value 0 (stdin), 1 (stdout) or 2 (stderr) if one
9of the standard ones have been dismissed in between. So, it may
10confuse the program if it is unaware of this change.
11
12The point of this patch is to avoid a reasign of standard file
13descriptors on newly created ones.
14
15Closes issue #16
16
17Conflict:NA
18Reference:https://gitlab.gnome.org/GNOME/glib/-/commit/d9ba6150909818beb05573f54f26232063492c5b
19
20---
21 glib/glib-unix.c | 24 ++++++++++++++++++++++++
22 1 file changed, 24 insertions(+)
23
24diff --git a/glib/glib-unix.c b/glib/glib-unix.c
25index d2dea10ef0..d67b8a357a 100644
26--- a/glib/glib-unix.c
27+++ b/glib/glib-unix.c
28@@ -108,6 +108,17 @@ g_unix_open_pipe (int     *fds,
29     ecode = pipe2 (fds, pipe2_flags);
30     if (ecode == -1 && errno != ENOSYS)
31       return g_unix_set_error_from_errno (error, errno);
32+    /* Don't reassign pipes to stdin, stdout, stderr if closed meanwhile */
33+    else if (fds[0] < 3 || fds[1] < 3)
34+      {
35+        int old_fds[2] = { fds[0], fds[1] };
36+        gboolean result = g_unix_open_pipe (fds, flags, error);
37+        close (old_fds[0]);
38+        close (old_fds[1]);
39+
40+        if (!result)
41+          g_unix_set_error_from_errno (error, errno);
42+      }
43     else if (ecode == 0)
44       return TRUE;
45     /* Fall through on -ENOSYS, we must be running on an old kernel */
46@@ -116,6 +127,19 @@ g_unix_open_pipe (int     *fds,
47   ecode = pipe (fds);
48   if (ecode == -1)
49     return g_unix_set_error_from_errno (error, errno);
50+  /* Don't reassign pipes to stdin, stdout, stderr if closed meanwhile */
51+  else if (fds[0] < 3 || fds[1] < 3)
52+    {
53+      int old_fds[2] = { fds[0], fds[1] };
54+      gboolean result = g_unix_open_pipe (fds, flags, error);
55+      close (old_fds[0]);
56+      close (old_fds[1]);
57+
58+      if (!result)
59+        g_unix_set_error_from_errno (error, errno);
60+
61+      return result;
62+    }
63
64   if (flags == 0)
65     return TRUE;
66--
67GitLab
68
69