ctdlvisor is now more sensible about how it stops its child processes.
[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 void run_in_foreground(void);
30
31 char *data_directory = "/usr/local/citadel";
32 char *http_port = "80";
33 char *https_port = "443";
34 pid_t citserver_pid;
35 pid_t webcit_pid;
36 pid_t webcits_pid;
37 int shutting_down = 0;
38
39
40 void ctdlvisor_exit(int code) {
41         printf("ctdlvisor: exit code %d\n", code);
42         exit(code);
43 }
44
45
46 void signal_handler(int signal) {
47         fprintf(stderr, "ctdlvisor: caught signal %d\n", signal);
48
49         while(shutting_down) {
50                 fprintf(stderr, "ctdlvisor: already shutting down\n");
51                 sleep(1);
52         }
53
54         int status;
55         pid_t who_exited;
56         char *what_exited = NULL;
57
58         shutting_down = 1;
59         kill(citserver_pid, SIGTERM);
60         kill(webcit_pid, SIGTERM);
61         kill(webcits_pid, SIGTERM);
62         do {
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";
67                 }
68                 else if (who_exited == webcit_pid) {
69                         what_exited = "WebCit HTTP";
70                 }
71                 else if (who_exited == webcits_pid) {
72                         what_exited = "WebCit HTTPS";
73                 }
74                 else {
75                         what_exited = "unknown";
76                 }
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);
79
80         ctdlvisor_exit(0);
81 }
82
83
84
85 void detach_from_tty(void) {
86         signal(SIGHUP, SIG_IGN);
87         signal(SIGINT, SIG_IGN);
88         signal(SIGQUIT, SIG_IGN);
89
90         setsid();
91         umask(0);
92         if (    (freopen("/dev/null", "r", stdin) != stdin) ||
93                 (freopen("/dev/null", "w", stdout) != stdout) ||
94                 (freopen("/dev/null", "w", stderr) != stderr)
95         ) {
96                 fprintf(stderr, "sysdep: unable to reopen stdio: %s\n", strerror(errno));
97         }
98 }
99
100
101 pid_t start_citadel() {
102         char bin[1024];
103         sprintf(bin, "%s/usr/local/citadel/citserver", getenv("APPDIR"));
104         pid_t pid = fork();
105         if (pid == 0) {
106                 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
107                 detach_from_tty();
108                 execlp(bin, "citserver", "-x9", "-h", data_directory, NULL);
109                 exit(errno);
110         }
111         else {
112                 return(pid);
113         }
114 }
115
116
117 pid_t start_webcit() {
118         char bin[1024];
119         sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
120         char wchome[1024];
121         sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
122         pid_t pid = fork();
123         if (pid == 0) {
124                 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
125                 detach_from_tty();
126                 execlp(bin, "webcit", "-x9", wchome, "-p", http_port, "uds", data_directory, NULL);
127                 exit(errno);
128         }
129         else {
130                 return(pid);
131         }
132 }
133
134
135 pid_t start_webcits() {
136         char bin[1024];
137         sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
138         char wchome[1024];
139         sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
140         pid_t pid = fork();
141         if (pid == 0) {
142                 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
143                 detach_from_tty();
144                 execlp(bin, "webcit", "-x9", wchome, "-s", "-p", https_port, "uds", data_directory, NULL);
145                 exit(errno);
146         }
147         else {
148                 return(pid);
149         }
150 }
151
152
153 static char *usage =
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"
156 ;
157
158 int main(int argc, char **argv) {
159         int c;
160
161         while ((c = getopt (argc, argv, "h:p:s:")) != -1)  switch(c) {
162                 case 'h':
163                         data_directory = optarg;
164                         break;
165                 case 'p':
166                         http_port = optarg;
167                         break;
168                 case 's':
169                         https_port = optarg;
170                         break;
171                 default:
172                         fprintf(stderr, "%s", usage);
173                         exit(1);
174         }
175
176
177         if (argc != optind+1) {
178                 fprintf(stderr, "%s", usage);
179                 exit(1);
180         }
181
182         if (!strcasecmp(argv[optind], "run")) {
183                 run_in_foreground();
184         }
185         else {
186                 fprintf(stderr, "%s", usage);
187                 exit(1);
188         }
189
190         exit(0);
191 }
192
193
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);
202
203         if (access(data_directory, R_OK|W_OK|X_OK)) {
204                 fprintf(stderr, "ctdlvisor: %s: %s\n", data_directory, strerror(errno));
205                 exit(errno);
206         }
207
208         signal(SIGTERM, signal_handler);
209         signal(SIGHUP, signal_handler);
210         signal(SIGINT, signal_handler);
211         signal(SIGQUIT, signal_handler);
212
213         citserver_pid = start_citadel();
214         webcit_pid = start_webcit();
215         webcits_pid = start_webcits();
216
217         main_loop();
218         exit(0);
219 }
220
221
222 void main_loop(void) {
223         int status;
224         pid_t who_exited;
225         int citserver_exit_code = 0;
226
227         do {
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));
231
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");
238                                 shutting_down = 1;
239                                 kill(webcit_pid, SIGTERM);
240                                 kill(webcits_pid, SIGTERM);
241                         }
242                         else if ((citserver_exit_code >= 101) && (citserver_exit_code <= 109)) {
243                                 fprintf(stderr, "ctdlvisor: citserver exited intentionally - ending AppImage session\n");
244                                 shutting_down = 1;
245                                 kill(webcit_pid, SIGTERM);
246                                 kill(webcits_pid, SIGTERM);
247                         }
248                         else {
249                                 citserver_pid = start_citadel();
250                         }
251                 }
252
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();
256
257                 // If we somehow end up in an endless loop, at least slow it down.
258                 sleep(1);
259
260         } while (who_exited >= 0);
261
262         ctdlvisor_exit(citserver_exit_code);
263 }