#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "wait-process.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#if defined _MSC_VER || defined __MINGW32__
#include <process.h>
#define waitpid(pid,statusp,options) _cwait (statusp, pid, WAIT_CHILD)
#define WAIT_T int
#define WTERMSIG(x) ((x) & 0xff)
#define WCOREDUMP(x) 0
#define WEXITSTATUS(x) (((x) >> 8) & 0xff)
#define WIFSIGNALED(x) (WTERMSIG (x) != 0)
#define WIFEXITED(x) (WTERMSIG (x) == 0)
#define WIFSTOPPED(x) 0
#else
#include <sys/wait.h>
#if HAVE_UNION_WAIT
# define WAIT_T union wait
# ifndef WTERMSIG
# define WTERMSIG(x) ((x).w_termsig)
# endif
# ifndef WCOREDUMP
# define WCOREDUMP(x) ((x).w_coredump)
# endif
# ifndef WEXITSTATUS
# define WEXITSTATUS(x) ((x).w_retcode)
# endif
#else
# define WAIT_T int
# ifndef WTERMSIG
# define WTERMSIG(x) ((x) & 0x7f)
# endif
# ifndef WCOREDUMP
# define WCOREDUMP(x) ((x) & 0x80)
# endif
# ifndef WEXITSTATUS
# define WEXITSTATUS(x) (((x) >> 8) & 0xff)
# endif
#endif
#ifndef WIFSIGNALED
# define WIFSIGNALED(x) (WTERMSIG (x) != 0 && WTERMSIG(x) != 0x7f)
#endif
#ifndef WIFEXITED
# define WIFEXITED(x) (WTERMSIG (x) == 0)
#endif
#ifndef WIFSTOPPED
# define WIFSTOPPED(x) (WTERMSIG (x) == 0x7f)
#endif
#endif
#include "error.h"
#include "exit.h"
#include "fatal-signal.h"
#include "xalloc.h"
#include "gettext.h"
#define _(str) gettext (str)
#define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
#if defined _MSC_VER || defined __MINGW32__
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define kill(pid,sig) TerminateProcess ((HANDLE) (pid), sig)
#endif
typedef struct
{
volatile sig_atomic_t used;
volatile pid_t child;
}
slaves_entry_t;
static slaves_entry_t static_slaves[32];
static slaves_entry_t * volatile slaves = static_slaves;
static sig_atomic_t volatile slaves_count = 0;
static size_t slaves_allocated = SIZEOF (static_slaves);
#ifdef SIGHUP
# define TERMINATOR SIGHUP
#else
# define TERMINATOR SIGTERM
#endif
static void
cleanup_slaves (void)
{
for (;;)
{
size_t n = slaves_count;
if (n == 0)
break;
n--;
slaves_count = n;
if (slaves[n].used)
{
pid_t slave = slaves[n].child;
kill (slave, TERMINATOR);
}
}
}
void
register_slave_subprocess (pid_t child)
{
static bool cleanup_slaves_registered = false;
if (!cleanup_slaves_registered)
{
atexit (cleanup_slaves);
at_fatal_signal (cleanup_slaves);
cleanup_slaves_registered = true;
}
{
slaves_entry_t *s = slaves;
slaves_entry_t *s_end = s + slaves_count;
for (; s < s_end; s++)
if (!s->used)
{
s->child = child;
s->used = 1;
return;
}
}
if (slaves_count == slaves_allocated)
{
slaves_entry_t *old_slaves = slaves;
size_t new_slaves_allocated = 2 * slaves_allocated;
slaves_entry_t *new_slaves =
malloc (new_slaves_allocated * sizeof (slaves_entry_t));
if (new_slaves == NULL)
{
kill (child, TERMINATOR);
xalloc_die ();
}
memcpy (new_slaves, old_slaves,
slaves_allocated * sizeof (slaves_entry_t));
slaves = new_slaves;
slaves_allocated = new_slaves_allocated;
if (old_slaves != static_slaves)
free (old_slaves);
}
slaves[slaves_count].child = child;
slaves[slaves_count].used = 1;
slaves_count++;
}
static inline void
unregister_slave_subprocess (pid_t child)
{
slaves_entry_t *s = slaves;
slaves_entry_t *s_end = s + slaves_count;
for (; s < s_end; s++)
if (s->used && s->child == child)
s->used = 0;
}
int
wait_subprocess (pid_t child, const char *progname,
bool null_stderr,
bool slave_process, bool exit_on_error)
{
#if HAVE_WAITID && defined WNOWAIT && 0
siginfo_t info;
for (;;)
{
if (waitid (P_PID, child, &info, slave_process ? WNOWAIT : 0) < 0)
{
# ifdef EINTR
if (errno == EINTR)
continue;
# endif
if (exit_on_error || !null_stderr)
error (exit_on_error ? EXIT_FAILURE : 0, errno,
_("%s subprocess"), progname);
return 127;
}
if (info.si_code == CLD_EXITED
|| info.si_code == CLD_KILLED || info.si_code == CLD_DUMPED)
break;
}
if (slave_process)
{
unregister_slave_subprocess (child);
for (;;)
{
if (waitid (P_PID, child, &info, 0) < 0)
{
# ifdef EINTR
if (errno == EINTR)
continue;
# endif
if (exit_on_error || !null_stderr)
error (exit_on_error ? EXIT_FAILURE : 0, errno,
_("%s subprocess"), progname);
return 127;
}
break;
}
}
switch (info.si_code)
{
case CLD_KILLED:
case CLD_DUMPED:
if (exit_on_error || !null_stderr)
error (exit_on_error ? EXIT_FAILURE : 0, 0,
_("%s subprocess got fatal signal %d"),
progname, info.si_status);
return 127;
case CLD_EXITED:
if (info.si_status == 127)
{
if (exit_on_error || !null_stderr)
error (exit_on_error ? EXIT_FAILURE : 0, 0,
_("%s subprocess failed"), progname);
return 127;
}
return info.si_status;
default:
abort ();
}
#else
WAIT_T status;
*(int *) &status = 0;
for (;;)
{
int result = waitpid (child, &status, 0);
if (result != child)
{
# ifdef EINTR
if (errno == EINTR)
continue;
# endif
# if 0
if (errno == ECHILD)
{
*(int *) &status = 0;
break;
}
# endif
if (exit_on_error || !null_stderr)
error (exit_on_error ? EXIT_FAILURE : 0, errno,
_("%s subprocess"), progname);
return 127;
}
if (!WIFSTOPPED (status))
break;
}
if (slave_process)
unregister_slave_subprocess (child);
if (WIFSIGNALED (status))
{
if (exit_on_error || !null_stderr)
error (exit_on_error ? EXIT_FAILURE : 0, 0,
_("%s subprocess got fatal signal %d"),
progname, (int) WTERMSIG (status));
return 127;
}
if (WEXITSTATUS (status) == 127)
{
if (exit_on_error || !null_stderr)
error (exit_on_error ? EXIT_FAILURE : 0, 0,
_("%s subprocess failed"), progname);
return 127;
}
return WEXITSTATUS (status);
#endif
}