trying to figure out why webcit doesn't chdir properly when run from appimage
[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         char wchome[1024];
46         sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
47         pid_t pid = fork();
48         if (pid == 0) {
49                 printf("Executing %s\n", bin);
50                 execlp(bin, "webcit", "-x9", wchome, "-p80", "uds", "/usr/local/citadel", NULL);
51                 perror("execlp");
52                 exit(errno);
53         }
54         else {
55                 return(pid);
56         }
57 }
58
59
60 pid_t start_webcits() {
61         char bin[1024];
62         sprintf(bin, "%s/usr/local/webcit/webcit", getenv("APPDIR"));
63         char wchome[1024];
64         sprintf(wchome, "-h%s/usr/local/webcit", getenv("APPDIR"));
65         pid_t pid = fork();
66         if (pid == 0) {
67                 printf("Executing %s\n", bin);
68                 execlp(bin, "webcit", "-x9", wchome, "-s", "-p443", "uds", "/usr/local/citadel", NULL);
69                 perror("execlp");
70                 exit(errno);
71         }
72         else {
73                 return(pid);
74         }
75 }
76
77
78 main() {
79         int status;
80         pid_t who_exited;
81
82         pid_t citserver_pid = start_citadel();
83         pid_t webcit_pid = start_webcit();
84         pid_t webcits_pid = start_webcits();
85
86         do {
87                 printf("LD_LIBRARY_PATH = %s\n", getenv("LD_LIBRARY_PATH"));
88                 printf("PATH = %s\n", getenv("PATH"));
89                 printf("APPDIR = %s\n", getenv("APPDIR"));
90
91                 printf("waiting...\n");
92                 who_exited = waitpid(-1, &status, 0);
93                 printf("pid=%d exited, status=%d\n", who_exited, status);
94
95                 if (who_exited == citserver_pid) {
96                         if (WEXITSTATUS(status) == 0) {
97                                 printf("ctdlvisor: citserver exited normally - ending AppImage session\n");
98                                 exit(0);
99                         }
100                         citserver_pid = start_citadel();
101                 }
102
103                 if (who_exited == webcit_pid)           webcit_pid = start_webcit();
104                 if (who_exited == webcits_pid)          webcits_pid = start_webcits();
105
106                 sleep(1);                               // slow down any accidental loops
107
108         } while (who_exited >= 0);
109
110         printf("ctdlvisor: exiting.\n");
111         exit(0);
112 }