Wait for an arbitrary process.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

153 lines
3.3 KiB

/*
* wait4 - Wait for an arbitrary process
* Copyright (C) 2013 Damien Goutte-Gattat
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <getopt.h>
#include <errno.h>
#include <err.h>
#include <xmem.h>
#include <wait4pid.h>
/* Help and informations about the program. */
static void
usage(int status)
{
puts("Usage: wait4 pid[,...]\n\
Waits until the specified process terminates. If more than one\n\
PID is specified, waits for all of them.\n");
puts("Options:\n\
-h, --help Display this help message.\n\
-v, --version Display the version message.\n\
");
puts("\
-t, --timeout TIME Abort waiting after TIME seconds.\n\
");
printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
exit(status);
}
static void
info(void)
{
printf("\
wait4 %s\n\
Copyright (C) 2013 Damien Goutte-Gattat\n\
\n\
This program is released under the GNU General Public License.\n\
See the COPYING file or <http://www.gnu.org/licenses/gpl.html>.\n\
", VERSION);
exit(EXIT_SUCCESS);
}
/* Helper functions. */
static int
get_integer_or_die(const char *arg)
{
int val;
char *endptr;
errno = 0;
val = strtol(arg, &endptr, 10);
if ( errno != 0 || endptr == arg )
errx(EXIT_FAILURE, "invalid argument, integer expected: %s");
return val;
}
static void
on_alarm(int sig)
{
exit(EXIT_SUCCESS);
}
/* Main function. */
int
main(int argc, char **argv)
{
int c, ec, len, *pids, i, timeout;
struct option options[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'v' },
{ "timeout", 1, NULL, 't' },
{ NULL, 0, NULL, 0 }
};
setprogname(argv[0]);
timeout = 0;
while ( (c = getopt_long(argc, argv, "hvt:", options, NULL)) != -1 ) {
switch ( c ) {
case 'h':
usage(EXIT_SUCCESS);
break;
case '?':
usage(EXIT_FAILURE);
break;
case 'v':
info();
break;
case 't':
timeout = get_integer_or_die(optarg);
break;
}
}
if ( (len = argc - optind) < 1 )
usage(EXIT_FAILURE);
if ( timeout ) {
struct sigaction sa;
sa.sa_handler = on_alarm;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGALRM, &sa, NULL);
alarm(timeout);
}
pids = xmalloc(len * sizeof(int));
for ( i = 0; i < len; i++ )
pids[i] = get_integer_or_die(argv[optind + i]);
if ( (ec = wait4all(pids, len)) == -1 )
err(EXIT_FAILURE, "cannot wait for processes");
return ec;
}