Add backtrace to the appimage
[citadel.git] / appimage / ctdlvisor.c
1 // This is a supervisor program that handles start/stop/restart of
2 // the various Citadel System components, when we are running on
3 // an AppImage instance.
4 //
5 // Copyright (c) 2021 by the citadel.org team
6 //
7 // This program is open source software.  It runs great on the
8 // Linux operating system (and probably elsewhere).  You can use,
9 // copy, and run it under the terms of the GNU General Public
10 // License version 3.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16
17
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <sys/wait.h>
22 #include <errno.h>
23 #include <signal.h>
24 #include <string.h>
25 #include <limits.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28
29 pid_t citserver_pid;
30 pid_t webcit_pid;
31 pid_t webcits_pid;
32 int shutting_down = 0;
33
34 // Call this instead of exit() just for common diagnostics etc.
35 void ctdlvisor_exit(int code) {
36         printf("ctdlvisor: exit code %d\n", code);
37         exit(code);
38 }
39
40
41 // Interrupting this program with a signal will begin an orderly shutdown.
42 void signal_handler(int signal) {
43         fprintf(stderr, "ctdlvisor: caught signal %d\n", signal);
44
45         while(shutting_down) {
46                 fprintf(stderr, "ctdlvisor: already shutting down\n");
47                 sleep(1);
48         }
49
50         int status;
51         pid_t who_exited;
52         char *what_exited = NULL;
53
54         shutting_down = 1;
55         kill(citserver_pid, SIGTERM);
56         kill(webcit_pid, SIGTERM);
57         kill(webcits_pid, SIGTERM);
58         do {
59                 fprintf(stderr, "ctdlvisor: waiting for all child process to exit...\n");
60                 who_exited = waitpid(-1, &status, 0);
61                 if (who_exited == citserver_pid) {
62                         what_exited = "Citadel Server";
63                 }
64                 else if (who_exited == webcit_pid) {
65                         what_exited = "WebCit HTTP";
66                 }
67                 else if (who_exited == webcits_pid) {
68                         what_exited = "WebCit HTTPS";
69                 }
70                 else {
71                         what_exited = "unknown";
72                 }
73                 if (who_exited >= 0) {
74                         fprintf(stderr, "ctdlvisor: %d (%s) ended, status=%d\n", who_exited, what_exited, status);
75                 }
76         } while (who_exited >= 0);
77         ctdlvisor_exit(0);
78 }
79
80
81 void detach_from_tty(void) {
82         signal(SIGHUP, SIG_IGN);
83         signal(SIGINT, SIG_IGN);
84         signal(SIGQUIT, SIG_IGN);
85
86         setsid();       // become our own process group leader
87         umask(0);
88         if (    (freopen("/dev/null", "r", stdin) != stdin) ||
89                 (freopen("/dev/null", "w", stdout) != stdout) ||
90                 (freopen("/dev/null", "w", stderr) != stderr)
91         ) {
92                 fprintf(stderr, "sysdep: unable to reopen stdio: %s\n", strerror(errno));
93         }
94 }
95
96
97 pid_t start_citadel() {
98         char bin[1024];
99         char backtrace_filename[PATH_MAX];
100         sprintf(bin, "%s/usr/local/citadel/citserver", getenv("APPDIR"));
101         pid_t pid = fork();
102         if (pid == 0) {
103                 fprintf(stderr, "ctdlvisor: executing %s with data directory %s\n", bin, getenv("CTDL_DIR"));
104                 detach_from_tty();
105                 sprintf(backtrace_filename, "/tmp/citserver-backtrace.%d", getpid());
106                 execlp(bin, "citserver", "-x9", "-h", getenv("CTDL_DIR"), "-b", backtrace_filename, NULL);
107                 exit(errno);
108         }
109         else {
110                 fprintf(stderr, "ctdlvisor: citserver running on pid=%d\n", pid);
111                 return(pid);
112         }
113 }
114
115
116 pid_t start_webcit() {
117         char bin[1024];
118         sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
119         char wchome[1024];
120         sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
121         pid_t pid = fork();
122         if (pid == 0) {
123                 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
124                 detach_from_tty();
125                 execlp(bin, "webcit", "-x9", wchome, "-p", getenv("HTTP_PORT"), "uds", getenv("CTDL_DIR"), NULL);
126                 exit(errno);
127         }
128         else {
129                 fprintf(stderr, "ctdlvisor: webcit (HTTP) running on pid=%d\n", pid);
130                 return(pid);
131         }
132 }
133
134
135 pid_t start_webcits() {
136         char bin[1024];
137         sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
138         char wchome[1024];
139         sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
140         pid_t pid = fork();
141         if (pid == 0) {
142                 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
143                 detach_from_tty();
144                 execlp(bin, "webcit", "-x9", wchome, "-s", "-p", getenv("HTTPS_PORT"), "uds", getenv("CTDL_DIR"), NULL);
145                 exit(errno);
146         }
147         else {
148                 fprintf(stderr, "ctdlvisor: webcit (HTTPS) running on pid=%d\n", pid);
149                 return(pid);
150         }
151 }
152
153
154 void main_loop(void) {
155         int status;
156         pid_t who_exited;
157         int citserver_exit_code = 0;
158
159         do {
160                 who_exited = waitpid(-1, &status, 0);
161                 fprintf(stderr, "ctdlvisor: pid=%d exited, status=%d, exitcode=%d\n", who_exited, status, WEXITSTATUS(status));
162
163                 // A *deliberate* exit of citserver will cause ctdlvisor to shut the whole AppImage down.
164                 // If it crashes, however, we will start it back up.
165                 if (who_exited == citserver_pid) {
166                         citserver_exit_code = WEXITSTATUS(status);
167                         if ((WIFEXITED(status)) && (citserver_exit_code == 0)) {
168                                 fprintf(stderr, "ctdlvisor: citserver exited normally - ending AppImage session\n");
169                                 shutting_down = 1;
170                                 kill(webcit_pid, SIGTERM);
171                                 kill(webcits_pid, SIGTERM);
172                         }
173                         else if ((WIFEXITED(status)) && (citserver_exit_code >= 101) && (citserver_exit_code <= 109)) {
174                                 fprintf(stderr, "ctdlvisor: citserver exited intentionally - ending AppImage session\n");
175                                 shutting_down = 1;
176                                 kill(webcit_pid, SIGTERM);
177                                 kill(webcits_pid, SIGTERM);
178                         }
179                         else {
180                                 if (WIFSIGNALED(status)) {
181                                         fprintf(stderr, "ctdlvisor: citserver crashed on signal %d\n", WTERMSIG(status));
182                                 }
183                                 citserver_pid = start_citadel();
184                         }
185                 }
186
187                 // WebCit processes are restarted if they exit for any reason.
188                 if ((who_exited == webcit_pid) && (!shutting_down))     webcit_pid = start_webcit();
189                 if ((who_exited == webcits_pid) && (!shutting_down))    webcits_pid = start_webcits();
190
191                 // If we somehow end up in an endless loop, at least slow it down.
192                 sleep(1);
193
194         } while (who_exited >= 0);
195         ctdlvisor_exit(citserver_exit_code);
196 }
197
198
199 int main(int argc, char **argv) {
200         int a;
201         int migrate_mode = 0;
202
203         if (getenv("APPDIR") == NULL) {
204                 fprintf(stderr, "ctdlvisor: APPDIR is not set.  This program must be run from within an AppImage.\n");
205                 ctdlvisor_exit(1);
206         }
207
208         /* parse command-line arguments */
209         while ((a=getopt(argc, argv, "cm")) != EOF) switch(a) {
210
211                 // test this binary for compatibility and exit
212                 case 'c':
213                         fprintf(stderr, "%s: binary compatibility confirmed\n", argv[0]);
214                         exit(0);
215                         break;
216
217                 // run ctdlmigrate only
218                 case 'm':
219                         migrate_mode = 1;
220                         break;
221
222                 // any other parameter makes it crash and burn
223                 default:
224                         fprintf(stderr, "usage\n");
225                         exit(1);
226         }
227
228         fprintf(stderr, "ctdlvisor: Welcome to the Citadel System, brought to you using AppImage.\n");
229         fprintf(stderr, "ctdlvisor: LD_LIBRARY_PATH = %s\n", getenv("LD_LIBRARY_PATH"));
230         fprintf(stderr, "ctdlvisor:            PATH = %s\n", getenv("PATH"));
231         fprintf(stderr, "ctdlvisor:          APPDIR = %s\n", getenv("APPDIR"));
232         fprintf(stderr, "ctdlvisor:  data directory = %s\n", getenv("CTDL_DIR"));
233         fprintf(stderr, "ctdlvisor:       HTTP port = %s\n", getenv("HTTP_PORT"));
234         fprintf(stderr, "ctdlvisor:      HTTPS port = %s\n", getenv("HTTPS_PORT"));
235
236         if (access(getenv("CTDL_DIR"), R_OK|W_OK|X_OK)) {
237                 fprintf(stderr, "ctdlvisor: %s: %s\n", getenv("CTDL_DIR"), strerror(errno));
238                 ctdlvisor_exit(errno);
239         }
240
241         signal(SIGHUP, signal_handler);
242
243         // "migrate mode" means we just start the server and then run ctdlmigrate interactively.
244         if (migrate_mode) {
245                 citserver_pid = start_citadel();
246                 fprintf(stderr, "ctdlvisor: waiting a moment for citserver to initialize...\n");
247                 sleep(5);
248                 char bin[1024];
249                 sprintf(bin, "%s/usr/local/citadel/ctdlmigrate", getenv("APPDIR"));
250                 system(bin);
251                 kill(citserver_pid, SIGTERM);
252         }
253
254         // Otherwise, it's just a normal happy day in Citadel land.
255         else {
256                 signal(SIGTERM, signal_handler);
257                 signal(SIGINT, signal_handler);
258                 signal(SIGQUIT, signal_handler);
259         
260                 citserver_pid = start_citadel();
261                 webcit_pid = start_webcit();
262                 webcits_pid = start_webcits();
263         
264                 main_loop();
265         }
266
267         ctdlvisor_exit(0);
268 }