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.
6 // Copyright (c) 2021 by the citadel.org team
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.
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.
27 #include <sys/types.h>
31 void run_in_foreground(void);
33 char *data_directory = "/usr/local/citadel";
34 char *http_port = "80";
35 char *https_port = "443";
39 int shutting_down = 0;
42 void ctdlvisor_exit(int code) {
43 printf("ctdlvisor: exit code %d\n", code);
48 void signal_handler(int signal) {
49 fprintf(stderr, "ctdlvisor: caught signal %d\n", signal);
51 while(shutting_down) {
52 fprintf(stderr, "ctdlvisor: already shutting down\n");
58 char *what_exited = NULL;
61 kill(citserver_pid, SIGTERM);
62 kill(webcit_pid, SIGTERM);
63 kill(webcits_pid, SIGTERM);
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";
70 else if (who_exited == webcit_pid) {
71 what_exited = "WebCit HTTP";
73 else if (who_exited == webcits_pid) {
74 what_exited = "WebCit HTTPS";
77 what_exited = "unknown";
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);
87 void detach_from_tty(void) {
88 signal(SIGHUP, SIG_IGN);
89 signal(SIGINT, SIG_IGN);
90 signal(SIGQUIT, SIG_IGN);
94 if ( (freopen("/dev/null", "r", stdin) != stdin) ||
95 (freopen("/dev/null", "w", stdout) != stdout) ||
96 (freopen("/dev/null", "w", stderr) != stderr)
98 fprintf(stderr, "sysdep: unable to reopen stdio: %s\n", strerror(errno));
103 pid_t start_citadel() {
105 sprintf(bin, "%s/usr/local/citadel/citserver", getenv("APPDIR"));
108 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
110 execlp(bin, "citserver", "-x9", "-h", data_directory, NULL);
119 pid_t start_webcit() {
121 sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
123 sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
126 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
128 execlp(bin, "webcit", "-x9", wchome, "-p", http_port, "uds", data_directory, NULL);
137 pid_t start_webcits() {
139 sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
141 sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
144 fprintf(stderr, "ctdlvisor: executing %s\n", bin);
146 execlp(bin, "webcit", "-x9", wchome, "-s", "-p", https_port, "uds", data_directory, NULL);
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"
164 int main(int argc, char **argv) {
167 while ((c = getopt (argc, argv, "h:p:s:")) != -1) switch(c) {
169 data_directory = optarg;
178 fprintf(stderr, "%s", usage);
183 if (argc != optind+1) {
184 fprintf(stderr, "%s", usage);
188 if (!strcasecmp(argv[optind], "run")) {
191 else if (!strcasecmp(argv[optind], "install")) {
192 fprintf(stderr, "oops, this is not implemented yet\n");
194 else if (!strcasecmp(argv[optind], "upgrade")) {
195 fprintf(stderr, "oops, this is not implemented yet\n");
197 else if (!strcasecmp(argv[optind], "test")) {
198 fprintf(stderr, "oops, this is not implemented yet\n");
201 fprintf(stderr, "%s", usage);
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);
218 if (access(data_directory, R_OK|W_OK|X_OK)) {
219 fprintf(stderr, "ctdlvisor: %s: %s\n", data_directory, strerror(errno));
223 signal(SIGTERM, signal_handler);
224 signal(SIGHUP, signal_handler);
225 signal(SIGINT, signal_handler);
226 signal(SIGQUIT, signal_handler);
228 citserver_pid = start_citadel();
229 webcit_pid = start_webcit();
230 webcits_pid = start_webcits();
237 void main_loop(void) {
240 int citserver_exit_code = 0;
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));
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");
254 kill(webcit_pid, SIGTERM);
255 kill(webcits_pid, SIGTERM);
257 else if ((citserver_exit_code >= 101) && (citserver_exit_code <= 109)) {
258 fprintf(stderr, "ctdlvisor: citserver exited intentionally - ending AppImage session\n");
260 kill(webcit_pid, SIGTERM);
261 kill(webcits_pid, SIGTERM);
264 citserver_pid = start_citadel();
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();
272 // If we somehow end up in an endless loop, at least slow it down.
275 } while (who_exited >= 0);
277 ctdlvisor_exit(citserver_exit_code);