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.
28 char *data_directory = "/usr/local/citadel";
29 char *http_port = "80";
30 char *https_port = "443";
32 pid_t start_citadel() {
34 sprintf(bin, "%s/usr/local/citadel/citserver", getenv("APPDIR"));
37 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
38 execlp(bin, "citserver", "-x9", "-h", data_directory, NULL);
48 pid_t start_webcit() {
50 sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
52 sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
55 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
56 execlp(bin, "webcit", "-x9", wchome, "-p", http_port, "uds", data_directory, NULL);
66 pid_t start_webcits() {
68 sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
70 sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
73 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
74 execlp(bin, "webcit", "-x9", wchome, "-s", "-p", https_port, "uds", data_directory, NULL);
84 int main(int argc, char **argv) {
88 int shutting_down = 0;
93 while ((c = getopt (argc, argv, "h:p:s:")) != -1) switch(c) {
95 data_directory = optarg;
104 fprintf(stderr, "ctdlvisor: usage: ctdlvisor [-h data_directory] [-p http_port] [-s https_port]\n");
108 fprintf(stderr, "ctdlvisor: Welcome to the Citadel System, brought to you using AppImage.\n");
109 fprintf(stderr, "ctdlvisor: LD_LIBRARY_PATH = %s\n", getenv("LD_LIBRARY_PATH"));
110 fprintf(stderr, "ctdlvisor: PATH = %s\n", getenv("PATH"));
111 fprintf(stderr, "ctdlvisor: APPDIR = %s\n", getenv("APPDIR"));
112 fprintf(stderr, "ctdlvisor: data directory = %s\n", data_directory);
113 fprintf(stderr, "ctdlvisor: HTTP port = %s\n", http_port);
114 fprintf(stderr, "ctdlvisor: HTTPS port = %s\n", https_port);
116 if (access(data_directory, R_OK|W_OK|X_OK)) {
117 fprintf(stderr, "ctdlvisor: %s: %s\n", data_directory, strerror(errno));
121 citserver_pid = start_citadel();
122 webcit_pid = start_webcit();
123 webcits_pid = start_webcits();
126 fprintf(stderr, "ctdlvisor: waiting for any child process to exit...\n");
127 who_exited = waitpid(-1, &status, 0);
128 fprintf(stderr, "ctdlvisor: pid=%d exited, status=%d, exitcode=%d\n", who_exited, status, WEXITSTATUS(status));
130 // A *deliberate* exit of citserver will cause ctdlvisor to shut the whole AppImage down.
131 // If it crashes, however, we will start it back up.
132 if (who_exited == citserver_pid) {
133 if (WEXITSTATUS(status) == 0) {
134 fprintf(stderr, "ctdlvisor: citserver exited normally - ending AppImage session\n");
136 kill(webcit_pid, SIGTERM);
137 kill(webcits_pid, SIGTERM);
140 citserver_pid = start_citadel();
144 // WebCit processes are restarted if they exit for any reason.
145 if ((who_exited == webcit_pid) && (!shutting_down)) webcit_pid = start_webcit();
146 if ((who_exited == webcits_pid) && (!shutting_down)) webcits_pid = start_webcits();
148 // If we somehow end up in an endless loop, at least slow it down.
151 } while ((who_exited >= 0) && (shutting_down == 0));
153 printf("ctdlvisor: exiting.\n");