Module: check_mk
Branch: master
Commit: df3a7be41c2017fd5a6ecf2b62a56183bb877d35
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=df3a7be41c2017…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Mon Nov 23 10:37:01 2015 +0100
Cleaned up waitmax. Clang-formatted, iwyu'd, cppchecked.
---
agents/waitmax | Bin 9956 -> 13224 bytes
agents/waitmax.c | 204 ++++++++++++++++++++++++++----------------------------
2 files changed, 97 insertions(+), 107 deletions(-)
diff --git a/agents/waitmax b/agents/waitmax
index cb77121..b956084 100755
Binary files a/agents/waitmax and b/agents/waitmax differ
diff --git a/agents/waitmax.c b/agents/waitmax.c
index de09216..54d71be 100644
--- a/agents/waitmax.c
+++ b/agents/waitmax.c
@@ -22,136 +22,126 @@
// to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
// Boston, MA 02110-1301 USA.
-#include <stdio.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/errno.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <string.h>
#include <errno.h>
-#include <signal.h>
#include <getopt.h>
+#include <signal.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <unistd.h>
-/* macros for using write(2) instead of fprintf(stderr, ) */
-#define out(text) write(2, text, strlen(text));
+void out(const char *buf)
+{
+ size_t bytes_to_write = strlen(buf);
+ while (bytes_to_write > 0) {
+ ssize_t written = write(STDERR_FILENO, buf, bytes_to_write);
+ if (written == -1) {
+ if (errno == EINTR) continue;
+ return;
+ }
+ buf += written;
+ bytes_to_write -= written;
+ }
+}
int g_pid;
int g_timeout = 0;
int g_signum = 15;
-struct option long_options[] = {
- { "version" , no_argument, 0, 'V' },
- { "help" , no_argument, 0, 'h' },
- { "signal" , required_argument, 0, 's' },
- { 0, 0, 0, 0 } };
+struct option long_options[] = {{"version", no_argument, 0, 'V'},
+ {"help", no_argument, 0, 'h'},
+ {"signal", required_argument, 0, 's'},
+ {0, 0, 0, 0}};
void version()
{
- out("waitmax version 1.1\n"
- "Copyright Mathias Kettner 2008\n"
- "This is free software; see the source for copying conditions. There is
NO\n"
- "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.\n");
- exit(0);
+ out(
+ "waitmax version 1.1\n"
+ "Copyright Mathias Kettner 2008\n"
+ "This is free software; see the source for copying conditions. "
+ "There is NO\n"
+ "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR "
+ "PURPOSE.\n");
+ exit(0);
}
-
void usage()
{
- out("Usage: waitmax [-s SIGNUM] MAXTIME PROGRAM [ARGS...]\n"
- "\n"
- "Execute PROGRAM as a subprocess. If PROGRAM does not exit before
MAXTIME\n"
- "seconds, it will be killed with SIGTERM or an alternative signal.\n"
- "\n"
- " -s, --signal SIGNUM kill with SIGNUM on timeout\n"
- " -h, --help this help\n"
- " -V, --version show version an exit\n\n");
- exit(1);
+ out(
+ "Usage: waitmax [-s SIGNUM] MAXTIME PROGRAM [ARGS...]\n"
+ "\n"
+ "Execute PROGRAM as a subprocess. If PROGRAM does not exit before "
+ "MAXTIME\n"
+ "seconds, it will be killed with SIGTERM or an alternative signal.\n"
+ "\n"
+ " -s, --signal SIGNUM kill with SIGNUM on timeout\n"
+ " -h, --help this help\n"
+ " -V, --version show version an exit\n\n");
+ exit(1);
}
-
-void signalhandler(int signum)
+void signalhandler(int signum __attribute__((__unused__)))
{
- if (0 == kill(g_pid, g_signum))
- g_timeout = 1;
+ if (kill(g_pid, g_signum) == 0) g_timeout = 1;
}
-
int main(int argc, char **argv)
{
- int indexptr=0;
- int ret;
- setenv("POSIXLY_CORRECT", "true", 0);
- while (0 <= (ret = getopt_long(argc, argv, "Vhs:", long_options,
&indexptr))) {
- switch (ret)
- {
- case 'V':
- version();
-
- case 'h':
- usage();
-
- case 's':
- g_signum = strtoul(optarg, 0, 10);
- if (g_signum < 1 || g_signum > 32) {
- out("Signalnumber must be between 1 and 32.\n");
- exit(1);
- }
- break;
-
- default:
- usage(argv[0]);
- exit(1);
- break;
- }
- }
-
- if (optind + 1 >= argc) usage();
-
- int maxtime = atoi(argv[optind]);
- if (maxtime <= 0) usage();
-
- g_pid = fork();
- if (g_pid == 0) {
+ int indexptr = 0;
+ int ret;
+ setenv("POSIXLY_CORRECT", "true", 0);
+ while (0 <=
+ (ret = getopt_long(argc, argv, "Vhs:", long_options,
&indexptr))) {
+ switch (ret) {
+ case 'V': version();
+
+ case 'h': usage();
+
+ case 's':
+ g_signum = strtoul(optarg, 0, 10);
+ if (g_signum < 1 || g_signum > 32) {
+ out("Signalnumber must be between 1 and 32.\n");
+ exit(1);
+ }
+ break;
+
+ default: usage(); exit(1);
+ }
+ }
+
+ if (optind + 1 >= argc) usage();
+
+ int maxtime = atoi(argv[optind]);
+ if (maxtime <= 0) usage();
+
+ g_pid = fork();
+ if (g_pid == 0) {
+ signal(SIGALRM, signalhandler);
+ execvp(argv[optind + 1], argv + optind + 1);
+ out("Cannot execute ");
+ out(argv[optind + 1]);
+ out(": ");
+ out(strerror(errno));
+ out("\n");
+ exit(253);
+ }
+
signal(SIGALRM, signalhandler);
- execvp(argv[optind + 1], argv + optind + 1);
- out("Cannot execute ");
- out(argv[optind + 1]);
- out(": ");
- out(strerror(errno));
- out("\n");
- exit(253);
- }
-
- signal(SIGALRM, signalhandler);
- alarm(maxtime);
- int status;
- while (1) {
- int pid = waitpid(g_pid, &status, 0);
- if (pid <= 0) {
- if (errno == EINTR) continue; // interuppted by alarm
- else
- out("Strange: waitpid() fails: ");
- out(strerror(errno));
- out("\n");
- exit(1);
- }
- else break;
- }
-
- if (WIFEXITED(status)) {
- int exitcode = WEXITSTATUS(status);
- return exitcode;
- }
- else if (WIFSIGNALED(status)) {
- int signum = WTERMSIG(status);
- if (g_timeout)
- return 255;
- else
- return 128 + signum;
- }
- else {
+ alarm(maxtime);
+
+ int status;
+ while (waitpid(g_pid, &status, 0) == -1) {
+ if (errno != EINTR) {
+ out("Strange: waitpid() fails: ");
+ out(strerror(errno));
+ out("\n");
+ exit(1);
+ }
+ }
+
+ if (WIFEXITED(status)) return WEXITSTATUS(status);
+ if (WIFSIGNALED(status)) return g_timeout ? 255 : 128 + WTERMSIG(status);
out("Strange: program did neither exit nor was signalled.\n");
return 254;
- }
}