a3f52f5cb5217e48e7d635ec00b892963ebda185
[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 char *data_directory = "/usr/local/citadel";
29 char *http_port = "80";
30 char *https_port = "443";
31
32 pid_t start_citadel() {
33         char bin[1024];
34         sprintf(bin, "%s/usr/local/citadel/citserver", getenv("APPDIR"));
35         pid_t pid = fork();
36         if (pid == 0) {
37                 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
38                 execlp(bin, "citserver", "-x9", "-h", data_directory, NULL);
39                 perror("execlp");
40                 exit(errno);
41         }
42         else {
43                 return(pid);
44         }
45 }
46
47
48 pid_t start_webcit() {
49         char bin[1024];
50         sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
51         char wchome[1024];
52         sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
53         pid_t pid = fork();
54         if (pid == 0) {
55                 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
56                 execlp(bin, "webcit", "-x9", wchome, "-p", http_port, "uds", data_directory, NULL);
57                 perror("execlp");
58                 exit(errno);
59         }
60         else {
61                 return(pid);
62         }
63 }
64
65
66 pid_t start_webcits() {
67         char bin[1024];
68         sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
69         char wchome[1024];
70         sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
71         pid_t pid = fork();
72         if (pid == 0) {
73                 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
74                 execlp(bin, "webcit", "-x9", wchome, "-s", "-p", https_port, "uds", data_directory, NULL);
75                 perror("execlp");
76                 exit(errno);
77         }
78         else {
79                 return(pid);
80         }
81 }
82
83
84 int main(int argc, char **argv) {
85         pid_t citserver_pid;
86         pid_t webcit_pid;
87         pid_t webcits_pid;
88         int shutting_down = 0;
89         int status;
90         pid_t who_exited;
91         int c;
92
93         while ((c = getopt (argc, argv, "h:p:s:")) != -1)  switch(c) {
94                 case 'h':
95                         data_directory = optarg;
96                         break;
97                 case 'p':
98                         http_port = optarg;
99                         break;
100                 case 's':
101                         https_port = optarg;
102                         break;
103                 default:
104                         fprintf(stderr, "ctdlvisor: usage: ctdlvisor [-h data_directory] [-p http_port] [-s https_port]\n");
105                         exit(1);
106         }
107
108         fprintf(stderr, "ctdlvisor: Welcome to the Citadel System, brought to you using AppImage.\n");
109         fprintf(stderr, "ctdlvisor: LD_LIBRARY_PATH = %s\n", getenv("LD_LIBRARY_PATH"));
110         fprintf(stderr, "ctdlvisor:            PATH = %s\n", getenv("PATH"));
111         fprintf(stderr, "ctdlvisor:          APPDIR = %s\n", getenv("APPDIR"));
112         fprintf(stderr, "ctdlvisor:  data directory = %s\n", data_directory);
113         fprintf(stderr, "ctdlvisor:       HTTP port = %s\n", http_port);
114         fprintf(stderr, "ctdlvisor:      HTTPS port = %s\n", https_port);
115
116         if (access(data_directory, R_OK|W_OK|X_OK)) {
117                 fprintf(stderr, "ctdlvisor: %s: %s\n", data_directory, strerror(errno));
118                 exit(errno);
119         }
120
121         citserver_pid = start_citadel();
122         webcit_pid = start_webcit();
123         webcits_pid = start_webcits();
124
125         do {
126                 fprintf(stderr, "ctdlvisor: waiting for any child process to exit...\n");
127                 who_exited = waitpid(-1, &status, 0);
128                 fprintf(stderr, "ctdlvisor: pid=%d exited, status=%d, exitcode=%d\n", who_exited, status, WEXITSTATUS(status));
129
130                 // A *deliberate* exit of citserver will cause ctdlvisor to shut the whole AppImage down.
131                 // If it crashes, however, we will start it back up.
132                 if (who_exited == citserver_pid) {
133                         if (WEXITSTATUS(status) == 0) {
134                                 fprintf(stderr, "ctdlvisor: citserver exited normally - ending AppImage session\n");
135                                 shutting_down = 1;
136                                 kill(webcit_pid, SIGTERM);
137                                 kill(webcits_pid, SIGTERM);
138                         }
139                         else {
140                                 citserver_pid = start_citadel();
141                         }
142                 }
143
144                 // WebCit processes are restarted if they exit for any reason.
145                 if ((who_exited == webcit_pid) && (!shutting_down))             webcit_pid = start_webcit();
146                 if ((who_exited == webcits_pid) && (!shutting_down))            webcits_pid = start_webcits();
147
148                 // If we somehow end up in an endless loop, at least slow it down.
149                 sleep(1);
150
151         } while ((who_exited >= 0) && (shutting_down == 0));
152
153         printf("ctdlvisor: exiting.\n");
154         exit(0);
155 }