[Cherokee-commits] rev 912 - in cherokee/trunk
cherokee at cherokee-project.com
cherokee at cherokee-project.com
Fri Sep 7 21:43:35 CEST 2007
Author: alo
Date: 2007-09-07 21:43:34 +0200 (Fri, 07 Sep 2007)
New Revision: 912
Log:
Get the changeset with:
svn diff -r 911:912 svn://svn.cherokee-project.com/
Added:
cherokee/trunk/cherokee/main_guardian.c
Modified:
cherokee/trunk/ChangeLog
5 +
cherokee/trunk/cherokee/Makefile.am
9 +
cherokee/trunk/cherokee/main.c
8 -
Modified: cherokee/trunk/ChangeLog
===================================================================
--- cherokee/trunk/ChangeLog 2007-09-07 14:19:38 UTC (rev 911)
+++ cherokee/trunk/ChangeLog 2007-09-07 19:43:34 UTC (rev 912)
@@ -1,5 +1,10 @@
2007-09-07 Alvaro Lopez Ortega <alvaro at alobbs.com>
+ * cherokee/main_guardian.c, cherokee/Makefile.am: The Cherokee
+ guardian is a new binary that takes care of launching the server
+ and looking after it. If the server dies, the guardian will launch
+ a new instance of the server.
+
* cherokee/connection.c (cherokee_connection_build_header),
cherokee/handler_proxy.c (cherokee_handler_proxy_new),
cherokee/handler_mirror.c (cherokee_handler_mirror_new),
Modified: cherokee/trunk/cherokee/Makefile.am
===================================================================
--- cherokee/trunk/cherokee/Makefile.am 2007-09-07 14:19:38 UTC (rev 911)
+++ cherokee/trunk/cherokee/Makefile.am 2007-09-07 19:43:34 UTC (rev 912)
@@ -9,6 +9,7 @@
-DCHEROKEE_DEPSDIR=\""$(cherokeedepsdir)"\" \
-DCHEROKEE_DATADIR=\""$(cherokeedatadir)"\" \
-DCHEROKEE_CONFDIR=\""$(sysconfdir)/cherokee"\" \
+-DCHEROKEE_SRV_PATH=\""$(prefix)/sbin/cherokee"\" \
-DCHEROKEE_ICONSDIR=\""$(prefix)/share/cherokee/icons"\" \
-DCHEROKEE_THEMEDIR=\""$(prefix)/share/cherokee/themes"\"
@@ -978,7 +979,7 @@
#
# Main binary: Cherokee server
#
-sbin_PROGRAMS = cherokee cherokee-admin
+sbin_PROGRAMS = cherokee cherokee-guardian cherokee-admin
cherokee_SOURCES = main.c
cherokee_LDADD = \
@@ -989,12 +990,18 @@
cherokee_LDFLAGS = -export-dynamic
#
+# Cherokee guardians
+#
+cherokee_guardian_SOURCES = main_guardian.c
+
+#
# Cherokee admin
#
cherokee_admin_SOURCES = main_admin.c
cherokee_admin_LDADD = $(cherokee_LDADD)
cherokee_admin_LDFLAGS = $(cherokee_LDFLAGS)
+
#
# Log rotate utility
#
Modified: cherokee/trunk/cherokee/main.c
===================================================================
--- cherokee/trunk/cherokee/main.c 2007-09-07 14:19:38 UTC (rev 911)
+++ cherokee/trunk/cherokee/main.c 2007-09-07 19:43:34 UTC (rev 912)
@@ -32,8 +32,8 @@
#define DEFAULT_CONFIG_FILE "/etc/cherokee/cherokee.conf"
-# define GETOPT_OPT "C:r:bhv"
-# define CONFIG_FILE_HELP "[-C configfile] [-r]"
+# define GETOPT_OPT "C:r:bhvg"
+# define CONFIG_FILE_HELP "[-C configfile] [-r] [-g]"
#define BASIC_CONFIG \
"vserver!default!directory!/!handler = common\n" \
@@ -152,20 +152,16 @@
case 'C':
config_file = strdup(optarg);
break;
-
case 'b':
daemon_mode = true;
break;
-
case 'r':
document_root = strdup(optarg);
break;
-
case 'v':
fprintf (stdout, "%s\n", PACKAGE_STRING);
exit(1);
break;
-
case 'h':
default:
fprintf (stderr, "Usage: %s " CONFIG_FILE_HELP "[-b] -h -v\n", argv[0]);
Added: cherokee/trunk/cherokee/main_guardian.c
===================================================================
--- cherokee/trunk/cherokee/main_guardian.c (rev 0)
+++ cherokee/trunk/cherokee/main_guardian.c 2007-09-07 19:43:34 UTC (rev 912)
@@ -0,0 +1,151 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+/* Cherokee
+ *
+ * Authors:
+ * Alvaro Lopez Ortega <alvaro at alobbs.com>
+ *
+ * Copyright (C) 2001-2007 Alvaro Lopez Ortega
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ */
+
+#include "common-internal.h"
+#include <signal.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include <errno.h>
+#include "server.h"
+
+#ifdef HAVE_GETOPT_H
+# include <getopt.h>
+#endif
+
+#define ERROR_DELAY 3000 * 1000
+#define RESTARTING_DELAY 500 * 1000
+
+
+static cherokee_boolean_t exit_guardian = false;
+static pid_t pid;
+
+
+static void
+guardian_signals_handler (int sig, siginfo_t *si, void *context)
+{
+ int exitcode;
+
+ switch (sig) {
+ case SIGHUP:
+ /* Restart Cherokee */
+ kill (pid, SIGINT);
+ break;
+
+ case SIGCHLD:
+ /* Child exited */
+ wait (&exitcode);
+ break;
+ default:
+ /* Forward the signal */
+ kill (pid, sig);
+ }
+}
+
+
+static void
+set_guardian_signals (void)
+{
+ struct sigaction act;
+
+ /* Signals it handles
+ */
+ memset(&act, 0, sizeof(act));
+
+ act.sa_handler = SIG_IGN;
+ sigaction (SIGPIPE, &act, NULL);
+ sigaction (SIGUSR1, &act, NULL);
+
+ /* Signals it handles
+ */
+ act.sa_sigaction = guardian_signals_handler;
+ sigemptyset (&act.sa_mask);
+ act.sa_flags = SA_SIGINFO;
+
+ sigaction (SIGHUP, &act, NULL);
+ sigaction (SIGSEGV, &act, NULL);
+ sigaction (SIGTERM, &act, NULL);
+}
+
+static pid_t
+process_launch (const char *path, int argc, char *argv[])
+{
+ pid_t pid;
+
+ pid = fork();
+ if (pid == 0) {
+ argv[0] = CHEROKEE_SRV_PATH;
+ execvp (CHEROKEE_SRV_PATH, argv);
+ exit (1);
+ }
+
+ return pid;
+}
+
+static ret_t
+process_wait (pid_t pid)
+{
+ pid_t re;
+ int exitcode = 0;
+
+ re = waitpid (pid, &exitcode, 0);
+ if (re == -1)
+ return ret_error;
+
+ if (WIFEXITED(exitcode)) {
+ int re = WEXITSTATUS(exitcode);
+
+ /* Child terminated normally */
+ PRINT_MSG ("Server PID=%d exited re=%d\n", pid, re);
+ if (re != 0)
+ return ret_error;
+ }
+ else if (WIFSIGNALED(exitcode)) {
+ /* Child process terminated by a signal */
+ PRINT_MSG ("Server PID=%d received a signal=%d\n", pid, WTERMSIG(exitcode));
+ }
+
+ return ret_ok;
+}
+
+
+int
+main (int argc, char *argv[])
+{
+ ret_t ret;
+
+ set_guardian_signals();
+
+ while (! exit_guardian) {
+ pid = process_launch (CHEROKEE_SRV_PATH, argc, argv);
+ if (pid < 0) {
+ PRINT_MSG ("Couldn't launch '%s'\n", CHEROKEE_SRV_PATH);
+ exit (1);
+ }
+
+ ret = process_wait (pid);
+ usleep ((ret == ret_ok) ? RESTARTING_DELAY : ERROR_DELAY);
+ }
+
+ return 0;
+}
More information about the Cherokee-commits
mailing list