troubleshooting 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 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
38
39 void signal_handler(int signal) {
40         fprintf(stderr, "ctdlvisor: caught signal %d\n", signal);
41
42         int status;
43         pid_t who_exited;
44         char *what_exited = NULL;
45
46         do {
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";
51                 }
52                 else if (who_exited == webcit_pid) {
53                         what_exited = "WebCit HTTP";
54                 }
55                 else if (who_exited == webcits_pid) {
56                         what_exited = "WebCit HTTPS";
57                 }
58                 else {
59                         what_exited = "unknown";
60                 }
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);
63
64         printf("ctdlvisor: exiting from signal catcher.\n");
65         exit(0);
66 }
67
68
69
70 pid_t start_citadel() {
71         char bin[1024];
72         sprintf(bin, "%s/usr/local/citadel/citserver", getenv("APPDIR"));
73         pid_t pid = fork();
74         if (pid == 0) {
75                 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
76                 close(0); close(1); close(2);
77                 execlp(bin, "citserver", "-x9", "-h", data_directory, NULL);
78                 exit(errno);
79         }
80         else {
81                 return(pid);
82         }
83 }
84
85
86 pid_t start_webcit() {
87         char bin[1024];
88         sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
89         char wchome[1024];
90         sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
91         pid_t pid = fork();
92         if (pid == 0) {
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);
96                 exit(errno);
97         }
98         else {
99                 return(pid);
100         }
101 }
102
103
104 pid_t start_webcits() {
105         char bin[1024];
106         sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
107         char wchome[1024];
108         sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
109         pid_t pid = fork();
110         if (pid == 0) {
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);
114                 exit(errno);
115         }
116         else {
117                 return(pid);
118         }
119 }
120
121
122 static char *usage =
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"
125 ;
126
127 int main(int argc, char **argv) {
128         int c;
129
130         while ((c = getopt (argc, argv, "h:p:s:")) != -1)  switch(c) {
131                 case 'h':
132                         data_directory = optarg;
133                         break;
134                 case 'p':
135                         http_port = optarg;
136                         break;
137                 case 's':
138                         https_port = optarg;
139                         break;
140                 default:
141                         fprintf(stderr, "%s", usage);
142                         exit(1);
143         }
144
145
146         if (argc != optind+1) {
147                 fprintf(stderr, "%s", usage);
148                 exit(1);
149         }
150
151         if (!strcasecmp(argv[optind], "run")) {
152                 run_in_foreground();
153         }
154         else {
155                 fprintf(stderr, "%s", usage);
156                 exit(1);
157         }
158
159         exit(0);
160 }
161
162
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);
171
172         if (access(data_directory, R_OK|W_OK|X_OK)) {
173                 fprintf(stderr, "ctdlvisor: %s: %s\n", data_directory, strerror(errno));
174                 exit(errno);
175         }
176
177         signal(SIGTERM, signal_handler);
178         signal(SIGHUP, signal_handler);
179         signal(SIGINT, signal_handler);
180         signal(SIGQUIT, signal_handler);
181
182         citserver_pid = start_citadel();
183         webcit_pid = start_webcit();
184         webcits_pid = start_webcits();
185
186         main_loop();
187 }
188
189
190 void main_loop(void) {
191         int status;
192         pid_t who_exited;
193         int shutting_down = 0;
194         int citserver_exit_code = 0;
195
196         do {
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));
200
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");
207                                 shutting_down = 1;
208                                 kill(webcit_pid, SIGTERM);
209                                 kill(webcits_pid, SIGTERM);
210                         }
211                         else if ((citserver_exit_code >= 101) && (citserver_exit_code <= 109)) {
212                                 fprintf(stderr, "ctdlvisor: citserver exited intentionally - ending AppImage session\n");
213                                 shutting_down = 1;
214                                 kill(webcit_pid, SIGTERM);
215                                 kill(webcits_pid, SIGTERM);
216                         }
217                         else {
218                                 citserver_pid = start_citadel();
219                         }
220                 }
221
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();
225
226                 // If we somehow end up in an endless loop, at least slow it down.
227                 sleep(1);
228
229         } while (who_exited >= 0);
230
231         printf("ctdlvisor: exit code %d\n", citserver_exit_code);
232         exit(citserver_exit_code);
233 }