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