This is it, I figured out how to create a client link
[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 void install_client_link(void) {
191
192         FILE *fp;
193         char path_to_link[PATH_MAX];
194         snprintf(path_to_link, sizeof path_to_link, "%s/citadel", getenv("CTDL_DIR"));
195         fp = fopen(path_to_link, "w");
196         if (!fp) {
197                 fprintf(stderr, "%s\n", strerror(errno));
198                 return;
199         }
200
201         fprintf(fp,     "#!/bin/bash\n"
202                         "export APPDIR=%s\n"
203                         "export LD_LIBRARY_PATH=${APPDIR}/usr/bin:$LD_LIBRARY_PATH\n"
204                         "export PATH=${APPDIR}/usr/bin:$PATH\n"
205                         "exec citadel\n"
206         ,
207                         getenv("APPDIR")
208         );
209
210         fchmod(fileno(fp), 0755);
211         fclose(fp);
212 }
213
214
215 int main(int argc, char **argv) {
216         int a;
217         int migrate_mode = 0;
218
219         if (getenv("APPDIR") == NULL) {
220                 fprintf(stderr, "ctdlvisor: APPDIR is not set.  This program must be run from within an AppImage.\n");
221                 ctdlvisor_exit(1);
222         }
223
224         /* parse command-line arguments */
225         while ((a=getopt(argc, argv, "cm")) != EOF) switch(a) {
226
227                 // test this binary for compatibility and exit
228                 case 'c':
229                         fprintf(stderr, "%s: binary compatibility confirmed\n", argv[0]);
230                         exit(0);
231                         break;
232
233                 // run ctdlmigrate only
234                 case 'm':
235                         migrate_mode = 1;
236                         break;
237
238                 // any other parameter makes it crash and burn
239                 default:
240                         fprintf(stderr, "usage\n");
241                         exit(1);
242         }
243
244         fprintf(stderr, "ctdlvisor: Welcome to the Citadel System, brought to you using AppImage.\n");
245         fprintf(stderr, "ctdlvisor: LD_LIBRARY_PATH = %s\n", getenv("LD_LIBRARY_PATH"));
246         fprintf(stderr, "ctdlvisor:            PATH = %s\n", getenv("PATH"));
247         fprintf(stderr, "ctdlvisor:          APPDIR = %s\n", getenv("APPDIR"));
248         fprintf(stderr, "ctdlvisor:  data directory = %s\n", getenv("CTDL_DIR"));
249         fprintf(stderr, "ctdlvisor:       HTTP port = %s\n", getenv("HTTP_PORT"));
250         fprintf(stderr, "ctdlvisor:      HTTPS port = %s\n", getenv("HTTPS_PORT"));
251
252         if (access(getenv("CTDL_DIR"), R_OK|W_OK|X_OK)) {
253                 fprintf(stderr, "ctdlvisor: %s: %s\n", getenv("CTDL_DIR"), strerror(errno));
254                 ctdlvisor_exit(errno);
255         }
256
257         signal(SIGHUP, signal_handler);
258
259         // "migrate mode" means we just start the server and then run ctdlmigrate interactively.
260         if (migrate_mode) {
261                 citserver_pid = start_citadel();
262                 fprintf(stderr, "ctdlvisor: waiting a moment for citserver to initialize...\n");
263                 sleep(5);
264                 char bin[1024];
265                 sprintf(bin, "%s/usr/local/citadel/ctdlmigrate -h %s", getenv("APPDIR"), getenv("CTDL_DIR"));
266                 system(bin);
267                 kill(citserver_pid, SIGTERM);
268         }
269
270         // Otherwise, it's just a normal happy day in Citadel land.
271         else {
272                 signal(SIGTERM, signal_handler);
273                 signal(SIGINT, signal_handler);
274                 signal(SIGQUIT, signal_handler);
275         
276                 citserver_pid = start_citadel();                // start Citadel Server
277                 webcit_pid = start_webcit();                    // start WebCit HTTP
278                 webcits_pid = start_webcits();                  // start WebCit HTTPS
279
280                 install_client_link();
281         
282                 main_loop();
283         }
284
285         ctdlvisor_exit(0);
286 }