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  _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 }
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 60 of file gnunet-timeout.c.

Referenced by main().

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

◆ main()

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

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

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

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 }
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().