working on the options
[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 #include <sys/types.h>
28 #include <sys/stat.h>
29
30 void main_loop(void);
31 void run_in_foreground(void);
32
33 char *data_directory = "/usr/local/citadel";
34 char *http_port = "80";
35 char *https_port = "443";
36 pid_t citserver_pid;
37 pid_t webcit_pid;
38 pid_t webcits_pid;
39 int shutting_down = 0;
40
41
42 void ctdlvisor_exit(int code) {
43         printf("ctdlvisor: exit code %d\n", code);
44         exit(code);
45 }
46
47
48 void signal_handler(int signal) {
49         fprintf(stderr, "ctdlvisor: caught signal %d\n", signal);
50
51         while(shutting_down) {
52                 fprintf(stderr, "ctdlvisor: already shutting down\n");
53                 sleep(1);
54         }
55
56         int status;
57         pid_t who_exited;
58         char *what_exited = NULL;
59
60         shutting_down = 1;
61         kill(citserver_pid, SIGTERM);
62         kill(webcit_pid, SIGTERM);
63         kill(webcits_pid, SIGTERM);
64         do {
65                 fprintf(stderr, "ctdlvisor: waiting for all child process to exit...\n");
66                 who_exited = waitpid(-1, &status, 0);
67                 if (who_exited == citserver_pid) {
68                         what_exited = "Citadel Server";
69                 }
70                 else if (who_exited == webcit_pid) {
71                         what_exited = "WebCit HTTP";
72                 }
73                 else if (who_exited == webcits_pid) {
74                         what_exited = "WebCit HTTPS";
75                 }
76                 else {
77                         what_exited = "unknown";
78                 }
79                 fprintf(stderr, "ctdlvisor: pid=%d (%s) exited, status=%d, exitcode=%d\n", who_exited, what_exited, status, WEXITSTATUS(status));
80         } while (who_exited >= 0);
81
82         ctdlvisor_exit(0);
83 }
84
85
86
87 void detach_from_tty(void) {
88         signal(SIGHUP, SIG_IGN);
89         signal(SIGINT, SIG_IGN);
90         signal(SIGQUIT, SIG_IGN);
91
92         setsid();
93         umask(0);
94         if (    (freopen("/dev/null", "r", stdin) != stdin) ||
95                 (freopen("/dev/null", "w", stdout) != stdout) ||
96                 (freopen("/dev/null", "w", stderr) != stderr)
97         ) {
98                 fprintf(stderr, "sysdep: unable to reopen stdio: %s\n", strerror(errno));
99         }
100 }
101
102
103 pid_t start_citadel() {
104         char bin[1024];
105         sprintf(bin, "%s/usr/local/citadel/citserver", getenv("APPDIR"));
106         pid_t pid = fork();
107         if (pid == 0) {
108                 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
109                 detach_from_tty();
110                 execlp(bin, "citserver", "-x9", "-h", data_directory, NULL);
111                 exit(errno);
112         }
113         else {
114                 return(pid);
115         }
116 }
117
118
119 pid_t start_webcit() {
120         char bin[1024];
121         sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
122         char wchome[1024];
123         sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
124         pid_t pid = fork();
125         if (pid == 0) {
126                 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
127                 detach_from_tty();
128                 execlp(bin, "webcit", "-x9", wchome, "-p", http_port, "uds", data_directory, NULL);
129                 exit(errno);
130         }
131         else {
132                 return(pid);
133         }
134 }
135
136
137 pid_t start_webcits() {
138         char bin[1024];
139         sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
140         char wchome[1024];
141         sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
142         pid_t pid = fork();
143         if (pid == 0) {
144                 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
145                 detach_from_tty();
146                 execlp(bin, "webcit", "-x9", wchome, "-s", "-p", https_port, "uds", data_directory, NULL);
147                 exit(errno);
148         }
149         else {
150                 return(pid);
151         }
152 }
153
154
155 static char *usage =
156         "ctdlvisor: usage: ctdlvisor [-h data_directory] [-p http_port] [-s https_port] command\n"
157         "           'command' must be one of:\n"
158         "           run     - start Citadel services in the foreground without detaching\n"
159         "           install - deploy systemd unit files for automatic startup at boot\n"
160         "           upgrade - check for new version of the software\n"
161         "           test    - check this appimage for compatibility with your system\n"
162 ;
163
164 int main(int argc, char **argv) {
165         int c;
166
167         while ((c = getopt (argc, argv, "h:p:s:")) != -1)  switch(c) {
168                 case 'h':
169                         data_directory = optarg;
170                         break;
171                 case 'p':
172                         http_port = optarg;
173                         break;
174                 case 's':
175                         https_port = optarg;
176                         break;
177                 default:
178                         fprintf(stderr, "%s", usage);
179                         exit(1);
180         }
181
182
183         if (argc != optind+1) {
184                 fprintf(stderr, "%s", usage);
185                 exit(1);
186         }
187
188         if (!strcasecmp(argv[optind], "run")) {
189                 run_in_foreground();
190         }
191         else if (!strcasecmp(argv[optind], "install")) {
192                 fprintf(stderr, "oops, this is not implemented yet\n");
193         }
194         else if (!strcasecmp(argv[optind], "upgrade")) {
195                 fprintf(stderr, "oops, this is not implemented yet\n");
196         }
197         else if (!strcasecmp(argv[optind], "test")) {
198                 fprintf(stderr, "oops, this is not implemented yet\n");
199         }
200         else {
201                 fprintf(stderr, "%s", usage);
202                 exit(1);
203         }
204
205         exit(0);
206 }
207
208
209 void run_in_foreground(void) {
210         fprintf(stderr, "ctdlvisor: Welcome to the Citadel System, brought to you using AppImage.\n");
211         fprintf(stderr, "ctdlvisor: LD_LIBRARY_PATH = %s\n", getenv("LD_LIBRARY_PATH"));
212         fprintf(stderr, "ctdlvisor:            PATH = %s\n", getenv("PATH"));
213         fprintf(stderr, "ctdlvisor:          APPDIR = %s\n", getenv("APPDIR"));
214         fprintf(stderr, "ctdlvisor:  data directory = %s\n", data_directory);
215         fprintf(stderr, "ctdlvisor:       HTTP port = %s\n", http_port);
216         fprintf(stderr, "ctdlvisor:      HTTPS port = %s\n", https_port);
217
218         if (access(data_directory, R_OK|W_OK|X_OK)) {
219                 fprintf(stderr, "ctdlvisor: %s: %s\n", data_directory, strerror(errno));
220                 exit(errno);
221         }
222
223         signal(SIGTERM, signal_handler);
224         signal(SIGHUP, signal_handler);
225         signal(SIGINT, signal_handler);
226         signal(SIGQUIT, signal_handler);
227
228         citserver_pid = start_citadel();
229         webcit_pid = start_webcit();
230         webcits_pid = start_webcits();
231
232         main_loop();
233         exit(0);
234 }
235
236
237 void main_loop(void) {
238         int status;
239         pid_t who_exited;
240         int citserver_exit_code = 0;
241
242         do {
243                 fprintf(stderr, "ctdlvisor: waiting for any child process to exit...\n");
244                 who_exited = waitpid(-1, &status, 0);
245                 fprintf(stderr, "ctdlvisor: pid=%d exited, status=%d, exitcode=%d\n", who_exited, status, WEXITSTATUS(status));
246
247                 // A *deliberate* exit of citserver will cause ctdlvisor to shut the whole AppImage down.
248                 // If it crashes, however, we will start it back up.
249                 if (who_exited == citserver_pid) {
250                         citserver_exit_code = WEXITSTATUS(status);
251                         if (citserver_exit_code == 0) {
252                                 fprintf(stderr, "ctdlvisor: citserver exited normally - ending AppImage session\n");
253                                 shutting_down = 1;
254                                 kill(webcit_pid, SIGTERM);
255                                 kill(webcits_pid, SIGTERM);
256                         }
257                         else if ((citserver_exit_code >= 101) && (citserver_exit_code <= 109)) {
258                                 fprintf(stderr, "ctdlvisor: citserver exited intentionally - ending AppImage session\n");
259                                 shutting_down = 1;
260                                 kill(webcit_pid, SIGTERM);
261                                 kill(webcits_pid, SIGTERM);
262                         }
263                         else {
264                                 citserver_pid = start_citadel();
265                         }
266                 }
267
268                 // WebCit processes are restarted if they exit for any reason.
269                 if ((who_exited == webcit_pid) && (!shutting_down))     webcit_pid = start_webcit();
270                 if ((who_exited == webcits_pid) && (!shutting_down))    webcits_pid = start_webcits();
271
272                 // If we somehow end up in an endless loop, at least slow it down.
273                 sleep(1);
274
275         } while (who_exited >= 0);
276
277         ctdlvisor_exit(citserver_exit_code);
278 }