Removed test_binary_compatibility() from ctdlvisor.c because we don't do it that...
[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 main_loop(void) {
161         int status;
162         pid_t who_exited;
163         int citserver_exit_code = 0;
164
165         do {
166                 who_exited = waitpid(-1, &status, 0);
167                 fprintf(stderr, "ctdlvisor: pid=%d exited, status=%d, exitcode=%d\n", who_exited, status, WEXITSTATUS(status));
168
169                 // A *deliberate* exit of citserver will cause ctdlvisor to shut the whole AppImage down.
170                 // If it crashes, however, we will start it back up.
171                 if (who_exited == citserver_pid) {
172                         citserver_exit_code = WEXITSTATUS(status);
173                         if (citserver_exit_code == 0) {
174                                 fprintf(stderr, "ctdlvisor: citserver exited normally - ending AppImage session\n");
175                                 shutting_down = 1;
176                                 kill(webcit_pid, SIGTERM);
177                                 kill(webcits_pid, SIGTERM);
178                         }
179                         else if ((citserver_exit_code >= 101) && (citserver_exit_code <= 109)) {
180                                 fprintf(stderr, "ctdlvisor: citserver exited intentionally - ending AppImage session\n");
181                                 shutting_down = 1;
182                                 kill(webcit_pid, SIGTERM);
183                                 kill(webcits_pid, SIGTERM);
184                         }
185                         else {
186                                 citserver_pid = start_citadel();
187                         }
188                 }
189
190                 // WebCit processes are restarted if they exit for any reason.
191                 if ((who_exited == webcit_pid) && (!shutting_down))     webcit_pid = start_webcit();
192                 if ((who_exited == webcits_pid) && (!shutting_down))    webcits_pid = start_webcits();
193
194                 // If we somehow end up in an endless loop, at least slow it down.
195                 sleep(1);
196
197         } while (who_exited >= 0);
198         ctdlvisor_exit(citserver_exit_code);
199 }
200
201
202 int main(int argc, char **argv) {
203
204         if (getenv("APPDIR") == NULL) {
205                 fprintf(stderr, "ctdlvisor: APPDIR is not set.  This program must be run from within an AppImage.\n");
206                 ctdlvisor_exit(1);
207         }
208
209         fprintf(stderr, "ctdlvisor: Welcome to the Citadel System, brought to you using AppImage.\n");
210         fprintf(stderr, "ctdlvisor: LD_LIBRARY_PATH = %s\n", getenv("LD_LIBRARY_PATH"));
211         fprintf(stderr, "ctdlvisor:            PATH = %s\n", getenv("PATH"));
212         fprintf(stderr, "ctdlvisor:          APPDIR = %s\n", getenv("APPDIR"));
213         fprintf(stderr, "ctdlvisor:  data directory = %s\n", getenv("CTDL_DIR"));
214         fprintf(stderr, "ctdlvisor:       HTTP port = %s\n", getenv("HTTP_PORT"));
215         fprintf(stderr, "ctdlvisor:      HTTPS port = %s\n", getenv("HTTPS_PORT"));
216
217         if (access(getenv("CTDL_DIR"), R_OK|W_OK|X_OK)) {
218                 fprintf(stderr, "ctdlvisor: %s: %s\n", getenv("CTDL_DIR"), strerror(errno));
219                 ctdlvisor_exit(errno);
220         }
221
222         signal(SIGTERM, signal_handler);
223         signal(SIGHUP, signal_handler);
224         signal(SIGINT, signal_handler);
225         signal(SIGQUIT, signal_handler);
226
227         citserver_pid = start_citadel();
228         webcit_pid = start_webcit();
229         webcits_pid = start_webcits();
230
231         main_loop();
232         ctdlvisor_exit(0);
233 }