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. It runs great on the
8 // Linux operating system (and probably elsewhere). You can use,
9 // copy, and run it under the terms of the GNU General Public
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
26 #include <sys/types.h>
32 int shutting_down = 0;
34 // Call this instead of exit() just for common diagnostics etc.
35 void ctdlvisor_exit(int code) {
36 printf("ctdlvisor: exit code %d\n", code);
41 // Interrupting this program with a signal will begin an orderly shutdown.
42 void signal_handler(int signal) {
43 fprintf(stderr, "ctdlvisor: caught signal %d\n", signal);
45 while(shutting_down) {
46 fprintf(stderr, "ctdlvisor: already shutting down\n");
52 char *what_exited = NULL;
55 kill(citserver_pid, SIGTERM);
56 kill(webcit_pid, SIGTERM);
57 kill(webcits_pid, SIGTERM);
59 fprintf(stderr, "ctdlvisor: waiting for all child process to exit...\n");
60 who_exited = waitpid(-1, &status, 0);
61 if (who_exited == citserver_pid) {
62 what_exited = "Citadel Server";
64 else if (who_exited == webcit_pid) {
65 what_exited = "WebCit HTTP";
67 else if (who_exited == webcits_pid) {
68 what_exited = "WebCit HTTPS";
71 what_exited = "unknown";
73 if (who_exited >= 0) {
74 fprintf(stderr, "ctdlvisor: %d (%s) ended, status=%d\n", who_exited, what_exited, status);
76 } while (who_exited >= 0);
81 void detach_from_tty(void) {
82 signal(SIGHUP, SIG_IGN);
83 signal(SIGINT, SIG_IGN);
84 signal(SIGQUIT, SIG_IGN);
86 setsid(); // become our own process group leader
88 if ( (freopen("/dev/null", "r", stdin) != stdin) ||
89 (freopen("/dev/null", "w", stdout) != stdout) ||
90 (freopen("/dev/null", "w", stderr) != stderr)
92 fprintf(stderr, "sysdep: unable to reopen stdio: %s\n", strerror(errno));
97 pid_t start_citadel() {
99 char backtrace_filename[PATH_MAX];
100 sprintf(bin, "%s/usr/local/citadel/citserver", getenv("APPDIR"));
103 fprintf(stderr, "ctdlvisor: executing %s with data directory %s\n", bin, getenv("CTDL_DIR"));
105 sprintf(backtrace_filename, "/tmp/citserver-backtrace.%d", getpid());
106 execlp(bin, "citserver", "-x9", "-h", getenv("CTDL_DIR"), "-b", backtrace_filename, NULL);
110 fprintf(stderr, "ctdlvisor: citserver running on pid=%d\n", pid);
116 pid_t start_webcit() {
118 sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
120 sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
123 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
125 execlp(bin, "webcit", "-x9", wchome, "-p", getenv("HTTP_PORT"), "uds", getenv("CTDL_DIR"), NULL);
129 fprintf(stderr, "ctdlvisor: webcit (HTTP) running on pid=%d\n", pid);
135 pid_t start_webcits() {
137 sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
139 sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
142 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
144 execlp(bin, "webcit", "-x9", wchome, "-s", "-p", getenv("HTTPS_PORT"), "uds", getenv("CTDL_DIR"), NULL);
148 fprintf(stderr, "ctdlvisor: webcit (HTTPS) running on pid=%d\n", pid);
154 void main_loop(void) {
157 int citserver_exit_code = 0;
160 who_exited = waitpid(-1, &status, 0);
161 fprintf(stderr, "ctdlvisor: pid=%d exited, status=%d, exitcode=%d\n", who_exited, status, WEXITSTATUS(status));
163 // A *deliberate* exit of citserver will cause ctdlvisor to shut the whole AppImage down.
164 // If it crashes, however, we will start it back up.
165 if (who_exited == citserver_pid) {
166 citserver_exit_code = WEXITSTATUS(status);
167 if ((WIFEXITED(status)) && (citserver_exit_code == 0)) {
168 fprintf(stderr, "ctdlvisor: citserver exited normally - ending AppImage session\n");
170 kill(webcit_pid, SIGTERM);
171 kill(webcits_pid, SIGTERM);
173 else if ((WIFEXITED(status)) && (citserver_exit_code >= 101) && (citserver_exit_code <= 109)) {
174 fprintf(stderr, "ctdlvisor: citserver exited intentionally - ending AppImage session\n");
176 kill(webcit_pid, SIGTERM);
177 kill(webcits_pid, SIGTERM);
180 if (WIFSIGNALED(status)) {
181 fprintf(stderr, "ctdlvisor: citserver crashed on signal %d\n", WTERMSIG(status));
183 citserver_pid = start_citadel();
187 // WebCit processes are restarted if they exit for any reason.
188 if ((who_exited == webcit_pid) && (!shutting_down)) webcit_pid = start_webcit();
189 if ((who_exited == webcits_pid) && (!shutting_down)) webcits_pid = start_webcits();
191 // If we somehow end up in an endless loop, at least slow it down.
194 } while (who_exited >= 0);
195 ctdlvisor_exit(citserver_exit_code);
199 int main(int argc, char **argv) {
201 int migrate_mode = 0;
203 if (getenv("APPDIR") == NULL) {
204 fprintf(stderr, "ctdlvisor: APPDIR is not set. This program must be run from within an AppImage.\n");
208 /* parse command-line arguments */
209 while ((a=getopt(argc, argv, "cm")) != EOF) switch(a) {
211 // test this binary for compatibility and exit
213 fprintf(stderr, "%s: binary compatibility confirmed\n", argv[0]);
217 // run ctdlmigrate only
222 // any other parameter makes it crash and burn
224 fprintf(stderr, "usage\n");
228 fprintf(stderr, "ctdlvisor: Welcome to the Citadel System, brought to you using AppImage.\n");
229 fprintf(stderr, "ctdlvisor: LD_LIBRARY_PATH = %s\n", getenv("LD_LIBRARY_PATH"));
230 fprintf(stderr, "ctdlvisor: PATH = %s\n", getenv("PATH"));
231 fprintf(stderr, "ctdlvisor: APPDIR = %s\n", getenv("APPDIR"));
232 fprintf(stderr, "ctdlvisor: data directory = %s\n", getenv("CTDL_DIR"));
233 fprintf(stderr, "ctdlvisor: HTTP port = %s\n", getenv("HTTP_PORT"));
234 fprintf(stderr, "ctdlvisor: HTTPS port = %s\n", getenv("HTTPS_PORT"));
236 if (access(getenv("CTDL_DIR"), R_OK|W_OK|X_OK)) {
237 fprintf(stderr, "ctdlvisor: %s: %s\n", getenv("CTDL_DIR"), strerror(errno));
238 ctdlvisor_exit(errno);
241 signal(SIGHUP, signal_handler);
243 // "migrate mode" means we just start the server and then run ctdlmigrate interactively.
245 citserver_pid = start_citadel();
246 fprintf(stderr, "ctdlvisor: waiting a moment for citserver to initialize...\n");
249 sprintf(bin, "%s/usr/local/citadel/ctdlmigrate", getenv("APPDIR"));
251 kill(citserver_pid, SIGTERM);
254 // Otherwise, it's just a normal happy day in Citadel land.
256 signal(SIGTERM, signal_handler);
257 signal(SIGINT, signal_handler);
258 signal(SIGQUIT, signal_handler);
260 citserver_pid = start_citadel();
261 webcit_pid = start_webcit();
262 webcits_pid = start_webcits();