5 * Gonzalo Paniagua Javier (gonzalo@novell.com
7 * (C) 2006 Novell, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 #include <sys/types.h>
40 #define set_error(msg...) do { if (error != NULL) *error = g_error_new (G_LOG_DOMAIN, 1, msg); } while (0)
41 #define set_error_cond(cond,msg...) do { if ((cond) && error != NULL) *error = g_error_new (G_LOG_DOMAIN, 1, msg); } while (0)
42 #define set_error_status(status,msg...) do { if (error != NULL) *error = g_error_new (G_LOG_DOMAIN, status, msg); } while (0)
43 #define NO_INTR(var,cmd) do { (var) = (cmd); } while ((var) == -1 && errno == EINTR)
44 #define CLOSE_PIPE(p) do { close (p [0]); close (p [1]); } while (0)
47 safe_read (int fd, gchar *buffer, gint count, GError **error)
51 NO_INTR (res, read (fd, buffer, count));
52 set_error_cond (res == -1, "Error reading from pipe.");
57 read_pipes (int outfd, gchar **out_str, int errfd, gchar **err_str, GError **error)
68 out_closed = (outfd < 0);
69 err_closed = (errfd < 0);
72 out = g_string_new ("");
77 err = g_string_new ("");
81 if (out_closed && err_closed)
85 if (!out_closed && outfd >= 0)
86 FD_SET (outfd, &rfds);
87 if (!err_closed && errfd >= 0)
88 FD_SET (errfd, &rfds);
90 res = select (MAX (outfd, errfd) + 1, &rfds, NULL, NULL, NULL);
93 buffer = g_malloc (1024);
94 if (!out_closed && FD_ISSET (outfd, &rfds)) {
95 nread = safe_read (outfd, buffer, 1024, error);
101 g_string_append_len (out, buffer, nread);
108 if (!err_closed && FD_ISSET (errfd, &rfds)) {
109 nread = safe_read (errfd, buffer, 1024, error);
115 g_string_append_len (err, buffer, nread);
122 } while (res > 0 || (res == -1 && errno == EINTR));
126 *out_str = g_string_free (out, FALSE);
129 *err_str = g_string_free (err, FALSE);
135 create_pipe (int *fds, GError **error)
137 if (pipe (fds) == -1) {
138 set_error ("Error creating pipe.");
145 g_spawn_command_line_sync (const gchar *command_line,
146 gchar **standard_output,
147 gchar **standard_error,
154 int stdout_pipe [2] = { -1, -1 };
155 int stderr_pipe [2] = { -1, -1 };
159 if (!g_shell_parse_argv (command_line, &argc, &argv, error))
162 if (standard_output && !create_pipe (stdout_pipe, error))
165 if (standard_error && !create_pipe (stderr_pipe, error)) {
166 if (standard_output) {
167 CLOSE_PIPE (stdout_pipe);
176 if (standard_output) {
177 close (stdout_pipe [0]);
178 dup2 (stdout_pipe [1], STDOUT_FILENO);
181 if (standard_error) {
182 close (stderr_pipe [0]);
183 dup2 (stderr_pipe [1], STDERR_FILENO);
185 for (i = getdtablesize () - 1; i >= 3; i--)
188 execv (argv [0], argv);
189 exit (1); /* TODO: What now? */
194 close (stdout_pipe [1]);
197 close (stderr_pipe [1]);
199 if (standard_output || standard_error) {
200 res = read_pipes (stdout_pipe [0], standard_output, stderr_pipe [0], standard_error, error);
202 waitpid (pid, &status, WNOHANG); /* avoid zombie */
207 NO_INTR (res, waitpid (pid, &status, 0));
209 /* TODO: What if error? */
210 if (WIFEXITED (status) && exit_status) {
211 *exit_status = WEXITSTATUS (status);
218 * This is the only use we have in mono/metadata
219 !g_spawn_async_with_pipes (NULL, (char**)addr_argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &child_pid, &ch_in, &ch_out, NULL, NULL)
222 g_spawn_async_with_pipes (const gchar *working_directory,
226 GSpawnChildSetupFunc child_setup,
229 gint *standard_input,
230 gint *standard_output,
231 gint *standard_error,
236 int in_pipe [2] = { -1, -1 };
237 int out_pipe [2] = { -1, -1 };
238 int err_pipe [2] = { -1, -1 };
241 g_return_val_if_fail (argv != NULL, FALSE); /* Only mandatory arg */
243 if (!create_pipe (info_pipe, error))
246 if (standard_output && !create_pipe (out_pipe, error)) {
247 CLOSE_PIPE (info_pipe);
251 if (standard_error && !create_pipe (err_pipe, error)) {
252 CLOSE_PIPE (info_pipe);
253 CLOSE_PIPE (out_pipe);
257 if (standard_input && !create_pipe (in_pipe, error)) {
258 CLOSE_PIPE (info_pipe);
259 CLOSE_PIPE (out_pipe);
260 CLOSE_PIPE (err_pipe);
266 CLOSE_PIPE (info_pipe);
267 CLOSE_PIPE (out_pipe);
268 CLOSE_PIPE (err_pipe);
269 CLOSE_PIPE (in_pipe);
270 set_error ("Error in fork ()");
275 /* No zombie left behind */
276 if ((flags & G_SPAWN_DO_NOT_REAP_CHILD) == 0) {
281 exit (pid == -1 ? 1 : 0);
289 close (info_pipe [0]);
291 close (out_pipe [0]);
292 close (err_pipe [0]);
294 /* when exec* succeeds, we want to close this fd, which will return
295 * a 0 read on the parent. We're not supposed to keep it open forever.
296 * If exec fails, we still can write the error to it before closing.
298 fcntl (info_pipe [1], F_SETFD, FD_CLOEXEC);
300 if ((flags & G_SPAWN_DO_NOT_REAP_CHILD) == 0) {
302 NO_INTR (unused, write (info_pipe [1], &pid, sizeof (pid_t)));
305 if (working_directory && chdir (working_directory) == -1) {
307 NO_INTR (unused, write (info_pipe [1], &err, sizeof (int)));
311 if (standard_output) {
312 dup2 (out_pipe [1], STDOUT_FILENO);
313 } else if ((flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0) {
314 fd = open ("/dev/null", O_WRONLY);
315 dup2 (fd, STDOUT_FILENO);
318 if (standard_error) {
319 dup2 (err_pipe [1], STDERR_FILENO);
320 } else if ((flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0) {
321 fd = open ("/dev/null", O_WRONLY);
322 dup2 (fd, STDERR_FILENO);
325 if (standard_input) {
326 dup2 (in_pipe [0], STDERR_FILENO);
327 } else if ((flags & G_SPAWN_CHILD_INHERITS_STDIN) == 0) {
328 fd = open ("/dev/null", O_RDONLY);
329 dup2 (fd, STDERR_FILENO);
332 if ((flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN) != 0) {
333 for (i = getdtablesize () - 1; i >= 3; i--)
337 actual_args = ((flags & G_SPAWN_FILE_AND_ARGV_ZERO) == 0) ? argv : argv + 1;
342 child_setup (user_data);
345 if (!g_path_is_absolute (arg0) || (flags & G_SPAWN_SEARCH_PATH) != 0) {
346 arg0 = g_find_program_in_path (argv [0]);
349 write (info_pipe [1], &err, sizeof (int));
354 execve (arg0, actual_args, envp);
355 write (info_pipe [1], &errno, sizeof (int));
358 } else if ((flags & G_SPAWN_DO_NOT_REAP_CHILD) == 0) {
360 /* Wait for the first child if two are created */
361 NO_INTR (w, waitpid (pid, &status, 0));
362 if (status == 1 || w == -1) {
363 CLOSE_PIPE (info_pipe);
364 CLOSE_PIPE (out_pipe);
365 CLOSE_PIPE (err_pipe);
366 CLOSE_PIPE (in_pipe);
367 set_error ("Error in fork (): %d", status);
371 close (info_pipe [1]);
373 close (out_pipe [1]);
374 close (err_pipe [1]);
376 if ((flags & G_SPAWN_DO_NOT_REAP_CHILD) == 0) {
378 NO_INTR (x, read (info_pipe [0], &pid, sizeof (pid_t))); /* if we read < sizeof (pid_t)... */
385 if (read (info_pipe [0], &status, sizeof (int)) != 0) {
386 close (info_pipe [0]);
388 close (out_pipe [1]);
389 close (err_pipe [1]);
390 set_error_status (status, "Error in exec (%d -> %s)", status, strerror (status));
394 close (info_pipe [0]);
396 *standard_input = in_pipe [1];
398 *standard_output = out_pipe [0];
400 *standard_error = err_pipe [0];