Fleshing out the command line options for run-citadel.sh
[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 container 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 container 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 container 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 int main(int argc, char **argv) {
183         int a;
184         int migrate_mode = 0;
185
186         fprintf(stderr, "ctdlvisor: Welcome to the Citadel System running in a container.\n");
187         fprintf(stderr, "ctdlvisor: command line arguments: ");
188         for (a=0; a<argc; ++a) {
189                 fprintf(stderr, "%s ", argv[a]);
190         }
191         fprintf(stderr, "\n");
192
193         char *dirs[] = {
194                 CTDL_DIR,
195                 CTDL_DIR "/data",
196                 CTDL_DIR "/files",
197                 CTDL_DIR "/keys"
198         };
199
200         for (a=0; a<4; ++a) {
201                 mkdir(dirs[a], 0777);
202                 if (access(dirs[a], R_OK|W_OK|X_OK)) {
203                         fprintf(stderr, "ctdlvisor: %s: %s\n", dirs[a], strerror(errno));
204                         ctdlvisor_exit(errno);
205                 }
206                 else {
207                         fprintf(stderr, "ctdlvisor: %s is writable\n", dirs[a]);
208                 }
209         }
210
211         symlink(CTDL_DIR "/keys", "/usr/local/webcit/keys");
212
213         /* parse command-line arguments */
214         while ((a=getopt(argc, argv, "cm")) != EOF) switch(a) {
215
216                 // test this binary for compatibility and exit
217                 case 'c':
218                         fprintf(stderr, "%s: binary compatibility confirmed\n", argv[0]);
219                         exit(0);
220                         break;
221
222                 // run ctdlmigrate only
223                 case 'm':
224                         migrate_mode = 1;
225                         break;
226
227                 // any other parameter makes it crash and burn
228                 default:
229                         fprintf(stderr, "usage\n");
230                         exit(1);
231         }
232
233
234         signal(SIGHUP, signal_handler);
235
236         // "migrate mode" means we just start the server and then run ctdlmigrate interactively.
237         if (migrate_mode) {
238                 citserver_pid = start_citadel();
239                 fprintf(stderr, "ctdlvisor: waiting a moment for citserver to initialize...\n");
240                 sleep(5);
241                 char bin[1024];
242                 sprintf(bin, "/usr/local/citadel/ctdlmigrate -h %s", CTDL_DIR);
243                 system(bin);
244                 kill(citserver_pid, SIGTERM);
245         }
246
247         // Otherwise, it's just a normal happy day in Citadel land.
248         else {
249                 signal(SIGTERM, signal_handler);
250                 signal(SIGINT, signal_handler);
251                 signal(SIGQUIT, signal_handler);
252         
253                 citserver_pid = start_citadel();                // start Citadel Server
254                 webcit_pid = start_webcit();                    // start WebCit HTTP
255                 webcits_pid = start_webcits();                  // start WebCit HTTPS
256
257                 main_loop();
258         }
259
260         ctdlvisor_exit(0);
261 }