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