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