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-2023 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 fprintf(stderr, "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) {
187 int database_cleanup_mode = 0;
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]);
195 fprintf(stderr, "\n");
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);
211 fprintf(stderr, "ctdlvisor: %s is writable\n", dirs[a]);
215 symlink(CTDL_DIR "/keys", "/usr/local/webcit/keys");
217 /* parse command-line arguments */
218 while ((a=getopt(argc, argv, "ceidx:")) != EOF) switch(a) {
220 // test this binary for compatibility and exit
222 fprintf(stderr, "%s: binary compatibility CONFIRMED! You CAN run Citadel in a container on this system.\n", argv[0]);
226 // export your database
231 // import your database
236 // run database_cleanup.sh only
238 database_cleanup_mode = 1;
243 logging_flag = malloc(50);
244 snprintf(logging_flag, 50, "-x%d", atoi(optarg));
247 // any other parameter makes it crash and burn
249 fprintf(stderr, "%s: usage: %s [-c] [-e] [-i] [-d] [-x log_level]\n", argv[0], argv[0]);
254 signal(SIGHUP, signal_handler);
256 // "export mode" means we only run ctdldump
258 sprintf(bin, "/usr/local/citadel/ctdldump -y -h %s", CTDL_DIR);
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);
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);
272 kill(citserver_pid, SIGTERM);
275 // Otherwise, it's just a normal happy day in Citadel land.
277 signal(SIGTERM, signal_handler);
278 signal(SIGINT, signal_handler);
279 signal(SIGQUIT, signal_handler);
281 citserver_pid = start_citadel(); // start Citadel Server
282 webcit_pid = start_webcit(); // start WebCit HTTP
283 webcits_pid = start_webcits(); // start WebCit HTTPS