fixed a bug with ctdlmigrate running in appimage
[citadel.git] / appimage / ctdlvisor.c
1 // This is a supervisor program that handles start/stop/restart of
2 // the various Citadel System components, when we are running on
3 // an AppImage instance.
4 //
5 // Copyright (c) 2021 by the citadel.org team
6 //
7 // This program is open source software.  Use, duplication, or disclosure
8 // is subject to the terms of the GNU General Public License, version 3.
9 // The program is distributed without any warranty, expressed or implied.
10
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <sys/wait.h>
15 #include <errno.h>
16 #include <signal.h>
17 #include <string.h>
18 #include <limits.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21
22 pid_t citserver_pid;
23 pid_t webcit_pid;
24 pid_t webcits_pid;
25 int shutting_down = 0;
26
27 // Call this instead of exit() just for common diagnostics etc.
28 void ctdlvisor_exit(int code) {
29         printf("ctdlvisor: exit code %d\n", code);
30         exit(code);
31 }
32
33
34 // Interrupting this program with a signal will begin an orderly shutdown.
35 void signal_handler(int signal) {
36         fprintf(stderr, "ctdlvisor: caught signal %d\n", signal);
37
38         while(shutting_down) {
39                 fprintf(stderr, "ctdlvisor: already shutting down\n");
40                 sleep(1);
41         }
42
43         int status;
44         pid_t who_exited;
45         char *what_exited = NULL;
46
47         shutting_down = 1;
48         kill(citserver_pid, SIGTERM);
49         kill(webcit_pid, SIGTERM);
50         kill(webcits_pid, SIGTERM);
51         do {
52                 fprintf(stderr, "ctdlvisor: waiting for all child process to exit...\n");
53                 who_exited = waitpid(-1, &status, 0);
54                 if (who_exited == citserver_pid) {
55                         what_exited = "Citadel Server";
56                 }
57                 else if (who_exited == webcit_pid) {
58                         what_exited = "WebCit HTTP";
59                 }
60                 else if (who_exited == webcits_pid) {
61                         what_exited = "WebCit HTTPS";
62                 }
63                 else {
64                         what_exited = "unknown";
65                 }
66                 if (who_exited >= 0) {
67                         fprintf(stderr, "ctdlvisor: %d (%s) ended, status=%d\n", who_exited, what_exited, status);
68                 }
69         } while (who_exited >= 0);
70         ctdlvisor_exit(0);
71 }
72
73
74 void detach_from_tty(void) {
75         signal(SIGHUP, SIG_IGN);
76         signal(SIGINT, SIG_IGN);
77         signal(SIGQUIT, SIG_IGN);
78
79         setsid();       // become our own process group leader
80         umask(0);
81         if (    (freopen("/dev/null", "r", stdin) != stdin) ||
82                 (freopen("/dev/null", "w", stdout) != stdout) ||
83                 (freopen("/dev/null", "w", stderr) != stderr)
84         ) {
85                 fprintf(stderr, "sysdep: unable to reopen stdio: %s\n", strerror(errno));
86         }
87 }
88
89
90 pid_t start_citadel() {
91         char bin[1024];
92         sprintf(bin, "%s/usr/local/citadel/citserver", getenv("APPDIR"));
93         pid_t pid = fork();
94         if (pid == 0) {
95                 fprintf(stderr, "ctdlvisor: executing %s with data directory %s\n", bin, getenv("CTDL_DIR"));
96                 detach_from_tty();
97                 execlp(bin, "citserver", "-x9", "-h", getenv("CTDL_DIR"), NULL);
98                 exit(errno);
99         }
100         else {
101                 fprintf(stderr, "ctdlvisor: citserver running on pid=%d\n", pid);
102                 return(pid);
103         }
104 }
105
106
107 pid_t start_webcit() {
108         char bin[1024];
109         sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
110         char wchome[1024];
111         sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
112         pid_t pid = fork();
113         if (pid == 0) {
114                 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
115                 detach_from_tty();
116                 execlp(bin, "webcit", "-x9", wchome, "-p", getenv("HTTP_PORT"), "uds", getenv("CTDL_DIR"), NULL);
117                 exit(errno);
118         }
119         else {
120                 fprintf(stderr, "ctdlvisor: webcit (HTTP) running on pid=%d\n", pid);
121                 return(pid);
122         }
123 }
124
125
126 pid_t start_webcits() {
127         char bin[1024];
128         sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
129         char wchome[1024];
130         sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
131         pid_t pid = fork();
132         if (pid == 0) {
133                 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
134                 detach_from_tty();
135                 execlp(bin, "webcit", "-x9", wchome, "-s", "-p", getenv("HTTPS_PORT"), "uds", getenv("CTDL_DIR"), NULL);
136                 exit(errno);
137         }
138         else {
139                 fprintf(stderr, "ctdlvisor: webcit (HTTPS) running on pid=%d\n", pid);
140                 return(pid);
141         }
142 }
143
144
145 void main_loop(void) {
146         int status;
147         pid_t who_exited;
148         int citserver_exit_code = 0;
149
150         do {
151                 who_exited = waitpid(-1, &status, 0);
152                 fprintf(stderr, "ctdlvisor: pid=%d exited, status=%d, exitcode=%d\n", who_exited, status, WEXITSTATUS(status));
153
154                 // A *deliberate* exit of citserver will cause ctdlvisor to shut the whole AppImage down.
155                 // If it crashes, however, we will start it back up.
156                 if (who_exited == citserver_pid) {
157                         citserver_exit_code = WEXITSTATUS(status);
158                         if ((WIFEXITED(status)) && (citserver_exit_code == 0)) {
159                                 fprintf(stderr, "ctdlvisor: citserver exited normally - ending AppImage session\n");
160                                 shutting_down = 1;
161                                 kill(webcit_pid, SIGTERM);
162                                 kill(webcits_pid, SIGTERM);
163                         }
164                         else if ((WIFEXITED(status)) && (citserver_exit_code >= 101) && (citserver_exit_code <= 109)) {
165                                 fprintf(stderr, "ctdlvisor: citserver exited intentionally - ending AppImage session\n");
166                                 shutting_down = 1;
167                                 kill(webcit_pid, SIGTERM);
168                                 kill(webcits_pid, SIGTERM);
169                         }
170                         else {
171                                 if (WIFSIGNALED(status)) {
172                                         fprintf(stderr, "ctdlvisor: citserver crashed on signal %d\n", WTERMSIG(status));
173                                 }
174                                 citserver_pid = start_citadel();
175                         }
176                 }
177
178                 // WebCit processes are restarted if they exit for any reason.
179                 if ((who_exited == webcit_pid) && (!shutting_down))     webcit_pid = start_webcit();
180                 if ((who_exited == webcits_pid) && (!shutting_down))    webcits_pid = start_webcits();
181
182                 // If we somehow end up in an endless loop, at least slow it down.
183                 sleep(1);
184
185         } while (who_exited >= 0);
186         ctdlvisor_exit(citserver_exit_code);
187 }
188
189
190 int main(int argc, char **argv) {
191         int a;
192         int migrate_mode = 0;
193
194         if (getenv("APPDIR") == NULL) {
195                 fprintf(stderr, "ctdlvisor: APPDIR is not set.  This program must be run from within an AppImage.\n");
196                 ctdlvisor_exit(1);
197         }
198
199         /* parse command-line arguments */
200         while ((a=getopt(argc, argv, "cm")) != EOF) switch(a) {
201
202                 // test this binary for compatibility and exit
203                 case 'c':
204                         fprintf(stderr, "%s: binary compatibility confirmed\n", argv[0]);
205                         exit(0);
206                         break;
207
208                 // run ctdlmigrate only
209                 case 'm':
210                         migrate_mode = 1;
211                         break;
212
213                 // any other parameter makes it crash and burn
214                 default:
215                         fprintf(stderr, "usage\n");
216                         exit(1);
217         }
218
219         fprintf(stderr, "ctdlvisor: Welcome to the Citadel System, brought to you using AppImage.\n");
220         fprintf(stderr, "ctdlvisor: LD_LIBRARY_PATH = %s\n", getenv("LD_LIBRARY_PATH"));
221         fprintf(stderr, "ctdlvisor:            PATH = %s\n", getenv("PATH"));
222         fprintf(stderr, "ctdlvisor:          APPDIR = %s\n", getenv("APPDIR"));
223         fprintf(stderr, "ctdlvisor:  data directory = %s\n", getenv("CTDL_DIR"));
224         fprintf(stderr, "ctdlvisor:       HTTP port = %s\n", getenv("HTTP_PORT"));
225         fprintf(stderr, "ctdlvisor:      HTTPS port = %s\n", getenv("HTTPS_PORT"));
226
227         if (access(getenv("CTDL_DIR"), R_OK|W_OK|X_OK)) {
228                 fprintf(stderr, "ctdlvisor: %s: %s\n", getenv("CTDL_DIR"), strerror(errno));
229                 ctdlvisor_exit(errno);
230         }
231
232         signal(SIGHUP, signal_handler);
233
234         // "migrate mode" means we just start the server and then run ctdlmigrate interactively.
235         if (migrate_mode) {
236                 citserver_pid = start_citadel();
237                 fprintf(stderr, "ctdlvisor: waiting a moment for citserver to initialize...\n");
238                 sleep(5);
239                 char bin[1024];
240                 sprintf(bin, "%s/usr/local/citadel/ctdlmigrate -h %s", getenv("APPDIR"), getenv("CTDL_DIR"));
241                 system(bin);
242                 kill(citserver_pid, SIGTERM);
243         }
244
245         // Otherwise, it's just a normal happy day in Citadel land.
246         else {
247                 signal(SIGTERM, signal_handler);
248                 signal(SIGINT, signal_handler);
249                 signal(SIGQUIT, signal_handler);
250         
251                 citserver_pid = start_citadel();
252                 webcit_pid = start_webcit();
253                 webcits_pid = start_webcits();
254         
255                 main_loop();
256         }
257
258         ctdlvisor_exit(0);
259 }