Removed the comments about RMS being an asshole communist. I don't want anyone to...
[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                         if (WIFEXITED(status)) {
75                                 fprintf(stderr, "ctdlvisor: %d (%s) exited, exitcode=%d\n", who_exited, what_exited, WEXITSTATUS(status));
76                         }
77                         else if (WIFSIGNALED(status)) {
78                                 fprintf(stderr, "ctdlvisor: %d (%s) crashed, signal=%d\n", who_exited, what_exited, WTERMSIG(status));
79                         }
80                         else {
81                                 fprintf(stderr, "ctdlvisor: %d (%s) ended, status=%d\n", who_exited, what_exited, status);
82                         }
83                 }
84         } while (who_exited >= 0);
85         ctdlvisor_exit(0);
86 }
87
88
89 void detach_from_tty(void) {
90         signal(SIGHUP, SIG_IGN);
91         signal(SIGINT, SIG_IGN);
92         signal(SIGQUIT, SIG_IGN);
93
94         setsid();       // become our own process group leader
95         umask(0);
96         if (    (freopen("/dev/null", "r", stdin) != stdin) ||
97                 (freopen("/dev/null", "w", stdout) != stdout) ||
98                 (freopen("/dev/null", "w", stderr) != stderr)
99         ) {
100                 fprintf(stderr, "sysdep: unable to reopen stdio: %s\n", strerror(errno));
101         }
102 }
103
104
105 pid_t start_citadel() {
106         char bin[1024];
107         sprintf(bin, "%s/usr/local/citadel/citserver", getenv("APPDIR"));
108         pid_t pid = fork();
109         if (pid == 0) {
110                 fprintf(stderr, "ctdlvisor: executing %s with data directory %s\n", bin, getenv("CTDL_DIR"));
111                 detach_from_tty();
112                 execlp(bin, "citserver", "-x9", "-h", getenv("CTDL_DIR"), NULL);
113                 exit(errno);
114         }
115         else {
116                 fprintf(stderr, "ctdlvisor: citserver running on pid=%d\n", pid);
117                 return(pid);
118         }
119 }
120
121
122 pid_t start_webcit() {
123         char bin[1024];
124         sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
125         char wchome[1024];
126         sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
127         pid_t pid = fork();
128         if (pid == 0) {
129                 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
130                 detach_from_tty();
131                 execlp(bin, "webcit", "-x9", wchome, "-p", getenv("HTTP_PORT"), "uds", getenv("CTDL_DIR"), NULL);
132                 exit(errno);
133         }
134         else {
135                 fprintf(stderr, "ctdlvisor: webcit (HTTP) running on pid=%d\n", pid);
136                 return(pid);
137         }
138 }
139
140
141 pid_t start_webcits() {
142         char bin[1024];
143         sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
144         char wchome[1024];
145         sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
146         pid_t pid = fork();
147         if (pid == 0) {
148                 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
149                 detach_from_tty();
150                 execlp(bin, "webcit", "-x9", wchome, "-s", "-p", getenv("HTTPS_PORT"), "uds", getenv("CTDL_DIR"), NULL);
151                 exit(errno);
152         }
153         else {
154                 fprintf(stderr, "ctdlvisor: webcit (HTTPS) running on pid=%d\n", pid);
155                 return(pid);
156         }
157 }
158
159
160 void test_binary_compatibility(void) {
161         char cmd[1024];
162         int ret;
163         fprintf(stderr, "ctdlvisor: testing compatibility...\n");
164         sprintf(cmd, "%s/usr/local/citadel/citserver -c", getenv("APPDIR"));
165         ret = system(cmd);
166         if (ret) {
167                 fprintf(stderr, "ctdlvisor: this appimage cannot run on your system.\n"
168                                 "           The reason may be indicated by any error messages appearing above.\n");
169         }
170         else {
171                 fprintf(stderr, "ctdlvisor: this appimage appears to be compatible with your system.\n");
172         }
173         exit(ret);
174 }
175
176
177 void main_loop(void) {
178         int status;
179         pid_t who_exited;
180         int citserver_exit_code = 0;
181
182         do {
183                 who_exited = waitpid(-1, &status, 0);
184                 fprintf(stderr, "ctdlvisor: pid=%d exited, status=%d, exitcode=%d\n", who_exited, status, WEXITSTATUS(status));
185
186                 // A *deliberate* exit of citserver will cause ctdlvisor to shut the whole AppImage down.
187                 // If it crashes, however, we will start it back up.
188                 if (who_exited == citserver_pid) {
189                         citserver_exit_code = WEXITSTATUS(status);
190                         if (citserver_exit_code == 0) {
191                                 fprintf(stderr, "ctdlvisor: citserver exited normally - ending AppImage session\n");
192                                 shutting_down = 1;
193                                 kill(webcit_pid, SIGTERM);
194                                 kill(webcits_pid, SIGTERM);
195                         }
196                         else if ((citserver_exit_code >= 101) && (citserver_exit_code <= 109)) {
197                                 fprintf(stderr, "ctdlvisor: citserver exited intentionally - ending AppImage session\n");
198                                 shutting_down = 1;
199                                 kill(webcit_pid, SIGTERM);
200                                 kill(webcits_pid, SIGTERM);
201                         }
202                         else {
203                                 citserver_pid = start_citadel();
204                         }
205                 }
206
207                 // WebCit processes are restarted if they exit for any reason.
208                 if ((who_exited == webcit_pid) && (!shutting_down))     webcit_pid = start_webcit();
209                 if ((who_exited == webcits_pid) && (!shutting_down))    webcits_pid = start_webcits();
210
211                 // If we somehow end up in an endless loop, at least slow it down.
212                 sleep(1);
213
214         } while (who_exited >= 0);
215         ctdlvisor_exit(citserver_exit_code);
216 }
217
218
219 int main(int argc, char **argv) {
220
221         if (getenv("APPDIR") == NULL) {
222                 fprintf(stderr, "ctdlvisor: APPDIR is not set.  This program must be run from within an AppImage.\n");
223                 ctdlvisor_exit(1);
224         }
225
226         fprintf(stderr, "ctdlvisor: Welcome to the Citadel System, brought to you using AppImage.\n");
227         fprintf(stderr, "ctdlvisor: LD_LIBRARY_PATH = %s\n", getenv("LD_LIBRARY_PATH"));
228         fprintf(stderr, "ctdlvisor:            PATH = %s\n", getenv("PATH"));
229         fprintf(stderr, "ctdlvisor:          APPDIR = %s\n", getenv("APPDIR"));
230         fprintf(stderr, "ctdlvisor:  data directory = %s\n", getenv("CTDL_DIR"));
231         fprintf(stderr, "ctdlvisor:       HTTP port = %s\n", getenv("HTTP_PORT"));
232         fprintf(stderr, "ctdlvisor:      HTTPS port = %s\n", getenv("HTTPS_PORT"));
233
234         if (access(getenv("CTDL_DIR"), R_OK|W_OK|X_OK)) {
235                 fprintf(stderr, "ctdlvisor: %s: %s\n", getenv("CTDL_DIR"), strerror(errno));
236                 ctdlvisor_exit(errno);
237         }
238
239         signal(SIGTERM, signal_handler);
240         signal(SIGHUP, signal_handler);
241         signal(SIGINT, signal_handler);
242         signal(SIGQUIT, signal_handler);
243
244         citserver_pid = start_citadel();
245         webcit_pid = start_webcit();
246         webcits_pid = start_webcits();
247
248         main_loop();
249         ctdlvisor_exit(0);
250 }