1 // This is a supervisor program that handles start/stop/restart of
2 // the various Citadel System components, when we are running in
5 // Copyright (c) 2021 by the citadel.org team
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.
11 #define CTDL_DIR "/citadel-data"
21 #include <sys/types.h>
27 int shutting_down = 0;
28 char *logging_flag = "-x1";
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);
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);
41 while(shutting_down) {
42 fprintf(stderr, "ctdlvisor: already shutting down\n");
48 char *what_exited = NULL;
51 kill(citserver_pid, SIGTERM);
52 kill(webcit_pid, SIGTERM);
53 kill(webcits_pid, SIGTERM);
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";
60 else if (who_exited == webcit_pid) {
61 what_exited = "WebCit HTTP";
63 else if (who_exited == webcits_pid) {
64 what_exited = "WebCit HTTPS";
67 what_exited = "unknown";
69 if (who_exited >= 0) {
70 fprintf(stderr, "ctdlvisor: %d (%s) ended, status=%d\n", who_exited, what_exited, status);
72 } while (who_exited >= 0);
77 void detach_from_tty(void) {
78 signal(SIGHUP, SIG_IGN);
79 signal(SIGINT, SIG_IGN);
80 signal(SIGQUIT, SIG_IGN);
82 setsid(); // become our own process group leader
84 if ( (freopen("/dev/null", "r", stdin) != stdin) ||
85 (freopen("/dev/null", "w", stdout) != stdout) ||
86 (freopen("/dev/null", "w", stderr) != stderr)
88 fprintf(stderr, "sysdep: unable to reopen stdio: %s\n", strerror(errno));
93 pid_t start_citadel() {
96 fprintf(stderr, "ctdlvisor: executing citserver\n");
98 execlp("/usr/local/citadel/citserver", "citserver", logging_flag, "-h", CTDL_DIR, NULL);
102 fprintf(stderr, "ctdlvisor: citserver running on pid=%d\n", pid);
108 pid_t start_webcit() {
111 fprintf(stderr, "ctdlvisor: executing webcit (http)\n");
113 execlp("/usr/local/webcit/webcit", "webcit", logging_flag, "-p", "80", "uds", CTDL_DIR, NULL);
117 fprintf(stderr, "ctdlvisor: webcit (HTTP) running on pid=%d\n", pid);
123 pid_t start_webcits() {
126 fprintf(stderr, "ctdlvisor: executing webcit (https)\n");
128 execlp("/usr/local/webcit/webcit", "webcit", logging_flag, "-s", "-p", "443", "uds", CTDL_DIR, NULL);
132 fprintf(stderr, "ctdlvisor: webcit (HTTPS) running on pid=%d\n", pid);
138 void main_loop(void) {
141 int citserver_exit_code = 0;
144 who_exited = waitpid(-1, &status, 0);
145 fprintf(stderr, "ctdlvisor: pid=%d exited, status=%d, exitcode=%d\n", who_exited, status, WEXITSTATUS(status));
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");
154 kill(webcit_pid, SIGTERM);
155 kill(webcits_pid, SIGTERM);
157 else if ((WIFEXITED(status)) && (citserver_exit_code >= 101) && (citserver_exit_code <= 109)) {
158 fprintf(stderr, "ctdlvisor: citserver exited intentionally - ending container session\n");
160 kill(webcit_pid, SIGTERM);
161 kill(webcits_pid, SIGTERM);
164 if (WIFSIGNALED(status)) {
165 fprintf(stderr, "ctdlvisor: citserver crashed on signal %d\n", WTERMSIG(status));
167 citserver_pid = start_citadel();
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();
175 // If we somehow end up in an endless loop, at least slow it down.
178 } while (who_exited >= 0);
179 ctdlvisor_exit(citserver_exit_code);
183 int main(int argc, char **argv) {
185 int migrate_mode = 0;
186 int database_cleanup_mode = 0;
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]);
193 fprintf(stderr, "\n");
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);
209 fprintf(stderr, "ctdlvisor: %s is writable\n", dirs[a]);
213 symlink(CTDL_DIR "/keys", "/usr/local/webcit/keys");
215 /* parse command-line arguments */
216 while ((a=getopt(argc, argv, "cmdx:")) != EOF) switch(a) {
218 // test this binary for compatibility and exit
220 fprintf(stderr, "%s: binary compatibility confirmed\n", argv[0]);
224 // run ctdlmigrate only
229 // run database_cleanup.sh only
231 database_cleanup_mode = 1;
236 logging_flag = malloc(50);
237 snprintf(logging_flag, 50, "-x%d", atoi(optarg));
240 // any other parameter makes it crash and burn
242 fprintf(stderr, "%s: usage: %s [-c] [-m] [-d] [-x log_level]\n");
247 signal(SIGHUP, signal_handler);
249 // "migrate mode" means we just start the server and then run ctdlmigrate interactively.
251 citserver_pid = start_citadel();
252 fprintf(stderr, "ctdlvisor: waiting a moment for citserver to initialize...\n");
255 sprintf(bin, "/usr/local/citadel/ctdlmigrate -h %s", CTDL_DIR);
257 kill(citserver_pid, SIGTERM);
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");
266 sprintf(bin, "/usr/local/citadel/database_cleanup.sh -h %s", CTDL_DIR);
268 kill(citserver_pid, SIGTERM);
271 // Otherwise, it's just a normal happy day in Citadel land.
273 signal(SIGTERM, signal_handler);
274 signal(SIGINT, signal_handler);
275 signal(SIGQUIT, signal_handler);
277 citserver_pid = start_citadel(); // start Citadel Server
278 webcit_pid = start_webcit(); // start WebCit HTTP
279 webcits_pid = start_webcits(); // start WebCit HTTPS