1 // This is a supervisor program that handles start/stop/restart of
2 // the various Citadel System components, when we are running on
3 // an AppImage instance.
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.
19 #include <sys/types.h>
25 int shutting_down = 0;
27 // Call this instead of exit() just for common diagnostics etc.
28 void ctdlvisor_exit(int code) {
29 printf("ctdlvisor: exit code %d\n", code);
34 // Interrupting this program with a signal will begin an orderly shutdown.
35 void signal_handler(int signal) {
36 fprintf(stderr, "ctdlvisor: caught signal %d\n", signal);
38 while(shutting_down) {
39 fprintf(stderr, "ctdlvisor: already shutting down\n");
45 char *what_exited = NULL;
48 kill(citserver_pid, SIGTERM);
49 kill(webcit_pid, SIGTERM);
50 kill(webcits_pid, SIGTERM);
52 fprintf(stderr, "ctdlvisor: waiting for all child process to exit...\n");
53 who_exited = waitpid(-1, &status, 0);
54 if (who_exited == citserver_pid) {
55 what_exited = "Citadel Server";
57 else if (who_exited == webcit_pid) {
58 what_exited = "WebCit HTTP";
60 else if (who_exited == webcits_pid) {
61 what_exited = "WebCit HTTPS";
64 what_exited = "unknown";
66 if (who_exited >= 0) {
67 fprintf(stderr, "ctdlvisor: %d (%s) ended, status=%d\n", who_exited, what_exited, status);
69 } while (who_exited >= 0);
74 void detach_from_tty(void) {
75 signal(SIGHUP, SIG_IGN);
76 signal(SIGINT, SIG_IGN);
77 signal(SIGQUIT, SIG_IGN);
79 setsid(); // become our own process group leader
81 if ( (freopen("/dev/null", "r", stdin) != stdin) ||
82 (freopen("/dev/null", "w", stdout) != stdout) ||
83 (freopen("/dev/null", "w", stderr) != stderr)
85 fprintf(stderr, "sysdep: unable to reopen stdio: %s\n", strerror(errno));
90 pid_t start_citadel() {
92 sprintf(bin, "%s/usr/local/citadel/citserver", getenv("APPDIR"));
95 fprintf(stderr, "ctdlvisor: executing %s with data directory %s\n", bin, getenv("CTDL_DIR"));
97 execlp(bin, "citserver", "-x9", "-h", getenv("CTDL_DIR"), NULL);
101 fprintf(stderr, "ctdlvisor: citserver running on pid=%d\n", pid);
107 pid_t start_webcit() {
109 sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
111 sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
114 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
116 execlp(bin, "webcit", "-x9", wchome, "-p", getenv("HTTP_PORT"), "uds", getenv("CTDL_DIR"), NULL);
120 fprintf(stderr, "ctdlvisor: webcit (HTTP) running on pid=%d\n", pid);
126 pid_t start_webcits() {
128 sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
130 sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
133 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
135 execlp(bin, "webcit", "-x9", wchome, "-s", "-p", getenv("HTTPS_PORT"), "uds", getenv("CTDL_DIR"), NULL);
139 fprintf(stderr, "ctdlvisor: webcit (HTTPS) running on pid=%d\n", pid);
145 void main_loop(void) {
148 int citserver_exit_code = 0;
151 who_exited = waitpid(-1, &status, 0);
152 fprintf(stderr, "ctdlvisor: pid=%d exited, status=%d, exitcode=%d\n", who_exited, status, WEXITSTATUS(status));
154 // A *deliberate* exit of citserver will cause ctdlvisor to shut the whole AppImage down.
155 // If it crashes, however, we will start it back up.
156 if (who_exited == citserver_pid) {
157 citserver_exit_code = WEXITSTATUS(status);
158 if ((WIFEXITED(status)) && (citserver_exit_code == 0)) {
159 fprintf(stderr, "ctdlvisor: citserver exited normally - ending AppImage session\n");
161 kill(webcit_pid, SIGTERM);
162 kill(webcits_pid, SIGTERM);
164 else if ((WIFEXITED(status)) && (citserver_exit_code >= 101) && (citserver_exit_code <= 109)) {
165 fprintf(stderr, "ctdlvisor: citserver exited intentionally - ending AppImage session\n");
167 kill(webcit_pid, SIGTERM);
168 kill(webcits_pid, SIGTERM);
171 if (WIFSIGNALED(status)) {
172 fprintf(stderr, "ctdlvisor: citserver crashed on signal %d\n", WTERMSIG(status));
174 citserver_pid = start_citadel();
178 // WebCit processes are restarted if they exit for any reason.
179 if ((who_exited == webcit_pid) && (!shutting_down)) webcit_pid = start_webcit();
180 if ((who_exited == webcits_pid) && (!shutting_down)) webcits_pid = start_webcits();
182 // If we somehow end up in an endless loop, at least slow it down.
185 } while (who_exited >= 0);
186 ctdlvisor_exit(citserver_exit_code);
190 int main(int argc, char **argv) {
192 int migrate_mode = 0;
194 if (getenv("APPDIR") == NULL) {
195 fprintf(stderr, "ctdlvisor: APPDIR is not set. This program must be run from within an AppImage.\n");
199 /* parse command-line arguments */
200 while ((a=getopt(argc, argv, "cm")) != EOF) switch(a) {
202 // test this binary for compatibility and exit
204 fprintf(stderr, "%s: binary compatibility confirmed\n", argv[0]);
208 // run ctdlmigrate only
213 // any other parameter makes it crash and burn
215 fprintf(stderr, "usage\n");
219 fprintf(stderr, "ctdlvisor: Welcome to the Citadel System, brought to you using AppImage.\n");
220 fprintf(stderr, "ctdlvisor: LD_LIBRARY_PATH = %s\n", getenv("LD_LIBRARY_PATH"));
221 fprintf(stderr, "ctdlvisor: PATH = %s\n", getenv("PATH"));
222 fprintf(stderr, "ctdlvisor: APPDIR = %s\n", getenv("APPDIR"));
223 fprintf(stderr, "ctdlvisor: data directory = %s\n", getenv("CTDL_DIR"));
224 fprintf(stderr, "ctdlvisor: HTTP port = %s\n", getenv("HTTP_PORT"));
225 fprintf(stderr, "ctdlvisor: HTTPS port = %s\n", getenv("HTTPS_PORT"));
227 if (access(getenv("CTDL_DIR"), R_OK|W_OK|X_OK)) {
228 fprintf(stderr, "ctdlvisor: %s: %s\n", getenv("CTDL_DIR"), strerror(errno));
229 ctdlvisor_exit(errno);
232 signal(SIGHUP, signal_handler);
234 // "migrate mode" means we just start the server and then run ctdlmigrate interactively.
236 citserver_pid = start_citadel();
237 fprintf(stderr, "ctdlvisor: waiting a moment for citserver to initialize...\n");
240 sprintf(bin, "%s/usr/local/citadel/ctdlmigrate -h %s", getenv("APPDIR"), getenv("CTDL_DIR"));
242 kill(citserver_pid, SIGTERM);
245 // Otherwise, it's just a normal happy day in Citadel land.
247 signal(SIGTERM, signal_handler);
248 signal(SIGINT, signal_handler);
249 signal(SIGQUIT, signal_handler);
251 citserver_pid = start_citadel();
252 webcit_pid = start_webcit();
253 webcits_pid = start_webcits();