summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarco Trevisan <mail@3v1n0.net>2022-09-19 14:35:45 +0000
committerMarco Trevisan <mail@3v1n0.net>2022-09-19 14:35:45 +0000
commit7e9625a79b8825fa595629b4aa1eafa8b091acc2 (patch)
tree992691b397c7494262fda8802ddbb55d240dfa63
parent22f0522c1082883353e4d4379a4ffe6c74c3ef30 (diff)
parentd9ba6150909818beb05573f54f26232063492c5b (diff)
downloadglib-7e9625a79b8825fa595629b4aa1eafa8b091acc2.tar.gz
Merge branch 'handling_collision_over_standard_file_descriptors' into 'main'
Handling collision between standard i/o file descriptors and newly created ones Closes #16 See merge request GNOME/glib!2846
-rw-r--r--glib/glib-unix.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/glib/glib-unix.c b/glib/glib-unix.c
index bc152d766..4710c5116 100644
--- a/glib/glib-unix.c
+++ b/glib/glib-unix.c
@@ -108,6 +108,17 @@ g_unix_open_pipe (int *fds,
ecode = pipe2 (fds, pipe2_flags);
if (ecode == -1 && errno != ENOSYS)
return g_unix_set_error_from_errno (error, errno);
+ /* Don't reassign pipes to stdin, stdout, stderr if closed meanwhile */
+ else if (fds[0] < 3 || fds[1] < 3)
+ {
+ int old_fds[2] = { fds[0], fds[1] };
+ gboolean result = g_unix_open_pipe (fds, flags, error);
+ close (old_fds[0]);
+ close (old_fds[1]);
+
+ if (!result)
+ g_unix_set_error_from_errno (error, errno);
+ }
else if (ecode == 0)
return TRUE;
/* Fall through on -ENOSYS, we must be running on an old kernel */
@@ -116,6 +127,19 @@ g_unix_open_pipe (int *fds,
ecode = pipe (fds);
if (ecode == -1)
return g_unix_set_error_from_errno (error, errno);
+ /* Don't reassign pipes to stdin, stdout, stderr if closed meanwhile */
+ else if (fds[0] < 3 || fds[1] < 3)
+ {
+ int old_fds[2] = { fds[0], fds[1] };
+ gboolean result = g_unix_open_pipe (fds, flags, error);
+ close (old_fds[0]);
+ close (old_fds[1]);
+
+ if (!result)
+ g_unix_set_error_from_errno (error, errno);
+
+ return result;
+ }
if (flags == 0)
return TRUE;