>From 7fbf62ebb9e7466a157b5f339d15ff1d11b18d45 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 26 Dec 2020 14:36:35 +0100 Subject: [PATCH 10/15] execl: New module. * lib/execl.c: New file. * m4/execl.m4: New file. * modules/execl: New file. * doc/posix-functions/execl.texi: Mention more Windows problems and the new module. --- ChangeLog | 7 ++++ doc/posix-functions/execl.texi | 21 ++++++++--- lib/execl.c | 82 ++++++++++++++++++++++++++++++++++++++++++ m4/execl.m4 | 15 ++++++++ modules/execl | 29 +++++++++++++++ 5 files changed, 149 insertions(+), 5 deletions(-) create mode 100644 lib/execl.c create mode 100644 m4/execl.m4 create mode 100644 modules/execl diff --git a/ChangeLog b/ChangeLog index 1af6b70..5b8e553 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2020-12-26 Bruno Haible + execl: New module. + * lib/execl.c: New file. + * m4/execl.m4: New file. + * modules/execl: New file. + * doc/posix-functions/execl.texi: Mention more Windows problems and the + new module. + execv: Add tests. * tests/test-execv-main.c: New file. * tests/test-execv.sh: New file. diff --git a/doc/posix-functions/execl.texi b/doc/posix-functions/execl.texi index 70c9f8e..28460e2 100644 --- a/doc/posix-functions/execl.texi +++ b/doc/posix-functions/execl.texi @@ -4,19 +4,30 @@ POSIX specification:@* @url{https://pubs.opengroup.org/onlinepubs/9699919799/functions/execl.html} -Gnulib module: --- +Gnulib module: execl 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/execl.c b/lib/execl.c new file mode 100644 index 0000000..d1df0c0 --- /dev/null +++ b/lib/execl.c @@ -0,0 +1,82 @@ +/* execl() 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 +execl (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 *); + va_end (args); + } + + /* Invoke execv. */ + execv (program, argv); + + /* If execv returned, it must have failed. */ + int saved_errno = errno; + freea (argv); + errno = saved_errno; + return -1; +} diff --git a/m4/execl.m4 b/m4/execl.m4 new file mode 100644 index 0000000..0f2c89c --- /dev/null +++ b/m4/execl.m4 @@ -0,0 +1,15 @@ +# execl.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_EXECL], +[ + AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) + AC_REQUIRE([AC_CANONICAL_HOST]) + + case "$host_os" in + mingw*) REPLACE_EXECL=1 ;; + esac +]) diff --git a/modules/execl b/modules/execl new file mode 100644 index 0000000..9d64c5c --- /dev/null +++ b/modules/execl @@ -0,0 +1,29 @@ +Description: +execl() function: Execute a program, replacing the current process. + +Files: +lib/execl.c +m4/execl.m4 + +Depends-on: +unistd +execv [test $REPLACE_EXECL = 1] +malloca [test $REPLACE_EXECL = 1] + +configure.ac: +gl_FUNC_EXECL +if test $REPLACE_EXECL = 1; then + AC_LIBOBJ([execl]) +fi +gl_UNISTD_MODULE_INDICATOR([execl]) + +Makefile.am: + +Include: + + +License: +LGPLv2+ + +Maintainer: +all -- 2.7.4