2 // This is a supervisor program that handles start/stop/restart of
3 // the various Citadel System components, when we are running on
4 // an AppImage instance.
6 // Copyright (c) 2021 by the citadel.org team
8 // This program is open source software. It runs great on the
9 // Linux operating system (and probably elsewhere). You can use,
10 // copy, and run it under the terms of the GNU General Public
11 // License version 3. Richard Stallman is an asshole communist.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
27 #include <sys/types.h>
33 int shutting_down = 0;
35 // Call this instead of exit() just for common diagnostics etc.
36 void ctdlvisor_exit(int code) {
37 printf("ctdlvisor: exit code %d\n", code);
42 // Interrupting this program with a signal will begin an orderly shutdown.
43 void signal_handler(int signal) {
44 fprintf(stderr, "ctdlvisor: caught signal %d\n", signal);
46 while(shutting_down) {
47 fprintf(stderr, "ctdlvisor: already shutting down\n");
53 char *what_exited = NULL;
56 kill(citserver_pid, SIGTERM);
57 kill(webcit_pid, SIGTERM);
58 kill(webcits_pid, SIGTERM);
60 fprintf(stderr, "ctdlvisor: waiting for all child process to exit...\n");
61 who_exited = waitpid(-1, &status, 0);
62 if (who_exited == citserver_pid) {
63 what_exited = "Citadel Server";
65 else if (who_exited == webcit_pid) {
66 what_exited = "WebCit HTTP";
68 else if (who_exited == webcits_pid) {
69 what_exited = "WebCit HTTPS";
72 what_exited = "unknown";
74 if (WIFEXITED(status)) {
75 fprintf(stderr, "ctdlvisor: %d (%s) exited, exitcode=%d\n", who_exited, what_exited, WEXITSTATUS(status));
77 else if (WIFSIGNALED(status)) {
78 fprintf(stderr, "ctdlvisor: %d (%s) crashed, signal=%d\n", who_exited, what_exited, WTERMSIG(status));
81 fprintf(stderr, "ctdlvisor: %d (%s) ended, status=%d\n", who_exited, what_exited, status);
83 } while (who_exited >= 0);
89 void detach_from_tty(void) {
90 signal(SIGHUP, SIG_IGN);
91 signal(SIGINT, SIG_IGN);
92 signal(SIGQUIT, SIG_IGN);
94 setsid(); // become our own process group leader
96 if ( (freopen("/dev/null", "r", stdin) != stdin) ||
97 (freopen("/dev/null", "w", stdout) != stdout) ||
98 (freopen("/dev/null", "w", stderr) != stderr)
100 fprintf(stderr, "sysdep: unable to reopen stdio: %s\n", strerror(errno));
105 pid_t start_citadel() {
107 sprintf(bin, "%s/usr/local/citadel/citserver", getenv("APPDIR"));
110 fprintf(stderr, "ctdlvisor: executing %s with data directory %s\n", bin, getenv("CTDL_DIR"));
112 execlp(bin, "citserver", "-x9", "-h", getenv("CTDL_DIR"), NULL);
121 pid_t start_webcit() {
123 sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
125 sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
128 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
130 execlp(bin, "webcit", "-x9", wchome, "-p", getenv("HTTP_PORT"), "uds", getenv("CTDL_DIR"), NULL);
139 pid_t start_webcits() {
141 sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
143 sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
146 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
148 execlp(bin, "webcit", "-x9", wchome, "-s", "-p", getenv("HTTPS_PORT"), "uds", getenv("CTDL_DIR"), NULL);
157 void test_binary_compatibility(void) {
160 fprintf(stderr, "ctdlvisor: testing compatibility...\n");
161 sprintf(cmd, "%s/usr/local/citadel/citserver -c", getenv("APPDIR"));
164 fprintf(stderr, "ctdlvisor: this appimage cannot run on your system.\n"
165 " The reason may be indicated by any error messages appearing above.\n");
168 fprintf(stderr, "ctdlvisor: this appimage appears to be compatible with your system.\n");
174 void main_loop(void) {
177 int citserver_exit_code = 0;
180 fprintf(stderr, "ctdlvisor: waiting for any child process to exit...\n");
181 who_exited = waitpid(-1, &status, 0);
182 fprintf(stderr, "ctdlvisor: pid=%d exited, status=%d, exitcode=%d\n", who_exited, status, WEXITSTATUS(status));
184 // A *deliberate* exit of citserver will cause ctdlvisor to shut the whole AppImage down.
185 // If it crashes, however, we will start it back up.
186 if (who_exited == citserver_pid) {
187 citserver_exit_code = WEXITSTATUS(status);
188 if (citserver_exit_code == 0) {
189 fprintf(stderr, "ctdlvisor: citserver exited normally - ending AppImage session\n");
191 kill(webcit_pid, SIGTERM);
192 kill(webcits_pid, SIGTERM);
194 else if ((citserver_exit_code >= 101) && (citserver_exit_code <= 109)) {
195 fprintf(stderr, "ctdlvisor: citserver exited intentionally - ending AppImage session\n");
197 kill(webcit_pid, SIGTERM);
198 kill(webcits_pid, SIGTERM);
201 citserver_pid = start_citadel();
205 // WebCit processes are restarted if they exit for any reason.
206 if ((who_exited == webcit_pid) && (!shutting_down)) webcit_pid = start_webcit();
207 if ((who_exited == webcits_pid) && (!shutting_down)) webcits_pid = start_webcits();
209 // If we somehow end up in an endless loop, at least slow it down.
212 } while (who_exited >= 0);
213 ctdlvisor_exit(citserver_exit_code);
217 int main(int argc, char **argv) {
219 if (getenv("APPDIR") == NULL) {
220 fprintf(stderr, "ctdlvisor: APPDIR is not set. This program must be run from within an AppImage.\n");
224 fprintf(stderr, "ctdlvisor: Welcome to the Citadel System, brought to you using AppImage.\n");
225 fprintf(stderr, "ctdlvisor: LD_LIBRARY_PATH = %s\n", getenv("LD_LIBRARY_PATH"));
226 fprintf(stderr, "ctdlvisor: PATH = %s\n", getenv("PATH"));
227 fprintf(stderr, "ctdlvisor: APPDIR = %s\n", getenv("APPDIR"));
228 fprintf(stderr, "ctdlvisor: data directory = %s\n", getenv("CTDL_DIR"));
229 fprintf(stderr, "ctdlvisor: HTTP port = %s\n", getenv("HTTP_PORT"));
230 fprintf(stderr, "ctdlvisor: HTTPS port = %s\n", getenv("HTTPS_PORT"));
232 if (access(getenv("CTDL_DIR"), R_OK|W_OK|X_OK)) {
233 fprintf(stderr, "ctdlvisor: %s: %s\n", getenv("CTDL_DIR"), strerror(errno));
234 ctdlvisor_exit(errno);
237 signal(SIGTERM, signal_handler);
238 signal(SIGHUP, signal_handler);
239 signal(SIGINT, signal_handler);
240 signal(SIGQUIT, signal_handler);
242 citserver_pid = start_citadel();
243 webcit_pid = start_webcit();
244 webcits_pid = start_webcits();