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