diff options
author | Christian Grothoff <christian@grothoff.org> | 2018-07-01 19:38:06 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2018-07-01 19:38:06 +0200 |
commit | d409018c1da52d051bf2e95cd97a73e72cb5accd (patch) | |
tree | ae9865b0fe7c57f6553c8a8afc3706142e9595ef /src/util/gnunet-timeout.c | |
parent | dd68d665e140970c7d9dfd899fbdd4fefacb54a7 (diff) | |
download | gnunet-d409018c1da52d051bf2e95cd97a73e72cb5accd.tar.gz gnunet-d409018c1da52d051bf2e95cd97a73e72cb5accd.zip |
move gnunet-timeout to src/util/, fix issues related to #5375
Diffstat (limited to 'src/util/gnunet-timeout.c')
-rw-r--r-- | src/util/gnunet-timeout.c | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/src/util/gnunet-timeout.c b/src/util/gnunet-timeout.c new file mode 100644 index 000000000..8dfb6ad17 --- /dev/null +++ b/src/util/gnunet-timeout.c | |||
@@ -0,0 +1,128 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2010 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, or | ||
8 | (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | /** | ||
20 | * @file src/util/gnunet-timeout.c | ||
21 | * @brief small tool starting a child process, waiting that it terminates or killing it after a given timeout period | ||
22 | * @author Matthias Wachs | ||
23 | */ | ||
24 | |||
25 | #include <sys/types.h> | ||
26 | #include <sys/wait.h> | ||
27 | #include <signal.h> | ||
28 | #include <stdio.h> | ||
29 | #include <stdlib.h> | ||
30 | #include <unistd.h> | ||
31 | |||
32 | static pid_t child; | ||
33 | |||
34 | |||
35 | static void | ||
36 | sigchld_handler (int val) | ||
37 | { | ||
38 | int status = 0; | ||
39 | int ret = 0; | ||
40 | |||
41 | (void) val; | ||
42 | waitpid (child, | ||
43 | &status, | ||
44 | 0); | ||
45 | if (WIFEXITED (status) != 0) | ||
46 | { | ||
47 | ret = WEXITSTATUS (status); | ||
48 | fprintf (stderr, | ||
49 | "Process exited with result %u\n", | ||
50 | ret); | ||
51 | exit (ret); /* return same status code */ | ||
52 | } | ||
53 | if (WIFSIGNALED (status) != 0) | ||
54 | { | ||
55 | ret = WTERMSIG (status); | ||
56 | fprintf (stderr, | ||
57 | "Process received signal %u\n", | ||
58 | ret); | ||
59 | kill (getpid (), | ||
60 | ret); /* kill self with the same signal */ | ||
61 | } | ||
62 | exit (-1); | ||
63 | } | ||
64 | |||
65 | |||
66 | static void | ||
67 | sigint_handler (int val) | ||
68 | { | ||
69 | kill (0, | ||
70 | val); | ||
71 | exit (val); | ||
72 | } | ||
73 | |||
74 | |||
75 | int | ||
76 | main (int argc, | ||
77 | char *argv[]) | ||
78 | { | ||
79 | int timeout = 0; | ||
80 | pid_t gpid = 0; | ||
81 | |||
82 | if (argc < 3) | ||
83 | { | ||
84 | fprintf (stderr, | ||
85 | "arg 1: timeout in sec., arg 2: executable, arg<n> arguments\n"); | ||
86 | exit (-1); | ||
87 | } | ||
88 | |||
89 | timeout = atoi (argv[1]); | ||
90 | |||
91 | if (timeout == 0) | ||
92 | timeout = 600; | ||
93 | |||
94 | /* with getpgid() it does not compile, but getpgrp is the BSD version and working */ | ||
95 | gpid = getpgrp (); | ||
96 | |||
97 | signal (SIGCHLD, sigchld_handler); | ||
98 | signal (SIGABRT, sigint_handler); | ||
99 | signal (SIGFPE, sigint_handler); | ||
100 | signal (SIGILL, sigint_handler); | ||
101 | signal (SIGINT, sigint_handler); | ||
102 | signal (SIGSEGV, sigint_handler); | ||
103 | signal (SIGTERM, sigint_handler); | ||
104 | |||
105 | child = fork (); | ||
106 | if (child == 0) | ||
107 | { | ||
108 | /* int setpgrp(pid_t pid, pid_t pgid); is not working on this machine */ | ||
109 | //setpgrp (0, pid_t gpid); | ||
110 | if (-1 != gpid) | ||
111 | setpgid (0, gpid); | ||
112 | execvp (argv[2], | ||
113 | &argv[2]); | ||
114 | exit (-1); | ||
115 | } | ||
116 | if (child > 0) | ||
117 | { | ||
118 | sleep (timeout); | ||
119 | printf ("Child processes were killed after timeout of %u seconds\n", | ||
120 | timeout); | ||
121 | kill (0, | ||
122 | SIGTERM); | ||
123 | exit (3); | ||
124 | } | ||
125 | exit (-1); | ||
126 | } | ||
127 | |||
128 | /* end of timeout_watchdog.c */ | ||