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";
37 int shutting_down = 0;
40 void ctdlvisor_exit(int code) {
41 printf("ctdlvisor: exit code %d\n", code);
46 void signal_handler(int signal) {
47 fprintf(stderr, "ctdlvisor: caught signal %d\n", signal);
49 while(shutting_down) {
50 fprintf(stderr, "ctdlvisor: already shutting down\n");
56 char *what_exited = NULL;
59 kill(citserver_pid, SIGTERM);
60 kill(webcit_pid, SIGTERM);
61 kill(webcits_pid, SIGTERM);
63 fprintf(stderr, "ctdlvisor: waiting for all child process to exit...\n");
64 who_exited = waitpid(-1, &status, 0);
65 if (who_exited == citserver_pid) {
66 what_exited = "Citadel Server";
68 else if (who_exited == webcit_pid) {
69 what_exited = "WebCit HTTP";
71 else if (who_exited == webcits_pid) {
72 what_exited = "WebCit HTTPS";
75 what_exited = "unknown";
77 fprintf(stderr, "ctdlvisor: pid=%d (%s) exited, status=%d, exitcode=%d\n", who_exited, what_exited, status, WEXITSTATUS(status));
78 } while (who_exited >= 0);
85 void detach_from_tty(void) {
86 signal(SIGHUP, SIG_IGN);
87 signal(SIGINT, SIG_IGN);
88 signal(SIGQUIT, SIG_IGN);
92 if ( (freopen("/dev/null", "r", stdin) != stdin) ||
93 (freopen("/dev/null", "w", stdout) != stdout) ||
94 (freopen("/dev/null", "w", stderr) != stderr)
96 fprintf(stderr, "sysdep: unable to reopen stdio: %s\n", strerror(errno));
101 pid_t start_citadel() {
103 sprintf(bin, "%s/usr/local/citadel/citserver", getenv("APPDIR"));
106 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
108 execlp(bin, "citserver", "-x9", "-h", data_directory, NULL);
117 pid_t start_webcit() {
119 sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
121 sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
124 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
126 execlp(bin, "webcit", "-x9", wchome, "-p", http_port, "uds", data_directory, NULL);
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", https_port, "uds", data_directory, NULL);
154 "ctdlvisor: usage: ctdlvisor [-h data_directory] [-p http_port] [-s https_port] command\n"
155 " 'command' must be one of: run, install, start, stop\n"
158 int main(int argc, char **argv) {
161 while ((c = getopt (argc, argv, "h:p:s:")) != -1) switch(c) {
163 data_directory = optarg;
172 fprintf(stderr, "%s", usage);
177 if (argc != optind+1) {
178 fprintf(stderr, "%s", usage);
182 if (!strcasecmp(argv[optind], "run")) {
186 fprintf(stderr, "%s", usage);
194 void run_in_foreground(void) {
195 fprintf(stderr, "ctdlvisor: Welcome to the Citadel System, brought to you using AppImage.\n");
196 fprintf(stderr, "ctdlvisor: LD_LIBRARY_PATH = %s\n", getenv("LD_LIBRARY_PATH"));
197 fprintf(stderr, "ctdlvisor: PATH = %s\n", getenv("PATH"));
198 fprintf(stderr, "ctdlvisor: APPDIR = %s\n", getenv("APPDIR"));
199 fprintf(stderr, "ctdlvisor: data directory = %s\n", data_directory);
200 fprintf(stderr, "ctdlvisor: HTTP port = %s\n", http_port);
201 fprintf(stderr, "ctdlvisor: HTTPS port = %s\n", https_port);
203 if (access(data_directory, R_OK|W_OK|X_OK)) {
204 fprintf(stderr, "ctdlvisor: %s: %s\n", data_directory, strerror(errno));
208 signal(SIGTERM, signal_handler);
209 signal(SIGHUP, signal_handler);
210 signal(SIGINT, signal_handler);
211 signal(SIGQUIT, signal_handler);
213 citserver_pid = start_citadel();
214 webcit_pid = start_webcit();
215 webcits_pid = start_webcits();
222 void main_loop(void) {
225 int citserver_exit_code = 0;
228 fprintf(stderr, "ctdlvisor: waiting for any child process to exit...\n");
229 who_exited = waitpid(-1, &status, 0);
230 fprintf(stderr, "ctdlvisor: pid=%d exited, status=%d, exitcode=%d\n", who_exited, status, WEXITSTATUS(status));
232 // A *deliberate* exit of citserver will cause ctdlvisor to shut the whole AppImage down.
233 // If it crashes, however, we will start it back up.
234 if (who_exited == citserver_pid) {
235 citserver_exit_code = WEXITSTATUS(status);
236 if (citserver_exit_code == 0) {
237 fprintf(stderr, "ctdlvisor: citserver exited normally - ending AppImage session\n");
239 kill(webcit_pid, SIGTERM);
240 kill(webcits_pid, SIGTERM);
242 else if ((citserver_exit_code >= 101) && (citserver_exit_code <= 109)) {
243 fprintf(stderr, "ctdlvisor: citserver exited intentionally - ending AppImage session\n");
245 kill(webcit_pid, SIGTERM);
246 kill(webcits_pid, SIGTERM);
249 citserver_pid = start_citadel();
253 // WebCit processes are restarted if they exit for any reason.
254 if ((who_exited == webcit_pid) && (!shutting_down)) webcit_pid = start_webcit();
255 if ((who_exited == webcits_pid) && (!shutting_down)) webcits_pid = start_webcits();
257 // If we somehow end up in an endless loop, at least slow it down.
260 } while (who_exited >= 0);
262 ctdlvisor_exit(citserver_exit_code);