* lib/execve.c: New file.
* m4/execve.m4: New file.
* modules/execve: New file.
* doc/posix-functions/execve.texi: Mention more Windows problems and the
new module.
2020-12-26 Bruno Haible <bruno@clisp.org>
+ execve: New module.
+ * lib/execve.c: New file.
+ * m4/execve.m4: New file.
+ * modules/execve: New file.
+ * doc/posix-functions/execve.texi: Mention more Windows problems and the
+ new module.
+
execve, execvpe, execvp, execv, execl, execle, execlp: Prepare modules.
* lib/unistd.in.h (execl, execle, execlp, execv, execve, execvp,
execvpe): Add declarations for the new modules.
POSIX specification:@* @url{https://pubs.opengroup.org/onlinepubs/9699919799/functions/execve.html}
-Gnulib module: ---
+Gnulib module: execve
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
--- /dev/null
+/* execve() 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 <https://www.gnu.org/licenses/>. */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2020. */
+
+/* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
+ may optimize away the program == NULL and argv == NULL tests below. */
+#define _GL_ARG_NONNULL(params)
+
+#include <config.h>
+
+/* Specification. */
+#include <unistd.h>
+
+#if defined _WIN32 && !defined __CYGWIN__
+
+# include <errno.h>
+# include <stdio.h>
+# include <spawn.h>
+
+# include <sys/types.h>
+# include <sys/wait.h>
+
+int
+execve (const char *program, char * const *argv, char * const *env)
+{
+ if (program == NULL
+ || argv == NULL
+ /* The callee is not expecting a NULL argv[0]. */
+ || argv[0] == NULL
+ || env == NULL)
+ {
+ errno = EINVAL;
+ return -1;
+ }
+
+ pid_t child;
+ int err = posix_spawn (&child, program, NULL, NULL, argv, env);
+ if (err == 0)
+ {
+ /* Close all file descriptors, so that
+ - for opened files, the child may close and unlink() the file,
+ - for pipe handles, the pipe's reader or writer does not get stuck.
+ Ideally we would also suspend all other threads and close all other
+ HANDLEs (not associated with file descriptors) and SOCKETs, but we
+ can't do so since we have no way to enumerate them. */
+ {
+ unsigned int fdmax = _getmaxstdio ();
+ unsigned int fd;
+ for (fd = 0; fd < fdmax; fd++)
+ close (fd);
+ }
+
+ /* Wait until the child process is terminated. */
+ int status = 127;
+ waitpid (child, &status, 0);
+ int exitcode = status;
+
+ /* Pass its exit code to the parent process. */
+ _exit (exitcode);
+ }
+ errno = err;
+ return -1;
+}
+
+#endif
--- /dev/null
+# execve.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_EXECVE],
+[
+ AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+ AC_REQUIRE([AC_CANONICAL_HOST])
+
+ case "$host_os" in
+ mingw*) REPLACE_EXECVE=1 ;;
+ esac
+])
--- /dev/null
+Description:
+execve() function: Execute a program, replacing the current process.
+
+Files:
+lib/execve.c
+m4/execve.m4
+
+Depends-on:
+unistd
+posix_spawn [test $REPLACE_EXECVE = 1]
+close [test $REPLACE_EXECVE = 1]
+sys_wait [test $REPLACE_EXECVE = 1]
+waitpid [test $REPLACE_EXECVE = 1]
+
+configure.ac:
+gl_FUNC_EXECVE
+if test $REPLACE_EXECVE = 1; then
+ AC_LIBOBJ([execve])
+fi
+gl_UNISTD_MODULE_INDICATOR([execve])
+
+Makefile.am:
+
+Include:
+<unistd.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+all