diff options
author | kris <kris@FreeBSD.org> | 2002-11-03 04:49:39 +0000 |
---|---|---|
committer | kris <kris@FreeBSD.org> | 2002-11-03 04:49:39 +0000 |
commit | f71322a1421a78627fbc81d04cf5093ae25e3152 (patch) | |
tree | ef3459d4bc486153a0bd3cb9f2aa2860ccd151ba /Tools | |
parent | 792264e4c8889a39351d2ac67b49dd1a1c4879b3 (diff) | |
download | FreeBSD-ports-f71322a1421a78627fbc81d04cf5093ae25e3152.zip FreeBSD-ports-f71322a1421a78627fbc81d04cf5093ae25e3152.tar.gz |
Add C versions of the pnohang and ptimeout scripts (previously written in
perl). Thanks to all those who submitted versions of these.
Submitted by: marcus (based on)
Diffstat (limited to 'Tools')
-rw-r--r-- | Tools/portbuild/scripts/pnohang.c | 117 | ||||
-rw-r--r-- | Tools/portbuild/scripts/ptimeout.c | 75 |
2 files changed, 192 insertions, 0 deletions
diff --git a/Tools/portbuild/scripts/pnohang.c b/Tools/portbuild/scripts/pnohang.c new file mode 100644 index 0000000..a85b7b5 --- /dev/null +++ b/Tools/portbuild/scripts/pnohang.c @@ -0,0 +1,117 @@ +/* pnohang: executes command ($4-) with output in file ($3) + * kills command if no output with $1 seconds with message in $2 + * usage: pnohang timeout file command args ... + */ + +#include <sys/param.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <sys/stat.h> +#include <signal.h> +#include <unistd.h> +#include <time.h> +#include <errno.h> +#include <fcntl.h> +#include <string.h> +#include <stdlib.h> +#include <stdio.h> + +int +main(int argc, char *argv[]) +{ + int timeout, status, i, result, ofd; + char *command, *outfile, *message, args[MAXPATHLEN + 1]; + char logstr[BUFSIZ + 1]; + pid_t pid, pid1, pid2, child; + time_t now; + struct stat st; + struct sigaction sv; + + if (argc < 3) { + printf("usage: %s timeout outfile message command [args...]\n", + argv[0]); + exit(1); + } + + timeout = atoi(argv[1]); + outfile = argv[2]; + message = argv[3]; + command = argv[4]; + + bzero(args, MAXPATHLEN + 1); + for (i = 4; i < argc; i++) { + strlcat(args, argv[i], MAXPATHLEN - strlen(args)); + strlcat(args, " ", MAXPATHLEN - strlen(args)); + } + + pid = getpid(); + + /*printf("timeout is %d\n", timeout); + printf("outfile is %s\n", outfile); + printf("message is %s\n", message); + printf("arguments are %s", args);*/ + + if ((ofd = open(outfile, O_CREAT|O_TRUNC|O_WRONLY, 0600)) == -1) + err(1, "open"); + + if (dup2(ofd, STDOUT_FILENO) == -1) + err(1, "dup2 stdout"); + if (dup2(ofd, STDERR_FILENO) == -1) + err(1, "dup2 stderr"); + + if ((pid1 = fork()) > 0) { + if ((pid2 = fork()) > 0) { + sv.sa_handler = SIG_IGN; + sigemptyset(&sv.sa_mask); + sv.sa_flags = 0; + sigaction(SIGTERM, &sv, 0); + + /* parent */ + child = wait(&status); + /*printf("exited child is %d, status is %d\n", child, status);*/ + + if (pid1 = child) { + /*printf("killing process %d (second child)\n", pid2);*/ + kill(pid2, SIGTERM); + } else { + /*printf("killing process %d (first child)\n", pid1);*/ + kill(pid1, SIGTERM); + } + /* exit status in upper 8 bits, killed signal (if any) in + * lower 8 bits + */ + exit((status >> 8) | (status & 0xff)); + } else { + /* second child */ + for (;;) { + sleep(timeout/10); + now = time(NULL); + stat(outfile, &st); + if ((now - st.st_mtime) > timeout) { + /*snprintf(logstr, BUFSIZ, "logger -t %s killing %s %s, pid %d since no output in %d seconds", argv[0], args, message, pid, timeout); + system(logstr);*/ + printf("%s: killing %s (%s, pid %d and %d) since no output in %d seconds since %s", argv[0], args, message, pid1, pid, timeout, ctime(&now)); + printf("ps jgx before the singal\n"); + system("ps jgxww"); + sleep(1); /* give it a chance to output the message */ + kill(pid1, SIGTERM); + sleep(1); + kill(pid, SIGTERM); + sleep(1); + system("ps jgxww"); + exit(1); + } + } + } + } else { + /* first child */ + /*printf("executing %s\n", args);*/ + result = execvp(command, argv + 4); + if (result < 0) { + printf("Failed to exec %s: %s\n", args, strerror(errno)); + exit(1); + } + } + + return 0; +} diff --git a/Tools/portbuild/scripts/ptimeout.c b/Tools/portbuild/scripts/ptimeout.c new file mode 100644 index 0000000..33e9ecc --- /dev/null +++ b/Tools/portbuild/scripts/ptimeout.c @@ -0,0 +1,75 @@ +/* ptimeout: executes command but kills it after a specified timeout + * usage: ptimeout timeout command args ... + * + * $FreeBSD$ + */ + +#include <sys/param.h> +#include <sys/types.h> +#include <signal.h> +#include <sys/wait.h> +#include <unistd.h> +#include <time.h> +#include <string.h> +#include <stdlib.h> +#include <stdio.h> + +int +main(int argc, char *argv[]) +{ + int timeout, status, i; + pid_t pid1, pid2, child; + char *command, args[MAXPATHLEN + 1]; + time_t t; + + if (argc < 3) { + printf("usage: %s timeout command [args ...]\n", argv[0]); + exit(1); + } + + timeout = atoi(argv[1]); + command = argv[2]; + + bzero(args, MAXPATHLEN + 1); + for (i = 2; i < argc; i++) { + strlcat(args, argv[i], MAXPATHLEN - strlen(args)); + strlcat(args, " ", MAXPATHLEN - strlen(args)); + } + + /*printf("timeout is %d\n", timeout); + printf("arguments are %s\n", args);*/ + + if ((pid1 = fork()) > 0) { + if ((pid2 = fork()) > 0) { + /* parent */ + /*printf("child pids are %d %d\n", pid1, pid2);*/ + child = wait(&status); + /*printf("exited child is %d, status is %d\n", child, status);*/ + if (pid1 = child) { + /*printf("killing process %d\n", pid2);*/ + kill(pid2, SIGTERM); + } else { + /*printf("killing process %d\n", pid1);*/ + kill(pid1, SIGTERM); + } + /* exit status in upper 8 bits, killed signal (if any) + * in lower 8 bits + */ + exit((status >> 8) | (status & 0xff)); + } else { + /* second child */ + sleep(timeout); + t = time(NULL); + printf("ptimeout: killing %s (pid %d) since timeout of %d expired at %s", args, pid1, timeout, ctime(&t)); + kill(pid1, SIGTERM); + exit(1); + } + } else { + /* first child */ + /*printf("executing %s\n", args);*/ + execvp(command, argv + 2); + } + + /* Shouldn't be reached. */ + return 0; +} |