patch to newsyslog: run command instead of to sent signal

From: Denis Shaposhnikov <dsh_at_vlink.ru>
Date: Mon, 19 Jun 2006 17:12:58 +0400
Hi all!

What do you think about this patch? The patch adds new flag "R" to
newsyslog.conf which means execute programm specified in
path_to_pid_file field after rotating instead of to send signal.

This behavior usefull for software like ejabberd/mysql/asterisk which
can't reload logfiles on signal. They use external programms for
it, like asterisk -rx 'logger reload'.

diff -Nru usr.sbin/newsyslog.orig/newsyslog.c usr.sbin/newsyslog/newsyslog.c
--- usr.sbin/newsyslog.orig/newsyslog.c	Mon Jan 23 17:02:31 2006
+++ usr.sbin/newsyslog/newsyslog.c	Mon Jun 19 15:43:34 2006
_at__at_ -111,6 +111,8 _at__at_
 				/*    process when trimming this file. */
 #define	CE_CREATE	0x0100	/* Create the log file if it does not exist. */
 #define	CE_NODUMP	0x0200	/* Set 'nodump' on newly created log file. */
+#define	CE_RUNCMD	0x0400	/* Execute program on rotate instead */
+				/*    of signal. */
 
 #define	MIN_PID         5	/* Don't touch pids lower than this */
 #define	MAX_PID		99999	/* was lower, see /usr/include/sys/proc.h */
_at__at_ -147,6 +149,7 _at__at_
 	pid_t	 sw_pid;		/* the process id from the PID file */
 	const char *sw_pidtype;		/* "daemon" or "process group" */
 	char	 sw_fname[1];		/* file the PID was read from */
+	int	 sw_runcmd;		/* true if we going to run prog. */
 };
 
 struct zipwork_entry {
_at__at_ -1348,6 +1351,9 _at__at_
 			case 'n':
 				working->flags |= CE_NOSIGNAL;
 				break;
+			case 'r':
+				working->flags |= CE_RUNCMD;
+				break;
 			case 'u':
 				working->flags |= CE_SIGNALGROUP;
 				break;
_at__at_ -1702,7 +1708,7 _at__at_
 	struct sigwork_entry *nextsig;
 	int kres, secs;
 
-	if (!(swork->sw_pidok) || swork->sw_pid == 0)
+	if (!(swork->sw_pidok) || (swork->sw_pid == 0 && !swork->sw_runcmd))
 		return;			/* no work to do... */
 
 	/*
_at__at_ -1743,7 +1749,11 _at__at_
 		return;
 	}
 
-	kres = kill(swork->sw_pid, swork->sw_signum);
+	if (swork->sw_runcmd)
+		kres = system(swork->sw_fname);
+	else
+		kres = kill(swork->sw_pid, swork->sw_signum);
+
 	if (kres != 0) {
 		/*
 		 * Assume that "no such process" (ESRCH) is something
_at__at_ -1754,12 +1764,22 _at__at_
 		 */
 		if (errno != ESRCH)
 			swork->sw_pidok = 0;
-		warn("can't notify %s, pid %d", swork->sw_pidtype,
-		    (int)swork->sw_pid);
+		if (swork->sw_runcmd)
+			warn("can't notify %s by %s", swork->sw_pidtype,
+			     swork->sw_fname);
+		else
+		  	warn("can't notify %s, pid %d", swork->sw_pidtype,
+			     (int)swork->sw_pid);
 	} else {
-		if (verbose)
-			printf("Notified %s pid %d = %s\n", swork->sw_pidtype,
-			    (int)swork->sw_pid, swork->sw_fname);
+		if (verbose) {
+			if (swork->sw_runcmd)
+				printf("Notified %s by %s\n",
+				       swork->sw_pidtype, swork->sw_fname);
+			else
+				printf("Notified %s pid %d = %s\n",
+				       swork->sw_pidtype, (int)swork->sw_pid,
+				       swork->sw_fname);
+		}
 		if (secs > 0) {
 			if (verbose)
 				printf("Pause %d second(s) between signals\n",
_at__at_ -1956,6 +1976,12 _at__at_
 	swork->sw_pidok = 0;
 	swork->sw_pid = 0;
 	swork->sw_pidtype = "daemon";
+	swork->sw_runcmd = 0;
+	if (ent->flags & CE_RUNCMD) {
+		swork->sw_pidok = swork->sw_runcmd = 1;
+		return;
+	}
+
 	if (ent->flags & CE_SIGNALGROUP) {
 		/*
 		 * If we are expected to signal a process-group when
diff -Nru usr.sbin/newsyslog.orig/newsyslog.conf.5 usr.sbin/newsyslog/newsyslog.conf.5
--- usr.sbin/newsyslog.orig/newsyslog.conf.5	Fri Jan 28 01:41:06 2005
+++ usr.sbin/newsyslog/newsyslog.conf.5	Mon Jun 19 15:43:34 2006
_at__at_ -290,6 +290,11 _at__at_
 .It Cm N
 indicates that there is no process which needs to be signaled
 when this log file is rotated.
+.It Cm R
+indicates that the file specified by
+.Ar path_to_pid_file
+should be executed when this log file is rotated instead of to send
+signal.
 .It Cm U
 indicates that the file specified by
 .Ar path_to_pid_file

-- 
DSS5-RIPE DSS-RIPN 2:550/5068_at_fidonet 2:550/5069_at_fidonet
xmpp:dsh_at_vlink.ru mailto:dsh_at_vlink.ru http://neva.vlink.ru/~dsh/
Received on Mon Jun 19 2006 - 11:13:02 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:57 UTC