From sanewo@sanewo.ba2.so-net.or.jp  Sun Jun 15 07:08:24 1997
Received: from sanewo.ba2.so-net.or.jp (pppba24.pppp.ap.so-net.or.jp [210.132.186.36])
          by hub.freebsd.org (8.8.5/8.8.5) with ESMTP id HAA10885
          for <FreeBSD-gnats-submit@freebsd.org>; Sun, 15 Jun 1997 07:08:18 -0700 (PDT)
Received: (from sanewo@localhost) by sanewo.ba2.so-net.or.jp (8.8.5/8.7.3) id SAA07766; Sun, 15 Jun 1997 18:04:22 +0900 (JST)
Message-Id: <199706150904.SAA07766@sanewo.ba2.so-net.or.jp>
Date: Sun, 15 Jun 1997 18:04:22 +0900 (JST)
From: sanewo@ba2.so-net.or.jp
Reply-To: sanewo@ba2.so-net.or.jp
To: FreeBSD-gnats-submit@freebsd.org
Subject: ppp pidfile does not match actual pid of ppp
X-Send-Pr-Version: 3.2

>Number:         3874
>Category:       bin
>Synopsis:       ppp pidfile (/var/run/tunN.pid) does not match actual pid of ppp
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    brian
>State:          closed
>Quarter:
>Keywords:
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Jun 15 07:10:00 PDT 1997
>Closed-Date:    Mon Jun 16 06:24:11 PDT 1997
>Last-Modified:  Mon Jun 16 06:27:15 PDT 1997
>Originator:     Takanori Saneto
>Release:        FreeBSD 3.0-CURRENT i386
>Organization:
an individual
>Environment:

	FreeBSD 3.0-CURRENT as of Jun 12.

>Description:

	In auto or background mode, ppp's pid file (/var/run/tunN.pid)
	contains incorrect pid in it.

	Here is the scenario:
	Parent forks child and exits.
	Child creates pid file and then calls daemon(), which forks grandchild and then exits.
	Grandchild, which perfoms rest of the job, has different pid than that in pid file.

>How-To-Repeat:

	Invoke ppp in auto or background mode and see contents of /var/run/tunN.pid.

>Fix:
	
	Calling daemon() before creating pid file seems to fix this problem.
	I don't know if there are any side-effects by doing this.

Index: main.c
===================================================================
RCS file: /sd0/FreeBSD/cvs/src/usr.sbin/ppp/main.c,v
retrieving revision 1.61
diff -u -r1.61 main.c
--- main.c	1997-06-11 12:57:48+09	1.61
+++ main.c	1997-06-15 17:58:12+09
@@ -485,6 +485,18 @@
           close(BGFiledes[0]);
     }
 
+    VarTerm = 0;   /* We know it's currently stdin */
+
+#ifdef DOTTYINIT
+    if (mode & (MODE_DIRECT|MODE_DEDICATED)) { /* } */
+#else
+    if (mode & MODE_DIRECT) {
+#endif
+      chdir("/");  /* Be consistent with daemon() */
+      TtyInit();
+    } else
+      daemon(0,0);
+
     snprintf(pid_filename, sizeof (pid_filename), "%stun%d.pid",
              _PATH_VARRUN, tunno);
     (void)unlink(pid_filename);
@@ -509,18 +521,6 @@
 
     if (server >= 0)
 	LogPrintf(LogPHASE, "Listening at %d.\n", port);
-
-    VarTerm = 0;   /* We know it's currently stdin */
-
-#ifdef DOTTYINIT
-    if (mode & (MODE_DIRECT|MODE_DEDICATED)) { /* } */
-#else
-    if (mode & MODE_DIRECT) {
-#endif
-      chdir("/");  /* Be consistent with daemon() */
-      TtyInit();
-    } else
-      daemon(0,0);
   } else {
     TtyInit();
     TtyCommandMode(1);
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->brian 
Responsible-Changed-By: brian 
Responsible-Changed-When: Mon Jun 16 06:20:44 PDT 1997 
Responsible-Changed-Why:  
I'll fix it 
State-Changed-From-To: open->closed 
State-Changed-By: brian 
State-Changed-When: Mon Jun 16 06:24:11 PDT 1997 
State-Changed-Why:  
Changed daemon() call to setsid() + a pile of closes. 
>Unformatted:
