summarylogtreecommitdiffstats
path: root/0001-Revert-linux-spawni.c-simplify-error-reporting-to-pa.patch
blob: 6a897db659f51c7fda334d4f9eed11ae3c86d09c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
From ac8a2ae80345fbd494b3e459014da3099d0af678 Mon Sep 17 00:00:00 2001
From: Patrick Stewart <patrick@rfcreations.com>
Date: Tue, 12 Sep 2017 20:35:51 +0100
Subject: [PATCH] Revert "linux: spawni.c: simplify error reporting to parent"

This reverts commit 4b4d4056bb154603f36c6f8845757c1012758158.
---
 sysdeps/unix/sysv/linux/spawni.c | 72 ++++++++++++++++++++++++++--------------
 1 file changed, 47 insertions(+), 25 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/spawni.c b/sysdeps/unix/sysv/linux/spawni.c
index c56f894a82..29083e0998 100644
--- a/sysdeps/unix/sysv/linux/spawni.c
+++ b/sysdeps/unix/sysv/linux/spawni.c
@@ -17,7 +17,6 @@
    <http://www.gnu.org/licenses/>.  */
 
 #include <spawn.h>
-#include <assert.h>
 #include <fcntl.h>
 #include <paths.h>
 #include <string.h>
@@ -45,12 +44,11 @@
    3. Child must synchronize with parent to enforce 2. and to possible
       return execv issues.
 
-   The first issue is solved by blocking all signals in child, even
-   the NPTL-internal ones (SIGCANCEL and SIGSETXID).  The second and
-   third issue is done by a stack allocation in parent, and by using a
-   field in struct spawn_args where the child can write an error
-   code. CLONE_VFORK ensures that the parent does not run until the
-   child has either exec'ed successfully or exited.  */
+   The first issue is solved by blocking all signals in child, even the
+   NPTL-internal ones (SIGCANCEL and SIGSETXID).  The second and third issue
+   is done by a stack allocation in parent and a synchronization with using
+   a pipe or waitpid (in case or error).  The pipe has the advantage of
+   allowing the child the communicate an exec error.  */
 
 
 /* The Unix standard contains a long explanation of the way to signal
@@ -78,6 +76,7 @@
 
 struct posix_spawn_args
 {
+  int pipe[2];
   sigset_t oldmask;
   const char *file;
   int (*exec) (const char *, char *const *, char *const *);
@@ -87,7 +86,6 @@ struct posix_spawn_args
   ptrdiff_t argc;
   char *const *envp;
   int xflags;
-  int err;
 };
 
 /* Older version requires that shell script without shebang definition
@@ -124,6 +122,10 @@ __spawni_child (void *arguments)
   struct posix_spawn_args *args = arguments;
   const posix_spawnattr_t *restrict attr = args->attr;
   const posix_spawn_file_actions_t *file_actions = args->fa;
+  int p = args->pipe[1];
+  int ret;
+
+  close_not_cancel (args->pipe[0]);
 
   /* The child must ensure that no signal handler are enabled because it shared
      memory with parent, so the signal disposition must be either SIG_DFL or
@@ -202,6 +204,17 @@ __spawni_child (void *arguments)
 	{
 	  struct __spawn_action *action = &file_actions->__actions[cnt];
 
+	  /* Dup the pipe fd onto an unoccupied one to avoid any file
+	     operation to clobber it.  */
+	  if ((action->action.close_action.fd == p)
+	      || (action->action.open_action.fd == p)
+	      || (action->action.dup2_action.fd == p))
+	    {
+	      if ((ret = __dup (p)) < 0)
+		goto fail;
+	      p = ret;
+	    }
+
 	  switch (action->tag)
 	    {
 	    case spawn_do_close:
@@ -268,7 +281,6 @@ __spawni_child (void *arguments)
   __sigprocmask (SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
 		 ? &attr->__ss : &args->oldmask, 0);
 
-  args->err = 0;
   args->exec (args->file, args->argv, args->envp);
 
   /* This is compatibility function required to enable posix_spawn run
@@ -276,13 +288,14 @@ __spawni_child (void *arguments)
      (2.15).  */
   maybe_script_execute (args);
 
+  ret = -errno;
+
 fail:
-  /* errno should have an appropriate non-zero value; otherwise,
-     there's a bug in glibc or the kernel.  For lack of an error code
-     (EINTERNALBUG) describing that, use ECHILD.  Another option would
-     be to set args->err to some negative sentinel and have the parent
-     abort(), but that seems needlessly harsh.  */
-  args->err = errno ? : ECHILD;
+  /* Since sizeof errno < PIPE_BUF, the write is atomic. */
+  ret = -ret;
+  if (ret)
+    while (write_not_cancel (p, &ret, sizeof ret) < 0)
+      continue;
   _exit (SPAWN_ERROR);
 }
 
@@ -299,6 +312,9 @@ __spawnix (pid_t * pid, const char *file,
   struct posix_spawn_args args;
   int ec;
 
+  if (__pipe2 (args.pipe, O_CLOEXEC))
+    return errno;
+
   /* To avoid imposing hard limits on posix_spawn{p} the total number of
      arguments is first calculated to allocate a mmap to hold all possible
      values.  */
@@ -330,16 +346,17 @@ __spawnix (pid_t * pid, const char *file,
   void *stack = __mmap (NULL, stack_size, prot,
 			MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
   if (__glibc_unlikely (stack == MAP_FAILED))
-    return errno;
+    {
+      close_not_cancel (args.pipe[0]);
+      close_not_cancel (args.pipe[1]);
+      return errno;
+    }
 
   /* Disable asynchronous cancellation.  */
   int state;
   __libc_ptf_call (__pthread_setcancelstate,
                    (PTHREAD_CANCEL_DISABLE, &state), 0);
 
-  /* Child must set args.err to something non-negative - we rely on
-     the parent and child sharing VM.  */
-  args.err = -1;
   args.file = file;
   args.exec = exec;
   args.fa = file_actions;
@@ -353,8 +370,9 @@ __spawnix (pid_t * pid, const char *file,
 
   /* The clone flags used will create a new child that will run in the same
      memory space (CLONE_VM) and the execution of calling thread will be
-     suspend until the child calls execve or _exit.
-
+     suspend until the child calls execve or _exit.  These condition as
+     signal below either by pipe write (_exit with SPAWN_ERROR) or
+     a successful execve.
      Also since the calling thread execution will be suspend, there is not
      need for CLONE_SETTLS.  Although parent and child share the same TLS
      namespace, there will be no concurrent access for TLS variables (errno
@@ -362,18 +380,22 @@ __spawnix (pid_t * pid, const char *file,
   new_pid = CLONE (__spawni_child, STACK (stack, stack_size), stack_size,
 		   CLONE_VM | CLONE_VFORK | SIGCHLD, &args);
 
+  close_not_cancel (args.pipe[1]);
+
   if (new_pid > 0)
     {
-      ec = args.err;
-      assert (ec >= 0);
-      if (ec != 0)
-	  __waitpid (new_pid, NULL, 0);
+      if (__read (args.pipe[0], &ec, sizeof ec) != sizeof ec)
+	ec = 0;
+      else
+	__waitpid (new_pid, NULL, 0);
     }
   else
     ec = -new_pid;
 
   __munmap (stack, stack_size);
 
+  close_not_cancel (args.pipe[0]);
+
   if ((ec == 0) && (pid != NULL))
     *pid = new_pid;
 
-- 
2.14.1