diff options
author | Nick Clifton <nickc@redhat.com> | 2018-12-07 11:32:55 +0000 |
---|---|---|
committer | Nick Clifton <nickc@redhat.com> | 2018-12-07 11:32:55 +0000 |
commit | af03af8f55f2536b6e20928e6b1fa0324a5f3d6e (patch) | |
tree | fa6b5ef700e811c0d6bfaec08d2ebb8291dae17f /libiberty | |
parent | 67bb16f345cb72740ab523018709c183ff3f8c8d (diff) | |
download | binutils-af03af8f55f2536b6e20928e6b1fa0324a5f3d6e.zip binutils-af03af8f55f2536b6e20928e6b1fa0324a5f3d6e.tar.gz binutils-af03af8f55f2536b6e20928e6b1fa0324a5f3d6e.tar.bz2 |
Synchronize libiberty with gcc and add --no-recruse-limit option to tools that support name demangling.
This patch addresses the multitude of bug reports about resource exhaustion
in libiberty's name demangling code. It adds a limit to the amount of
recursion that is allowed, before an error is triggered. It also adds a
new demangling option to disable this limit. (The limit is enabled by
default).
PR 87681
PR 87675
PR 87636
PR 87335
libiberty * cp-demangle.h (struct d_info): Add recursion_limit field.
* cp-demangle.c (d_function_type): If the recursion limit is
enabled and reached, return with a failure result.
(d_demangle_callback): If the recursion limit is enabled, check
for a mangled string that is so long that there is not enough
stack space for the local arrays.
* cplus-dem.c (struct work): Add recursion_level field.
(demangle_nested_args): If the recursion limit is enabled and
reached, return with a failure result.
include * demangle.h (DMGL_RECURSE_LIMIT): Define.
(DEMANGLE_RECURSION_LIMIT): Prototype.
binutuils * addr2line.c (demangle_flags): New static variable.
(long_options): Add --recurse-limit and --no-recurse-limit.
(translate_address): Pass demangle_flags to bfd_demangle.
(main): Handle --recurse-limit and --no-recurse-limit options.
* cxxfilt.c (flags): Add DMGL_RECURSE_LIMIT.
(long_options): Add --recurse-limit and --no-recurse-limit.
(main): Handle new options.
* dlltool.c (gen_def_file): Include DMGL_RECURSE_LIMIT in flags
passed to cplus_demangle.
* nm.c (demangle_flags): New static variable.
(long_options): Add --recurse-limit and --no-recurse-limit.
(main): Handle new options.
* objdump.c (demangle_flags): New static variable.
(usage): Add --recurse-limit and --no-recurse-limit.
(long_options): Likewise.
(objdump_print_symname): Pass demangle_flags to bfd_demangle.
(disassemble_section): Likewise.
(dump_dymbols): Likewise.
(main): Handle new options.
* prdbg.c (demangle_flags): New static variable.
(tg_variable): Pass demangle_flags to demangler.
(tg_start_function): Likewise.
* stabs.c (demangle_flags): New static variable.
(stab_demangle_template): Pass demangle_flags to demangler.
(stab_demangle_v3_argtypes): Likewise.
(stab_demangle_v3_arg): Likewise.
* doc/binutuls.texi: Document new command line options.
* NEWS: Mention the new feature.
* testsuite/config/default.exp (CXXFILT): Define if not already
defined.
(CXXFILTFLAGS): Likewise.
* testsuite/binutils-all/cxxfilt.exp: New file. Runs a few
simple tests of the cxxfilt program.
Diffstat (limited to 'libiberty')
-rw-r--r-- | libiberty/ChangeLog | 65 | ||||
-rw-r--r-- | libiberty/config.in | 3 | ||||
-rwxr-xr-x | libiberty/configure | 4 | ||||
-rw-r--r-- | libiberty/configure.ac | 4 | ||||
-rw-r--r-- | libiberty/cp-demangle.c | 68 | ||||
-rw-r--r-- | libiberty/cp-demangle.h | 3 | ||||
-rw-r--r-- | libiberty/cplus-dem.c | 40 | ||||
-rw-r--r-- | libiberty/pex-unix.c | 268 |
8 files changed, 323 insertions, 132 deletions
diff --git a/libiberty/ChangeLog b/libiberty/ChangeLog index b9cbee3..6859027 100644 --- a/libiberty/ChangeLog +++ b/libiberty/ChangeLog @@ -1,3 +1,68 @@ +2018-12-07 Nick Clifton <nickc@redhat.com> + + PR 87681 + PR 87675 + PR 87636 + PR 87350 + PR 87335 + * cp-demangle.h (struct d_info): Add recursion_level field. + * cp-demangle.c (d_function_type): Add recursion counter. + If the recursion limit is reached and the check is not disabled, + then return with a failure result. + (cplus_demangle_init_info): Initialise the recursion_level field. + (d_demangle_callback): If the recursion limit is enabled, check + for a mangled string that is so long that there is not enough + stack space for the local arrays. + * cplus-dem.c (struct work): Add recursion_level field. + (squangle_mop_up): Set the numb and numk fields to zero. + (work_stuff_copy_to_from): Handle the case where a btypevec or + ktypevec field is NULL. + (demangle_nested_args): Add recursion counter. If + the recursion limit is not disabled and reached, return with a + failure result. + +2018-10-23 Jason Merrill <jason@redhat.com> + + Implement P0732R2, class types in non-type template parameters. + * cp-demangle.c (d_dump, d_make_comp, d_count_templates_scopes) + (d_print_comp_inner): Handle DEMANGLE_COMPONENT_TPARM_OBJ. + (d_special_name): Handle TA. + (d_expresion_1): Fix demangling of brace-enclosed initializer list. + +2018-10-31 Joseph Myers <joseph@codesourcery.com> + + PR bootstrap/82856 + Merge from binutils-gdb: + 2018-06-19 Simon Marchi <simon.marchi@ericsson.com> + + * configure.ac: Remove AC_PREREQ. + * configure: Re-generate. + * config.in: Re-generate. + +2018-10-01 Nathan Sidwell <nathan@acm.org> + + * configure.ac (checkfuncs): Add pipe2. + * config.in, configure: Rebuilt. + * pex-unix.c (pex_unix_exec_child): Comminicate errors from child + to parent with a pipe, when possible. + +2018-08-23 Nathan Sidwell <nathan@acm.org> + Martin Liska <mliska@suse.cz> + + PR driver/87056 + * pex-unix.c (pex_unix_exec_child): Duplicate bad_fn into local + scopes to avoid potential clobber. + +2018-08-20 Nathan Sidwell <nathan@acm.org> + + * pex-unix.c (pex_child_error): Delete. + (pex_unix_exec_child): Commonize error paths to single message & + exit. + +2018-07-30 Tom Tromey <tom@tromey.com> + + * cplus-dem.c (remember_Btype): Don't call memcpy with LEN==0. + 2018-08-01 Richard Earnshaw <rearnsha@arm.com> Copy over from GCC diff --git a/libiberty/config.in b/libiberty/config.in index 10dbbd9..f7052b5 100644 --- a/libiberty/config.in +++ b/libiberty/config.in @@ -195,6 +195,9 @@ /* Define to 1 if you have the `on_exit' function. */ #undef HAVE_ON_EXIT +/* Define to 1 if you have the `pipe2' function. */ +#undef HAVE_PIPE2 + /* Define to 1 if you have the <process.h> header file. */ #undef HAVE_PROCESS_H diff --git a/libiberty/configure b/libiberty/configure index 460b44b..7a34dab 100755 --- a/libiberty/configure +++ b/libiberty/configure @@ -5797,7 +5797,7 @@ funcs="$funcs setproctitle" vars="sys_errlist sys_nerr sys_siglist" checkfuncs="__fsetlocking canonicalize_file_name dup3 getrlimit getrusage \ - getsysinfo gettimeofday on_exit psignal pstat_getdynamic pstat_getstatic \ + getsysinfo gettimeofday on_exit pipe2 psignal pstat_getdynamic pstat_getstatic \ realpath setrlimit sbrk spawnve spawnvpe strerror strsignal sysconf sysctl \ sysmp table times wait3 wait4" @@ -5813,7 +5813,7 @@ if test "x" = "y"; then index insque \ memchr memcmp memcpy memmem memmove memset mkstemps \ on_exit \ - psignal pstat_getdynamic pstat_getstatic putenv \ + pipe2 psignal pstat_getdynamic pstat_getstatic putenv \ random realpath rename rindex \ sbrk setenv setproctitle setrlimit sigsetmask snprintf spawnve spawnvpe \ stpcpy stpncpy strcasecmp strchr strdup \ diff --git a/libiberty/configure.ac b/libiberty/configure.ac index 54f7fb4..f1ce760 100644 --- a/libiberty/configure.ac +++ b/libiberty/configure.ac @@ -390,7 +390,7 @@ funcs="$funcs setproctitle" vars="sys_errlist sys_nerr sys_siglist" checkfuncs="__fsetlocking canonicalize_file_name dup3 getrlimit getrusage \ - getsysinfo gettimeofday on_exit psignal pstat_getdynamic pstat_getstatic \ + getsysinfo gettimeofday on_exit pipe2 psignal pstat_getdynamic pstat_getstatic \ realpath setrlimit sbrk spawnve spawnvpe strerror strsignal sysconf sysctl \ sysmp table times wait3 wait4" @@ -406,7 +406,7 @@ if test "x" = "y"; then index insque \ memchr memcmp memcpy memmem memmove memset mkstemps \ on_exit \ - psignal pstat_getdynamic pstat_getstatic putenv \ + pipe2 psignal pstat_getdynamic pstat_getstatic putenv \ random realpath rename rindex \ sbrk setenv setproctitle setrlimit sigsetmask snprintf spawnve spawnvpe \ stpcpy stpncpy strcasecmp strchr strdup \ diff --git a/libiberty/cp-demangle.c b/libiberty/cp-demangle.c index 3f2a097..47bbc94 100644 --- a/libiberty/cp-demangle.c +++ b/libiberty/cp-demangle.c @@ -625,6 +625,9 @@ d_dump (struct demangle_component *dc, int indent) case DEMANGLE_COMPONENT_TEMPLATE_PARAM: printf ("template parameter %ld\n", dc->u.s_number.number); return; + case DEMANGLE_COMPONENT_TPARM_OBJ: + printf ("template parameter object\n"); + break; case DEMANGLE_COMPONENT_FUNCTION_PARAM: printf ("function parameter %ld\n", dc->u.s_number.number); return; @@ -1007,6 +1010,7 @@ d_make_comp (struct d_info *di, enum demangle_component_type type, case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS: case DEMANGLE_COMPONENT_NULLARY: case DEMANGLE_COMPONENT_TRINARY_ARG2: + case DEMANGLE_COMPONENT_TPARM_OBJ: if (left == NULL) return NULL; break; @@ -2007,6 +2011,7 @@ d_java_resource (struct d_info *di) ::= TT <type> ::= TI <type> ::= TS <type> + ::= TA <template-arg> ::= GV <(object) name> ::= T <call-offset> <(base) encoding> ::= Tc <call-offset> <call-offset> <(base) encoding> @@ -2099,6 +2104,10 @@ d_special_name (struct d_info *di) return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER, d_name (di), NULL); + case 'A': + return d_make_comp (di, DEMANGLE_COMPONENT_TPARM_OBJ, + d_template_arg (di), NULL); + default: return NULL; } @@ -2843,21 +2852,35 @@ d_ref_qualifier (struct d_info *di, struct demangle_component *sub) static struct demangle_component * d_function_type (struct d_info *di) { - struct demangle_component *ret; + struct demangle_component *ret = NULL; - if (! d_check_char (di, 'F')) - return NULL; - if (d_peek_char (di) == 'Y') + if ((di->options & DMGL_NO_RECURSE_LIMIT) == 0) { - /* Function has C linkage. We don't print this information. - FIXME: We should print it in verbose mode. */ - d_advance (di, 1); + if (di->recursion_level > DEMANGLE_RECURSION_LIMIT) + /* FIXME: There ought to be a way to report + that the recursion limit has been reached. */ + return NULL; + + di->recursion_level ++; } - ret = d_bare_function_type (di, 1); - ret = d_ref_qualifier (di, ret); - if (! d_check_char (di, 'E')) - return NULL; + if (d_check_char (di, 'F')) + { + if (d_peek_char (di) == 'Y') + { + /* Function has C linkage. We don't print this information. + FIXME: We should print it in verbose mode. */ + d_advance (di, 1); + } + ret = d_bare_function_type (di, 1); + ret = d_ref_qualifier (di, ret); + + if (! d_check_char (di, 'E')) + ret = NULL; + } + + if ((di->options & DMGL_NO_RECURSE_LIMIT) == 0) + di->recursion_level --; return ret; } @@ -3327,11 +3350,11 @@ d_expression_1 (struct d_info *di) { /* Brace-enclosed initializer list, untyped or typed. */ struct demangle_component *type = NULL; + d_advance (di, 2); if (peek == 't') type = cplus_demangle_type (di); if (!d_peek_next_char (di)) return NULL; - d_advance (di, 2); return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST, type, d_exprlist (di, 'E')); } @@ -4101,6 +4124,7 @@ d_count_templates_scopes (int *num_templates, int *num_scopes, case DEMANGLE_COMPONENT_VECTOR_TYPE: case DEMANGLE_COMPONENT_ARGLIST: case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST: + case DEMANGLE_COMPONENT_TPARM_OBJ: case DEMANGLE_COMPONENT_INITIALIZER_LIST: case DEMANGLE_COMPONENT_CAST: case DEMANGLE_COMPONENT_CONVERSION: @@ -4872,6 +4896,11 @@ d_print_comp_inner (struct d_print_info *dpi, int options, } return; + case DEMANGLE_COMPONENT_TPARM_OBJ: + d_append_string (dpi, "template parameter object for "); + d_print_comp (dpi, options, d_left (dc)); + return; + case DEMANGLE_COMPONENT_CTOR: d_print_comp (dpi, options, dc->u.s_ctor.name); return; @@ -6188,6 +6217,7 @@ cplus_demangle_init_info (const char *mangled, int options, size_t len, di->expansion = 0; di->is_expression = 0; di->is_conversion = 0; + di->recursion_level = 0; } /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI @@ -6227,6 +6257,20 @@ d_demangle_callback (const char *mangled, int options, cplus_demangle_init_info (mangled, options, strlen (mangled), &di); + /* PR 87675 - Check for a mangled string that is so long + that we do not have enough stack space to demangle it. */ + if (((options & DMGL_NO_RECURSE_LIMIT) == 0) + /* This check is a bit arbitrary, since what we really want to do is to + compare the sizes of the di.comps and di.subs arrays against the + amount of stack space remaining. But there is no portable way to do + this, so instead we use the recursion limit as a guide to the maximum + size of the arrays. */ + && (unsigned long) di.num_comps > DEMANGLE_RECURSION_LIMIT) + { + /* FIXME: We need a way to indicate that a stack limit has been reached. */ + return 0; + } + { #ifdef CP_DYNAMIC_ARRAYS __extension__ struct demangle_component comps[di.num_comps]; diff --git a/libiberty/cp-demangle.h b/libiberty/cp-demangle.h index 51b8a24..d87a830 100644 --- a/libiberty/cp-demangle.h +++ b/libiberty/cp-demangle.h @@ -122,6 +122,9 @@ struct d_info /* Non-zero if we are parsing the type operand of a conversion operator, but not when in an expression. */ int is_conversion; + /* If DMGL_NO_RECURSE_LIMIT is not active then this is set to + the current recursion level. */ + unsigned int recursion_level; }; /* To avoid running past the ending '\0', don't: diff --git a/libiberty/cplus-dem.c b/libiberty/cplus-dem.c index 6d58bd8..48c0cfd 100644 --- a/libiberty/cplus-dem.c +++ b/libiberty/cplus-dem.c @@ -146,6 +146,7 @@ struct work_stuff int *proctypevec; /* Indices of currently processed remembered typevecs. */ int proctypevec_size; int nproctypes; + unsigned int recursion_level; }; #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI) @@ -1292,12 +1293,14 @@ squangle_mop_up (struct work_stuff *work) free ((char *) work -> btypevec); work->btypevec = NULL; work->bsize = 0; + work->numb = 0; } if (work -> ktypevec != NULL) { free ((char *) work -> ktypevec); work->ktypevec = NULL; work->ksize = 0; + work->numk = 0; } } @@ -1331,8 +1334,15 @@ work_stuff_copy_to_from (struct work_stuff *to, struct work_stuff *from) for (i = 0; i < from->numk; i++) { - int len = strlen (from->ktypevec[i]) + 1; + int len; + if (from->ktypevec[i] == NULL) + { + to->ktypevec[i] = NULL; + continue; + } + + len = strlen (from->ktypevec[i]) + 1; to->ktypevec[i] = XNEWVEC (char, len); memcpy (to->ktypevec[i], from->ktypevec[i], len); } @@ -1342,8 +1352,15 @@ work_stuff_copy_to_from (struct work_stuff *to, struct work_stuff *from) for (i = 0; i < from->numb; i++) { - int len = strlen (from->btypevec[i]) + 1; + int len; + if (from->btypevec[i] == NULL) + { + to->btypevec[i] = NULL; + continue; + } + + len = strlen (from->btypevec[i]) + 1; to->btypevec[i] = XNEWVEC (char , len); memcpy (to->btypevec[i], from->btypevec[i], len); } @@ -1401,6 +1418,7 @@ delete_non_B_K_work_stuff (struct work_stuff *work) free ((char*) work->tmpl_argvec); work->tmpl_argvec = NULL; + work->ntmpl_args = 0; } if (work->previous_argument) { @@ -4471,12 +4489,14 @@ remember_Btype (struct work_stuff *work, const char *start, char *tem; tem = XNEWVEC (char, len + 1); - memcpy (tem, start, len); + if (len > 0) + memcpy (tem, start, len); tem[len] = '\0'; work -> btypevec[index] = tem; } /* Lose all the info related to B and K type codes. */ + static void forget_B_and_K_types (struct work_stuff *work) { @@ -4502,6 +4522,7 @@ forget_B_and_K_types (struct work_stuff *work) } } } + /* Forget the remembered types, but not the type vector itself. */ static void @@ -4696,6 +4717,16 @@ demangle_nested_args (struct work_stuff *work, const char **mangled, int result; int saved_nrepeats; + if ((work->options & DMGL_NO_RECURSE_LIMIT) == 0) + { + if (work->recursion_level > DEMANGLE_RECURSION_LIMIT) + /* FIXME: There ought to be a way to report + that the recursion limit has been reached. */ + return 0; + + work->recursion_level ++; + } + /* The G++ name-mangling algorithm does not remember types on nested argument lists, unless -fsquangling is used, and in that case the type vector updated by remember_type is not used. So, we turn @@ -4722,6 +4753,9 @@ demangle_nested_args (struct work_stuff *work, const char **mangled, --work->forgetting_types; work->nrepeats = saved_nrepeats; + if ((work->options & DMGL_NO_RECURSE_LIMIT) == 0) + --work->recursion_level; + return result; } diff --git a/libiberty/pex-unix.c b/libiberty/pex-unix.c index 21d5606..0f283e6 100644 --- a/libiberty/pex-unix.c +++ b/libiberty/pex-unix.c @@ -298,8 +298,6 @@ pex_wait (struct pex_obj *obj, pid_t pid, int *status, struct pex_time *time) #endif /* ! defined (HAVE_WAITPID) */ #endif /* ! defined (HAVE_WAIT4) */ -static void pex_child_error (struct pex_obj *, const char *, const char *, int) - ATTRIBUTE_NORETURN; static int pex_unix_open_read (struct pex_obj *, const char *, int); static int pex_unix_open_write (struct pex_obj *, const char *, int, int); static pid_t pex_unix_exec_child (struct pex_obj *, int, const char *, @@ -366,28 +364,6 @@ pex_unix_close (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd) return close (fd); } -/* Report an error from a child process. We don't use stdio routines, - because we might be here due to a vfork call. */ - -static void -pex_child_error (struct pex_obj *obj, const char *executable, - const char *errmsg, int err) -{ - int retval = 0; -#define writeerr(s) retval |= (write (STDERR_FILE_NO, s, strlen (s)) < 0) - writeerr (obj->pname); - writeerr (": error trying to exec '"); - writeerr (executable); - writeerr ("': "); - writeerr (errmsg); - writeerr (": "); - writeerr (xstrerror (err)); - writeerr ("\n"); -#undef writeerr - /* Exit with -2 if the error output failed, too. */ - _exit (retval == 0 ? -1 : -2); -} - /* Execute a child. */ #if defined(HAVE_SPAWNVE) && defined(HAVE_SPAWNVPE) @@ -592,21 +568,53 @@ pex_unix_exec_child (struct pex_obj *obj, int flags, const char *executable, int in, int out, int errdes, int toclose, const char **errmsg, int *err) { - pid_t pid; + pid_t pid = -1; + /* Tuple to communicate error from child to parent. We can safely + transfer string literal pointers as both run with identical + address mappings. */ + struct fn_err + { + const char *fn; + int err; + }; + volatile int do_pipe = 0; + volatile int pipes[2]; /* [0]:reader,[1]:writer. */ +#ifdef O_CLOEXEC + do_pipe = 1; +#endif + if (do_pipe) + { +#ifdef HAVE_PIPE2 + if (pipe2 ((int *)pipes, O_CLOEXEC)) + do_pipe = 0; +#else + if (pipe ((int *)pipes)) + do_pipe = 0; + else + { + if (fcntl (pipes[1], F_SETFD, FD_CLOEXEC) == -1) + { + close (pipes[0]); + close (pipes[1]); + do_pipe = 0; + } + } +#endif + } /* We declare these to be volatile to avoid warnings from gcc about them being clobbered by vfork. */ - volatile int sleep_interval; + volatile int sleep_interval = 1; volatile int retries; /* We vfork and then set environ in the child before calling execvp. This clobbers the parent's environ so we need to restore it. It would be nice to use one of the exec* functions that takes an - environment as a parameter, but that may have portability issues. */ - char **save_environ = environ; + environment as a parameter, but that may have portability + issues. It is marked volatile so the child doesn't consider it a + dead variable and therefore clobber where ever it is stored. */ + char **volatile save_environ = environ; - sleep_interval = 1; - pid = -1; for (retries = 0; retries < 4; ++retries) { pid = vfork (); @@ -619,104 +627,138 @@ pex_unix_exec_child (struct pex_obj *obj, int flags, const char *executable, switch (pid) { case -1: + if (do_pipe) + { + close (pipes[0]); + close (pipes[1]); + } *err = errno; *errmsg = VFORK_STRING; return (pid_t) -1; case 0: /* Child process. */ - if (in != STDIN_FILE_NO) - { - if (dup2 (in, STDIN_FILE_NO) < 0) - pex_child_error (obj, executable, "dup2", errno); - if (close (in) < 0) - pex_child_error (obj, executable, "close", errno); - } - if (out != STDOUT_FILE_NO) - { - if (dup2 (out, STDOUT_FILE_NO) < 0) - pex_child_error (obj, executable, "dup2", errno); - if (close (out) < 0) - pex_child_error (obj, executable, "close", errno); - } - if (errdes != STDERR_FILE_NO) - { - if (dup2 (errdes, STDERR_FILE_NO) < 0) - pex_child_error (obj, executable, "dup2", errno); - if (close (errdes) < 0) - pex_child_error (obj, executable, "close", errno); - } - if (toclose >= 0) - { - if (close (toclose) < 0) - pex_child_error (obj, executable, "close", errno); - } - if ((flags & PEX_STDERR_TO_STDOUT) != 0) - { - if (dup2 (STDOUT_FILE_NO, STDERR_FILE_NO) < 0) - pex_child_error (obj, executable, "dup2", errno); - } - - if (env) - { - /* NOTE: In a standard vfork implementation this clobbers the - parent's copy of environ "too" (in reality there's only one copy). - This is ok as we restore it below. */ - environ = (char**) env; - } - - if ((flags & PEX_SEARCH) != 0) - { - execvp (executable, to_ptr32 (argv)); - pex_child_error (obj, executable, "execvp", errno); - } - else - { - execv (executable, to_ptr32 (argv)); - pex_child_error (obj, executable, "execv", errno); - } + { + struct fn_err failed; + failed.fn = NULL; + + if (do_pipe) + close (pipes[0]); + if (!failed.fn && in != STDIN_FILE_NO) + { + if (dup2 (in, STDIN_FILE_NO) < 0) + failed.fn = "dup2", failed.err = errno; + else if (close (in) < 0) + failed.fn = "close", failed.err = errno; + } + if (!failed.fn && out != STDOUT_FILE_NO) + { + if (dup2 (out, STDOUT_FILE_NO) < 0) + failed.fn = "dup2", failed.err = errno; + else if (close (out) < 0) + failed.fn = "close", failed.err = errno; + } + if (!failed.fn && errdes != STDERR_FILE_NO) + { + if (dup2 (errdes, STDERR_FILE_NO) < 0) + failed.fn = "dup2", failed.err = errno; + else if (close (errdes) < 0) + failed.fn = "close", failed.err = errno; + } + if (!failed.fn && toclose >= 0) + { + if (close (toclose) < 0) + failed.fn = "close", failed.err = errno; + } + if (!failed.fn && (flags & PEX_STDERR_TO_STDOUT) != 0) + { + if (dup2 (STDOUT_FILE_NO, STDERR_FILE_NO) < 0) + failed.fn = "dup2", failed.err = errno; + } + if (!failed.fn) + { + if (env) + /* NOTE: In a standard vfork implementation this clobbers + the parent's copy of environ "too" (in reality there's + only one copy). This is ok as we restore it below. */ + environ = (char**) env; + if ((flags & PEX_SEARCH) != 0) + { + execvp (executable, to_ptr32 (argv)); + failed.fn = "execvp", failed.err = errno; + } + else + { + execv (executable, to_ptr32 (argv)); + failed.fn = "execv", failed.err = errno; + } + } + + /* Something failed, report an error. We don't use stdio + routines, because we might be here due to a vfork call. */ + ssize_t retval = 0; + + if (!do_pipe + || write (pipes[1], &failed, sizeof (failed)) != sizeof (failed)) + { + /* The parent will not see our scream above, so write to + stdout. */ +#define writeerr(s) (retval |= write (STDERR_FILE_NO, s, strlen (s))) + writeerr (obj->pname); + writeerr (": error trying to exec '"); + writeerr (executable); + writeerr ("': "); + writeerr (failed.fn); + writeerr (": "); + writeerr (xstrerror (failed.err)); + writeerr ("\n"); +#undef writeerr + } + /* Exit with -2 if the error output failed, too. */ + _exit (retval < 0 ? -2 : -1); + } /* NOTREACHED */ return (pid_t) -1; default: /* Parent process. */ - - /* Restore environ. - Note that the parent either doesn't run until the child execs/exits - (standard vfork behaviour), or if it does run then vfork is behaving - more like fork. In either case we needn't worry about clobbering - the child's copy of environ. */ - environ = save_environ; - - if (in != STDIN_FILE_NO) - { + { + /* Restore environ. Note that the parent either doesn't run + until the child execs/exits (standard vfork behaviour), or + if it does run then vfork is behaving more like fork. In + either case we needn't worry about clobbering the child's + copy of environ. */ + environ = save_environ; + + struct fn_err failed; + failed.fn = NULL; + if (do_pipe) + { + close (pipes[1]); + ssize_t len = read (pipes[0], &failed, sizeof (failed)); + if (len < 0) + failed.fn = NULL; + close (pipes[0]); + } + + if (!failed.fn && in != STDIN_FILE_NO) if (close (in) < 0) - { - *err = errno; - *errmsg = "close"; - return (pid_t) -1; - } - } - if (out != STDOUT_FILE_NO) - { + failed.fn = "close", failed.err = errno; + if (!failed.fn && out != STDOUT_FILE_NO) if (close (out) < 0) - { - *err = errno; - *errmsg = "close"; - return (pid_t) -1; - } - } - if (errdes != STDERR_FILE_NO) - { + failed.fn = "close", failed.err = errno; + if (!failed.fn && errdes != STDERR_FILE_NO) if (close (errdes) < 0) - { - *err = errno; - *errmsg = "close"; - return (pid_t) -1; - } - } - + failed.fn = "close", failed.err = errno; + + if (failed.fn) + { + *err = failed.err; + *errmsg = failed.fn; + return (pid_t) -1; + } + } return pid; } } |