From 2ccab0dc11c26919ce8a315a5004e44cad0a0476 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wilfried=20G=C3=B6esgens?= Date: Thu, 28 Dec 2006 12:16:59 +0000 Subject: [PATCH] * buildpackages can create deb source tarballs now. * we don't ship setup in debs anymore. * have port 8504 as default for without ssl in debs * deamonize by ourselfs, remove ctdlsvc * modified setup to use -d in the init script --- webcit/Makefile.in | 8 +- webcit/buildpackages | 6 +- webcit/ctdlsvc.c | 101 ----------------------- webcit/debian/citadel-webcit.templates | 2 +- webcit/debian/control | 4 +- webcit/debian/rules | 6 +- webcit/debian/webcit.init | 9 +-- webcit/setup.c | 9 +-- webcit/webserver.c | 108 ++++++++++++++++++++++--- 9 files changed, 118 insertions(+), 135 deletions(-) delete mode 100644 webcit/ctdlsvc.c diff --git a/webcit/Makefile.in b/webcit/Makefile.in index 0512e25ce..64d7c10c0 100644 --- a/webcit/Makefile.in +++ b/webcit/Makefile.in @@ -21,13 +21,13 @@ SUBDIRS=$(LIB_SUBDIRS) $(PROG_SUBDIRS) # End of configuration section -all: all-progs-recursive webserver setup ctdlsvc +all: all-progs-recursive webserver setup .SUFFIXES: .cpp .c .o clean: - rm -f *.o webcit webserver ctdlsvc + rm -f *.o webcit webserver rm -fr locale/* distclean: clean @@ -39,9 +39,6 @@ setup: setup.o tools.o $(CC) setup.o tools.o \ $(LIBOBJS) $(LIBS) $(LDFLAGS) $(SETUP_LIBS) -o setup -ctdlsvc: ctdlsvc.o - $(CC) ctdlsvc.o -o ctdlsvc - webserver: webserver.o context_loop.o tools.o ical_dezonify.o \ cookie_conversion.o locate_host.o floors.o summary.o \ webcit.o auth.o tcp_sockets.o mainmenu.o serv_func.o who.o \ @@ -96,7 +93,6 @@ install: for i in \ webserver \ setup \ - ctdlsvc \ `find static -type f | grep -v .svn` \ `find tiny_mce -type f | grep -v .svn` \ `find locale -type f | grep -v .svn` \ diff --git a/webcit/buildpackages b/webcit/buildpackages index 3f10f911a..f1eb9fd4b 100755 --- a/webcit/buildpackages +++ b/webcit/buildpackages @@ -48,7 +48,11 @@ case $1 in mv /tmp/webcit_changelog debian/changelog fi - fakeroot dpkg-buildpackage + if test "$2" == "src"; then + cd ..; dpkg-source -b "webcit-$WEBCIT_VERSION" + else + fakeroot dpkg-buildpackage + fi ;; *) echo "Not yet implemented. we have: debian " diff --git a/webcit/ctdlsvc.c b/webcit/ctdlsvc.c deleted file mode 100644 index a734c0a1d..000000000 --- a/webcit/ctdlsvc.c +++ /dev/null @@ -1,101 +0,0 @@ -/* - * $Id: $ - * - * This is just a quick little hack to start a program in the background, - * and automatically restart it if it exits with a nonzero exit status. - * - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -char *pidfilename = NULL; -pid_t current_child = 0; - -void graceful_shutdown(int signum) { - kill(current_child, signum); - if (pidfilename != NULL) { - unlink(pidfilename); - } - exit(0); -} - - -int main(int argc, char **argv) -{ - pid_t child = 0; - int status = 0; - FILE *fp; - int nullfd; - - --argc; - ++argv; - - pidfilename = argv[0]; - --argc; - ++argv; - - if (access(argv[0], X_OK)) { - fprintf(stderr, "%s: cannot execute\n", argv[0]); - exit(1); - } - - /* Close stdin/stdout/stderr and replace them with /dev/null. - * We don't just call close() because we don't want these fd's - * to be reused for other files. - */ - nullfd = open("/dev/null", O_RDWR); - if (nullfd < 0) { - fprintf(stderr, "/dev/null: %s\n", strerror(errno)); - exit(2); - } - dup2(nullfd, 0); - dup2(nullfd, 1); - dup2(nullfd, 2); - close(nullfd); - - signal(SIGHUP, SIG_IGN); - signal(SIGINT, SIG_IGN); - signal(SIGQUIT, SIG_IGN); - - child = fork(); - if (child != 0) { - fp = fopen(pidfilename, "w"); - if (fp != NULL) { - fprintf(fp, "%d\n", child); - fclose(fp); - } - exit(0); - } - - do { - current_child = fork(); - - signal(SIGTERM, graceful_shutdown); - - if (current_child < 0) { - perror("fork"); - exit(errno); - } - - else if (current_child == 0) { - exit(execvp(argv[0], &argv[0])); - } - - else { - waitpid(current_child, &status, 0); - } - - } while (status != 0); - - unlink(pidfilename); - exit(0); -} - diff --git a/webcit/debian/citadel-webcit.templates b/webcit/debian/citadel-webcit.templates index 8e35ea99f..d8587f58f 100644 --- a/webcit/debian/citadel-webcit.templates +++ b/webcit/debian/citadel-webcit.templates @@ -1,6 +1,6 @@ Template: citadel/WebcitHttpPort Type: string -Default: 2020 +Default: 8504 Description: Webcit HTTP Port Select the Port, which the plain HTTP webcit server should listen on. -1 to disable. diff --git a/webcit/debian/control b/webcit/debian/control index 66136ff8d..aae257cd5 100644 --- a/webcit/debian/control +++ b/webcit/debian/control @@ -2,13 +2,13 @@ Source: webcit Section: unknown Priority: optional Maintainer: Wilfried Goesgens -Build-Depends: debhelper (>= 4.0.0), libical0-dev +Build-Depends: debhelper (>= 4.0.0), libical0-dev, gettext Standards-Version: 3.6.1 Package: citadel-webcit Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends}, debconf -Sugests: citadel-server +Suggests: citadel-server Description: Webcit, the Webfrontend to the Citadel. WebCit puts a web-based front end on Citadel, allowing users to access the system from any web browser. And diff --git a/webcit/debian/rules b/webcit/debian/rules index d07ffaee4..61bb40a95 100755 --- a/webcit/debian/rules +++ b/webcit/debian/rules @@ -82,10 +82,8 @@ install: build $(CURDIR)/debian/citadel-webcit/usr/share/locale cd $(CURDIR)/debian/citadel-webcit/usr/share/doc/citadel-webcit/examples/; \ md5sum `find -type f |grep -v md5sums `>md5sums; - mv $(CURDIR)/debian/webcit/var/lib/citadel/www/setup \ - $(CURDIR)/debian/citadel-webcit/usr/lib/citadel-webcit/ - mv $(CURDIR)/debian/webcit/var/lib/citadel/www/ctdlsvc \ - $(CURDIR)/debian/citadel-webcit/usr/lib/citadel-webcit/ +# we don't use the setup anymore, omit it. + rm -f $(CURDIR)/debian/webcit/var/lib/citadel/www/setup mv $(CURDIR)/debian/webcit/var/lib/citadel/www/webserver \ $(CURDIR)/debian/citadel-webcit/usr/sbin/ diff --git a/webcit/debian/webcit.init b/webcit/debian/webcit.init index c963a30c8..c8a872621 100644 --- a/webcit/debian/webcit.init +++ b/webcit/debian/webcit.init @@ -23,7 +23,6 @@ PATH=/sbin:/usr/sbin:/bin:/usr/bin DESC="Citadel Groupware Webserver " NAME=webserver DAEMON=/usr/sbin/$NAME -CTDLSVC=/usr/lib/citadel-webcit/ctdlsvc DAEMON_ARGS="" PIDFILE=$RUNDIR/$NAME.pid SCRIPTNAME=/etc/init.d/webcit @@ -90,8 +89,8 @@ do_start() # 0 if daemon has been started # 1 if daemon was already running # 2 if daemon could not be started - if $CTDLSVC $PIDFILE.$1 $DAEMON \ - -p$@ -t$LOGDIR/access.${1}.log ; then + if $DAEMON -d$PIDFILE.$1 \ + -p$@ -t$LOGDIR/access.${1}.log ; then return 0 else return 2 @@ -112,10 +111,10 @@ do_stop() # 2 if daemon could not be stopped # other if a failure occurred - for i in $PIDFILE.*; do + for i in $PIDFILE.*; do if test -f $i; then kill `cat $i` rm -f $i - done + fi; done #while test -d /proc/`cat $PIDFILE`; do # /usr/bin/printf '.' diff --git a/webcit/setup.c b/webcit/setup.c index 3e13ebe7f..8022ca0d2 100644 --- a/webcit/setup.c +++ b/webcit/setup.c @@ -393,14 +393,13 @@ void install_init_scripts(void) fprintf(fp, "CTDL_HOSTNAME=%s\n", hostname); fprintf(fp, "CTDL_PORTNAME=%s\n", portname); fprintf(fp, "\n" - "test -x $WEBCIT_DIR/ctdlsvc || exit 0\n" "test -d /var/run || exit 0\n" "\n" "case \"$1\" in\n" "\n" "start) echo -n \"Starting WebCit... \"\n" - " if $WEBCIT_DIR/ctdlsvc /var/run/webcit.pid " - "$WEBCIT_DIR/webserver " + " if $WEBCIT_DIR/webserver " + "-d/var/run/webcit.pid " "-t/dev/null " "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n" " then\n" @@ -410,8 +409,8 @@ void install_init_scripts(void) " fi\n"); #ifdef HAVE_OPENSSL fprintf(fp, " echo -n \"Starting WebCit SSL... \"\n" - " if $WEBCIT_DIR/ctdlsvc /var/run/webcit-ssl.pid " - "$WEBCIT_DIR/webserver " + " if $WEBCIT_DIR/webserver " + "-d/var/run/webcit-ssl.pid " "-t/dev/null " "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n" " then\n" diff --git a/webcit/webserver.c b/webcit/webserver.c index f90e445c7..038ab57ae 100644 --- a/webcit/webserver.c +++ b/webcit/webserver.c @@ -445,25 +445,110 @@ int client_getln(int sock, char *buf, int bufsiz) return (retval); } +/** + * \brief shut us down the regular way. + * param signum the signal we want to forward + */ +pid_t current_child; +void graceful_shutdown(int signum) { + kill(current_child, signum); + exit(0); +} + /** * \brief Start running as a daemon. * * param do_close_stdio Only close stdio if set. */ -void start_daemon(int do_close_stdio) + +/* + * Start running as a daemon. + */ +void start_daemon(int do_close_stdio, char *pid_file) { - if (do_close_stdio) { - /* close(0); */ - close(1); - close(2); + int status = 0; + pid_t child = 0; + FILE *fp; + int do_restart = 0; + + current_child = 0; + + /* Close stdin/stdout/stderr and replace them with /dev/null. + * We don't just call close() because we don't want these fd's + * to be reused for other files. + */ + chdir("/"); + + child = fork(); + if (child != 0) { + fp = fopen(pid_file, "w"); + if (fp != NULL) { + fprintf(fp, "%d\n", child); + fclose(fp); + } + exit(0); } + signal(SIGHUP, SIG_IGN); signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN); - if (fork() != 0) { - exit(0); + + setsid(); + umask(0); + if (do_close_stdio) { + freopen("/dev/null", "r", stdin); + freopen("/dev/null", "w", stdout); + freopen("/dev/null", "w", stderr); } + do { + current_child = fork(); + + signal(SIGTERM, graceful_shutdown); + + if (current_child < 0) { + perror("fork"); + exit(errno); + } + + else if (current_child == 0) { + return; /* continue starting citadel. */ + } + + else { + waitpid(current_child, &status, 0); + } + + do_restart = 0; + + /* Did the main process exit with an actual exit code? */ + if (WIFEXITED(status)) { + + /* Exit code 0 means the watcher should exit */ + if (WEXITSTATUS(status) == 0) { + do_restart = 0; + } + + /* Exit code 101-109 means the watcher should exit */ + else if ( (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109) ) { + do_restart = 0; + } + + /* Any other exit code means we should restart. */ + else { + do_restart = 1; + } + } + + /* Any other type of termination (signals, etc.) should also restart. */ + else { + do_restart = 1; + } + + } while (do_restart); + + unlink(pid_file); + exit(WEXITSTATUS(status)); } /** @@ -521,6 +606,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 *hdir; const char *basedir; #ifdef ENABLE_NLS @@ -533,9 +619,9 @@ int main(int argc, char **argv) /** Parse command line */ #ifdef HAVE_OPENSSL - while ((a = getopt(argc, argv, "h:i:p:t:x:dcfs")) != EOF) + while ((a = getopt(argc, argv, "h:i:p:t:x:d:cfs")) != EOF) #else - while ((a = getopt(argc, argv, "h:i:p:t:x:dcf")) != EOF) + while ((a = getopt(argc, argv, "h:i:p:t:x:d:cf")) != EOF) #endif switch (a) { case 'h': @@ -551,6 +637,8 @@ 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 'i': @@ -612,7 +700,7 @@ int main(int argc, char **argv) /* daemonize, if we were asked to */ if (running_as_daemon) { - start_daemon(0); + start_daemon(0, pidfile); } /** Tell 'em who's in da house */ -- 2.30.2