GNUnet  0.10.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
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  fprintf(stderr, "Process exited with result %u\n", ret);
49  _exit(ret); /* return same status code */
50  }
51  if (WIFSIGNALED(status) != 0)
52  {
53  ret = WTERMSIG(status);
54  fprintf(stderr, "Process received signal %u\n", ret);
55  kill(getpid(), ret); /* kill self with the same signal */
56  }
57  _exit(-1);
58 }
59 
60 
61 static void
63 {
64  kill(0, val);
65  _exit(val);
66 }
67 
68 
69 int
70 main(int argc, char *argv[])
71 {
72  int timeout = 0;
73  pid_t gpid = 0;
74 
75  if (argc < 3)
76  {
77  fprintf(stderr,
78  "arg 1: timeout in sec., arg 2: executable, arg<n> arguments\n");
79  exit(-1);
80  }
81 
82  timeout = atoi(argv[1]);
83 
84  if (timeout == 0)
85  timeout = 600;
86 
87  /* with getpgid() it does not compile, but getpgrp is the BSD version and working */
88  gpid = getpgrp();
89 
90  signal(SIGCHLD, sigchld_handler);
91  signal(SIGABRT, sigint_handler);
92  signal(SIGFPE, sigint_handler);
93  signal(SIGILL, sigint_handler);
94  signal(SIGINT, sigint_handler);
95  signal(SIGSEGV, sigint_handler);
96  signal(SIGTERM, sigint_handler);
97 
98  child = fork();
99  if (child == 0)
100  {
101  /* int setpgrp(pid_t pid, pid_t pgid); is not working on this machine */
102  //setpgrp (0, pid_t gpid);
103  if (-1 != gpid)
104  setpgid(0, gpid);
105  execvp(argv[2], &argv[2]);
106  exit(-1);
107  }
108  if (child > 0)
109  {
110  sleep(timeout);
111  printf("Child processes were killed after timeout of %u seconds\n",
112  timeout);
113  kill(0, SIGTERM);
114  exit(3);
115  }
116  exit(-1);
117 }
118 
119 /* end of timeout_watchdog.c */
static pid_t child
static int ret
Final status code.
Definition: gnunet-arm.c:89
static void sigint_handler(int val)
static struct GNUNET_TIME_Relative timeout
User defined timestamp for completing operations.
Definition: gnunet-arm.c:114
uint16_t status
See PRISM_STATUS_*-constants.
static void sigchld_handler(int val)
int main(int argc, char *argv[])