| 1 | /* Copied and modified from: |
| 2 | * |
| 3 | * gspawn.c - Process launching |
| 4 | * |
| 5 | * Copyright 2000 Red Hat, Inc. |
| 6 | * g_execvpe implementation based on GNU libc execvp: |
| 7 | * Copyright 1991, 92, 95, 96, 97, 98, 99 Free Software Foundation, Inc. |
| 8 | * |
| 9 | * GLib is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU Lesser General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 of the |
| 12 | * License, or (at your option) any later version. |
| 13 | * |
| 14 | * GLib is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * Lesser General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser General Public |
| 20 | * License along with GLib; see the file COPYING.LIB. If not, write |
| 21 | * to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 22 | * Boston, MA 02111-1307, USA. |
| 23 | */ |
| 24 | |
| 25 | #include "config.h" |
| 26 | |
| 27 | #include <sys/time.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/wait.h> |
| 30 | #include <unistd.h> |
| 31 | #include <errno.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <signal.h> |
| 34 | #include <string.h> |
| 35 | |
| 36 | #ifdef HAVE_SYS_SELECT_H |
| 37 | #include <sys/select.h> |
| 38 | #endif /* HAVE_SYS_SELECT_H */ |
| 39 | |
| 40 | #include <stdlib.h> |
| 41 | #include <process.h> |
| 42 | |
| 43 | #include "glib.h" |
| 44 | #include "galias.h" |
| 45 | |
| 46 | #include "glibintl.h" |
| 47 | |
| 48 | static gint g_execute (const gchar *file, |
| 49 | gchar **argv, |
| 50 | gchar **envp, |
| 51 | gboolean search_path); |
| 52 | |
| 53 | static gboolean make_pipe (gint p[2], |
| 54 | GError **error); |
| 55 | static gboolean fork_exec_with_pipes (gboolean intermediate_child, |
| 56 | const gchar *working_directory, |
| 57 | gchar **argv, |
| 58 | gchar **envp, |
| 59 | gboolean close_descriptors, |
| 60 | gboolean search_path, |
| 61 | gboolean stdout_to_null, |
| 62 | gboolean stderr_to_null, |
| 63 | gboolean child_inherits_stdin, |
| 64 | gboolean file_and_argv_zero, |
| 65 | GSpawnChildSetupFunc child_setup, |
| 66 | gpointer user_data, |
| 67 | GPid *child_pid, |
| 68 | gint *standard_input, |
| 69 | gint *standard_output, |
| 70 | gint *standard_error, |
| 71 | GError **error); |
| 72 | |
| 73 | GQuark |
| 74 | g_spawn_error_quark (void) |
| 75 | { |
| 76 | static GQuark quark = 0; |
| 77 | if (quark == 0) |
| 78 | quark = g_quark_from_static_string ("g-exec-error-quark"); |
| 79 | return quark; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * g_spawn_async: |
| 84 | * @working_directory: child's current working directory, or %NULL to inherit parent's |
| 85 | * @argv: child's argument vector |
| 86 | * @envp: child's environment, or %NULL to inherit parent's |
| 87 | * @flags: flags from #GSpawnFlags |
| 88 | * @child_setup: function to run in the child just before exec() |
| 89 | * @user_data: user data for @child_setup |
| 90 | * @child_pid: return location for child process ID, or %NULL |
| 91 | * @error: return location for error |
| 92 | * |
| 93 | * See g_spawn_async_with_pipes() for a full description; this function |
| 94 | * simply calls the g_spawn_async_with_pipes() without any pipes. |
| 95 | * |
| 96 | * Return value: %TRUE on success, %FALSE if error is set |
| 97 | **/ |
| 98 | gboolean |
| 99 | g_spawn_async (const gchar *working_directory, |
| 100 | gchar **argv, |
| 101 | gchar **envp, |
| 102 | GSpawnFlags flags, |
| 103 | GSpawnChildSetupFunc child_setup, |
| 104 | gpointer user_data, |
| 105 | GPid *child_pid, |
| 106 | GError **error) |
| 107 | { |
| 108 | g_return_val_if_fail (argv != NULL, FALSE); |
| 109 | |
| 110 | return g_spawn_async_with_pipes (working_directory, |
| 111 | argv, envp, |
| 112 | flags, |
| 113 | child_setup, |
| 114 | user_data, |
| 115 | child_pid, |
| 116 | NULL, NULL, NULL, |
| 117 | error); |
| 118 | } |
| 119 | |
| 120 | /* Avoids a danger in threaded situations (calling close() |
| 121 | * on a file descriptor twice, and another thread has |
| 122 | * re-opened it since the first close) |
| 123 | */ |
| 124 | static gint |
| 125 | close_and_invalidate (gint *fd) |
| 126 | { |
| 127 | gint ret; |
| 128 | |
| 129 | if (*fd < 0) |
| 130 | return -1; |
| 131 | else |
| 132 | { |
| 133 | ret = close (*fd); |
| 134 | *fd = -1; |
| 135 | } |
| 136 | |
| 137 | return ret; |
| 138 | } |
| 139 | |
| 140 | typedef enum |
| 141 | { |
| 142 | READ_FAILED = 0, /* FALSE */ |
| 143 | READ_OK, |
| 144 | READ_EOF |
| 145 | } ReadResult; |
| 146 | |
| 147 | static ReadResult |
| 148 | read_data (GString *str, |
| 149 | gint fd, |
| 150 | GError **error) |
| 151 | { |
| 152 | gssize bytes; |
| 153 | gchar buf[4096]; |
| 154 | |
| 155 | again: |
| 156 | |
| 157 | bytes = read (fd, buf, 4096); |
| 158 | |
| 159 | if (bytes == 0) |
| 160 | return READ_EOF; |
| 161 | else if (bytes > 0) |
| 162 | { |
| 163 | g_string_append_len (str, buf, bytes); |
| 164 | return READ_OK; |
| 165 | } |
| 166 | else if (bytes < 0 && errno == EINTR) |
| 167 | goto again; |
| 168 | else if (bytes < 0) |
| 169 | { |
| 170 | g_set_error (error, |
| 171 | G_SPAWN_ERROR, |
| 172 | G_SPAWN_ERROR_READ, |
| 173 | _("Failed to read data from child process (%s)"), |
| 174 | g_strerror (errno)); |
| 175 | |
| 176 | return READ_FAILED; |
| 177 | } |
| 178 | else |
| 179 | return READ_OK; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * g_spawn_sync: |
| 184 | * @working_directory: child's current working directory, or %NULL to inherit parent's |
| 185 | * @argv: child's argument vector |
| 186 | * @envp: child's environment, or %NULL to inherit parent's |
| 187 | * @flags: flags from #GSpawnFlags |
| 188 | * @child_setup: function to run in the child just before exec() |
| 189 | * @user_data: user data for @child_setup |
| 190 | * @standard_output: return location for child output |
| 191 | * @standard_error: return location for child error messages |
| 192 | * @exit_status: child exit status, as returned by waitpid() |
| 193 | * @error: return location for error |
| 194 | * |
| 195 | * Executes a child synchronously (waits for the child to exit before returning). |
| 196 | * All output from the child is stored in @standard_output and @standard_error, |
| 197 | * if those parameters are non-%NULL. If @exit_status is non-%NULL, the exit |
| 198 | * status of the child is stored there as it would be returned by |
| 199 | * waitpid(); standard UNIX macros such as WIFEXITED() and WEXITSTATUS() |
| 200 | * must be used to evaluate the exit status. If an error occurs, no data is |
| 201 | * returned in @standard_output, @standard_error, or @exit_status. |
| 202 | * |
| 203 | * This function calls g_spawn_async_with_pipes() internally; see that function |
| 204 | * for full details on the other parameters. |
| 205 | * |
| 206 | * Return value: %TRUE on success, %FALSE if an error was set. |
| 207 | **/ |
| 208 | gboolean |
| 209 | g_spawn_sync (const gchar *working_directory, |
| 210 | gchar **argv, |
| 211 | gchar **envp, |
| 212 | GSpawnFlags flags, |
| 213 | GSpawnChildSetupFunc child_setup, |
| 214 | gpointer user_data, |
| 215 | gchar **standard_output, |
| 216 | gchar **standard_error, |
| 217 | gint *exit_status, |
| 218 | GError **error) |
| 219 | { |
| 220 | gint outpipe = -1; |
| 221 | gint errpipe = -1; |
| 222 | GPid pid; |
| 223 | fd_set fds; |
| 224 | gint ret; |
| 225 | GString *outstr = NULL; |
| 226 | GString *errstr = NULL; |
| 227 | gboolean failed; |
| 228 | gint status; |
| 229 | |
| 230 | g_return_val_if_fail (argv != NULL, FALSE); |
| 231 | g_return_val_if_fail (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), FALSE); |
| 232 | g_return_val_if_fail (standard_output == NULL || |
| 233 | !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE); |
| 234 | g_return_val_if_fail (standard_error == NULL || |
| 235 | !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE); |
| 236 | |
| 237 | /* Just to ensure segfaults if callers try to use |
| 238 | * these when an error is reported. |
| 239 | */ |
| 240 | if (standard_output) |
| 241 | *standard_output = NULL; |
| 242 | |
| 243 | if (standard_error) |
| 244 | *standard_error = NULL; |
| 245 | |
| 246 | if (!fork_exec_with_pipes (FALSE, |
| 247 | working_directory, |
| 248 | argv, |
| 249 | envp, |
| 250 | !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN), |
| 251 | (flags & G_SPAWN_SEARCH_PATH) != 0, |
| 252 | (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0, |
| 253 | (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0, |
| 254 | (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0, |
| 255 | (flags & G_SPAWN_FILE_AND_ARGV_ZERO) != 0, |
| 256 | child_setup, |
| 257 | user_data, |
| 258 | &pid, |
| 259 | NULL, |
| 260 | standard_output ? &outpipe : NULL, |
| 261 | standard_error ? &errpipe : NULL, |
| 262 | error)) |
| 263 | return FALSE; |
| 264 | |
| 265 | /* Read data from child. */ |
| 266 | |
| 267 | failed = FALSE; |
| 268 | |
| 269 | if (outpipe >= 0) |
| 270 | { |
| 271 | outstr = g_string_new (NULL); |
| 272 | } |
| 273 | |
| 274 | if (errpipe >= 0) |
| 275 | { |
| 276 | errstr = g_string_new (NULL); |
| 277 | } |
| 278 | |
| 279 | /* Read data until we get EOF on both pipes. */ |
| 280 | while (!failed && |
| 281 | (outpipe >= 0 || |
| 282 | errpipe >= 0)) |
| 283 | { |
| 284 | ret = 0; |
| 285 | |
| 286 | FD_ZERO (&fds); |
| 287 | if (outpipe >= 0) |
| 288 | FD_SET (outpipe, &fds); |
| 289 | if (errpipe >= 0) |
| 290 | FD_SET (errpipe, &fds); |
| 291 | |
| 292 | ret = select (MAX (outpipe, errpipe) + 1, |
| 293 | &fds, |
| 294 | NULL, NULL, |
| 295 | NULL /* no timeout */); |
| 296 | |
| 297 | if (ret < 0 && errno != EINTR) |
| 298 | { |
| 299 | failed = TRUE; |
| 300 | |
| 301 | g_set_error (error, |
| 302 | G_SPAWN_ERROR, |
| 303 | G_SPAWN_ERROR_READ, |
| 304 | _("Unexpected error in select() reading data from a child process (%s)"), |
| 305 | g_strerror (errno)); |
| 306 | |
| 307 | break; |
| 308 | } |
| 309 | |
| 310 | if (outpipe >= 0 && FD_ISSET (outpipe, &fds)) |
| 311 | { |
| 312 | switch (read_data (outstr, outpipe, error)) |
| 313 | { |
| 314 | case READ_FAILED: |
| 315 | failed = TRUE; |
| 316 | break; |
| 317 | case READ_EOF: |
| 318 | close_and_invalidate (&outpipe); |
| 319 | outpipe = -1; |
| 320 | break; |
| 321 | default: |
| 322 | break; |
| 323 | } |
| 324 | |
| 325 | if (failed) |
| 326 | break; |
| 327 | } |
| 328 | |
| 329 | if (errpipe >= 0 && FD_ISSET (errpipe, &fds)) |
| 330 | { |
| 331 | switch (read_data (errstr, errpipe, error)) |
| 332 | { |
| 333 | case READ_FAILED: |
| 334 | failed = TRUE; |
| 335 | break; |
| 336 | case READ_EOF: |
| 337 | close_and_invalidate (&errpipe); |
| 338 | errpipe = -1; |
| 339 | break; |
| 340 | default: |
| 341 | break; |
| 342 | } |
| 343 | |
| 344 | if (failed) |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /* These should only be open still if we had an error. */ |
| 350 | |
| 351 | if (outpipe >= 0) |
| 352 | close_and_invalidate (&outpipe); |
| 353 | if (errpipe >= 0) |
| 354 | close_and_invalidate (&errpipe); |
| 355 | |
| 356 | /* Wait for child to exit, even if we have |
| 357 | * an error pending. |
| 358 | */ |
| 359 | again: |
| 360 | |
| 361 | ret = waitpid (pid, &status, 0); |
| 362 | |
| 363 | if (ret < 0) |
| 364 | { |
| 365 | if (errno == EINTR) |
| 366 | goto again; |
| 367 | else if (errno == ECHILD) |
| 368 | { |
| 369 | if (exit_status) |
| 370 | { |
| 371 | g_warning ("In call to g_spawn_sync(), exit status of a child process was requested but SIGCHLD action was set to SIG_IGN and ECHILD was received by waitpid(), so exit status can't be returned. This is a bug in the program calling g_spawn_sync(); either don't request the exit status, or don't set the SIGCHLD action."); |
| 372 | } |
| 373 | else |
| 374 | { |
| 375 | /* We don't need the exit status. */ |
| 376 | } |
| 377 | } |
| 378 | else |
| 379 | { |
| 380 | if (!failed) /* avoid error pileups */ |
| 381 | { |
| 382 | failed = TRUE; |
| 383 | |
| 384 | g_set_error (error, |
| 385 | G_SPAWN_ERROR, |
| 386 | G_SPAWN_ERROR_READ, |
| 387 | _("Unexpected error in waitpid() (%s)"), |
| 388 | g_strerror (errno)); |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | if (failed) |
| 394 | { |
| 395 | if (outstr) |
| 396 | g_string_free (outstr, TRUE); |
| 397 | if (errstr) |
| 398 | g_string_free (errstr, TRUE); |
| 399 | |
| 400 | return FALSE; |
| 401 | } |
| 402 | else |
| 403 | { |
| 404 | if (exit_status) |
| 405 | *exit_status = status; |
| 406 | |
| 407 | if (standard_output) |
| 408 | *standard_output = g_string_free (outstr, FALSE); |
| 409 | |
| 410 | if (standard_error) |
| 411 | *standard_error = g_string_free (errstr, FALSE); |
| 412 | |
| 413 | return TRUE; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * g_spawn_async_with_pipes: |
| 419 | * @working_directory: child's current working directory, or %NULL to inherit parent's |
| 420 | * @argv: child's argument vector |
| 421 | * @envp: child's environment, or %NULL to inherit parent's |
| 422 | * @flags: flags from #GSpawnFlags |
| 423 | * @child_setup: function to run in the child just before exec() |
| 424 | * @user_data: user data for @child_setup |
| 425 | * @child_pid: return location for child process ID, or %NULL |
| 426 | * @standard_input: return location for file descriptor to write to child's stdin, or %NULL |
| 427 | * @standard_output: return location for file descriptor to read child's stdout, or %NULL |
| 428 | * @standard_error: return location for file descriptor to read child's stderr, or %NULL |
| 429 | * @error: return location for error |
| 430 | * |
| 431 | * Executes a child program asynchronously (your program will not |
| 432 | * block waiting for the child to exit). The child program is |
| 433 | * specified by the only argument that must be provided, @argv. @argv |
| 434 | * should be a %NULL-terminated array of strings, to be passed as the |
| 435 | * argument vector for the child. The first string in @argv is of |
| 436 | * course the name of the program to execute. By default, the name of |
| 437 | * the program must be a full path; the <envar>PATH</envar> shell variable |
| 438 | * will only be searched if you pass the %G_SPAWN_SEARCH_PATH flag. |
| 439 | * |
| 440 | * On Windows, the low-level child process creation API |
| 441 | * (CreateProcess())doesn't use argument vectors, |
| 442 | * but a command line. The C runtime library's |
| 443 | * <function>spawn*()</function> family of functions (which |
| 444 | * g_spawn_async_with_pipes() eventually calls) paste the argument |
| 445 | * vector elements into a command line, and the C runtime startup code |
| 446 | * does a corresponding reconstruction of an argument vector from the |
| 447 | * command line, to be passed to main(). Complications arise when you have |
| 448 | * argument vector elements that contain spaces of double quotes. The |
| 449 | * <function>spawn*()</function> functions don't do any quoting or |
| 450 | * escaping, but on the other hand the startup code does do unquoting |
| 451 | * and unescaping in order to enable receiving arguments with embedded |
| 452 | * spaces or double quotes. To work around this asymmetry, |
| 453 | * g_spawn_async_with_pipes() will do quoting and escaping on argument |
| 454 | * vector elements that need it before calling the C runtime |
| 455 | * spawn() function. |
| 456 | * |
| 457 | * @envp is a %NULL-terminated array of strings, where each string |
| 458 | * has the form <literal>KEY=VALUE</literal>. This will become |
| 459 | * the child's environment. If @envp is %NULL, the child inherits its |
| 460 | * parent's environment. |
| 461 | * |
| 462 | * @flags should be the bitwise OR of any flags you want to affect the |
| 463 | * function's behaviour. The %G_SPAWN_DO_NOT_REAP_CHILD means that |
| 464 | * the child will not automatically be reaped; you must use a |
| 465 | * #GChildWatch source to be notified about the death of the child |
| 466 | * process. Eventually you must call g_spawn_close_pid() on the |
| 467 | * @child_pid, in order to free resources which may be associated |
| 468 | * with the child process. (On Unix, using a #GChildWatch source is |
| 469 | * equivalent to calling waitpid() or handling the %SIGCHLD signal |
| 470 | * manually. On Windows, calling g_spawn_close_pid() is equivalent |
| 471 | * to calling CloseHandle() on the process handle returned in |
| 472 | * @child_pid). |
| 473 | * |
| 474 | * %G_SPAWN_LEAVE_DESCRIPTORS_OPEN means that the parent's open file |
| 475 | * descriptors will be inherited by the child; otherwise all |
| 476 | * descriptors except stdin/stdout/stderr will be closed before |
| 477 | * calling exec() in the child. %G_SPAWN_SEARCH_PATH |
| 478 | * means that <literal>argv[0]</literal> need not be an absolute path, it |
| 479 | * will be looked for in the user's <envar>PATH</envar>. |
| 480 | * %G_SPAWN_STDOUT_TO_DEV_NULL means that the child's standard output will |
| 481 | * be discarded, instead of going to the same location as the parent's |
| 482 | * standard output. If you use this flag, @standard_output must be %NULL. |
| 483 | * %G_SPAWN_STDERR_TO_DEV_NULL means that the child's standard error |
| 484 | * will be discarded, instead of going to the same location as the parent's |
| 485 | * standard error. If you use this flag, @standard_error must be %NULL. |
| 486 | * %G_SPAWN_CHILD_INHERITS_STDIN means that the child will inherit the parent's |
| 487 | * standard input (by default, the child's standard input is attached to |
| 488 | * /dev/null). If you use this flag, @standard_input must be %NULL. |
| 489 | * %G_SPAWN_FILE_AND_ARGV_ZERO means that the first element of @argv is |
| 490 | * the file to execute, while the remaining elements are the |
| 491 | * actual argument vector to pass to the file. Normally |
| 492 | * g_spawn_async_with_pipes() uses @argv[0] as the file to execute, and |
| 493 | * passes all of @argv to the child. |
| 494 | * |
| 495 | * @child_setup and @user_data are a function and user data. On POSIX |
| 496 | * platforms, the function is called in the child after GLib has |
| 497 | * performed all the setup it plans to perform (including creating |
| 498 | * pipes, closing file descriptors, etc.) but before calling |
| 499 | * exec(). That is, @child_setup is called just |
| 500 | * before calling exec() in the child. Obviously |
| 501 | * actions taken in this function will only affect the child, not the |
| 502 | * parent. On Windows, there is no separate fork() and exec() |
| 503 | * functionality. Child processes are created and run right away with |
| 504 | * one API call, CreateProcess(). @child_setup is |
| 505 | * called in the parent process just before creating the child |
| 506 | * process. You should carefully consider what you do in @child_setup |
| 507 | * if you intend your software to be portable to Windows. |
| 508 | * |
| 509 | * If non-%NULL, @child_pid will on Unix be filled with the child's |
| 510 | * process ID. You can use the process ID to send signals to the |
| 511 | * child, or to waitpid() if you specified the |
| 512 | * %G_SPAWN_DO_NOT_REAP_CHILD flag. On Windows, @child_pid will be |
| 513 | * filled with a handle to the child process only if you specified the |
| 514 | * %G_SPAWN_DO_NOT_REAP_CHILD flag. You can then access the child |
| 515 | * process using the Win32 API, for example wait for its termination |
| 516 | * with the <function>WaitFor*()</function> functions, or examine its |
| 517 | * exit code with GetExitCodeProcess(). You should close the handle |
| 518 | * with CloseHandle() when you no longer need it. |
| 519 | * |
| 520 | * If non-%NULL, the @standard_input, @standard_output, @standard_error |
| 521 | * locations will be filled with file descriptors for writing to the child's |
| 522 | * standard input or reading from its standard output or standard error. |
| 523 | * The caller of g_spawn_async_with_pipes() must close these file descriptors |
| 524 | * when they are no longer in use. If these parameters are %NULL, the corresponding |
| 525 | * pipe won't be created. |
| 526 | * |
| 527 | * If @standard_input is NULL, the child's standard input is attached to /dev/null |
| 528 | * unless %G_SPAWN_CHILD_INHERITS_STDIN is set. |
| 529 | * |
| 530 | * If @standard_error is NULL, the child's standard error goes to the same location |
| 531 | * as the parent's standard error unless %G_SPAWN_STDERR_TO_DEV_NULL is set. |
| 532 | * |
| 533 | * If @standard_output is NULL, the child's standard output goes to the same location |
| 534 | * as the parent's standard output unless %G_SPAWN_STDOUT_TO_DEV_NULL is set. |
| 535 | * |
| 536 | * @error can be %NULL to ignore errors, or non-%NULL to report errors. |
| 537 | * If an error is set, the function returns %FALSE. Errors |
| 538 | * are reported even if they occur in the child (for example if the |
| 539 | * executable in <literal>argv[0]</literal> is not found). Typically |
| 540 | * the <literal>message</literal> field of returned errors should be displayed |
| 541 | * to users. Possible errors are those from the #G_SPAWN_ERROR domain. |
| 542 | * |
| 543 | * If an error occurs, @child_pid, @standard_input, @standard_output, |
| 544 | * and @standard_error will not be filled with valid values. |
| 545 | * |
| 546 | * If @child_pid is not %NULL and an error does not occur then the returned |
| 547 | * pid must be closed using g_spawn_close_pid(). |
| 548 | * |
| 549 | * Return value: %TRUE on success, %FALSE if an error was set |
| 550 | **/ |
| 551 | |
| 552 | static void |
| 553 | Waitpid (GPid *pid) |
| 554 | { |
| 555 | int stat_loc; |
| 556 | |
| 557 | waitpid (*pid, &stat_loc, 0); |
| 558 | /* Discard everything |
| 559 | */ |
| 560 | } |
| 561 | |
| 562 | gboolean |
| 563 | g_spawn_async_with_pipes (const gchar *working_directory, |
| 564 | gchar **argv, |
| 565 | gchar **envp, |
| 566 | GSpawnFlags flags, |
| 567 | GSpawnChildSetupFunc child_setup, |
| 568 | gpointer user_data, |
| 569 | GPid *child_pid, |
| 570 | gint *standard_input, |
| 571 | gint *standard_output, |
| 572 | gint *standard_error, |
| 573 | GError **error) |
| 574 | { |
| 575 | gboolean result; |
| 576 | int tid; |
| 577 | |
| 578 | g_return_val_if_fail (argv != NULL, FALSE); |
| 579 | g_return_val_if_fail (standard_output == NULL || |
| 580 | !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE); |
| 581 | g_return_val_if_fail (standard_error == NULL || |
| 582 | !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE); |
| 583 | /* can't inherit stdin if we have an input pipe. */ |
| 584 | g_return_val_if_fail (standard_input == NULL || |
| 585 | !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE); |
| 586 | |
| 587 | result = fork_exec_with_pipes (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), |
| 588 | working_directory, |
| 589 | argv, |
| 590 | envp, |
| 591 | !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN), |
| 592 | (flags & G_SPAWN_SEARCH_PATH) != 0, |
| 593 | (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0, |
| 594 | (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0, |
| 595 | (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0, |
| 596 | (flags & G_SPAWN_FILE_AND_ARGV_ZERO) != 0, |
| 597 | child_setup, |
| 598 | user_data, |
| 599 | child_pid, |
| 600 | standard_input, |
| 601 | standard_output, |
| 602 | standard_error, |
| 603 | error); |
| 604 | /* start a thread with waitpid() so the return code does not fill up memory |
| 605 | */ |
| 606 | tid = _beginthread (Waitpid, NULL, 0x2000, (void *) child_pid); |
| 607 | |
| 608 | return result; |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * g_spawn_command_line_sync: |
| 613 | * @command_line: a command line |
| 614 | * @standard_output: return location for child output |
| 615 | * @standard_error: return location for child errors |
| 616 | * @exit_status: return location for child exit status |
| 617 | * @error: return location for errors |
| 618 | * |
| 619 | * A simple version of g_spawn_sync() with little-used parameters |
| 620 | * removed, taking a command line instead of an argument vector. See |
| 621 | * g_spawn_sync() for full details. @command_line will be parsed by |
| 622 | * g_shell_parse_argv(). Unlike g_spawn_sync(), the %G_SPAWN_SEARCH_PATH flag |
| 623 | * is enabled. Note that %G_SPAWN_SEARCH_PATH can have security |
| 624 | * implications, so consider using g_spawn_sync() directly if |
| 625 | * appropriate. Possible errors are those from g_spawn_sync() and those |
| 626 | * from g_shell_parse_argv(). |
| 627 | * |
| 628 | * On Windows, please note the implications of g_shell_parse_argv() |
| 629 | * parsing @command_line. Space is a separator, and backslashes are |
| 630 | * special. Thus you cannot simply pass a @command_line containing |
| 631 | * canonical Windows paths, like "c:\\program files\\app\\app.exe", as |
| 632 | * the backslashes will be eaten, and the space will act as a |
| 633 | * separator. You need to enclose such paths with single quotes, like |
| 634 | * "'c:\\program files\\app\\app.exe' 'e:\\folder\\argument.txt'". |
| 635 | * |
| 636 | * Return value: %TRUE on success, %FALSE if an error was set |
| 637 | **/ |
| 638 | gboolean |
| 639 | g_spawn_command_line_sync (const gchar *command_line, |
| 640 | gchar **standard_output, |
| 641 | gchar **standard_error, |
| 642 | gint *exit_status, |
| 643 | GError **error) |
| 644 | { |
| 645 | gboolean retval; |
| 646 | gchar **argv = NULL; |
| 647 | |
| 648 | g_return_val_if_fail (command_line != NULL, FALSE); |
| 649 | |
| 650 | if (!g_shell_parse_argv (command_line, |
| 651 | NULL, &argv, |
| 652 | error)) |
| 653 | return FALSE; |
| 654 | |
| 655 | retval = g_spawn_sync (NULL, |
| 656 | argv, |
| 657 | NULL, |
| 658 | G_SPAWN_SEARCH_PATH, |
| 659 | NULL, |
| 660 | NULL, |
| 661 | standard_output, |
| 662 | standard_error, |
| 663 | exit_status, |
| 664 | error); |
| 665 | g_strfreev (argv); |
| 666 | |
| 667 | return retval; |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * g_spawn_command_line_async: |
| 672 | * @command_line: a command line |
| 673 | * @error: return location for errors |
| 674 | * |
| 675 | * A simple version of g_spawn_async() that parses a command line with |
| 676 | * g_shell_parse_argv() and passes it to g_spawn_async(). Runs a |
| 677 | * command line in the background. Unlike g_spawn_async(), the |
| 678 | * %G_SPAWN_SEARCH_PATH flag is enabled, other flags are not. Note |
| 679 | * that %G_SPAWN_SEARCH_PATH can have security implications, so |
| 680 | * consider using g_spawn_async() directly if appropriate. Possible |
| 681 | * errors are those from g_shell_parse_argv() and g_spawn_async(). |
| 682 | * |
| 683 | * The same concerns on Windows apply as for g_spawn_command_line_sync(). |
| 684 | * |
| 685 | * Return value: %TRUE on success, %FALSE if error is set. |
| 686 | **/ |
| 687 | gboolean |
| 688 | g_spawn_command_line_async (const gchar *command_line, |
| 689 | GError **error) |
| 690 | { |
| 691 | gboolean retval; |
| 692 | gchar **argv = NULL; |
| 693 | |
| 694 | g_return_val_if_fail (command_line != NULL, FALSE); |
| 695 | |
| 696 | if (!g_shell_parse_argv (command_line, |
| 697 | NULL, &argv, |
| 698 | error)) |
| 699 | return FALSE; |
| 700 | |
| 701 | retval = g_spawn_async (NULL, |
| 702 | argv, |
| 703 | NULL, |
| 704 | G_SPAWN_SEARCH_PATH, |
| 705 | NULL, |
| 706 | NULL, |
| 707 | NULL, |
| 708 | error); |
| 709 | g_strfreev (argv); |
| 710 | |
| 711 | return retval; |
| 712 | } |
| 713 | |
| 714 | static gint |
| 715 | exec_err_to_g_error (gint en) |
| 716 | { |
| 717 | switch (en) |
| 718 | { |
| 719 | #ifdef EACCES |
| 720 | case EACCES: |
| 721 | return G_SPAWN_ERROR_ACCES; |
| 722 | break; |
| 723 | #endif |
| 724 | |
| 725 | #ifdef EPERM |
| 726 | case EPERM: |
| 727 | return G_SPAWN_ERROR_PERM; |
| 728 | break; |
| 729 | #endif |
| 730 | |
| 731 | #ifdef E2BIG |
| 732 | case E2BIG: |
| 733 | return G_SPAWN_ERROR_2BIG; |
| 734 | break; |
| 735 | #endif |
| 736 | |
| 737 | #ifdef ENOEXEC |
| 738 | case ENOEXEC: |
| 739 | return G_SPAWN_ERROR_NOEXEC; |
| 740 | break; |
| 741 | #endif |
| 742 | |
| 743 | #ifdef ENAMETOOLONG |
| 744 | case ENAMETOOLONG: |
| 745 | return G_SPAWN_ERROR_NAMETOOLONG; |
| 746 | break; |
| 747 | #endif |
| 748 | |
| 749 | #ifdef ENOENT |
| 750 | case ENOENT: |
| 751 | return G_SPAWN_ERROR_NOENT; |
| 752 | break; |
| 753 | #endif |
| 754 | |
| 755 | #ifdef ENOMEM |
| 756 | case ENOMEM: |
| 757 | return G_SPAWN_ERROR_NOMEM; |
| 758 | break; |
| 759 | #endif |
| 760 | |
| 761 | #ifdef ENOTDIR |
| 762 | case ENOTDIR: |
| 763 | return G_SPAWN_ERROR_NOTDIR; |
| 764 | break; |
| 765 | #endif |
| 766 | |
| 767 | #ifdef ELOOP |
| 768 | case ELOOP: |
| 769 | return G_SPAWN_ERROR_LOOP; |
| 770 | break; |
| 771 | #endif |
| 772 | |
| 773 | #ifdef ETXTBUSY |
| 774 | case ETXTBUSY: |
| 775 | return G_SPAWN_ERROR_TXTBUSY; |
| 776 | break; |
| 777 | #endif |
| 778 | |
| 779 | #ifdef EIO |
| 780 | case EIO: |
| 781 | return G_SPAWN_ERROR_IO; |
| 782 | break; |
| 783 | #endif |
| 784 | |
| 785 | #ifdef ENFILE |
| 786 | case ENFILE: |
| 787 | return G_SPAWN_ERROR_NFILE; |
| 788 | break; |
| 789 | #endif |
| 790 | |
| 791 | #ifdef EMFILE |
| 792 | case EMFILE: |
| 793 | return G_SPAWN_ERROR_MFILE; |
| 794 | break; |
| 795 | #endif |
| 796 | |
| 797 | #ifdef EINVAL |
| 798 | case EINVAL: |
| 799 | return G_SPAWN_ERROR_INVAL; |
| 800 | break; |
| 801 | #endif |
| 802 | |
| 803 | #ifdef EISDIR |
| 804 | case EISDIR: |
| 805 | return G_SPAWN_ERROR_ISDIR; |
| 806 | break; |
| 807 | #endif |
| 808 | |
| 809 | #ifdef ELIBBAD |
| 810 | case ELIBBAD: |
| 811 | return G_SPAWN_ERROR_LIBBAD; |
| 812 | break; |
| 813 | #endif |
| 814 | |
| 815 | default: |
| 816 | return G_SPAWN_ERROR_FAILED; |
| 817 | break; |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | static void |
| 822 | set_cloexec (gint fd) |
| 823 | { |
| 824 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
| 825 | } |
| 826 | |
| 827 | static gint |
| 828 | sane_dup2 (gint fd1, gint fd2) |
| 829 | { |
| 830 | gint ret; |
| 831 | |
| 832 | retry: |
| 833 | ret = dup2 (fd1, fd2); |
| 834 | if (ret < 0 && errno == EINTR) |
| 835 | goto retry; |
| 836 | |
| 837 | return ret; |
| 838 | } |
| 839 | |
| 840 | enum |
| 841 | { |
| 842 | CHILD_CHDIR_FAILED, |
| 843 | CHILD_EXEC_FAILED, |
| 844 | CHILD_DUP2_FAILED, |
| 845 | CHILD_FORK_FAILED |
| 846 | }; |
| 847 | |
| 848 | static gint |
| 849 | do_exec (gint stdin_fd, |
| 850 | gint stdout_fd, |
| 851 | gint stderr_fd, |
| 852 | const gchar *working_directory, |
| 853 | gchar **argv, |
| 854 | gchar **envp, |
| 855 | gboolean close_descriptors, |
| 856 | gboolean search_path, |
| 857 | gboolean stdout_to_null, |
| 858 | gboolean stderr_to_null, |
| 859 | gboolean child_inherits_stdin, |
| 860 | gboolean file_and_argv_zero, |
| 861 | GSpawnChildSetupFunc child_setup, |
| 862 | gpointer user_data, |
| 863 | GError **error) |
| 864 | { |
| 865 | gint stdin_sav = -1; |
| 866 | gint stdout_sav = -1; |
| 867 | gint stderr_sav = -1; |
| 868 | gint pid = -1; |
| 869 | char wd[1024]; |
| 870 | |
| 871 | /* Save current working directory |
| 872 | */ |
| 873 | if (working_directory && *working_directory) |
| 874 | _getcwd2 (wd,sizeof(wd)); |
| 875 | |
| 876 | if (working_directory && *working_directory && _chdir2 (working_directory) < 0) |
| 877 | g_set_error (error, |
| 878 | G_SPAWN_ERROR, |
| 879 | G_SPAWN_ERROR_CHDIR, |
| 880 | _("Failed to change to directory '%s' (%s)"), |
| 881 | working_directory, |
| 882 | g_strerror (errno)); |
| 883 | |
| 884 | |
| 885 | /* Close all file descriptors but stdin stdout and stderr as |
| 886 | * soon as we exec. Note that this includes |
| 887 | * child_err_report_fd, which keeps the parent from blocking |
| 888 | * forever on the other end of that pipe. |
| 889 | */ |
| 890 | |
| 891 | if (close_descriptors) |
| 892 | { |
| 893 | gint open_max; |
| 894 | gint i; |
| 895 | |
| 896 | open_max = /*sysconf (_SC_OPEN_MAX)*/256; |
| 897 | for (i = 3; i < open_max; i++) |
| 898 | set_cloexec (i); |
| 899 | } |
| 900 | |
| 901 | /* Redirect pipes as required |
| 902 | * Save the handles before closing them!!!! |
| 903 | */ |
| 904 | |
| 905 | if (stdin_fd >= 0) |
| 906 | { |
| 907 | stdin_sav = dup (0); |
| 908 | /* dup2 can't actually fail here I don't think */ |
| 909 | |
| 910 | if (sane_dup2 (stdin_fd, 0) < 0) |
| 911 | g_set_error (error, |
| 912 | G_SPAWN_ERROR, |
| 913 | G_SPAWN_ERROR_FAILED, |
| 914 | _("Failed to redirect input or output of child process (%s)"), |
| 915 | g_strerror (errno)); |
| 916 | |
| 917 | /* ignore this if it doesn't work */ |
| 918 | close_and_invalidate (&stdin_fd); |
| 919 | } |
| 920 | else if (!child_inherits_stdin) |
| 921 | { |
| 922 | /* Keep process from blocking on a read of stdin */ |
| 923 | gint read_null = open ("NUL", O_RDONLY); |
| 924 | stdin_sav = dup (0); |
| 925 | sane_dup2 (read_null, 0); |
| 926 | close_and_invalidate (&read_null); |
| 927 | } |
| 928 | |
| 929 | if (stdout_fd >= 0) |
| 930 | { |
| 931 | stdout_sav = dup (1); |
| 932 | /* dup2 can't actually fail here I don't think */ |
| 933 | |
| 934 | if (sane_dup2 (stdout_fd, 1) < 0) |
| 935 | g_set_error (error, |
| 936 | G_SPAWN_ERROR, |
| 937 | G_SPAWN_ERROR_FAILED, |
| 938 | _("Failed to redirect input or ouput of child process (%s)"), |
| 939 | g_strerror (errno)); |
| 940 | |
| 941 | /* ignore this if it doesn't work */ |
| 942 | close_and_invalidate (&stdout_fd); |
| 943 | } |
| 944 | else if (stdout_to_null) |
| 945 | { |
| 946 | gint write_null = open ("NUL", O_WRONLY); |
| 947 | stdout_sav = dup (1); |
| 948 | sane_dup2 (write_null, 1); |
| 949 | close_and_invalidate (&write_null); |
| 950 | } |
| 951 | |
| 952 | if (stderr_fd >= 0) |
| 953 | { |
| 954 | stderr_sav = dup (2); |
| 955 | /* dup2 can't actually fail here I don't think */ |
| 956 | |
| 957 | if (sane_dup2 (stderr_fd, 2) < 0) |
| 958 | g_set_error (error, |
| 959 | G_SPAWN_ERROR, |
| 960 | G_SPAWN_ERROR_FAILED, |
| 961 | _("Failed to redirect input or ouput of child process (%s)"), |
| 962 | g_strerror (errno)); |
| 963 | |
| 964 | /* ignore this if it doesn't work */ |
| 965 | close_and_invalidate (&stderr_fd); |
| 966 | } |
| 967 | else if (stderr_to_null) |
| 968 | { |
| 969 | gint write_null = open ("NUL", O_WRONLY); |
| 970 | stderr_sav = dup (2); |
| 971 | sane_dup2 (write_null, 2); |
| 972 | close_and_invalidate (&write_null); |
| 973 | } |
| 974 | |
| 975 | /* Call user function just before we exec */ |
| 976 | if (child_setup) |
| 977 | { |
| 978 | (* child_setup) (user_data); |
| 979 | } |
| 980 | |
| 981 | pid = g_execute (argv[0], |
| 982 | file_and_argv_zero ? argv + 1 : argv, |
| 983 | envp, search_path); |
| 984 | |
| 985 | /* Restore working directory |
| 986 | */ |
| 987 | if (working_directory && *working_directory) |
| 988 | _chdir2 (wd); |
| 989 | |
| 990 | /* Restore the saved handles |
| 991 | */ |
| 992 | if (stdin_sav > -1) |
| 993 | { |
| 994 | sane_dup2 (stdin_sav, 0); |
| 995 | close (stdin_sav); |
| 996 | } |
| 997 | |
| 998 | if (stdout_sav > -1) |
| 999 | { |
| 1000 | sane_dup2 (stdout_sav, 1); |
| 1001 | close (stdout_sav); |
| 1002 | } |
| 1003 | |
| 1004 | if (stderr_sav > -1) |
| 1005 | { |
| 1006 | sane_dup2 (stderr_sav, 2); |
| 1007 | close (stderr_sav); |
| 1008 | } |
| 1009 | |
| 1010 | return pid; |
| 1011 | |
| 1012 | } |
| 1013 | |
| 1014 | static gboolean |
| 1015 | fork_exec_with_pipes (gboolean intermediate_child, |
| 1016 | const gchar *working_directory, |
| 1017 | gchar **argv, |
| 1018 | gchar **envp, |
| 1019 | gboolean close_descriptors, |
| 1020 | gboolean search_path, |
| 1021 | gboolean stdout_to_null, |
| 1022 | gboolean stderr_to_null, |
| 1023 | gboolean child_inherits_stdin, |
| 1024 | gboolean file_and_argv_zero, |
| 1025 | GSpawnChildSetupFunc child_setup, |
| 1026 | gpointer user_data, |
| 1027 | GPid *child_pid, |
| 1028 | gint *standard_input, |
| 1029 | gint *standard_output, |
| 1030 | gint *standard_error, |
| 1031 | GError **error) |
| 1032 | { |
| 1033 | GPid pid = -1; |
| 1034 | gint stdin_pipe[2] = { -1, -1 }; |
| 1035 | gint stdout_pipe[2] = { -1, -1 }; |
| 1036 | gint stderr_pipe[2] = { -1, -1 }; |
| 1037 | |
| 1038 | if (standard_input && !make_pipe (stdin_pipe, error)) |
| 1039 | goto cleanup_and_fail; |
| 1040 | |
| 1041 | if (standard_output && !make_pipe (stdout_pipe, error)) |
| 1042 | goto cleanup_and_fail; |
| 1043 | |
| 1044 | if (standard_error && !make_pipe (stderr_pipe, error)) |
| 1045 | goto cleanup_and_fail; |
| 1046 | |
| 1047 | /* Be sure we crash if the parent exits |
| 1048 | * and we write to the err_report_pipe |
| 1049 | */ |
| 1050 | signal (SIGPIPE, SIG_DFL); |
| 1051 | |
| 1052 | /* Close the parent's end of the pipes; |
| 1053 | * not needed in the close_descriptors case, |
| 1054 | * though |
| 1055 | */ |
| 1056 | set_cloexec (stdin_pipe[1]); |
| 1057 | set_cloexec (stdout_pipe[0]); |
| 1058 | set_cloexec (stderr_pipe[0]); |
| 1059 | |
| 1060 | /* Spawn the child |
| 1061 | */ |
| 1062 | pid = do_exec (stdin_pipe[0], |
| 1063 | stdout_pipe[1], |
| 1064 | stderr_pipe[1], |
| 1065 | working_directory, |
| 1066 | argv, |
| 1067 | envp, |
| 1068 | close_descriptors, |
| 1069 | search_path, |
| 1070 | stdout_to_null, |
| 1071 | stderr_to_null, |
| 1072 | child_inherits_stdin, |
| 1073 | file_and_argv_zero, |
| 1074 | child_setup, |
| 1075 | user_data, |
| 1076 | error); |
| 1077 | |
| 1078 | { |
| 1079 | |
| 1080 | /* Close the uncared-about ends of the pipes */ |
| 1081 | close_and_invalidate (&stdin_pipe[0]); |
| 1082 | close_and_invalidate (&stdout_pipe[1]); |
| 1083 | close_and_invalidate (&stderr_pipe[1]); |
| 1084 | |
| 1085 | /* Success against all odds! return the information */ |
| 1086 | |
| 1087 | if (child_pid) |
| 1088 | *child_pid = pid; |
| 1089 | |
| 1090 | if (standard_input) |
| 1091 | *standard_input = stdin_pipe[1]; |
| 1092 | if (standard_output) |
| 1093 | *standard_output = stdout_pipe[0]; |
| 1094 | if (standard_error) |
| 1095 | *standard_error = stderr_pipe[0]; |
| 1096 | |
| 1097 | return TRUE; |
| 1098 | } |
| 1099 | |
| 1100 | cleanup_and_fail: |
| 1101 | |
| 1102 | /* There was an error from the Child, reap the child to avoid it being |
| 1103 | a zombie. |
| 1104 | */ |
| 1105 | |
| 1106 | if (pid > 0) |
| 1107 | { |
| 1108 | wait_failed: |
| 1109 | if (waitpid (pid, NULL, 0) < 0) |
| 1110 | { |
| 1111 | if (errno == EINTR) |
| 1112 | goto wait_failed; |
| 1113 | else if (errno == ECHILD) |
| 1114 | ; /* do nothing, child already reaped */ |
| 1115 | else |
| 1116 | g_warning ("waitpid() should not fail in " |
| 1117 | "'fork_exec_with_pipes'"); |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | close_and_invalidate (&stdin_pipe[0]); |
| 1122 | close_and_invalidate (&stdin_pipe[1]); |
| 1123 | close_and_invalidate (&stdout_pipe[0]); |
| 1124 | close_and_invalidate (&stdout_pipe[1]); |
| 1125 | close_and_invalidate (&stderr_pipe[0]); |
| 1126 | close_and_invalidate (&stderr_pipe[1]); |
| 1127 | |
| 1128 | return FALSE; |
| 1129 | } |
| 1130 | |
| 1131 | static gboolean |
| 1132 | make_pipe (gint p[2], |
| 1133 | GError **error) |
| 1134 | { |
| 1135 | if (pipe (p) < 0) |
| 1136 | { |
| 1137 | g_set_error (error, |
| 1138 | G_SPAWN_ERROR, |
| 1139 | G_SPAWN_ERROR_FAILED, |
| 1140 | _("Failed to create pipe for communicating with child process (%s)"), |
| 1141 | g_strerror (errno)); |
| 1142 | return FALSE; |
| 1143 | } |
| 1144 | else |
| 1145 | return TRUE; |
| 1146 | } |
| 1147 | |
| 1148 | /* Based on execvp from GNU C Library */ |
| 1149 | |
| 1150 | static gint |
| 1151 | script_execute (const gchar *file, |
| 1152 | gchar **argv, |
| 1153 | gchar **envp, |
| 1154 | gboolean search_path) |
| 1155 | { |
| 1156 | /* Count the arguments. */ |
| 1157 | int argc = 0; |
| 1158 | gint pid = -1; |
| 1159 | |
| 1160 | while (argv[argc]) |
| 1161 | ++argc; |
| 1162 | |
| 1163 | /* Construct an argument list for the shell. */ |
| 1164 | { |
| 1165 | gchar **new_argv; |
| 1166 | |
| 1167 | new_argv = g_new0 (gchar*, argc + 2); /* /bin/sh and NULL */ |
| 1168 | |
| 1169 | new_argv[0] = (char *) "sh.exe"; |
| 1170 | new_argv[1] = (char *) file; |
| 1171 | while (argc > 0) |
| 1172 | { |
| 1173 | new_argv[argc + 1] = argv[argc]; |
| 1174 | --argc; |
| 1175 | } |
| 1176 | |
| 1177 | /* Execute the shell. */ |
| 1178 | if (envp) |
| 1179 | pid = spawnve (P_NOWAIT, new_argv[0], new_argv, envp); |
| 1180 | else |
| 1181 | pid = spawnv (P_NOWAIT, new_argv[0], new_argv); |
| 1182 | |
| 1183 | g_free (new_argv); |
| 1184 | |
| 1185 | return pid; |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | static gint |
| 1190 | g_execute (const gchar *file, |
| 1191 | gchar **argv, |
| 1192 | gchar **envp, |
| 1193 | gboolean search_path) |
| 1194 | { |
| 1195 | gint pid; |
| 1196 | |
| 1197 | if (*file == '\0') |
| 1198 | { |
| 1199 | /* We check the simple case first. */ |
| 1200 | errno = ENOENT; |
| 1201 | return -1; |
| 1202 | } |
| 1203 | |
| 1204 | if (envp) |
| 1205 | pid = spawnve (P_NOWAIT, file, argv, envp); |
| 1206 | else |
| 1207 | pid = spawnv (P_NOWAIT, file, argv); |
| 1208 | |
| 1209 | if (pid == -1) |
| 1210 | pid = script_execute (file, argv, envp, FALSE); |
| 1211 | |
| 1212 | return pid; |
| 1213 | } |
| 1214 | |
| 1215 | /** |
| 1216 | * g_spawn_close_pid: |
| 1217 | * @pid: The process identifier to close |
| 1218 | * |
| 1219 | * On some platforms, notably WIN32, the #GPid type represents a resource |
| 1220 | * which must be closed to prevent resource leaking. g_spawn_close_pid() |
| 1221 | * is provided for this purpose. It should be used on all platforms, even |
| 1222 | * though it doesn't do anything under UNIX. |
| 1223 | **/ |
| 1224 | void |
| 1225 | g_spawn_close_pid (GPid pid) |
| 1226 | { |
| 1227 | } |
| 1228 | |
| 1229 | #define __G_SPAWN_C__ |
| 1230 | #include "galiasdef.c" |