From sp@rvrng.ra.alkor.ru  Wed Oct 29 12:32:16 2003
Return-Path: <sp@rvrng.ra.alkor.ru>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 71BE616A4CE
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 29 Oct 2003 12:32:16 -0800 (PST)
Received: from rvrng.alkor.ru (rvrng.ra.alkor.ru [194.186.122.164])
	by mx1.FreeBSD.org (Postfix) with ESMTP id D2E6643FEC
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 29 Oct 2003 12:32:14 -0800 (PST)
	(envelope-from sp@rvrng.ra.alkor.ru)
Received: from rvrng.alkor.ru (localhost [127.0.0.1])
	by rvrng.alkor.ru (8.12.9p2/8.12.9) with ESMTP id h9TMPf7p008991
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 30 Oct 2003 01:25:41 +0300 (MSK)
	(envelope-from sp@rvrng.ra.alkor.ru)
Received: (from root@localhost)
	by rvrng.alkor.ru (8.12.9p2/8.12.9/Submit) id h9TMPf1K008990;
	Thu, 30 Oct 2003 01:25:41 +0300 (MSK)
	(envelope-from sp)
Message-Id: <200310292225.h9TMPf1K008990@rvrng.alkor.ru>
Date: Thu, 30 Oct 2003 01:25:41 +0300 (MSK)
From: sp@alkor.ru
Reply-To: sp@alkor.ru
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: /sbin/natd feature request & possible patch
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         58696
>Category:       bin
>Synopsis:       [patch] [request] natd(8): add possibility to alter pidfile location and shutdown timeout
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Wed Oct 29 12:40:10 PST 2003
>Closed-Date:    Sun Jun 22 22:16:43 UTC 2008
>Last-Modified:  Sun Jun 22 22:20:01 UTC 2008
>Originator:     
>Release:        FreeBSD 4.9-STABLE i386
>Organization:
>Environment:
System: FreeBSD rvrng.alkor.ru 4.9-STABLE FreeBSD 4.9-STABLE #1: Mon Oct 27 16:39:26 MSK 2003 root@:/usr/obj/usr/src/sys/RVRNG i386


>Description:
	Add possibility to alter pidfile location and shutdown timeout
>How-To-Repeat:
	When running multiple nat daemons all they are writing
	pid file in the same place.

	After receiving termination signal (SIGTERM or like) natd
	always sleeps 10 seconds before exit. It's not convenient in
	some environments.

	The patch attached adds to options -- one to set alternate
	pid file, and other sets shutdown timeout in milliseconds.
>Fix:

--- natd.c.orig	Thu Oct 30 00:56:31 2003
+++ natd.c	Thu Oct 30 01:07:43 2003
@@ -44,6 +44,8 @@
 #include <syslog.h>
 #include <unistd.h>
 
+#include <sys/param.h>
+
 #include "natd.h"
 
 /* 
@@ -123,6 +125,9 @@
 static	int			logFacility;
 static	int			logIpfwDenied;
 
+static  int                     exitTimeout;
+static  char                    pidPath[MAXPATHLEN];
+
 int main (int argc, char** argv)
 {
 	int			divertIn;
@@ -156,6 +161,8 @@
  	logDropped		= 0;
  	logFacility		= LOG_DAEMON;
 	logIpfwDenied		= -1;
+	strcpy(pidPath, PIDFILE);
+	exitTimeout		= EXIT_TIMEOUT;
 
 	ParseArgs (argc, argv);
 /*
@@ -380,7 +387,7 @@
 	}
 
 	if (background)
-		unlink (PIDFILE);
+		unlink (pidPath);
 
 	return 0;
 }
@@ -392,7 +399,7 @@
 	daemon (0, 0);
 	background = 1;
 
-	pidFile = fopen (PIDFILE, "w");
+	pidFile = fopen (pidPath, "w");
 	if (pidFile) {
 
 		fprintf (pidFile, "%d\n", getpid ());
@@ -805,7 +812,10 @@
  */
 	siginterrupt(SIGALRM, 1);
 	signal (SIGALRM, Shutdown);
-	alarm (10);
+	if(exitTimeout)
+		ualarm(1000*exitTimeout, 100);
+	else
+		ualarm(100, 100);
 }
 
 static void Shutdown (int sig)
@@ -836,7 +846,9 @@
  	LogDenied,
  	LogFacility,
 	PunchFW,
-	LogIpfwDenied
+	LogIpfwDenied,
+	pidPathOption,
+	exitTimeoutOption
 };
 
 enum Param {
@@ -1063,6 +1075,21 @@
 		"log packets converted by natd, but denied by ipfw",
 		"log_ipfw_denied",
 		NULL },
+	{ pidPathOption,
+		0,
+		String,
+		"file_path",
+		"store pid in specified file",
+		"pid_path",
+		"P" },
+	{ exitTimeoutOption,
+		0,
+		Numeric,
+		"milliseconds",
+		"timeout before exiting on signal in milliseconds",
+		"exit_timeout",
+		"X" },
+
 };
 	
 static void ParseOption (const char* option, const char* parms)
@@ -1249,6 +1276,19 @@
 
 	case LogIpfwDenied:
 		logIpfwDenied = yesNoValue;;
+		break;
+
+	case exitTimeoutOption:
+		if((exitTimeout = uNumValue) > MAX_EXIT_TIMEOUT)
+			err(1, "Exit timeout too large");
+		break;
+
+	case pidPathOption:
+		/* Need checking ? */
+		if(!*strValue)
+			errx(1, "Empty pid path");
+		if(realpath(strValue, pidPath) == 0)
+			err(1, "Invalid pid path");
 		break;
 	}
 }
--- natd.h.orig	Thu Oct 30 00:56:37 2003
+++ natd.h	Thu Oct 30 00:59:41 2003
@@ -13,6 +13,10 @@
  */
 
 #define PIDFILE	"/var/run/natd.pid"
+
+#define EXIT_TIMEOUT		10000
+#define MAX_EXIT_TIMEOUT	99999
+
 #define	INPUT		1
 #define	OUTPUT		2
 #define	DONT_KNOW	3


>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: mav 
State-Changed-When: Sun Jun 22 22:15:06 UTC 2008 
State-Changed-Why:  
Part about PID file was implemented before, part about delay 
has just gone to HEAD. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=58696 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/58696: commit references a PR
Date: Sun, 22 Jun 2008 22:14:17 +0000 (UTC)

 mav         2008-06-22 22:14:02 UTC
 
   FreeBSD src repository
 
   Modified files:
     sbin/natd            natd.8 natd.c natd.h 
   Log:
   SVN rev 179937 on 2008-06-22 22:14:02Z by mav
   
   Add exit_delay parameter to control daemon exit delay after signal.
   
   PR:             bin/58696
   Submitted by:   sp@alkor.ru
   
   Revision  Changes    Path
   1.69      +6 -1      src/sbin/natd/natd.8
   1.53      +22 -3     src/sbin/natd/natd.c
   1.6       +3 -0      src/sbin/natd/natd.h
 _______________________________________________
 cvs-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/cvs-all
 To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
 
>Unformatted:
