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.
 
 
 

122 lines
2.7 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 <getopt.h>
#include <errno.h>
#include <err.h>
/* Help and informations about the program. */
static void
usage(int status)
{
puts("Usage: wait4 pid\n\
Wait until the specified process terminates.\n");
puts("Options:\n\
-h, --help Display this help message.\n\
-v, --version Display the version message.\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;
}
/* Main function. */
int
main(int argc, char **argv)
{
int c, ec, len, *pids, i;
struct option options[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'v' },
{ NULL, 0, NULL, 0 }
};
setprogname(argv[0]);
while ( (c = getopt_long(argc, argv, "hv", options, NULL)) != -1 ) {
switch ( c ) {
case 'h':
usage(EXIT_SUCCESS);
break;
case '?':
usage(EXIT_FAILURE);
break;
case 'v':
info();
break;
}
}
if ( (len = argc - optind) < 1 )
usage(EXIT_FAILURE);
pids = malloc(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;
}