1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-02-11 01:42:17 +02:00

timeout: use fork and execvp instead of posix_spawn

* NEWS: Remove timeout from the list of programs that use posix_spawn.
* bootstrap.conf (gnulib_modules): Remove posix_spawnattr_setsigmask.
* src/timeout.c: Don't include spawn.h.
(main): Use fork and execvp instead of posix_spawn.

This reverts commit dac96ce3e3.
This commit is contained in:
Collin Funk
2025-10-30 21:10:52 -07:00
parent eff5f82e92
commit 58a66cf3fd
3 changed files with 26 additions and 28 deletions

4
NEWS
View File

@@ -56,8 +56,8 @@ GNU coreutils NEWS -*- outline -*-
'fmt', 'nl', and 'pr' will now exit promptly upon receiving a write error, 'fmt', 'nl', and 'pr' will now exit promptly upon receiving a write error,
which is significant when reading large / unbounded inputs. which is significant when reading large / unbounded inputs.
install, sort, split, and timeout now use posix_spawn() to invoke child install, sort, and split now use posix_spawn() to invoke child programs more
programs more efficiently and more independently from their own memory usage. efficiently and more independently from their own memory usage.
'numfmt': 'numfmt':
- parses numbers with a non-breaking space character before a unit - parses numbers with a non-breaking space character before a unit

View File

@@ -219,7 +219,6 @@ gnulib_modules="
posix_spawnattr_init posix_spawnattr_init
posix_spawnattr_setflags posix_spawnattr_setflags
posix_spawnattr_setsigdefault posix_spawnattr_setsigdefault
posix_spawnattr_setsigmask
posix_spawn_file_actions_addclose posix_spawn_file_actions_addclose
posix_spawn_file_actions_adddup2 posix_spawn_file_actions_adddup2
posix_spawn_file_actions_destroy posix_spawn_file_actions_destroy

View File

@@ -49,7 +49,6 @@
#include <stdio.h> #include <stdio.h>
#include <sys/types.h> #include <sys/types.h>
#include <signal.h> #include <signal.h>
#include <spawn.h>
#if HAVE_PRCTL #if HAVE_PRCTL
# include <sys/prctl.h> # include <sys/prctl.h>
#endif #endif
@@ -548,35 +547,35 @@ main (int argc, char **argv)
sigset_t orig_set; sigset_t orig_set;
block_cleanup_and_chld (term_signal, &orig_set); block_cleanup_and_chld (term_signal, &orig_set);
/* posix_spawn doesn't reset SIG_IGN -> SIG_DFL. */ /* We cannot use posix_spawn here since the child will have an exit status of
sigset_t default_set; 127 for any failure. If implemented through fork and exec, posix_spawn
sigemptyset (&default_set); will return successfully and 'timeout' will have no way to determine if it
sigaddset (&default_set, SIGTTIN); should exit with EXIT_CANNOT_INVOKE or EXIT_ENOENT upon checking the exit
sigaddset (&default_set, SIGTTOU); status of the child. */
monitored_pid = fork ();
int result; if (monitored_pid == -1)
posix_spawnattr_t attr;
if ((result = posix_spawnattr_init (&attr))
|| (result = posix_spawnattr_setflags (&attr,
(POSIX_SPAWN_USEVFORK
| POSIX_SPAWN_SETSIGDEF
| POSIX_SPAWN_SETSIGMASK)))
|| (result = posix_spawnattr_setsigdefault (&attr, &default_set))
|| (result = posix_spawnattr_setsigmask (&attr, &orig_set)))
{ {
error (0, result, _("posix_spawn initialization failed")); error (0, errno, _("fork system call failed"));
return EXIT_CANCELED; return EXIT_CANCELED;
} }
else if (monitored_pid == 0) /* child */
result = posix_spawnp (&monitored_pid, argv[0], nullptr, &attr, argv,
environ);
if (result)
{ {
/* Restore signal mask for child. */
if (sigprocmask (SIG_SETMASK, &orig_set, nullptr) != 0)
{
error (0, errno, _("child failed to reset signal mask"));
return EXIT_CANCELED;
}
/* exec doesn't reset SIG_IGN -> SIG_DFL. */
signal (SIGTTIN, SIG_DFL);
signal (SIGTTOU, SIG_DFL);
execvp (argv[0], argv);
/* exit like sh, env, nohup, ... */ /* exit like sh, env, nohup, ... */
int exit_status = result == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE; int exit_status = errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE;
error (0, result, _("failed to run command %s"), quote (command)); error (0, errno, _("failed to run command %s"), quote (command));
return exit_status; return exit_status;
} }
else else