summaryrefslogtreecommitdiff
path: root/pkg/binutils/patch/0001-libiberty-Implement-pex_unix_exec_child-using-posix_.patch
blob: 9bfd7b99d9c67e0f9036bb44b45045235a4ebd64 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
From 8e578ea68fa570aa46f04d4cf1c593ac11732bdf Mon Sep 17 00:00:00 2001
From: Michael Forney <mforney@mforney.org>
Date: Wed, 17 Apr 2019 16:44:05 -0700
Subject: [PATCH] libiberty: Implement pex_unix_exec_child using posix_spawn

---
 libiberty/config.in    |  6 +++
 libiberty/configure    | 10 ++---
 libiberty/configure.ac | 10 ++---
 libiberty/pex-unix.c   | 95 +++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 110 insertions(+), 11 deletions(-)

diff --git a/libiberty/config.in b/libiberty/config.in
index f7052b5d958..ce79a9e77f2 100644
--- a/libiberty/config.in
+++ b/libiberty/config.in
@@ -198,6 +198,9 @@
 /* Define to 1 if you have the `pipe2' function. */
 #undef HAVE_PIPE2
 
+/* Define to 1 if you have the `posix_spawn' function. */
+#undef HAVE_POSIX_SPAWN
+
 /* Define to 1 if you have the <process.h> header file. */
 #undef HAVE_PROCESS_H
 
@@ -249,6 +252,9 @@
 /* Define to 1 if you have the `spawnvpe' function. */
 #undef HAVE_SPAWNVPE
 
+/* Define to 1 if you have the <spawn.h> header file. */
+#undef HAVE_SPAWN_H
+
 /* Define to 1 if you have the <stdint.h> header file. */
 #undef HAVE_STDINT_H
 
diff --git a/libiberty/configure b/libiberty/configure
index 306c07bd37e..6f85b1fd529 100755
--- a/libiberty/configure
+++ b/libiberty/configure
@@ -5652,7 +5652,7 @@ host_makefile_frag=${frag}
 # It's OK to check for header files.  Although the compiler may not be
 # able to link anything, it had better be able to at least compile
 # something.
-for ac_header in sys/file.h sys/param.h limits.h stdlib.h malloc.h string.h unistd.h strings.h sys/time.h time.h sys/resource.h sys/stat.h sys/mman.h fcntl.h alloca.h sys/pstat.h sys/sysmp.h sys/sysinfo.h machine/hal_sysinfo.h sys/table.h sys/sysctl.h sys/systemcfg.h stdint.h stdio_ext.h process.h sys/prctl.h
+for ac_header in sys/file.h sys/param.h limits.h stdlib.h malloc.h string.h unistd.h strings.h sys/time.h time.h sys/resource.h sys/stat.h sys/mman.h fcntl.h alloca.h sys/pstat.h sys/sysmp.h sys/sysinfo.h machine/hal_sysinfo.h sys/table.h sys/sysctl.h sys/systemcfg.h stdint.h stdio_ext.h process.h sys/prctl.h spawn.h
 do :
   as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
 ac_fn_c_check_header_preproc "$LINENO" "$ac_header" "$as_ac_Header"
@@ -6161,9 +6161,9 @@ funcs="$funcs setproctitle"
 vars="sys_errlist sys_nerr sys_siglist"
 
 checkfuncs="__fsetlocking canonicalize_file_name dup3 getrlimit getrusage \
- getsysinfo gettimeofday on_exit pipe2 psignal pstat_getdynamic pstat_getstatic \
- realpath setrlimit spawnve spawnvpe strerror strsignal sysconf sysctl \
- sysmp table times wait3 wait4"
+ getsysinfo gettimeofday on_exit pipe2 posix_spawn psignal pstat_getdynamic \
+ pstat_getstatic realpath setrlimit spawnve spawnvpe strerror strsignal \
+ sysconf sysctl sysmp table times wait3 wait4"
 
 # Darwin has sbrk, but it is deprecated and that produces build-time warnings
 # so do not check for it.
@@ -6184,7 +6184,7 @@ if test "x" = "y"; then
     index insque \
     memchr memcmp memcpy memmem memmove memset mkstemps \
     on_exit \
-    pipe2 psignal pstat_getdynamic pstat_getstatic putenv \
+    pipe2 posix_spawn psignal pstat_getdynamic pstat_getstatic putenv \
     random realpath rename rindex \
     sbrk setenv setproctitle setrlimit sigsetmask snprintf spawnve spawnvpe \
      stpcpy stpncpy strcasecmp strchr strdup \
diff --git a/libiberty/configure.ac b/libiberty/configure.ac
index 6c1ff9c6093..5d21f383e7d 100644
--- a/libiberty/configure.ac
+++ b/libiberty/configure.ac
@@ -289,7 +289,7 @@ AC_SUBST_FILE(host_makefile_frag)
 # It's OK to check for header files.  Although the compiler may not be
 # able to link anything, it had better be able to at least compile
 # something.
-AC_CHECK_HEADERS(sys/file.h sys/param.h limits.h stdlib.h malloc.h string.h unistd.h strings.h sys/time.h time.h sys/resource.h sys/stat.h sys/mman.h fcntl.h alloca.h sys/pstat.h sys/sysmp.h sys/sysinfo.h machine/hal_sysinfo.h sys/table.h sys/sysctl.h sys/systemcfg.h stdint.h stdio_ext.h process.h sys/prctl.h)
+AC_CHECK_HEADERS(sys/file.h sys/param.h limits.h stdlib.h malloc.h string.h unistd.h strings.h sys/time.h time.h sys/resource.h sys/stat.h sys/mman.h fcntl.h alloca.h sys/pstat.h sys/sysmp.h sys/sysinfo.h machine/hal_sysinfo.h sys/table.h sys/sysctl.h sys/systemcfg.h stdint.h stdio_ext.h process.h sys/prctl.h spawn.h)
 AC_HEADER_SYS_WAIT
 AC_HEADER_TIME
 
@@ -412,9 +412,9 @@ funcs="$funcs setproctitle"
 vars="sys_errlist sys_nerr sys_siglist"
 
 checkfuncs="__fsetlocking canonicalize_file_name dup3 getrlimit getrusage \
- getsysinfo gettimeofday on_exit pipe2 psignal pstat_getdynamic pstat_getstatic \
- realpath setrlimit spawnve spawnvpe strerror strsignal sysconf sysctl \
- sysmp table times wait3 wait4"
+ getsysinfo gettimeofday on_exit pipe2 posix_spawn psignal pstat_getdynamic \
+ pstat_getstatic realpath setrlimit spawnve spawnvpe strerror strsignal \
+ sysconf sysctl sysmp table times wait3 wait4"
 
 # Darwin has sbrk, but it is deprecated and that produces build-time warnings
 # so do not check for it.
@@ -435,7 +435,7 @@ if test "x" = "y"; then
     index insque \
     memchr memcmp memcpy memmem memmove memset mkstemps \
     on_exit \
-    pipe2 psignal pstat_getdynamic pstat_getstatic putenv \
+    pipe2 posix_spawn psignal pstat_getdynamic pstat_getstatic putenv \
     random realpath rename rindex \
     sbrk setenv setproctitle setrlimit sigsetmask snprintf spawnve spawnvpe \
      stpcpy stpncpy strcasecmp strchr strdup \
diff --git a/libiberty/pex-unix.c b/libiberty/pex-unix.c
index 850d344ba58..ba3a6d5d38f 100644
--- a/libiberty/pex-unix.c
+++ b/libiberty/pex-unix.c
@@ -58,6 +58,9 @@ extern int errno;
 #ifdef HAVE_PROCESS_H
 #include <process.h>
 #endif
+#ifdef HAVE_SPAWN_H
+#include <spawn.h>
+#endif
 
 #ifdef vfork /* Autoconf may define this to fork for us. */
 # define VFORK_STRING "fork"
@@ -366,7 +369,97 @@ pex_unix_close (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd)
 
 /* Execute a child.  */
 
-#if defined(HAVE_SPAWNVE) && defined(HAVE_SPAWNVPE)
+#if defined(HAVE_POSIX_SPAWN)
+/* Implementation of pex->exec_child using the posix_spawn operation.  */
+
+static pid_t
+pex_unix_exec_child (struct pex_obj *obj, int flags, const char *executable,
+		     char * const * argv, char * const * env,
+                     int in, int out, int errdes,
+		     int toclose, const char **errmsg, int *err)
+{
+  posix_spawn_file_actions_t file_actions;
+  pid_t pid;
+  int ret;
+
+  *errmsg = "posix_spawn";
+  ret = posix_spawn_file_actions_init (&file_actions);
+  if (ret != 0)
+    goto error;
+  if (in != STDIN_FILE_NO)
+    {
+      ret = posix_spawn_file_actions_adddup2 (&file_actions, in, STDIN_FILE_NO);
+      if (ret != 0)
+        goto error;
+      ret = posix_spawn_file_actions_addclose (&file_actions, in);
+      if (ret != 0)
+        goto error;
+    }
+  if (out != STDOUT_FILE_NO)
+    {
+      ret = posix_spawn_file_actions_adddup2 (&file_actions, out, STDOUT_FILE_NO);
+      if (ret != 0)
+        goto error;
+      ret = posix_spawn_file_actions_addclose (&file_actions, out);
+      if (ret != 0)
+        goto error;
+    }
+  if ((flags & PEX_STDERR_TO_STDOUT) != 0)
+    {
+      ret = posix_spawn_file_actions_adddup2 (&file_actions, STDOUT_FILE_NO, STDERR_FILE_NO);
+      if (ret != 0)
+        goto error;
+    }
+  else if (errdes != STDERR_FILE_NO)
+    {
+      ret = posix_spawn_file_actions_adddup2 (&file_actions, errdes, STDERR_FILE_NO);
+      if (ret != 0)
+        goto error;
+      ret = posix_spawn_file_actions_addclose (&file_actions, errdes);
+      if (ret != 0)
+        goto error;
+    }
+  if (toclose >= 0)
+    {
+      ret = posix_spawn_file_actions_addclose (&file_actions, toclose);
+      if (ret != 0)
+        goto error;
+    }
+  if (env == NULL)
+    env = environ;
+  if ((flags & PEX_SEARCH) != 0)
+    ret = posix_spawnp (&pid, executable, &file_actions, NULL, argv, env);
+  else
+    ret = posix_spawn (&pid, executable, &file_actions, NULL, argv, env);
+  if (ret != 0)
+    goto error;
+  posix_spawn_file_actions_destroy(&file_actions);
+
+  *errmsg = "close";
+  if (in != STDIN_FILE_NO && close (in) < 0)
+    {
+      ret = errno;
+      goto error;
+    }
+  if (out != STDOUT_FILE_NO && close (out) < 0)
+    {
+      ret = errno;
+      goto error;
+    }
+  if (errdes != STDERR_FILE_NO && close (errdes) < 0)
+    {
+      ret = errno;
+      goto error;
+    }
+
+  return pid;
+
+ error:
+  *err = ret;
+  return (pid_t) -1;
+}
+
+#elif defined(HAVE_SPAWNVE) && defined(HAVE_SPAWNVPE)
 /* Implementation of pex->exec_child using the Cygwin spawn operation.  */
 
 /* Subroutine of pex_unix_exec_child.  Move OLD_FD to a new file descriptor
-- 
2.35.1