From nobody@www.freebsd.org  Wed Jun 12 12:59:14 2002
Return-Path: <nobody@www.freebsd.org>
Received: from nwww.freebsd.org (www.FreeBSD.org [216.136.204.117])
	by hub.freebsd.org (Postfix) with ESMTP id 9384437B403
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 12 Jun 2002 12:59:13 -0700 (PDT)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by nwww.freebsd.org (8.12.2/8.12.2) with ESMTP id g5CJxDhG042876
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 12 Jun 2002 12:59:13 -0700 (PDT)
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.12.2/8.12.2/Submit) id g5CJxDw7042875;
	Wed, 12 Jun 2002 12:59:13 -0700 (PDT)
Message-Id: <200206121959.g5CJxDw7042875@www.freebsd.org>
Date: Wed, 12 Jun 2002 12:59:13 -0700 (PDT)
From: Peter Edwards <pmedwards@eircom.net>
To: freebsd-gnats-submit@FreeBSD.org
Subject: ptrace(2) and rfork(RFLINUXTHPN) confuse wait()
X-Send-Pr-Version: www-1.0

>Number:         39201
>Category:       kern
>Synopsis:       [linux] [patch] ptrace(2) and rfork(RFLINUXTHPN) confuse wait()
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-emulation
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Jun 12 13:00:09 PDT 2002
>Closed-Date:    
>Last-Modified:  Mon May 01 11:55:03 GMT 2006
>Originator:     Peter Edwards
>Release:        4.6-RC
>Organization:
>Environment:
FreeBSD rocklobster 4.6-RC FreeBSD 4.6-RC #5: Wed Jun 12 20:34:22 IST 2002     petere@rocklobster:/a/archie/host/pub/FreeBSD/stable/src/sys/compile/ROCKLOBSTER  i386

>Description:
As reported on hackers@ and met with a hail if silence :-)

http://docs.freebsd.org/cgi/getmsg.cgi?fetch=80369+0+current/freebsd-hackers

kern_exit.c:wait1() has the following lines in -STABLE:
>  if ((p->p_sigparent != SIGCHLD) ^ ((uap->options & WLINUXCLONE)!= 0))
>          continue;

As it is, if you ptrace(PT_ATTACH) to a process started with
rfork(flags|RFLINUXTHPN), and do a waitpid() as you normally would, 
this causes waitpid() to fail with ECHILD, because the original
parent/child relationship doesn't hold, and the debugger doesn't
know that the debugee was started with RFLINUXTHPN. This can also
mean that the ptrace(PT_DETACH) ends up killing the process,
because you can't guarantee that it is stopped by the time you
get to do the ptrace(PT_DETACH). In order to allow existing
ptrace(2)-using programs to attach to such processes, would the
following be more appropriate?

> if ((p->p_sigparent != SIGCHILD && (p->p_flag & P_TRACED) == 0) ^
>     ((uap->options & WLINUXCLONE) != 0))

(BTW: Why "^" rather than "!=" ? I would have thought a boolean
operator more natural here.)

Cheers,
Peter.
>How-To-Repeat:
Try attaching to a process started with rfork(RFLINUXTHPN) with gdb.

>Fix:
This fixes it locally.

RCS file: /pub/FreeBSD/development/FreeBSD-CVS/src/sys/kern/kern_exit.c,v
retrieving revision 1.92.2.10
diff -u -r1.92.2.10 kern_exit.c
--- kern_exit.c 29 Apr 2002 09:42:35 -0000      1.92.2.10
+++ kern_exit.c 12 Jun 2002 19:27:23 -0000
@@ -438,7 +438,8 @@
                 * and the WLINUXCLONE option signifies we want to wait for threads
                 * and not processes.
                 */
-               if ((p->p_sigparent != SIGCHLD) ^ ((uap->options & WLINUXCLONE) != 0))
+               if ((p->p_sigparent != SIGCHLD && (p->p_flag & P_TRACED) == 0) ^
+                           ((uap->options & WLINUXCLONE) != 0))
                        continue;
 
                nfound++;


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->emulation 
Responsible-Changed-By: bms 
Responsible-Changed-When: Fri Jun 18 12:10:47 GMT 2004 
Responsible-Changed-Why:  
One for the emulation team 

http://www.freebsd.org/cgi/query-pr.cgi?pr=39201 
State-Changed-From-To: open->feedback 
State-Changed-By: netchild 
State-Changed-When: Thu Dec 29 16:02:59 UTC 2005 
State-Changed-Why:  
Is this still a problem with a recent FreeBSD? 

http://www.freebsd.org/cgi/query-pr.cgi?pr=39201 
State-Changed-From-To: feedback->open 
State-Changed-By: netchild 
State-Changed-When: Mon May 1 11:54:39 UTC 2006 
State-Changed-Why:  
Back to "open", maybe an upcomming SoC student is willing to have a look 
at this. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=39201 
>Unformatted:
