mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-02-24 11:12:11 +01:00
Merge branch '303-win32-spawn' into 'master'
gspawn: Make error codes on Windows more specific Closes #303 See merge request GNOME/glib!100
This commit is contained in:
commit
603d40467c
@ -220,6 +220,7 @@ EXTRA_libglib_2_0_la_SOURCES = \
|
||||
giounix.c \
|
||||
giowin32.c \
|
||||
gspawn.c \
|
||||
gspawn-private.h \
|
||||
gspawn-win32.c \
|
||||
gwin32.c
|
||||
|
||||
|
115
glib/gspawn-private.h
Normal file
115
glib/gspawn-private.h
Normal file
@ -0,0 +1,115 @@
|
||||
/* gspawn.c - Process launching
|
||||
*
|
||||
* Copyright 2000 Red Hat, Inc.
|
||||
* g_execvpe implementation based on GNU libc execvp:
|
||||
* Copyright 1991, 92, 95, 96, 97, 98, 99 Free Software Foundation, Inc.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this library; if not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include "gspawn.h"
|
||||
|
||||
static inline gint
|
||||
_g_spawn_exec_err_to_g_error (gint en)
|
||||
{
|
||||
switch (en)
|
||||
{
|
||||
#ifdef EACCES
|
||||
case EACCES:
|
||||
return G_SPAWN_ERROR_ACCES;
|
||||
#endif
|
||||
|
||||
#ifdef EPERM
|
||||
case EPERM:
|
||||
return G_SPAWN_ERROR_PERM;
|
||||
#endif
|
||||
|
||||
#ifdef E2BIG
|
||||
case E2BIG:
|
||||
return G_SPAWN_ERROR_TOO_BIG;
|
||||
#endif
|
||||
|
||||
#ifdef ENOEXEC
|
||||
case ENOEXEC:
|
||||
return G_SPAWN_ERROR_NOEXEC;
|
||||
#endif
|
||||
|
||||
#ifdef ENAMETOOLONG
|
||||
case ENAMETOOLONG:
|
||||
return G_SPAWN_ERROR_NAMETOOLONG;
|
||||
#endif
|
||||
|
||||
#ifdef ENOENT
|
||||
case ENOENT:
|
||||
return G_SPAWN_ERROR_NOENT;
|
||||
#endif
|
||||
|
||||
#ifdef ENOMEM
|
||||
case ENOMEM:
|
||||
return G_SPAWN_ERROR_NOMEM;
|
||||
#endif
|
||||
|
||||
#ifdef ENOTDIR
|
||||
case ENOTDIR:
|
||||
return G_SPAWN_ERROR_NOTDIR;
|
||||
#endif
|
||||
|
||||
#ifdef ELOOP
|
||||
case ELOOP:
|
||||
return G_SPAWN_ERROR_LOOP;
|
||||
#endif
|
||||
|
||||
#ifdef ETXTBUSY
|
||||
case ETXTBUSY:
|
||||
return G_SPAWN_ERROR_TXTBUSY;
|
||||
#endif
|
||||
|
||||
#ifdef EIO
|
||||
case EIO:
|
||||
return G_SPAWN_ERROR_IO;
|
||||
#endif
|
||||
|
||||
#ifdef ENFILE
|
||||
case ENFILE:
|
||||
return G_SPAWN_ERROR_NFILE;
|
||||
#endif
|
||||
|
||||
#ifdef EMFILE
|
||||
case EMFILE:
|
||||
return G_SPAWN_ERROR_MFILE;
|
||||
#endif
|
||||
|
||||
#ifdef EINVAL
|
||||
case EINVAL:
|
||||
return G_SPAWN_ERROR_INVAL;
|
||||
#endif
|
||||
|
||||
#ifdef EISDIR
|
||||
case EISDIR:
|
||||
return G_SPAWN_ERROR_ISDIR;
|
||||
#endif
|
||||
|
||||
#ifdef ELIBBAD
|
||||
case ELIBBAD:
|
||||
return G_SPAWN_ERROR_LIBBAD;
|
||||
#endif
|
||||
|
||||
default:
|
||||
return G_SPAWN_ERROR_FAILED;
|
||||
}
|
||||
}
|
@ -352,7 +352,12 @@ main (int ignored_argc, char **ignored_argv)
|
||||
saved_errno = errno;
|
||||
|
||||
if (handle == -1 && saved_errno != 0)
|
||||
write_err_and_exit (child_err_report_fd, CHILD_SPAWN_FAILED);
|
||||
{
|
||||
int ec = (saved_errno == ENOENT)
|
||||
? CHILD_SPAWN_NOENT
|
||||
: CHILD_SPAWN_FAILED;
|
||||
write_err_and_exit (child_err_report_fd, ec);
|
||||
}
|
||||
|
||||
write (child_err_report_fd, &no_error, sizeof (no_error));
|
||||
write (child_err_report_fd, &handle, sizeof (handle));
|
||||
|
@ -46,6 +46,7 @@
|
||||
#include "glib-private.h"
|
||||
#include "gprintfint.h"
|
||||
#include "glibintl.h"
|
||||
#include "gspawn-private.h"
|
||||
#include "gthread.h"
|
||||
|
||||
#include <string.h>
|
||||
@ -86,6 +87,7 @@ enum
|
||||
CHILD_NO_ERROR,
|
||||
CHILD_CHDIR_FAILED,
|
||||
CHILD_SPAWN_FAILED,
|
||||
CHILD_SPAWN_NOENT,
|
||||
};
|
||||
|
||||
enum {
|
||||
@ -316,6 +318,7 @@ read_helper_report (int fd,
|
||||
while (bytes < sizeof(gintptr)*2)
|
||||
{
|
||||
gint chunk;
|
||||
int errsv;
|
||||
|
||||
if (debug)
|
||||
g_print ("%s:read_helper_report: read %" G_GSIZE_FORMAT "...\n",
|
||||
@ -324,14 +327,13 @@ read_helper_report (int fd,
|
||||
|
||||
chunk = read (fd, ((gchar*)report) + bytes,
|
||||
sizeof(gintptr)*2 - bytes);
|
||||
errsv = errno;
|
||||
|
||||
if (debug)
|
||||
g_print ("...got %d bytes\n", chunk);
|
||||
|
||||
if (chunk < 0)
|
||||
{
|
||||
int errsv = errno;
|
||||
|
||||
/* Some weird shit happened, bail out */
|
||||
g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
|
||||
_("Failed to read from child pipe (%s)"),
|
||||
@ -374,6 +376,11 @@ set_child_error (gintptr report[2],
|
||||
_("Failed to execute child process (%s)"),
|
||||
g_strerror (report[1]));
|
||||
break;
|
||||
case CHILD_SPAWN_NOENT:
|
||||
g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_NOENT,
|
||||
_("Failed to execute child process (%s)"),
|
||||
g_strerror (report[1]));
|
||||
break;
|
||||
default:
|
||||
g_assert_not_reached ();
|
||||
}
|
||||
@ -429,7 +436,7 @@ do_spawn_directly (gint *exit_status,
|
||||
const int mode = (exit_status == NULL) ? P_NOWAIT : P_WAIT;
|
||||
char **new_argv;
|
||||
gintptr rc = -1;
|
||||
int saved_errno;
|
||||
int errsv;
|
||||
GError *conv_error = NULL;
|
||||
gint conv_error_index;
|
||||
wchar_t *wargv0, **wargv, **wenvp;
|
||||
@ -481,17 +488,17 @@ do_spawn_directly (gint *exit_status,
|
||||
else
|
||||
rc = _wspawnv (mode, wargv0, (const wchar_t **) wargv);
|
||||
|
||||
errsv = errno;
|
||||
|
||||
g_free (wargv0);
|
||||
g_strfreev ((gchar **) wargv);
|
||||
g_strfreev ((gchar **) wenvp);
|
||||
|
||||
saved_errno = errno;
|
||||
|
||||
if (rc == -1 && saved_errno != 0)
|
||||
if (rc == -1 && errsv != 0)
|
||||
{
|
||||
g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
|
||||
g_set_error (error, G_SPAWN_ERROR, _g_spawn_exec_err_to_g_error (errsv),
|
||||
_("Failed to execute child process (%s)"),
|
||||
g_strerror (saved_errno));
|
||||
g_strerror (errsv));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
@ -532,7 +539,7 @@ do_spawn_with_pipes (gint *exit_status,
|
||||
char **new_argv;
|
||||
int i;
|
||||
gintptr rc = -1;
|
||||
int saved_errno;
|
||||
int errsv;
|
||||
int argc;
|
||||
int stdin_pipe[2] = { -1, -1 };
|
||||
int stdout_pipe[2] = { -1, -1 };
|
||||
@ -752,7 +759,7 @@ do_spawn_with_pipes (gint *exit_status,
|
||||
else
|
||||
rc = _wspawnvp (P_NOWAIT, whelper, (const wchar_t **) wargv);
|
||||
|
||||
saved_errno = errno;
|
||||
errsv = errno;
|
||||
|
||||
g_free (whelper);
|
||||
g_strfreev ((gchar **) wargv);
|
||||
@ -774,11 +781,11 @@ do_spawn_with_pipes (gint *exit_status,
|
||||
g_free (new_argv);
|
||||
|
||||
/* Check if gspawn-win32-helper couldn't be run */
|
||||
if (rc == -1 && saved_errno != 0)
|
||||
if (rc == -1 && errsv != 0)
|
||||
{
|
||||
g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
|
||||
_("Failed to execute helper program (%s)"),
|
||||
g_strerror (saved_errno));
|
||||
g_strerror (errsv));
|
||||
goto cleanup_and_fail;
|
||||
}
|
||||
|
||||
|
110
glib/gspawn.c
110
glib/gspawn.c
@ -40,6 +40,7 @@
|
||||
#endif /* HAVE_SYS_RESOURCE_H */
|
||||
|
||||
#include "gspawn.h"
|
||||
#include "gspawn-private.h"
|
||||
#include "gthread.h"
|
||||
#include "glib/gstdio.h"
|
||||
|
||||
@ -925,113 +926,6 @@ g_spawn_check_exit_status (gint exit_status,
|
||||
return ret;
|
||||
}
|
||||
|
||||
static gint
|
||||
exec_err_to_g_error (gint en)
|
||||
{
|
||||
switch (en)
|
||||
{
|
||||
#ifdef EACCES
|
||||
case EACCES:
|
||||
return G_SPAWN_ERROR_ACCES;
|
||||
break;
|
||||
#endif
|
||||
|
||||
#ifdef EPERM
|
||||
case EPERM:
|
||||
return G_SPAWN_ERROR_PERM;
|
||||
break;
|
||||
#endif
|
||||
|
||||
#ifdef E2BIG
|
||||
case E2BIG:
|
||||
return G_SPAWN_ERROR_TOO_BIG;
|
||||
break;
|
||||
#endif
|
||||
|
||||
#ifdef ENOEXEC
|
||||
case ENOEXEC:
|
||||
return G_SPAWN_ERROR_NOEXEC;
|
||||
break;
|
||||
#endif
|
||||
|
||||
#ifdef ENAMETOOLONG
|
||||
case ENAMETOOLONG:
|
||||
return G_SPAWN_ERROR_NAMETOOLONG;
|
||||
break;
|
||||
#endif
|
||||
|
||||
#ifdef ENOENT
|
||||
case ENOENT:
|
||||
return G_SPAWN_ERROR_NOENT;
|
||||
break;
|
||||
#endif
|
||||
|
||||
#ifdef ENOMEM
|
||||
case ENOMEM:
|
||||
return G_SPAWN_ERROR_NOMEM;
|
||||
break;
|
||||
#endif
|
||||
|
||||
#ifdef ENOTDIR
|
||||
case ENOTDIR:
|
||||
return G_SPAWN_ERROR_NOTDIR;
|
||||
break;
|
||||
#endif
|
||||
|
||||
#ifdef ELOOP
|
||||
case ELOOP:
|
||||
return G_SPAWN_ERROR_LOOP;
|
||||
break;
|
||||
#endif
|
||||
|
||||
#ifdef ETXTBUSY
|
||||
case ETXTBUSY:
|
||||
return G_SPAWN_ERROR_TXTBUSY;
|
||||
break;
|
||||
#endif
|
||||
|
||||
#ifdef EIO
|
||||
case EIO:
|
||||
return G_SPAWN_ERROR_IO;
|
||||
break;
|
||||
#endif
|
||||
|
||||
#ifdef ENFILE
|
||||
case ENFILE:
|
||||
return G_SPAWN_ERROR_NFILE;
|
||||
break;
|
||||
#endif
|
||||
|
||||
#ifdef EMFILE
|
||||
case EMFILE:
|
||||
return G_SPAWN_ERROR_MFILE;
|
||||
break;
|
||||
#endif
|
||||
|
||||
#ifdef EINVAL
|
||||
case EINVAL:
|
||||
return G_SPAWN_ERROR_INVAL;
|
||||
break;
|
||||
#endif
|
||||
|
||||
#ifdef EISDIR
|
||||
case EISDIR:
|
||||
return G_SPAWN_ERROR_ISDIR;
|
||||
break;
|
||||
#endif
|
||||
|
||||
#ifdef ELIBBAD
|
||||
case ELIBBAD:
|
||||
return G_SPAWN_ERROR_LIBBAD;
|
||||
break;
|
||||
#endif
|
||||
|
||||
default:
|
||||
return G_SPAWN_ERROR_FAILED;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static gssize
|
||||
write_all (gint fd, gconstpointer vbuf, gsize to_write)
|
||||
{
|
||||
@ -1549,7 +1443,7 @@ fork_exec_with_pipes (gboolean intermediate_child,
|
||||
case CHILD_EXEC_FAILED:
|
||||
g_set_error (error,
|
||||
G_SPAWN_ERROR,
|
||||
exec_err_to_g_error (buf[1]),
|
||||
_g_spawn_exec_err_to_g_error (buf[1]),
|
||||
_("Failed to execute child process “%s” (%s)"),
|
||||
argv[0],
|
||||
g_strerror (buf[1]));
|
||||
|
@ -200,6 +200,28 @@ test_spawn_script (void)
|
||||
g_ptr_array_free (argv, TRUE);
|
||||
}
|
||||
|
||||
/* Test that spawning a non-existent executable returns %G_SPAWN_ERROR_NOENT. */
|
||||
static void
|
||||
test_spawn_nonexistent (void)
|
||||
{
|
||||
GError *error = NULL;
|
||||
GPtrArray *argv = NULL;
|
||||
gchar *stdout_str = NULL;
|
||||
gint exit_status = -1;
|
||||
|
||||
argv = g_ptr_array_new ();
|
||||
g_ptr_array_add (argv, "this does not exist");
|
||||
g_ptr_array_add (argv, NULL);
|
||||
|
||||
g_spawn_sync (NULL, (char**) argv->pdata, NULL, 0, NULL, NULL, &stdout_str,
|
||||
NULL, &exit_status, &error);
|
||||
g_assert_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_NOENT);
|
||||
g_assert_null (stdout_str);
|
||||
g_assert_cmpint (exit_status, ==, -1);
|
||||
|
||||
g_ptr_array_free (argv, TRUE);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
@ -230,6 +252,7 @@ main (int argc,
|
||||
g_test_add_func ("/gthread/spawn-single-sync", test_spawn_sync);
|
||||
g_test_add_func ("/gthread/spawn-single-async", test_spawn_async);
|
||||
g_test_add_func ("/gthread/spawn-script", test_spawn_script);
|
||||
g_test_add_func ("/gthread/spawn/nonexistent", test_spawn_nonexistent);
|
||||
|
||||
ret = g_test_run();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user