More work on the appimage
[citadel.git] / appimage / ctdlvisor.c
1 //
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.
5 //
6 // Copyright (c) 2021 by the citadel.org team
7 //
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.
12 //
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.
17
18
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <sys/wait.h>
23 #include <errno.h>
24 #include <signal.h>
25 #include <string.h>
26 #include <limits.h>
27
28 void main_loop(void);
29
30 char *data_directory = "/usr/local/citadel";
31 char *http_port = "80";
32 char *https_port = "443";
33 pid_t citserver_pid;
34 pid_t webcit_pid;
35 pid_t webcits_pid;
36
37
38 void signal_handler(int signal) {
39         fprintf(stderr, "ctdlvisor: caught signal %d", signal);
40
41         int status;
42         pid_t who_exited;
43         char *what_exited = NULL;
44
45         do {
46                 fprintf(stderr, "ctdlvisor: waiting for any child process to exit...\n");
47                 who_exited = waitpid(-1, &status, 0);
48                 if (who_exited == citserver_pid) {
49                         what_exited = "Citadel Server";
50                 }
51                 else if (who_exited == webcit_pid) {
52                         what_exited = "WebCit HTTP";
53                 }
54                 else if (who_exited == webcits_pid) {
55                         what_exited = "WebCit HTTPS";
56                 }
57                 else {
58                         what_exited = "unknown";
59                 }
60                 fprintf(stderr, "ctdlvisor: pid=%d (%s) exited, status=%d, exitcode=%d\n", who_exited, what_exited, status, WEXITSTATUS(status));
61         } while (who_exited >= 0);
62
63         printf("ctdlvisor: exiting from signal catcher.\n");
64         exit(0);
65 }
66
67
68
69 pid_t start_citadel() {
70         char bin[1024];
71         sprintf(bin, "%s/usr/local/citadel/citserver", getenv("APPDIR"));
72         pid_t pid = fork();
73         if (pid == 0) {
74                 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
75                 close(0); close(1); close(2);
76                 execlp(bin, "citserver", "-x9", "-h", data_directory, NULL);
77                 exit(errno);
78         }
79         else {
80                 return(pid);
81         }
82 }
83
84
85 pid_t start_webcit() {
86         char bin[1024];
87         sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
88         char wchome[1024];
89         sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
90         pid_t pid = fork();
91         if (pid == 0) {
92                 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
93                 close(0); close(1); close(2);
94                 execlp(bin, "webcit", "-x9", wchome, "-p", http_port, "uds", data_directory, NULL);
95                 exit(errno);
96         }
97         else {
98                 return(pid);
99         }
100 }
101
102
103 pid_t start_webcits() {
104         char bin[1024];
105         sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
106         char wchome[1024];
107         sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
108         pid_t pid = fork();
109         if (pid == 0) {
110                 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
111                 close(0); close(1); close(2);
112                 execlp(bin, "webcit", "-x9", wchome, "-s", "-p", https_port, "uds", data_directory, NULL);
113                 exit(errno);
114         }
115         else {
116                 return(pid);
117         }
118 }
119
120
121 int main(int argc, char **argv) {
122         int c;
123
124         while ((c = getopt (argc, argv, "h:p:s:")) != -1)  switch(c) {
125                 case 'h':
126                         data_directory = optarg;
127                         break;
128                 case 'p':
129                         http_port = optarg;
130                         break;
131                 case 's':
132                         https_port = optarg;
133                         break;
134                 default:
135                         fprintf(stderr, "ctdlvisor: usage: ctdlvisor [-h data_directory] [-p http_port] [-s https_port]\n");
136                         exit(1);
137         }
138
139         fprintf(stderr, "ctdlvisor: Welcome to the Citadel System, brought to you using AppImage.\n");
140         fprintf(stderr, "ctdlvisor: LD_LIBRARY_PATH = %s\n", getenv("LD_LIBRARY_PATH"));
141         fprintf(stderr, "ctdlvisor:            PATH = %s\n", getenv("PATH"));
142         fprintf(stderr, "ctdlvisor:          APPDIR = %s\n", getenv("APPDIR"));
143         fprintf(stderr, "ctdlvisor:  data directory = %s\n", data_directory);
144         fprintf(stderr, "ctdlvisor:       HTTP port = %s\n", http_port);
145         fprintf(stderr, "ctdlvisor:      HTTPS port = %s\n", https_port);
146
147         if (access(data_directory, R_OK|W_OK|X_OK)) {
148                 fprintf(stderr, "ctdlvisor: %s: %s\n", data_directory, strerror(errno));
149                 exit(errno);
150         }
151
152         signal(SIGTERM, signal_handler);
153         signal(SIGHUP, signal_handler);
154         signal(SIGINT, signal_handler);
155         signal(SIGQUIT, signal_handler);
156
157         citserver_pid = start_citadel();
158         webcit_pid = start_webcit();
159         webcits_pid = start_webcits();
160
161         main_loop();
162 }
163
164
165 void main_loop(void) {
166         int status;
167         pid_t who_exited;
168         int shutting_down = 0;
169         int citserver_exit_code = 0;
170
171         do {
172                 fprintf(stderr, "ctdlvisor: waiting for any child process to exit...\n");
173                 who_exited = waitpid(-1, &status, 0);
174                 fprintf(stderr, "ctdlvisor: pid=%d exited, status=%d, exitcode=%d\n", who_exited, status, WEXITSTATUS(status));
175
176                 // A *deliberate* exit of citserver will cause ctdlvisor to shut the whole AppImage down.
177                 // If it crashes, however, we will start it back up.
178                 if (who_exited == citserver_pid) {
179                         citserver_exit_code = WEXITSTATUS(status);
180                         if (citserver_exit_code == 0) {
181                                 fprintf(stderr, "ctdlvisor: citserver exited normally - ending AppImage session\n");
182                                 shutting_down = 1;
183                                 kill(webcit_pid, SIGTERM);
184                                 kill(webcits_pid, SIGTERM);
185                         }
186                         else if ((citserver_exit_code >= 101) && (citserver_exit_code <= 109)) {
187                                 fprintf(stderr, "ctdlvisor: citserver exited intentionally - ending AppImage session\n");
188                                 shutting_down = 1;
189                                 kill(webcit_pid, SIGTERM);
190                                 kill(webcits_pid, SIGTERM);
191                         }
192                         else {
193                                 citserver_pid = start_citadel();
194                         }
195                 }
196
197                 // WebCit processes are restarted if they exit for any reason.
198                 if ((who_exited == webcit_pid) && (!shutting_down))     webcit_pid = start_webcit();
199                 if ((who_exited == webcits_pid) && (!shutting_down))    webcits_pid = start_webcits();
200
201                 // If we somehow end up in an endless loop, at least slow it down.
202                 sleep(1);
203
204         } while (who_exited >= 0);
205
206         printf("ctdlvisor: exit code %d\n", citserver_exit_code);
207         exit(citserver_exit_code);
208 }