Patch to run_kannel_box to log to syslog. 

On Wed, 2003-07-09 at 12:54, Andreas Fink wrote:
> On Dienstag, Juli 8, 2003, at 11:13  Uhr, Alan McNatty wrote:
> 
>         Hello,
>         
>         run_kannel_box is a strange beast (at least to me). It's
>         purpose seems
>         unclear (or more appropriately 'undocumented'). I understand
>         it's part
>         of the utils directory (out of main source) but it is part of
>         standard
>         build (eg: debian packages). 
>         
>         Andres could you explain a bit about what it's function is ..
>         the man
>         page is brief. I have yet to review source code in detail -
>         but if it's
>         supposed to restarting failed services I would be interested
>         in doing
>         some work/testing with it (potentially adding syslog support
>         to begin
>         with).  
> 
> run_kannel_box starts up a process (either bearerbox, webbox orsmsbox)
> and make's sure that if it fails, it gets restartet.
> 
> Here's how I start kannel on my computer from
> /etc/rc.d/init.d/kannel.I let the processes run as user "kannel" at
> the same time:
> 
> 
> RUN=/var/run/kannel
> BIN=/usr/local/sbin
> DELAY="1"
> CONF="/etc/kannel.conf"
> 
> RUNBOX="${BIN}/run_kannel_box"
> BEARERBOX="${BIN}/bearerbox"
> SMSBOX="${BIN}/smsbox"
> WAPBOX="${BIN}/wapbox"
> 
> PID_SMSBOX="$RUN/smsbox.pid"
> PID_BEARERBOX="$RUN/bearerbox.pid"
> PID_WAPBOX="$RUN/wapbox.pid"
> 
> echo "Starting bearerbox"
> su -c "${RUNBOX} --pidfile ${PID_BEARERBOX} --min-delay
> ${DELAY}${BEARERBOX} ${CONF} &" kannel
> sleep 10
> 
> echo "Starting smsbox"
> su -c "${RUNBOX} --pidfile ${PID_SMSBOX} --min-delay ${DELAY}${SMSBOX}
> ${CONF} &" kannel
> sleep 10
> 
> echo "Starting wapbox"
> su -c "${RUNBOX} --pidfile ${PID_WAPBOX} --min-delay ${DELAY}${WAPBOX}
> ${CONF} &" kannel
> 
> 
> if you start a kannel box process with run_kannel_box it will keep
> theprocess restarting if it fails.
> Try it yourself and try to kill any of the box processes, and you
> willsee they restart. pretty much like safe_mysqld in mysql.
> to kill the processes, I kill the process of the PID file above
> whichis the corresponding run_kannel_box process.
> 
> 
> 
> 
> 
> Andreas Fink
> Global Networks Switzerland AG
> 
> ------------------------------------------------------------------
> Tel: +41-61-6666333  Fax: +41-61-6666334   Mobile: +41-79-2457333
> Global Networks, Inc. Clarastrasse 3, 4058 Basel, Switzerland
> Web: http://www.global-networks.ch/      [EMAIL PROTECTED]
> ------------------------------------------------------------------
-- 
Alan McNatty <[EMAIL PROTECTED]>
Index: utils/run_kannel_box.c
===================================================================
RCS file: /home/cvs/gateway/utils/run_kannel_box.c,v
retrieving revision 1.7
diff -u -r1.7 run_kannel_box.c
--- utils/run_kannel_box.c	26 Mar 2001 18:48:37 -0000	1.7
+++ utils/run_kannel_box.c	29 Jul 2003 01:14:54 -0000
@@ -1,3 +1,14 @@
+/*
+ *  run_kannel_box.c - Kannel box wrapper
+ *
+ *  run_kannel_box starts up a process (either bearerbox, wapbox 
+ *  or smsbox) and make's sure that if it fails, it gets restarted.
+ *  
+ *  Logs to syslog (if possible) with INFO message if a restart
+ *  is required. Logs a ERR message if restart fails.
+ *
+ */
+
 #include <stdio.h>
 #include <stddef.h>
 #include <stdlib.h>
@@ -14,6 +25,24 @@
 #include <fcntl.h>
 #include <signal.h>
 
+/* require config to get HAVE_SYSLOG_H */
+#include "config.h"
+
+#if HAVE_SYSLOG_H
+#include <syslog.h>
+#else
+                                                                                
+/*
+ * If we don't have syslog.h, then we'll use the following dummy definitions
+ * to avoid writing #if HAVE_SYSLOG_H everywhere.
+ */
+                                                                                
+enum { LOG_PID, LOG_DAEMON, LOG_ERR, LOG_INFO };
+static void openlog(const char *ident, int option, int facility) { }
+static void syslog(int translog, const char *buf) { }
+                                                                                
+#endif
+
 static char *progname;  /* The name of this program (for error messages) */
 static char **box_arglist;
 static int min_restart_delay = 60; /* in seconds */
@@ -205,11 +234,18 @@
  * every time it dies. */
 static int main_loop(char *boxfile)
 {
+	/** A few variables for syslog-ing */
+	int size = 100; /* 100 bytes of log should be fine */
+	char message[size];
+
+	/** Open syslog for internal logging */
+	openlog(progname, LOG_PID, LOG_DAEMON);
+
 	time_t next_fork = 0;
 
-	/* We can't report any errors here, because we are running
-	 * as a daemon and we have no logfile of our own.  So we
-	 * exit with errno as the exit code, to offer a minimal clue. */
+	/* We are running as a daemon and we have no logfile of our 
+	 * own. So we log to syslog if we can and exit with errno 
+	 * as the exit code, to offer a minimal clue. */
 
 	for (;;) {
 
@@ -224,10 +260,18 @@
 
 		child_box = fork();
 		if (child_box < 0) {
+			snprintf(message, size, 
+					"[ %s ] failed to fork child process - quitting.", 
+					boxfile);
+			syslog(LOG_ERR, message);
 			return errno;
 		}
 		if (child_box == 0) {
 			/* child.  exec the box */
+			snprintf(message, size, 
+					"[ %s ] not running, restarting.", 
+					boxfile);
+			syslog(LOG_INFO, message);
 			execvp(boxfile, box_arglist);
 			exit(127);
 		}
@@ -237,12 +281,20 @@
 				/* Something went wrong... we don't know what,
 				 * but we do know that our child does not
 				 * exist.  So restart it. */
+			        snprintf(message, size, 
+						"[ %s ] restart failed (child process does not exist)", 
+						boxfile);
+			        syslog(LOG_ERR, message);
 				break;
 			}
 			if (errno == EINTR) {
 				continue;
 			}
 			/* Something weird happened. */
+			snprintf(message, size, 
+					 "[ %s ] failed to start child process, unknown error - quitting.", 
+					 boxfile);
+			syslog(LOG_ERR, message);
 			return errno;
 		}
 	}

Reply via email to