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.
33 #include <sys/types.h>
41 #ifdef HAVE_SYS_TIME_H
45 #ifdef HAVE_SYS_WAIT_H
56 /* windows pipe api details: http://msdn2.microsoft.com/en-us/library/edze9h7e(VS.80).aspx */
57 #define pipe(x) _pipe(x, 256, 0)
60 #define set_error(msg, ...) do { if (error != NULL) *error = g_error_new (G_LOG_DOMAIN, 1, msg, __VA_ARGS__); } while (0)
61 #define set_error_cond(cond,msg, ...) do { if ((cond) && error != NULL) *error = g_error_new (G_LOG_DOMAIN, 1, msg, __VA_ARGS__); } while (0)
62 #define set_error_status(status,msg, ...) do { if (error != NULL) *error = g_error_new (G_LOG_DOMAIN, status, msg, __VA_ARGS__); } while (0)
63 #define NO_INTR(var,cmd) do { (var) = (cmd); } while ((var) == -1 && errno == EINTR)
64 #define CLOSE_PIPE(p) do { close (p [0]); close (p [1]); } while (0)
67 /* Apple defines this in crt_externs.h but doesn't provide that header for
68 * arm-apple-darwin9. We'll manually define the symbol on Apple as it does
69 * in fact exist on all implementations (so far)
71 gchar ***_NSGetEnviron();
72 #define environ (*_NSGetEnviron())
76 safe_read (int fd, gchar *buffer, gint count, GError **error)
80 NO_INTR (res, read (fd, buffer, count));
81 set_error_cond (res == -1, "%s", "Error reading from pipe.");
86 read_pipes (int outfd, gchar **out_str, int errfd, gchar **err_str, GError **error)
97 out_closed = (outfd < 0);
98 err_closed = (errfd < 0);
101 out = g_string_new ("");
106 err = g_string_new ("");
110 if (out_closed && err_closed)
114 #pragma warning(push)
115 #pragma warning(disable:4389)
119 if (!out_closed && outfd >= 0)
120 FD_SET (outfd, &rfds);
121 if (!err_closed && errfd >= 0)
122 FD_SET (errfd, &rfds);
128 res = select (MAX (outfd, errfd) + 1, &rfds, NULL, NULL, NULL);
131 buffer = g_malloc (1024);
132 if (!out_closed && FD_ISSET (outfd, &rfds)) {
133 nread = safe_read (outfd, buffer, 1024, error);
139 g_string_append_len (out, buffer, nread);
146 if (!err_closed && FD_ISSET (errfd, &rfds)) {
147 nread = safe_read (errfd, buffer, 1024, error);
153 g_string_append_len (err, buffer, nread);
160 } while (res > 0 || (res == -1 && errno == EINTR));
164 *out_str = g_string_free (out, FALSE);
167 *err_str = g_string_free (err, FALSE);
173 create_pipe (int *fds, GError **error)
175 if (pipe (fds) == -1) {
176 set_error ("%s", "Error creating pipe.");
183 g_spawn_command_line_sync (const gchar *command_line,
184 gchar **standard_output,
185 gchar **standard_error,
194 int stdout_pipe [2] = { -1, -1 };
195 int stderr_pipe [2] = { -1, -1 };
199 if (!g_shell_parse_argv (command_line, &argc, &argv, error))
202 if (standard_output && !create_pipe (stdout_pipe, error))
205 if (standard_error && !create_pipe (stderr_pipe, error)) {
206 if (standard_output) {
207 CLOSE_PIPE (stdout_pipe);
216 if (standard_output) {
217 close (stdout_pipe [0]);
218 dup2 (stdout_pipe [1], STDOUT_FILENO);
221 if (standard_error) {
222 close (stderr_pipe [0]);
223 dup2 (stderr_pipe [1], STDERR_FILENO);
225 for (i = getdtablesize () - 1; i >= 3; i--)
228 /* G_SPAWN_SEARCH_PATH is always enabled for g_spawn_command_line_sync */
229 if (!g_path_is_absolute (argv [0])) {
232 arg0 = g_find_program_in_path (argv [0]);
239 execv (argv [0], argv);
240 exit (1); /* TODO: What now? */
245 close (stdout_pipe [1]);
248 close (stderr_pipe [1]);
250 if (standard_output || standard_error) {
251 res = read_pipes (stdout_pipe [0], standard_output, stderr_pipe [0], standard_error, error);
253 waitpid (pid, &status, WNOHANG); /* avoid zombie */
258 NO_INTR (res, waitpid (pid, &status, 0));
260 /* TODO: What if error? */
261 if (WIFEXITED (status) && exit_status) {
262 *exit_status = WEXITSTATUS (status);
269 * This is the only use we have in mono/metadata
270 !g_spawn_async_with_pipes (NULL, (char**)addr_argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &child_pid, &ch_in, &ch_out, NULL, NULL)
273 g_spawn_async_with_pipes (const gchar *working_directory,
277 GSpawnChildSetupFunc child_setup,
280 gint *standard_input,
281 gint *standard_output,
282 gint *standard_error,
289 int in_pipe [2] = { -1, -1 };
290 int out_pipe [2] = { -1, -1 };
291 int err_pipe [2] = { -1, -1 };
294 g_return_val_if_fail (argv != NULL, FALSE); /* Only mandatory arg */
296 if (!create_pipe (info_pipe, error))
299 if (standard_output && !create_pipe (out_pipe, error)) {
300 CLOSE_PIPE (info_pipe);
304 if (standard_error && !create_pipe (err_pipe, error)) {
305 CLOSE_PIPE (info_pipe);
306 CLOSE_PIPE (out_pipe);
310 if (standard_input && !create_pipe (in_pipe, error)) {
311 CLOSE_PIPE (info_pipe);
312 CLOSE_PIPE (out_pipe);
313 CLOSE_PIPE (err_pipe);
319 CLOSE_PIPE (info_pipe);
320 CLOSE_PIPE (out_pipe);
321 CLOSE_PIPE (err_pipe);
322 CLOSE_PIPE (in_pipe);
323 set_error ("%s", "Error in fork ()");
328 /* No zombie left behind */
329 if ((flags & G_SPAWN_DO_NOT_REAP_CHILD) == 0) {
334 exit (pid == -1 ? 1 : 0);
342 close (info_pipe [0]);
344 close (out_pipe [0]);
345 close (err_pipe [0]);
347 /* when exec* succeeds, we want to close this fd, which will return
348 * a 0 read on the parent. We're not supposed to keep it open forever.
349 * If exec fails, we still can write the error to it before closing.
351 fcntl (info_pipe [1], F_SETFD, FD_CLOEXEC);
353 if ((flags & G_SPAWN_DO_NOT_REAP_CHILD) == 0) {
355 NO_INTR (unused, write (info_pipe [1], &pid, sizeof (pid_t)));
358 if (working_directory && chdir (working_directory) == -1) {
360 NO_INTR (unused, write (info_pipe [1], &err, sizeof (int)));
364 if (standard_output) {
365 dup2 (out_pipe [1], STDOUT_FILENO);
366 } else if ((flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0) {
367 fd = open ("/dev/null", O_WRONLY);
368 dup2 (fd, STDOUT_FILENO);
371 if (standard_error) {
372 dup2 (err_pipe [1], STDERR_FILENO);
373 } else if ((flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0) {
374 fd = open ("/dev/null", O_WRONLY);
375 dup2 (fd, STDERR_FILENO);
378 if (standard_input) {
379 dup2 (in_pipe [0], STDERR_FILENO);
380 } else if ((flags & G_SPAWN_CHILD_INHERITS_STDIN) == 0) {
381 fd = open ("/dev/null", O_RDONLY);
382 dup2 (fd, STDERR_FILENO);
385 if ((flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN) != 0) {
386 for (i = getdtablesize () - 1; i >= 3; i--)
390 actual_args = ((flags & G_SPAWN_FILE_AND_ARGV_ZERO) == 0) ? argv : argv + 1;
395 child_setup (user_data);
398 if (!g_path_is_absolute (arg0) || (flags & G_SPAWN_SEARCH_PATH) != 0) {
399 arg0 = g_find_program_in_path (argv [0]);
402 write (info_pipe [1], &err, sizeof (int));
407 execve (arg0, actual_args, envp);
408 write (info_pipe [1], &errno, sizeof (int));
411 } else if ((flags & G_SPAWN_DO_NOT_REAP_CHILD) == 0) {
413 /* Wait for the first child if two are created */
414 NO_INTR (w, waitpid (pid, &status, 0));
415 if (status == 1 || w == -1) {
416 CLOSE_PIPE (info_pipe);
417 CLOSE_PIPE (out_pipe);
418 CLOSE_PIPE (err_pipe);
419 CLOSE_PIPE (in_pipe);
420 set_error ("Error in fork (): %d", status);
424 close (info_pipe [1]);
426 close (out_pipe [1]);
427 close (err_pipe [1]);
429 if ((flags & G_SPAWN_DO_NOT_REAP_CHILD) == 0) {
431 NO_INTR (x, read (info_pipe [0], &pid, sizeof (pid_t))); /* if we read < sizeof (pid_t)... */
438 if (read (info_pipe [0], &status, sizeof (int)) != 0) {
439 close (info_pipe [0]);
441 close (out_pipe [1]);
442 close (err_pipe [1]);
443 set_error_status (status, "Error in exec (%d -> %s)", status, strerror (status));
447 close (info_pipe [0]);
449 *standard_input = in_pipe [1];
451 *standard_output = out_pipe [0];
453 *standard_error = err_pipe [0];