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 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);
84 } 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);
116 fprintf(stderr, "ctdlvisor: citserver running on pid=%d\n", pid);
122 pid_t start_webcit() {
124 sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
126 sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
129 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
131 execlp(bin, "webcit", "-x9", wchome, "-p", getenv("HTTP_PORT"), "uds", getenv("CTDL_DIR"), NULL);
135 fprintf(stderr, "ctdlvisor: webcit (HTTP) running on pid=%d\n", pid);
141 pid_t start_webcits() {
143 sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
145 sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
148 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
150 execlp(bin, "webcit", "-x9", wchome, "-s", "-p", getenv("HTTPS_PORT"), "uds", getenv("CTDL_DIR"), NULL);
154 fprintf(stderr, "ctdlvisor: webcit (HTTPS) running on pid=%d\n", pid);
160 void test_binary_compatibility(void) {
163 fprintf(stderr, "ctdlvisor: testing compatibility...\n");
164 sprintf(cmd, "%s/usr/local/citadel/citserver -c", getenv("APPDIR"));
167 fprintf(stderr, "ctdlvisor: this appimage cannot run on your system.\n"
168 " The reason may be indicated by any error messages appearing above.\n");
171 fprintf(stderr, "ctdlvisor: this appimage appears to be compatible with your system.\n");
177 void main_loop(void) {
180 int citserver_exit_code = 0;
183 who_exited = waitpid(-1, &status, 0);
184 fprintf(stderr, "ctdlvisor: pid=%d exited, status=%d, exitcode=%d\n", who_exited, status, WEXITSTATUS(status));
186 // A *deliberate* exit of citserver will cause ctdlvisor to shut the whole AppImage down.
187 // If it crashes, however, we will start it back up.
188 if (who_exited == citserver_pid) {
189 citserver_exit_code = WEXITSTATUS(status);
190 if (citserver_exit_code == 0) {
191 fprintf(stderr, "ctdlvisor: citserver exited normally - ending AppImage session\n");
193 kill(webcit_pid, SIGTERM);
194 kill(webcits_pid, SIGTERM);
196 else if ((citserver_exit_code >= 101) && (citserver_exit_code <= 109)) {
197 fprintf(stderr, "ctdlvisor: citserver exited intentionally - ending AppImage session\n");
199 kill(webcit_pid, SIGTERM);
200 kill(webcits_pid, SIGTERM);
203 citserver_pid = start_citadel();
207 // WebCit processes are restarted if they exit for any reason.
208 if ((who_exited == webcit_pid) && (!shutting_down)) webcit_pid = start_webcit();
209 if ((who_exited == webcits_pid) && (!shutting_down)) webcits_pid = start_webcits();
211 // If we somehow end up in an endless loop, at least slow it down.
214 } while (who_exited >= 0);
215 ctdlvisor_exit(citserver_exit_code);
219 int main(int argc, char **argv) {
221 if (getenv("APPDIR") == NULL) {
222 fprintf(stderr, "ctdlvisor: APPDIR is not set. This program must be run from within an AppImage.\n");
226 fprintf(stderr, "ctdlvisor: Welcome to the Citadel System, brought to you using AppImage.\n");
227 fprintf(stderr, "ctdlvisor: LD_LIBRARY_PATH = %s\n", getenv("LD_LIBRARY_PATH"));
228 fprintf(stderr, "ctdlvisor: PATH = %s\n", getenv("PATH"));
229 fprintf(stderr, "ctdlvisor: APPDIR = %s\n", getenv("APPDIR"));
230 fprintf(stderr, "ctdlvisor: data directory = %s\n", getenv("CTDL_DIR"));
231 fprintf(stderr, "ctdlvisor: HTTP port = %s\n", getenv("HTTP_PORT"));
232 fprintf(stderr, "ctdlvisor: HTTPS port = %s\n", getenv("HTTPS_PORT"));
234 if (access(getenv("CTDL_DIR"), R_OK|W_OK|X_OK)) {
235 fprintf(stderr, "ctdlvisor: %s: %s\n", getenv("CTDL_DIR"), strerror(errno));
236 ctdlvisor_exit(errno);
239 signal(SIGTERM, signal_handler);
240 signal(SIGHUP, signal_handler);
241 signal(SIGINT, signal_handler);
242 signal(SIGQUIT, signal_handler);
244 citserver_pid = start_citadel();
245 webcit_pid = start_webcit();
246 webcits_pid = start_webcits();