* shut down clean in case of not being able to bind.
[citadel.git] / webcit / webserver.c
index fa02e2dd473a8f5ede439a141ba467cb3fa57080..c3263ecb7820fb1d4e5abbf4197087aa60021aeb 100644 (file)
@@ -99,11 +99,10 @@ void graceful_shutdown_watcher(int signum) {
  */
 pid_t current_child;
 void graceful_shutdown(int signum) {
-       char wd[SIZ];
        FILE *FD;
        int fd;
-       getcwd(wd, SIZ);
-       lprintf (1, "bye going down gracefull.[%d][%s]\n", signum, wd);
+
+       lprintf (1, "WebCit is being shut down on signal %d.\n", signum);
        fd = msock;
        msock = -1;
        time_to_die = 1;
@@ -123,6 +122,8 @@ void start_daemon(char *pid_file)
        pid_t child = 0;
        FILE *fp;
        int do_restart = 0;
+       int rv;
+       FILE *rvfp = NULL;
 
        current_child = 0;
 
@@ -130,7 +131,7 @@ void start_daemon(char *pid_file)
         * We don't just call close() because we don't want these fd's
         * to be reused for other files.
         */
-       chdir("/");
+       rv = chdir("/");
 
        signal(SIGHUP, SIG_IGN);
        signal(SIGINT, SIG_IGN);
@@ -143,9 +144,9 @@ void start_daemon(char *pid_file)
 
        setsid();
        umask(0);
-       freopen("/dev/null", "r", stdin);
-       freopen("/dev/null", "w", stdout);
-       freopen("/dev/null", "w", stderr);
+       rvfp = freopen("/dev/null", "r", stdin);
+       rvfp = freopen("/dev/null", "w", stdout);
+       rvfp = freopen("/dev/null", "w", stderr);
        signal(SIGTERM, graceful_shutdown_watcher);
        signal(SIGHUP, graceful_shutdown_watcher);
 
@@ -219,16 +220,15 @@ void spawn_another_worker_thread()
        pthread_attr_t attr;    /* Thread attributes */
        int ret;
 
-       lprintf(3, "Creating a new thread\n");
+       lprintf(3, "Creating a new thread.  Pool size is now %d\n", ++num_threads);
 
        /* set attributes for the new thread */
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 
        /*
-        * Our per-thread stacks need to be bigger than the default size, otherwise
-        * the MIME parser crashes on FreeBSD, and the IMAP service crashes on
-        * 64-bit Linux.
+        * Our per-thread stacks need to be bigger than the default size,
+        * otherwise the MIME parser crashes on FreeBSD.
         */
        if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
                lprintf(1, "pthread_attr_setstacksize: %s\n",
@@ -331,6 +331,8 @@ int main(int argc, char **argv)
        const char *basedir = NULL;
        char uds_listen_path[PATH_MAX]; /* listen on a unix domain socket? */
        const char *I18nDumpFile = NULL;
+       FILE *rvfp = NULL;
+       int rv = 0;
 
        WildFireInitBacktrace(argv[0], 2);
 
@@ -353,7 +355,7 @@ int main(int argc, char **argv)
 
        /* Parse command line */
 #ifdef HAVE_OPENSSL
-       while ((a = getopt(argc, argv, "h:i:p:t:T:B:x:dD:G:cfsZ")) != EOF)
+       while ((a = getopt(argc, argv, "h:i:p:t:T:B:x:dD:G:cfsS:Z")) != EOF)
 #else
        while ((a = getopt(argc, argv, "h:i:p:t:T:B:x:dD:G:cfZ")) != EOF)
 #endif
@@ -361,11 +363,12 @@ int main(int argc, char **argv)
                case 'h':
                        hdir = strdup(optarg);
                        relh=hdir[0]!='/';
-                       if (!relh) safestrncpy(webcitdir, hdir,
-                                                                  sizeof webcitdir);
-                       else
-                               safestrncpy(relhome, relhome,
-                                                       sizeof relhome);
+                       if (!relh) {
+                               safestrncpy(webcitdir, hdir, sizeof webcitdir);
+                       }
+                       else {
+                               safestrncpy(relhome, relhome, sizeof relhome);
+                       }
                        /* free(hdir); TODO: SHOULD WE DO THIS? */
                        home_specified = 1;
                        home=1;
@@ -393,9 +396,9 @@ int main(int argc, char **argv)
                        break;
                case 't':
                        safestrncpy(tracefile, optarg, sizeof tracefile);
-                       freopen(tracefile, "w", stdout);
-                       freopen(tracefile, "w", stderr);
-                       freopen(tracefile, "r", stdin);
+                       rvfp = freopen(tracefile, "w", stdout);
+                       rvfp = freopen(tracefile, "w", stderr);
+                       rvfp = freopen(tracefile, "r", stdin);
                        break;
                case 'T':
                        LoadTemplates = atoi(optarg);
@@ -426,9 +429,15 @@ int main(int argc, char **argv)
                                }
                        }
                        break;
+#ifdef HAVE_OPENSSL
                case 's':
                        is_https = 1;
                        break;
+               case 'S':
+                       is_https = 1;
+                       ssl_cipher_list = strdup(optarg);
+                       break;
+#endif
                case 'G':
                        DumpTemplateI18NStrings = 1;
                        I18nDump = NewStrBufPlain(HKEY("int templatestrings(void)\n{\n"));
@@ -441,7 +450,7 @@ int main(int argc, char **argv)
                                "[-T Templatedebuglevel] "
                                "[-d] [-Z] [-G i18ndumpfile] "
 #ifdef HAVE_OPENSSL
-                               "[-s] "
+                               "[-s] [-S cipher_suites]"
 #endif
                                "[remotehost [remoteport]]\n");
                        return 1;
@@ -495,7 +504,7 @@ int main(int argc, char **argv)
                        lprintf(1, "********************************************************************************\n");
                        return -1;
                }
-               fwrite(ChrPtr(I18nDump), 1, StrLength(I18nDump), fd);
+               rv = fwrite(ChrPtr(I18nDump), 1, StrLength(I18nDump), fd);
                fclose(fd);
                return 0;
        }
@@ -543,6 +552,11 @@ int main(int argc, char **argv)
                lprintf(2, "Attempting to bind to port %d...\n", http_port);
                msock = ig_tcp_server(ip_addr, http_port, LISTEN_QUEUE_LENGTH);
        }
+       if (msock < 0)
+       {
+               ShutDownWebcit();
+               return -msock;
+       }
 
        lprintf(2, "Listening on socket %d\n", msock);
        signal(SIGPIPE, SIG_IGN);