Build from the published sources, not from git
[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 container.
4 //
5 // Copyright (c) 2021-2023 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 char *logging_flag = "-x1";
29
30 // Call this instead of exit() just for common diagnostics etc.
31 void ctdlvisor_exit(int code) {
32         fprintf(stderr, "ctdlvisor: exit code %d\n", code);
33         exit(code);
34 }
35
36
37 // Interrupting this program with a signal will begin an orderly shutdown.
38 void signal_handler(int signal) {
39         fprintf(stderr, "ctdlvisor: caught signal %d\n", signal);
40
41         while(shutting_down) {
42                 fprintf(stderr, "ctdlvisor: already shutting down\n");
43                 sleep(1);
44         }
45
46         int status;
47         pid_t who_exited;
48         char *what_exited = NULL;
49
50         shutting_down = 1;
51         kill(citserver_pid, SIGTERM);
52         kill(webcit_pid, SIGTERM);
53         kill(webcits_pid, SIGTERM);
54         do {
55                 fprintf(stderr, "ctdlvisor: waiting for all child process to exit...\n");
56                 who_exited = waitpid(-1, &status, 0);
57                 if (who_exited == citserver_pid) {
58                         what_exited = "Citadel Server";
59                 }
60                 else if (who_exited == webcit_pid) {
61                         what_exited = "WebCit HTTP";
62                 }
63                 else if (who_exited == webcits_pid) {
64                         what_exited = "WebCit HTTPS";
65                 }
66                 else {
67                         what_exited = "unknown";
68                 }
69                 if (who_exited >= 0) {
70                         fprintf(stderr, "ctdlvisor: %d (%s) ended, status=%d\n", who_exited, what_exited, status);
71                 }
72         } while (who_exited >= 0);
73         ctdlvisor_exit(0);
74 }
75
76
77 void detach_from_tty(void) {
78         signal(SIGHUP, SIG_IGN);
79         signal(SIGINT, SIG_IGN);
80         signal(SIGQUIT, SIG_IGN);
81
82         setsid();       // become our own process group leader
83         umask(0);
84         if (    (freopen("/dev/null", "r", stdin) != stdin) ||
85                 (freopen("/dev/null", "w", stdout) != stdout) ||
86                 (freopen("/dev/null", "w", stderr) != stderr)
87         ) {
88                 fprintf(stderr, "sysdep: unable to reopen stdio: %s\n", strerror(errno));
89         }
90 }
91
92
93 pid_t start_citadel() {
94         pid_t pid = fork();
95         if (pid == 0) {
96                 fprintf(stderr, "ctdlvisor: executing citserver\n");
97                 //detach_from_tty();
98                 execlp("/usr/local/citadel/citserver", "citserver", logging_flag, "-h", CTDL_DIR, NULL);
99                 exit(errno);
100         }
101         else {
102                 fprintf(stderr, "ctdlvisor: citserver running on pid=%d\n", pid);
103                 return(pid);
104         }
105 }
106
107
108 pid_t start_webcit() {
109         pid_t pid = fork();
110         if (pid == 0) {
111                 fprintf(stderr, "ctdlvisor: executing webcit (http)\n");
112                 //detach_from_tty();
113                 execlp("/usr/local/webcit/webcit", "webcit", logging_flag, "-p", "80", "uds", CTDL_DIR, NULL);
114                 exit(errno);
115         }
116         else {
117                 fprintf(stderr, "ctdlvisor: webcit (HTTP) running on pid=%d\n", pid);
118                 return(pid);
119         }
120 }
121
122
123 pid_t start_webcits() {
124         pid_t pid = fork();
125         if (pid == 0) {
126                 fprintf(stderr, "ctdlvisor: executing webcit (https)\n");
127                 //detach_from_tty();
128                 execlp("/usr/local/webcit/webcit", "webcit", logging_flag, "-s", "-p", "443", "uds", CTDL_DIR, NULL);
129                 exit(errno);
130         }
131         else {
132                 fprintf(stderr, "ctdlvisor: webcit (HTTPS) running on pid=%d\n", pid);
133                 return(pid);
134         }
135 }
136
137
138 void main_loop(void) {
139         int status;
140         pid_t who_exited;
141         int citserver_exit_code = 0;
142
143         do {
144                 who_exited = waitpid(-1, &status, 0);
145                 fprintf(stderr, "ctdlvisor: pid=%d exited, status=%d, exitcode=%d\n", who_exited, status, WEXITSTATUS(status));
146
147                 // A *deliberate* exit of citserver will cause ctdlvisor to shut the whole container down.
148                 // If it crashes, however, we will start it back up.
149                 if (who_exited == citserver_pid) {
150                         citserver_exit_code = WEXITSTATUS(status);
151                         if ((WIFEXITED(status)) && (citserver_exit_code == 0)) {
152                                 fprintf(stderr, "ctdlvisor: citserver exited normally - ending container session\n");
153                                 shutting_down = 1;
154                                 kill(webcit_pid, SIGTERM);
155                                 kill(webcits_pid, SIGTERM);
156                         }
157                         else if ((WIFEXITED(status)) && (citserver_exit_code >= 101) && (citserver_exit_code <= 109)) {
158                                 fprintf(stderr, "ctdlvisor: citserver exited intentionally - ending container session\n");
159                                 shutting_down = 1;
160                                 kill(webcit_pid, SIGTERM);
161                                 kill(webcits_pid, SIGTERM);
162                         }
163                         else {
164                                 if (WIFSIGNALED(status)) {
165                                         fprintf(stderr, "ctdlvisor: citserver crashed on signal %d\n", WTERMSIG(status));
166                                 }
167                                 citserver_pid = start_citadel();
168                         }
169                 }
170
171                 // WebCit processes are restarted if they exit for any reason.
172                 if ((who_exited == webcit_pid) && (!shutting_down))     webcit_pid = start_webcit();
173                 if ((who_exited == webcits_pid) && (!shutting_down))    webcits_pid = start_webcits();
174
175                 // If we somehow end up in an endless loop, at least slow it down.
176                 sleep(1);
177
178         } while (who_exited >= 0);
179         ctdlvisor_exit(citserver_exit_code);
180 }
181
182
183 int main(int argc, char **argv) {
184         int a;
185         int export_mode = 0;
186         int import_mode = 0;
187         int database_cleanup_mode = 0;
188         char bin[1024];
189
190         fprintf(stderr, "ctdlvisor: Welcome to the Citadel System running in a container.\n");
191         fprintf(stderr, "ctdlvisor: command line arguments: ");
192         for (a=0; a<argc; ++a) {
193                 fprintf(stderr, "%s ", argv[a]);
194         }
195         fprintf(stderr, "\n");
196
197         char *dirs[] = {
198                 CTDL_DIR,
199                 CTDL_DIR "/data",
200                 CTDL_DIR "/files",
201                 CTDL_DIR "/keys"
202         };
203
204         for (a=0; a<4; ++a) {
205                 mkdir(dirs[a], 0777);
206                 if (access(dirs[a], R_OK|W_OK|X_OK)) {
207                         fprintf(stderr, "ctdlvisor: %s: %s\n", dirs[a], strerror(errno));
208                         ctdlvisor_exit(errno);
209                 }
210                 else {
211                         fprintf(stderr, "ctdlvisor: %s is writable\n", dirs[a]);
212                 }
213         }
214
215         symlink(CTDL_DIR "/keys", "/usr/local/webcit/keys");
216
217         /* parse command-line arguments */
218         while ((a=getopt(argc, argv, "ceidx:")) != EOF) switch(a) {
219
220                 // test this binary for compatibility and exit
221                 case 'c':
222                         fprintf(stderr, "%s: binary compatibility CONFIRMED!  You CAN run Citadel in a container on this system.\n", argv[0]);
223                         exit(0);
224                         break;
225
226                 // export your database
227                 case 'e':
228                         export_mode = 1;
229                         break;
230
231                 // import your database
232                 case 'i':
233                         import_mode = 1;
234                         break;
235
236                 // run database_cleanup.sh only
237                 case 'd':
238                         database_cleanup_mode = 1;
239                         break;
240
241                 // logging level
242                 case 'x':
243                         logging_flag = malloc(50);
244                         snprintf(logging_flag, 50, "-x%d", atoi(optarg));
245                         break;
246
247                 // any other parameter makes it crash and burn
248                 default:
249                         fprintf(stderr, "%s: usage: %s [-c] [-e] [-i] [-d] [-x log_level]\n", argv[0], argv[0]);
250                         exit(1);
251         }
252
253
254         signal(SIGHUP, signal_handler);
255
256         // "export mode" means we only run ctdldump
257         if (export_mode) {
258                 sprintf(bin, "/usr/local/citadel/ctdldump -y -h %s", CTDL_DIR);
259                 system(bin);
260         }
261
262         // "import mode" means we only run ctdlload
263         else if (import_mode) {
264                 sprintf(bin, "/usr/local/citadel/ctdlload -y -h %s", CTDL_DIR);
265                 system(bin);
266         }
267
268         // "database cleanup mode" means we just start the server and then run database_cleanup.sh interactively
269         else if (database_cleanup_mode) {
270                 sprintf(bin, "/usr/local/citadel/database_cleanup.sh -h %s", CTDL_DIR);
271                 system(bin);
272                 kill(citserver_pid, SIGTERM);
273         }
274
275         // Otherwise, it's just a normal happy day in Citadel land.
276         else {
277                 signal(SIGTERM, signal_handler);
278                 signal(SIGINT, signal_handler);
279                 signal(SIGQUIT, signal_handler);
280         
281                 citserver_pid = start_citadel();                // start Citadel Server
282                 webcit_pid = start_webcit();                    // start WebCit HTTP
283                 webcits_pid = start_webcits();                  // start WebCit HTTPS
284
285                 main_loop();
286         }
287
288         ctdlvisor_exit(0);
289 }