63230afaef3338132212cb9105dbc0f7fe6f6c38
[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
25
26 pid_t start_citadel() {
27         char bin[1024];
28         sprintf(bin, "%s/usr/local/citadel/citserver", getenv("APPDIR"));
29         pid_t pid = fork();
30         if (pid == 0) {
31                 printf("Executing %s\n", bin);
32                 execlp(bin, "citserver", "-x9", "-h/usr/local/citadel", NULL);
33                 perror("execlp");
34                 exit(errno);
35         }
36         else {
37                 return(pid);
38         }
39 }
40
41
42 pid_t start_webcit() {
43         char bin[1024];
44         sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
45         pid_t pid = fork();
46         if (pid == 0) {
47                 printf("Executing %s\n", bin);
48                 execlp(bin, "webcit", "-x9", "-p80", "uds", "/usr/local/citadel", NULL);
49                 perror("execlp");
50                 exit(errno);
51         }
52         else {
53                 return(pid);
54         }
55 }
56
57
58 pid_t start_webcits() {
59         char bin[1024];
60         sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
61         pid_t pid = fork();
62         if (pid == 0) {
63                 printf("Executing %s\n", bin);
64                 execlp(bin, "webcit", "-x9", "-s", "-p443", "uds", "/usr/local/citadel", NULL);
65                 perror("execlp");
66                 exit(errno);
67         }
68         else {
69                 return(pid);
70         }
71 }
72
73
74 main() {
75         int status;
76         pid_t who_exited;
77
78         pid_t citserver_pid = start_citadel();
79         pid_t webcit_pid = start_webcit();
80         pid_t webcits_pid = start_webcits();
81
82         do {
83                 printf("LD_LIBRARY_PATH = %s\n", getenv("LD_LIBRARY_PATH"));
84                 printf("PATH = %s\n", getenv("PATH"));
85                 printf("APPDIR = %s\n", getenv("APPDIR"));
86
87                 printf("waiting...\n");
88                 who_exited = waitpid(-1, &status, 0);
89                 printf("pid=%d exited, status=%d\n", who_exited, status);
90
91                 if (who_exited == citserver_pid) {
92                         if (WEXITSTATUS(status) == 0) {
93                                 printf("ctdlvisor: citserver exited normally - ending AppImage session\n");
94                                 exit(0);
95                         }
96                         citserver_pid = start_citadel();
97                 }
98
99                 if (who_exited == webcit_pid)           webcit_pid = start_webcit();
100                 if (who_exited == webcits_pid)          webcits_pid = start_webcits();
101
102                 sleep(1);                               // slow down any accidental loops
103
104         } while (who_exited >= 0);
105
106         printf("ctdlvisor: exiting.\n");
107         exit(0);
108 }