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.
29 void run_in_foreground(void);
31 char *data_directory = "/usr/local/citadel";
32 char *http_port = "80";
33 char *https_port = "443";
39 void signal_handler(int signal) {
40 fprintf(stderr, "ctdlvisor: caught signal %d\n", signal);
44 char *what_exited = NULL;
47 fprintf(stderr, "ctdlvisor: waiting for any child process to exit...\n");
48 who_exited = waitpid(-1, &status, 0);
49 if (who_exited == citserver_pid) {
50 what_exited = "Citadel Server";
52 else if (who_exited == webcit_pid) {
53 what_exited = "WebCit HTTP";
55 else if (who_exited == webcits_pid) {
56 what_exited = "WebCit HTTPS";
59 what_exited = "unknown";
61 fprintf(stderr, "ctdlvisor: pid=%d (%s) exited, status=%d, exitcode=%d\n", who_exited, what_exited, status, WEXITSTATUS(status));
62 } while (who_exited >= 0);
64 printf("ctdlvisor: exiting from signal catcher.\n");
70 pid_t start_citadel() {
72 sprintf(bin, "%s/usr/local/citadel/citserver", getenv("APPDIR"));
75 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
76 close(0); close(1); close(2);
77 execlp(bin, "citserver", "-x9", "-h", data_directory, NULL);
86 pid_t start_webcit() {
88 sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
90 sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
93 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
94 close(0); close(1); close(2);
95 execlp(bin, "webcit", "-x9", wchome, "-p", http_port, "uds", data_directory, NULL);
104 pid_t start_webcits() {
106 sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
108 sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
111 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
112 close(0); close(1); close(2);
113 execlp(bin, "webcit", "-x9", wchome, "-s", "-p", https_port, "uds", data_directory, NULL);
123 "ctdlvisor: usage: ctdlvisor [-h data_directory] [-p http_port] [-s https_port] command\n"
124 " 'command' must be one of: run, install, start, stop\n"
127 int main(int argc, char **argv) {
130 while ((c = getopt (argc, argv, "h:p:s:")) != -1) switch(c) {
132 data_directory = optarg;
141 fprintf(stderr, "%s", usage);
146 if (argc != optind+1) {
147 fprintf(stderr, "%s", usage);
151 if (!strcasecmp(argv[optind], "run")) {
155 fprintf(stderr, "%s", usage);
163 void run_in_foreground(void) {
164 fprintf(stderr, "ctdlvisor: Welcome to the Citadel System, brought to you using AppImage.\n");
165 fprintf(stderr, "ctdlvisor: LD_LIBRARY_PATH = %s\n", getenv("LD_LIBRARY_PATH"));
166 fprintf(stderr, "ctdlvisor: PATH = %s\n", getenv("PATH"));
167 fprintf(stderr, "ctdlvisor: APPDIR = %s\n", getenv("APPDIR"));
168 fprintf(stderr, "ctdlvisor: data directory = %s\n", data_directory);
169 fprintf(stderr, "ctdlvisor: HTTP port = %s\n", http_port);
170 fprintf(stderr, "ctdlvisor: HTTPS port = %s\n", https_port);
172 if (access(data_directory, R_OK|W_OK|X_OK)) {
173 fprintf(stderr, "ctdlvisor: %s: %s\n", data_directory, strerror(errno));
177 signal(SIGTERM, signal_handler);
178 signal(SIGHUP, signal_handler);
179 signal(SIGINT, signal_handler);
180 signal(SIGQUIT, signal_handler);
182 citserver_pid = start_citadel();
183 webcit_pid = start_webcit();
184 webcits_pid = start_webcits();
190 void main_loop(void) {
193 int shutting_down = 0;
194 int citserver_exit_code = 0;
197 fprintf(stderr, "ctdlvisor: waiting for any child process to exit...\n");
198 who_exited = waitpid(-1, &status, 0);
199 fprintf(stderr, "ctdlvisor: pid=%d exited, status=%d, exitcode=%d\n", who_exited, status, WEXITSTATUS(status));
201 // A *deliberate* exit of citserver will cause ctdlvisor to shut the whole AppImage down.
202 // If it crashes, however, we will start it back up.
203 if (who_exited == citserver_pid) {
204 citserver_exit_code = WEXITSTATUS(status);
205 if (citserver_exit_code == 0) {
206 fprintf(stderr, "ctdlvisor: citserver exited normally - ending AppImage session\n");
208 kill(webcit_pid, SIGTERM);
209 kill(webcits_pid, SIGTERM);
211 else if ((citserver_exit_code >= 101) && (citserver_exit_code <= 109)) {
212 fprintf(stderr, "ctdlvisor: citserver exited intentionally - ending AppImage session\n");
214 kill(webcit_pid, SIGTERM);
215 kill(webcits_pid, SIGTERM);
218 citserver_pid = start_citadel();
222 // WebCit processes are restarted if they exit for any reason.
223 if ((who_exited == webcit_pid) && (!shutting_down)) webcit_pid = start_webcit();
224 if ((who_exited == webcits_pid) && (!shutting_down)) webcits_pid = start_webcits();
226 // If we somehow end up in an endless loop, at least slow it down.
229 } while (who_exited >= 0);
231 printf("ctdlvisor: exit code %d\n", citserver_exit_code);
232 exit(citserver_exit_code);