[merp] Use a separate program as the hang supervisor. (#15715)
[mono-project.git] / tools / mono-hang-watchdog / mono-hang-watchdog.c
blobd703d8c2f968ec4833d394f549b09ffcfcb8e7b3
1 /* Given a external process' id as argument, the program waits for a set timeout then attempts to abort that process */
2 /* Used by the Mono runtime's crash reporting. */
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <errno.h>
8 #include <unistd.h>
10 #include "config.h"
12 #ifdef HAVE_SIGNAL_H
13 #include <signal.h>
14 #endif
16 #define TIMEOUT 30
18 static char* program_name;
19 void program_exit (int exit_code, const char* message);
21 int main (int argc, char* argv[])
23 program_name = argv [0];
24 if (argc != 2)
25 program_exit (1, "Please provide one argument (pid)");
26 errno = 0;
27 pid_t pid = (pid_t)strtoul (argv [1], NULL, 10);
28 if (errno)
29 program_exit (2, "Invalid pid");
31 sleep (TIMEOUT);
33 /* if we survived the timeout, we consider the Mono process as hung */
35 #ifndef HAVE_KILL
36 /* just inform the user */
37 printf ("Mono process with pid %llu appears to be hung", (uint64_t)pid);
38 return 0;
39 #else
40 printf ("Mono process hang detected, sending kill signal to pid %llu\n", (uint64_t)pid);
41 return kill (pid, SIGKILL);
42 #endif
45 void program_exit (int exit_code, const char* message)
47 if (message)
48 printf ("%s\n", message);
49 printf ("Usage: '%s [pid]'\t\t[pid]: The id for the Mono process\n", program_name);
50 exit (exit_code);