GNUnet  0.20.0
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 "platform.h"
#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 39 of file gnunet-timeout.c.

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

References child, ret, and status.

Referenced by main().

Here is the caller graph for this function:

◆ sigint_handler()

static void sigint_handler ( int  val)
static

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

62 {
63  kill (0, val);
64  _exit (val);
65 }

Referenced by main().

Here is the caller graph for this function:

◆ main()

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

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

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 }
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
Definition: gnunet-abd.c:61
static void sigint_handler(int val)
static void sigchld_handler(int val)

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

Here is the call graph for this function:

Variable Documentation

◆ child

pid_t child
static

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

Referenced by get_file_information(), main(), mark_as_reachable(), and sigchld_handler().