GNUnet  0.11.x
Functions | Variables
gnunet-timeout.c File Reference

small tool starting a child process, waiting that it terminates or killing it after a given timeout period More...

#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Include dependency graph for gnunet-timeout.c:

Go to the source code of this file.

Functions

static void sigchld_handler (int val)
 
static void sigint_handler (int val)
 
int main (int argc, char *argv[])
 

Variables

static pid_t child
 

Detailed Description

small tool starting a child process, waiting that it terminates or killing it after a given timeout period

Author
Matthias Wachs

Definition in file gnunet-timeout.c.

Function Documentation

◆ sigchld_handler()

static void sigchld_handler ( int  val)
static

Definition at line 38 of file gnunet-timeout.c.

References child, ret, and status.

Referenced by main().

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 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static pid_t child
uint16_t status
See PRISM_STATUS_*-constants.
Here is the caller graph for this function:

◆ sigint_handler()

static void sigint_handler ( int  val)
static

Definition at line 62 of file gnunet-timeout.c.

Referenced by main().

63 {
64  kill (0, val);
65  _exit (val);
66 }
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 70 of file gnunet-timeout.c.

References child, sigchld_handler(), sigint_handler(), and timeout.

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 }
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
static void sigchld_handler(int val)
Here is the call graph for this function:

Variable Documentation

◆ child

pid_t child
static

Definition at line 34 of file gnunet-timeout.c.

Referenced by main(), and sigchld_handler().