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