Build from the published sources, not from git
[citadel-docker.git] / ctdlvisor.c
index 086f1a64fbacc6bfdc269e4249386ad474a8de9d..7c5632abd9eb55baa8a4ffdf9cf5c81a7d40cc7e 100644 (file)
@@ -1,8 +1,8 @@
 // This is a supervisor program that handles start/stop/restart of
 // the various Citadel System components, when we are running in
-// a Docker container.
+// a container.
 //
-// Copyright (c) 2021 by the citadel.org team
+// Copyright (c) 2021-2023 by the citadel.org team
 //
 // This program is open source software.  Use, duplication, or disclosure
 // is subject to the terms of the GNU General Public License, version 3.
@@ -25,10 +25,11 @@ pid_t citserver_pid;
 pid_t webcit_pid;
 pid_t webcits_pid;
 int shutting_down = 0;
+char *logging_flag = "-x1";
 
 // Call this instead of exit() just for common diagnostics etc.
 void ctdlvisor_exit(int code) {
-       printf("ctdlvisor: exit code %d\n", code);
+       fprintf(stderr, "ctdlvisor: exit code %d\n", code);
        exit(code);
 }
 
@@ -94,7 +95,7 @@ pid_t start_citadel() {
        if (pid == 0) {
                fprintf(stderr, "ctdlvisor: executing citserver\n");
                //detach_from_tty();
-               execlp("/usr/local/citadel/citserver", "citserver", "-x9", "-h", CTDL_DIR, NULL);
+               execlp("/usr/local/citadel/citserver", "citserver", logging_flag, "-h", CTDL_DIR, NULL);
                exit(errno);
        }
        else {
@@ -109,7 +110,7 @@ pid_t start_webcit() {
        if (pid == 0) {
                fprintf(stderr, "ctdlvisor: executing webcit (http)\n");
                //detach_from_tty();
-               execlp("/usr/local/webcit/webcit", "webcit", "-x9", "-p", "80", "uds", CTDL_DIR, NULL);
+               execlp("/usr/local/webcit/webcit", "webcit", logging_flag, "-p", "80", "uds", CTDL_DIR, NULL);
                exit(errno);
        }
        else {
@@ -124,7 +125,7 @@ pid_t start_webcits() {
        if (pid == 0) {
                fprintf(stderr, "ctdlvisor: executing webcit (https)\n");
                //detach_from_tty();
-               execlp("/usr/local/webcit/webcit", "webcit", "-x9", "-s", "-p", "443", "uds", CTDL_DIR, NULL);
+               execlp("/usr/local/webcit/webcit", "webcit", logging_flag, "-s", "-p", "443", "uds", CTDL_DIR, NULL);
                exit(errno);
        }
        else {
@@ -143,18 +144,18 @@ void main_loop(void) {
                who_exited = waitpid(-1, &status, 0);
                fprintf(stderr, "ctdlvisor: pid=%d exited, status=%d, exitcode=%d\n", who_exited, status, WEXITSTATUS(status));
 
-               // A *deliberate* exit of citserver will cause ctdlvisor to shut the whole AppImage down.
+               // A *deliberate* exit of citserver will cause ctdlvisor to shut the whole container down.
                // If it crashes, however, we will start it back up.
                if (who_exited == citserver_pid) {
                        citserver_exit_code = WEXITSTATUS(status);
                        if ((WIFEXITED(status)) && (citserver_exit_code == 0)) {
-                               fprintf(stderr, "ctdlvisor: citserver exited normally - ending AppImage session\n");
+                               fprintf(stderr, "ctdlvisor: citserver exited normally - ending container session\n");
                                shutting_down = 1;
                                kill(webcit_pid, SIGTERM);
                                kill(webcits_pid, SIGTERM);
                        }
                        else if ((WIFEXITED(status)) && (citserver_exit_code >= 101) && (citserver_exit_code <= 109)) {
-                               fprintf(stderr, "ctdlvisor: citserver exited intentionally - ending AppImage session\n");
+                               fprintf(stderr, "ctdlvisor: citserver exited intentionally - ending container session\n");
                                shutting_down = 1;
                                kill(webcit_pid, SIGTERM);
                                kill(webcits_pid, SIGTERM);
@@ -179,34 +180,12 @@ void main_loop(void) {
 }
 
 
-void install_client_link(void) {                       // FIXME this is all furkokt and needs to be rethought now that it's docker and not appimage
-
-       FILE *fp;
-       char path_to_link[PATH_MAX];
-       snprintf(path_to_link, sizeof path_to_link, CTDL_DIR "citadel");
-       fp = fopen(path_to_link, "w");
-       if (!fp) {
-               fprintf(stderr, "%s\n", strerror(errno));
-               return;
-       }
-
-       fprintf(fp,     "#!/bin/bash\n"
-                       "export APPDIR=%s\n"
-                       "export LD_LIBRARY_PATH=${APPDIR}/usr/bin:$LD_LIBRARY_PATH\n"
-                       "export PATH=${APPDIR}/usr/bin:$PATH\n"
-                       "exec citadel\n"
-       ,
-                       getenv("APPDIR")
-       );
-
-       fchmod(fileno(fp), 0755);
-       fclose(fp);
-}
-
-
 int main(int argc, char **argv) {
        int a;
-       int migrate_mode = 0;
+       int export_mode = 0;
+       int import_mode = 0;
+       int database_cleanup_mode = 0;
+       char bin[1024];
 
        fprintf(stderr, "ctdlvisor: Welcome to the Citadel System running in a container.\n");
        fprintf(stderr, "ctdlvisor: command line arguments: ");
@@ -236,35 +215,59 @@ int main(int argc, char **argv) {
        symlink(CTDL_DIR "/keys", "/usr/local/webcit/keys");
 
        /* parse command-line arguments */
-       while ((a=getopt(argc, argv, "cm")) != EOF) switch(a) {
+       while ((a=getopt(argc, argv, "ceidx:")) != EOF) switch(a) {
 
                // test this binary for compatibility and exit
                case 'c':
-                       fprintf(stderr, "%s: binary compatibility confirmed\n", argv[0]);
+                       fprintf(stderr, "%s: binary compatibility CONFIRMED!  You CAN run Citadel in a container on this system.\n", argv[0]);
                        exit(0);
                        break;
 
-               // run ctdlmigrate only
-               case 'm':
-                       migrate_mode = 1;
+               // export your database
+               case 'e':
+                       export_mode = 1;
+                       break;
+
+               // import your database
+               case 'i':
+                       import_mode = 1;
+                       break;
+
+               // run database_cleanup.sh only
+               case 'd':
+                       database_cleanup_mode = 1;
+                       break;
+
+               // logging level
+               case 'x':
+                       logging_flag = malloc(50);
+                       snprintf(logging_flag, 50, "-x%d", atoi(optarg));
                        break;
 
                // any other parameter makes it crash and burn
                default:
-                       fprintf(stderr, "usage\n");
+                       fprintf(stderr, "%s: usage: %s [-c] [-e] [-i] [-d] [-x log_level]\n", argv[0], argv[0]);
                        exit(1);
        }
 
 
        signal(SIGHUP, signal_handler);
 
-       // "migrate mode" means we just start the server and then run ctdlmigrate interactively.
-       if (migrate_mode) {
-               citserver_pid = start_citadel();
-               fprintf(stderr, "ctdlvisor: waiting a moment for citserver to initialize...\n");
-               sleep(5);
-               char bin[1024];
-               sprintf(bin, "/usr/local/citadel/ctdlmigrate -h %s", CTDL_DIR);
+       // "export mode" means we only run ctdldump
+       if (export_mode) {
+               sprintf(bin, "/usr/local/citadel/ctdldump -y -h %s", CTDL_DIR);
+               system(bin);
+       }
+
+       // "import mode" means we only run ctdlload
+       else if (import_mode) {
+               sprintf(bin, "/usr/local/citadel/ctdlload -y -h %s", CTDL_DIR);
+               system(bin);
+       }
+
+       // "database cleanup mode" means we just start the server and then run database_cleanup.sh interactively
+       else if (database_cleanup_mode) {
+               sprintf(bin, "/usr/local/citadel/database_cleanup.sh -h %s", CTDL_DIR);
                system(bin);
                kill(citserver_pid, SIGTERM);
        }
@@ -279,8 +282,6 @@ int main(int argc, char **argv) {
                webcit_pid = start_webcit();                    // start WebCit HTTP
                webcits_pid = start_webcits();                  // start WebCit HTTPS
 
-               install_client_link();
-       
                main_loop();
        }