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