GNUnet  0.11.x
gnunet-timeout.c
Go to the documentation of this file.
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  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 
34 static pid_t child;
35 
36 
37 static void
38 sigchld_handler (int val)
39 {
40  int status = 0;
41  int ret = 0;
42 
43  (void) val;
44  waitpid (child, &status, 0);
45  if (WIFEXITED (status) != 0)
46  {
47  ret = WEXITSTATUS (status);
48  _exit (ret); /* return same status code */
49  }
50  if (WIFSIGNALED (status) != 0)
51  {
52  ret = WTERMSIG (status);
53  kill (getpid (), ret); /* kill self with the same signal */
54  }
55  _exit (-1);
56 }
57 
58 
59 static void
60 sigint_handler (int val)
61 {
62  kill (0, val);
63  _exit (val);
64 }
65 
66 
67 int
68 main (int argc, char *argv[])
69 {
70  int timeout = 0;
71  pid_t gpid = 0;
72 
73  if (argc < 3)
74  {
75  fprintf (stderr,
76  "arg 1: timeout in sec., arg 2: executable, arg<n> arguments\n");
77  exit (-1);
78  }
79 
80  timeout = atoi (argv[1]);
81 
82  if (timeout == 0)
83  timeout = 600;
84 
85  /* with getpgid() it does not compile, but getpgrp is the BSD version and working */
86  gpid = getpgrp ();
87 
88  signal (SIGCHLD, sigchld_handler);
89  signal (SIGABRT, sigint_handler);
90  signal (SIGFPE, sigint_handler);
91  signal (SIGILL, sigint_handler);
92  signal (SIGINT, sigint_handler);
93  signal (SIGSEGV, sigint_handler);
94  signal (SIGTERM, sigint_handler);
95 
96  child = fork ();
97  if (child == 0)
98  {
99  /* int setpgrp(pid_t pid, pid_t pgid); is not working on this machine */
100  // setpgrp (0, pid_t gpid);
101  if (-1 != gpid)
102  setpgid (0, gpid);
103  execvp (argv[2], &argv[2]);
104  exit (-1);
105  }
106  if (child > 0)
107  {
108  sleep (timeout);
109  printf ("Child processes were killed after timeout of %u seconds\n",
110  timeout);
111  kill (0, SIGTERM);
112  exit (3);
113  }
114  exit (-1);
115 }
116 
117 
118 /* end of timeout_watchdog.c */
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static pid_t child
static void sigint_handler(int val)
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
Definition: gnunet-abd.c:61
uint16_t status
See PRISM_STATUS_*-constants.
static void sigchld_handler(int val)
int main(int argc, char *argv[])