Debian Codegraph @

lib/os2-spawn.c

raw
1 /* Auxiliary functions for the creation of subprocesses. OS/2 kLIBC API.
2 Copyright (C) 2001, 2003-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible <[email protected]>, 2003.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
20 /* Specification. */
21 #include "os2-spawn.h"
22
23 /* Get _open_osfhandle(). */
24 #include <io.h>
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <errno.h>
30
31 #include <process.h>
32 #include <dlfcn.h>
33 #if HAVE_LIBCX_SPAWN2_H
34 # include <libcx/spawn2.h>
35 #endif
36
37 #include "cloexec.h"
38 #include <error.h>
39 #include "gettext.h"
40
41 #define _(str) gettext (str)
42
43
44 /* Duplicates a file handle, making the copy uninheritable.
45 Returns -1 for a file handle that is equivalent to closed. */
46 static int
47 dup_noinherit (int fd)
48 {
49 fd = dup_cloexec (fd);
50 if (fd < 0 && errno == EMFILE)
51 error (EXIT_FAILURE, errno, _("_open_osfhandle failed"));
52
53 return fd;
54 }
55
56 /* Returns a file descriptor equivalent to FD, except that the resulting file
57 descriptor is none of STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
58 FD must be open and non-inheritable. The result will be non-inheritable as
59 well.
60 If FD < 0, FD itself is returned. */
61 static int
62 fd_safer_noinherit (int fd)
63 {
64 if (STDIN_FILENO <= fd && fd <= STDERR_FILENO)
65 {
66 /* The recursion depth is at most 3. */
67 int nfd = fd_safer_noinherit (dup_noinherit (fd));
68 int saved_errno = errno;
69 close (fd);
70 errno = saved_errno;
71 return nfd;
72 }
73 return fd;
74 }
75
76 int
77 dup_safer_noinherit (int fd)
78 {
79 return fd_safer_noinherit (dup_noinherit (fd));
80 }
81
82 void
83 undup_safer_noinherit (int tempfd, int origfd)
84 {
85 if (tempfd >= 0)
86 {
87 if (dup2 (tempfd, origfd) < 0)
88 error (EXIT_FAILURE, errno, _("cannot restore fd %d: dup2 failed"),
89 origfd);
90 close (tempfd);
91 }
92 else
93 {
94 /* origfd was closed or open to no handle at all. Set it to a closed
95 state. This is (nearly) equivalent to the original state. */
96 close (origfd);
97 }
98 }
99
100 const char **
101 prepare_spawn (const char * const *argv, char **mem_to_free)
102 {
103 size_t argc;
104 const char **new_argv;
105 size_t i;
106
107 /* Count number of arguments. */
108 for (argc = 0; argv[argc] != NULL; argc++)
109 ;
110
111 /* Allocate new argument vector. */
112 new_argv = (const char **) malloc ((1 + argc + 1) * sizeof (const char *));
113 if (new_argv == NULL)
114 return NULL;
115
116 /* Add an element upfront that can be used when argv[0] turns out to be a
117 script, not a program.
118 On Unix, this would be "/bin/sh". */
119 new_argv[0] = "sh.exe";
120
121 /* Put quoted arguments into the new argument vector. */
122 size_t needed_size = 0;
123 for (i = 0; i < argc; i++)
124 {
125 const char *string = argv[i];
126 const char *quoted_string = (string[0] == '\0' ? "\"\"" : string);
127 size_t length = strlen (quoted_string);
128 needed_size += length + 1;
129 }
130
131 char *mem;
132 if (needed_size == 0)
133 mem = NULL;
134 else
135 {
136 mem = (char *) malloc (needed_size);
137 if (mem == NULL)
138 {
139 /* Memory allocation failure. */
140 free (new_argv);
141 errno = ENOMEM;
142 return NULL;
143 }
144 }
145 *mem_to_free = mem;
146
147 for (i = 0; i < argc; i++)
148 {
149 const char *string = argv[i];
150
151 new_argv[1 + i] = mem;
152 const char *quoted_string = (string[0] == '\0' ? "\"\"" : string);
153 size_t length = strlen (quoted_string);
154 memcpy (mem, quoted_string, length + 1);
155 mem += length + 1;
156 }
157 new_argv[1 + argc] = NULL;
158
159 return new_argv;
160 }
161
162 int
163 spawnpvech (int mode,
164 const char *progname, const char * const *argv,
165 const char * const *envp,
166 const char *currdir,
167 int new_stdin, int new_stdout, int new_stderr)
168 {
169 #if HAVE_LIBCX_SPAWN2_H
170 static int (*libcx_spawn2) (int mode,
171 const char *name, const char * const argv[],
172 const char *cwd, const char * const envp[],
173 const int stdfds[]) = NULL;
174 static int libcx_spawn2_loaded = -1;
175 #else
176 static int libcx_spawn2_loaded = 0;
177 #endif
178
179 int saved_stdin = STDIN_FILENO;
180 int saved_stdout = STDOUT_FILENO;
181 int saved_stderr = STDERR_FILENO;
182 int ret = -1;
183
184 #if HAVE_LIBCX_SPAWN2_H
185 if (libcx_spawn2_loaded == -1)
186 {
187 void *libcx_handle;
188
189 libcx_handle = dlopen ("libcx0", RTLD_LAZY);
190 if (libcx_handle != NULL)
191 libcx_spawn2 = dlsym (libcx_handle, "_spawn2");
192
193 libcx_spawn2_loaded = libcx_handle != NULL && libcx_spawn2 != NULL;
194 }
195 #endif
196
197 if (!(libcx_spawn2_loaded
198 || (currdir == NULL || strcmp (currdir, ".") == 0)))
199 {
200 errno = EINVAL;
201 return -1;
202 }
203
204 /* Save standard file handles. */
205 /* 0 means no changes. This is a behavior of spawn2(). */
206 if (new_stdin != 0)
207 saved_stdin = dup_safer_noinherit (STDIN_FILENO);
208
209 if (!(new_stdout == 0 || new_stdout == 1))
210 saved_stdout = dup_safer_noinherit (STDOUT_FILENO);
211
212 if (!(new_stderr == 0 || new_stderr == 2))
213 saved_stderr = dup_safer_noinherit (STDERR_FILENO);
214
215 if ((saved_stdin == STDIN_FILENO || dup2 (new_stdin, STDIN_FILENO) >= 0)
216 && (saved_stdout == STDOUT_FILENO
217 || dup2 (new_stdout, STDOUT_FILENO) >= 0)
218 && (saved_stderr == STDERR_FILENO
219 || dup2 (new_stderr, STDERR_FILENO) >= 0))
220 {
221 if (!libcx_spawn2_loaded
222 || (currdir == NULL || strcmp (currdir, ".") == 0))
223 ret = spawnvpe (mode, progname, (char * const *) argv,
224 (char * const *) envp);
225 #if HAVE_LIBCX_SPAWN2_H
226 else
227 ret = libcx_spawn2 (mode | P_2_THREADSAFE, progname, argv, currdir,
228 envp, NULL);
229 #endif
230 }
231
232 /* Restores standard file handles. */
233 if (saved_stderr > STDERR_FILENO)
234 undup_safer_noinherit (saved_stderr, STDERR_FILENO);
235
236 if (saved_stdout > STDOUT_FILENO)
237 undup_safer_noinherit (saved_stdout, STDOUT_FILENO);
238
239 if (saved_stdin > STDIN_FILENO)
240 undup_safer_noinherit (saved_stdin, STDIN_FILENO);
241
242 return ret;
243 }