From 335c0d7c212480a8dcb91b2d747d205a112b5dd2 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Thu, 28 Dec 2006 16:34:20 +0000 Subject: [PATCH] * -d to daemonize, -D to daemonize with specifying the pid file. Updated setup.c to use this too. * Bumped the version number to 7.02 in preparation for release... --- webcit/README.txt | 2 +- webcit/configure.in | 2 +- webcit/crypto.c | 4 ++-- webcit/setup.c | 6 +++--- webcit/webcit.h | 10 ++++++++-- webcit/webserver.c | 42 ++++++++++++++++++++++++------------------ 6 files changed, 39 insertions(+), 27 deletions(-) diff --git a/webcit/README.txt b/webcit/README.txt index 049874410..20b9ef8cd 100644 --- a/webcit/README.txt +++ b/webcit/README.txt @@ -1,5 +1,5 @@ WEBCIT for the Citadel System - version 7.01 + version 7.02 Copyright (C) 1996-2006 by the authors. Portions written by: Art Cancro diff --git a/webcit/configure.in b/webcit/configure.in index dd7337afe..2cdd1c58e 100644 --- a/webcit/configure.in +++ b/webcit/configure.in @@ -4,7 +4,7 @@ AC_INIT(webserver.c) PACKAGE=webcit -VERSION=7.00 +VERSION=7.02 AC_DEFINE_UNQUOTED(PACKAGE, "$PACKAGE") AC_DEFINE_UNQUOTED(VERSION, "$VERSION") AC_SUBST(PACKAGE) diff --git a/webcit/crypto.c b/webcit/crypto.c index b7d36faf9..022b64ef0 100644 --- a/webcit/crypto.c +++ b/webcit/crypto.c @@ -63,7 +63,7 @@ void init_ssl(void) if (!SSLCritters) { lprintf(1, "citserver: can't allocate memory!!\n"); /* Nothing's been initialized, just die */ - exit(1); + exit(WC_EXIT_SSL); } else { int a; @@ -73,7 +73,7 @@ void init_ssl(void) lprintf(1, "citserver: can't allocate memory!!\n"); /** Nothing's been initialized, just die */ - exit(1); + exit(WC_EXIT_SSL); } pthread_mutex_init(SSLCritters[a], NULL); } diff --git a/webcit/setup.c b/webcit/setup.c index 8022ca0d2..136892f6a 100644 --- a/webcit/setup.c +++ b/webcit/setup.c @@ -310,7 +310,7 @@ void progress(char *text, long int curr, long int cmax) /* - * install_init_scripts() -- Make sure "webserver" is in /etc/inittab + * install_init_scripts() -- Create and deploy SysV init scripts. * */ void install_init_scripts(void) @@ -399,7 +399,7 @@ void install_init_scripts(void) "\n" "start) echo -n \"Starting WebCit... \"\n" " if $WEBCIT_DIR/webserver " - "-d/var/run/webcit.pid " + "-D/var/run/webcit.pid " "-t/dev/null " "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n" " then\n" @@ -410,7 +410,7 @@ void install_init_scripts(void) #ifdef HAVE_OPENSSL fprintf(fp, " echo -n \"Starting WebCit SSL... \"\n" " if $WEBCIT_DIR/webserver " - "-d/var/run/webcit-ssl.pid " + "-D/var/run/webcit-ssl.pid " "-t/dev/null " "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n" " then\n" diff --git a/webcit/webcit.h b/webcit/webcit.h index 804c2ba31..6dc14d2f1 100644 --- a/webcit/webcit.h +++ b/webcit/webcit.h @@ -122,10 +122,10 @@ extern locale_t wc_locales[]; #define SLEEPING 180 /* TCP connection timeout */ #define WEBCIT_TIMEOUT 900 /* WebCit session timeout */ #define PORT_NUM 2000 /* port number to listen on */ -#define SERVER "WebCit v7.00" /* who's in da house */ +#define SERVER "WebCit v7.02" /* who's in da house */ #define DEVELOPER_ID 0 #define CLIENT_ID 4 -#define CLIENT_VERSION 700 /* This version of WebCit */ +#define CLIENT_VERSION 702 /* This version of WebCit */ #define MINIMUM_CIT_VERSION 690 /* min required Citadel ver. */ #define DEFAULT_HOST "localhost" /* Default Citadel server */ #define DEFAULT_PORT "504" @@ -752,3 +752,9 @@ void http_datestring(char *buf, size_t n, time_t xtime); #define DEFAULT_HTTPAUTH_USER "" #define DEFAULT_HTTPAUTH_PASS "" + +/* Exit codes 101 through 109 are initialization failures so we don't want to + * just keep respawning indefinitely. + */ +#define WC_EXIT_BIND 101 /* Can't bind to the port */ +#define WC_EXIT_SSL 102 /* Can't initialize SSL */ diff --git a/webcit/webserver.c b/webcit/webserver.c index 038ab57ae..1652a4ff5 100644 --- a/webcit/webserver.c +++ b/webcit/webserver.c @@ -86,14 +86,14 @@ int ig_tcp_server(char *ip_addr, int port_number, int queue_len) if (port_number == 0) { lprintf(1, "Cannot start: no port number specified.\n"); - exit(1); + exit(WC_EXIT_BIND); } sin.sin_port = htons((u_short) port_number); s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto)); if (s < 0) { lprintf(1, "Can't create a socket: %s\n", strerror(errno)); - exit(errno); + exit(WC_EXIT_BIND); } /** Set some socket options that make sense. */ i = 1; @@ -101,11 +101,11 @@ int ig_tcp_server(char *ip_addr, int port_number, int queue_len) if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) { lprintf(1, "Can't bind: %s\n", strerror(errno)); - exit(errno); + exit(WC_EXIT_BIND); } if (listen(s, queue_len) < 0) { lprintf(1, "Can't listen: %s\n", strerror(errno)); - exit(errno); + exit(WC_EXIT_BIND); } return (s); } @@ -131,7 +131,7 @@ int ig_uds_server(char *sockpath, int queue_len) if (i != 0) if (errno != ENOENT) { lprintf(1, "citserver: can't unlink %s: %s\n", sockpath, strerror(errno)); - exit(errno); + exit(WC_EXIT_BIND); } memset(&addr, 0, sizeof(addr)); @@ -142,19 +142,19 @@ int ig_uds_server(char *sockpath, int queue_len) if (s < 0) { lprintf(1, "citserver: Can't create a socket: %s\n", strerror(errno)); - exit(errno); + exit(WC_EXIT_BIND); } if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { lprintf(1, "citserver: Can't bind: %s\n", strerror(errno)); - exit(errno); + exit(WC_EXIT_BIND); } if (listen(s, actual_queue_len) < 0) { lprintf(1, "citserver: Can't listen: %s\n", strerror(errno)); - exit(errno); + exit(WC_EXIT_BIND); } chmod(sockpath, 0777); @@ -482,10 +482,12 @@ void start_daemon(int do_close_stdio, char *pid_file) child = fork(); if (child != 0) { - fp = fopen(pid_file, "w"); - if (fp != NULL) { - fprintf(fp, "%d\n", child); - fclose(fp); + if (pid_file) { + fp = fopen(pid_file, "w"); + if (fp != NULL) { + fprintf(fp, "%d\n", child); + fclose(fp); + } } exit(0); } @@ -547,7 +549,9 @@ void start_daemon(int do_close_stdio, char *pid_file) } while (do_restart); - unlink(pid_file); + if (pid_file) { + unlink(pid_file); + } exit(WEXITSTATUS(status)); } @@ -606,7 +610,7 @@ int main(int argc, char **argv) int home_specified=0; char relhome[PATH_MAX]=""; char webcitdir[PATH_MAX] = DATADIR; - char pidfile[PATH_MAX] = ""; + char *pidfile = NULL; char *hdir; const char *basedir; #ifdef ENABLE_NLS @@ -619,9 +623,9 @@ int main(int argc, char **argv) /** Parse command line */ #ifdef HAVE_OPENSSL - while ((a = getopt(argc, argv, "h:i:p:t:x:d:cfs")) != EOF) + while ((a = getopt(argc, argv, "h:i:p:t:x:dD:cfs")) != EOF) #else - while ((a = getopt(argc, argv, "h:i:p:t:x:d:cf")) != EOF) + while ((a = getopt(argc, argv, "h:i:p:t:x:dD:cf")) != EOF) #endif switch (a) { case 'h': @@ -637,8 +641,10 @@ int main(int argc, char **argv) home=1; break; case 'd': - hdir = strdup(optarg); - safestrncpy(pidfile, hdir,sizeof pidfile); + running_as_daemon = 1; + break; + case 'D': + pidfile = strdup(optarg); running_as_daemon = 1; break; case 'i': -- 2.30.2