>From 6b1f82435bc2f900429b711d90e25857d817eafe Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 26 Dec 2020 14:39:39 +0100 Subject: [PATCH 12/15] execle: New module. * lib/execle.c: New file. * m4/execle.m4: New file. * modules/execle: New file. * doc/posix-functions/execle.texi: Mention more Windows problems and the new module. --- ChangeLog | 7 ++++ doc/posix-functions/execle.texi | 21 ++++++++--- lib/execle.c | 83 +++++++++++++++++++++++++++++++++++++++++ m4/execle.m4 | 15 ++++++++ modules/execle | 29 ++++++++++++++ 5 files changed, 150 insertions(+), 5 deletions(-) create mode 100644 lib/execle.c create mode 100644 m4/execle.m4 create mode 100644 modules/execle diff --git a/ChangeLog b/ChangeLog index 7f5c99b..6a8939c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2020-12-26 Bruno Haible + execle: New module. + * lib/execle.c: New file. + * m4/execle.m4: New file. + * modules/execle: New file. + * doc/posix-functions/execle.texi: Mention more Windows problems and the + new module. + execl: Add tests. * tests/test-execl-main.c: New file. * tests/test-execl.sh: New file. diff --git a/doc/posix-functions/execle.texi b/doc/posix-functions/execle.texi index 04ef7c2..e556260 100644 --- a/doc/posix-functions/execle.texi +++ b/doc/posix-functions/execle.texi @@ -4,19 +4,30 @@ POSIX specification:@* @url{https://pubs.opengroup.org/onlinepubs/9699919799/functions/execle.html} -Gnulib module: --- +Gnulib module: execle Portability problems fixed by Gnulib: @itemize +@item +On Windows platforms (excluding Cygwin), this function does not pass +command-line arguments correctly if they contain space, tab, backslash, +or double-quote characters. +@item +On Windows platforms (excluding Cygwin), this function spawns an asynchronous +child process and then exits the current process immediately. As a +consequence, the parent of the current process 1. may incorrectly proceed +as if its child had exited, and 2. will never see the child's exit status. +@item +On Windows platforms (excluding Cygwin), the return type of this function is +@code{intptr_t}, not @code{int}. @end itemize +Note: The Gnulib replacement for this function is not async-safe, that is, +it must not be invoked from a signal handler. + Portability problems not fixed by Gnulib: @itemize @item On some platforms, a script without executable permission is still run: Cygwin 1.5.x. -@item -On Windows platforms (excluding Cygwin), this function operates by spawning -and then by exiting the current process, which means the current -process's parent may incorrectly proceed as if its child had exited. @end itemize diff --git a/lib/execle.c b/lib/execle.c new file mode 100644 index 0000000..8e10d8a --- /dev/null +++ b/lib/execle.c @@ -0,0 +1,83 @@ +/* execle() function: Execute a program, replacing the current process. + Copyright (C) 2020 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Written by Bruno Haible , 2020. */ + +/* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc + may optimize away the arg0 == NULL test below. */ +#define _GL_ARG_NONNULL(params) + +#include + +/* Specification. */ +#include + +#include +#include + +#include "malloca.h" + +int +execle (const char *program, const char *arg0, ...) +{ + va_list args; + + /* The callee is not expecting a NULL argv[0]. */ + if (arg0 == NULL) + { + errno = EINVAL; + return -1; + } + + /* Count the number of arguments (including arg0 and the trailing NULL). */ + size_t count = 1; + va_start (args, arg0); + for (;;) + { + count++; + if (va_arg (args, const char *) == NULL) + break; + } + va_end (args); + + /* Allocate the argument vector. */ + const char **argv = (const char **) malloca (count * sizeof (const char *)); + if (argv == NULL) + { + errno = ENOMEM; + return -1; + } + + /* Copy the arguments into the argument vector. */ + { + size_t i = 0; + argv[i++] = arg0; + va_start (args, arg0); + for (; i < count;) + argv[i++] = va_arg (args, const char *); + } + char * const *env = va_arg (args, char * const *); + va_end (args); + + /* Invoke execve. */ + execve (program, argv, env); + + /* If execve returned, it must have failed. */ + int saved_errno = errno; + freea (argv); + errno = saved_errno; + return -1; +} diff --git a/m4/execle.m4 b/m4/execle.m4 new file mode 100644 index 0000000..47b273b --- /dev/null +++ b/m4/execle.m4 @@ -0,0 +1,15 @@ +# execle.m4 serial 1 +dnl Copyright (C) 2020 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_FUNC_EXECLE], +[ + AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) + AC_REQUIRE([AC_CANONICAL_HOST]) + + case "$host_os" in + mingw*) REPLACE_EXECLE=1 ;; + esac +]) diff --git a/modules/execle b/modules/execle new file mode 100644 index 0000000..dd3c9ad --- /dev/null +++ b/modules/execle @@ -0,0 +1,29 @@ +Description: +execle() function: Execute a program, replacing the current process. + +Files: +lib/execle.c +m4/execle.m4 + +Depends-on: +unistd +execve [test $REPLACE_EXECLE = 1] +malloca [test $REPLACE_EXECLE = 1] + +configure.ac: +gl_FUNC_EXECLE +if test $REPLACE_EXECLE = 1; then + AC_LIBOBJ([execle]) +fi +gl_UNISTD_MODULE_INDICATOR([execle]) + +Makefile.am: + +Include: + + +License: +LGPLv2+ + +Maintainer: +all -- 2.7.4