e8d7a5e53aac8787cb32819f2c12e6ba5880ca23
[lcr.git] / watch.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** PBX4Linux                                                                 **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** PBX Watchdog with debug function                                          **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <signal.h>
16 #include <stdarg.h>
17 #include <time.h>
18 #include <sys/time.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include "save.h"
22
23 time_t now;
24 #define GET_NOW() \
25         { \
26                 now = time(&now); \
27         }
28
29 int quit = 0;
30
31 void sighandler(int sigset)
32 {
33         if (sigset == SIGHUP)
34                 return;
35         if (sigset == SIGPIPE)
36                 return;
37         printf("Signal received: %d\n", sigset);
38         quit = 1;
39 }
40
41
42 int main(int argc, char *argv[])
43 {
44         int debug = 0;
45         int ret;
46         char command[256], file[64] = "pbx";
47
48         printf("** PBX-Watch ***\n\n");
49
50         /* show options */
51         if (argc <= 1)
52         {
53                 usage:
54                 printf("\n");
55                 printf("Usage: pbxwatch (run | debug) [fork]\n");
56                 printf("run   = Starts PBX4Linux (pbx) and restart it on every crash.\n");
57                 printf("debug = Starts PBX4Linux using debugger an restarts it on every crash.\n");
58                 printf("        Log files and back trace are moved into a seperate directory.\n");
59                 printf("fork  = Option to make pbxwatch running as daemon.\n");
60                 printf("\n");
61                 return(0);
62         }
63
64         if (!(strcasecmp(argv[1],"run")))
65                 debug = 0;
66         else
67         if (!(strcasecmp(argv[1],"debug")))
68                 debug = 1;
69         else
70                 goto usage;
71
72         /* do fork */
73         if (argc > 2)
74         if (!(strcasecmp(argv[2],"fork")))
75         {
76                 /* do daemon fork */
77                 pid_t pid;
78
79                 pid = fork();
80
81                 if (pid < 0)
82                 {
83                         fprintf(stderr, "Cannot fork!\n");
84                         exit(EXIT_FAILURE);
85                 }
86                 if (pid != 0)
87                 {
88                         printf("PBX-Watch: Starting daemon.\n");
89                         exit(0);
90                 }
91                 usleep(200000);
92                 printf("\n");
93         }
94
95         /* signal handlers */   
96         signal(SIGINT,sighandler);
97         signal(SIGHUP,sighandler);
98         signal(SIGTERM,sighandler);
99         signal(SIGPIPE,sighandler);
100
101         while(42)
102         {
103                 /* RUN */
104                 printf("*** RUNNING PBX4LINUX ***\n");
105                 if (debug)
106                 {
107                         /* write debugger batch list */
108                         SPRINT(command, "echo > /tmp/pbxwatch.batch -e \"handle SIGPIPE nostop\\\\nfile %s\\\\nrun start\\\\nbt\\\\n\"", file);
109                         system(command);
110                         SPRINT(command, "gdb --quiet --batch -x \"/tmp/pbxwatch.batch\" 2>&1 | tee %s/crashreport", INSTALL_DATA);
111                         printf("*** DEBUGGER STARTED ***\n");
112                         ret = system(command);
113                         printf("*** DEBUGGER FINISHED ***\n");
114                 } else
115                 {
116                         SCPY(command, file);
117                         ret = system(command);
118                         if (ret != 11)
119                         {
120                                 printf("*** PBX4LINUX exitted with return code %d ***\n", ret);
121                                 break;
122                         }
123                         printf("*** PBX4LINUX CRASHED ***\n");
124                 }
125
126                 /* LOG */
127                 printf("*** WRITING LOG  ***\n");
128                 GET_NOW();
129                 SPRINT(command, "mkdir \"%s/%d\" && mv \"%s/crashreport\" \"%s/%d\" && cp -a \"%s/debug*.log\" \"%s/%d\"\n", INSTALL_DATA, now, INSTALL_DATA, INSTALL_DATA, now, INSTALL_DATA, INSTALL_DATA, now);
130                 system(command);
131
132                 /* DELAY */
133                 printf("*** SLEEPING 10 SECONDS UNTIL RESTART ***\n");
134                 sleep(10);
135                 if (quit)
136                         break;
137         }
138
139         signal(SIGINT,SIG_DFL);
140         signal(SIGHUP,SIG_DFL);
141         signal(SIGTERM,SIG_DFL);
142         signal(SIGPIPE,SIG_DFL);
143
144         return(0);
145 }
146
147