From sdalu@loria.fr  Fri Jul 13 06:57:30 2001
Return-Path: <sdalu@loria.fr>
Received: from lorraine.loria.fr (lorraine.loria.fr [152.81.1.17])
	by hub.freebsd.org (Postfix) with ESMTP id 6CB0A37B401
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 13 Jul 2001 06:57:29 -0700 (PDT)
	(envelope-from sdalu@loria.fr)
Received: from hyperion.loria.fr (hyperion.loria.fr [152.81.7.193])
	by lorraine.loria.fr (8.9.3/8.9.3/8.9.3/JCG-DG) with ESMTP id PAA27905
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 13 Jul 2001 15:57:28 +0200 (MET DST)
Received: (from sdalu@localhost)
	by hyperion.loria.fr (8.11.1/8.11.1) id f6DDv4d71477;
	Fri, 13 Jul 2001 15:57:04 +0200 (CEST)
	(envelope-from sdalu)
Message-Id: <200107131357.f6DDv4d71477@hyperion.loria.fr>
Date: Fri, 13 Jul 2001 15:57:04 +0200 (CEST)
From: sdalu@loria.fr
Reply-To: sdalu@loria.fr
To: FreeBSD-gnats-submit@freebsd.org
Subject: dup2 closing pthread file descriptor
X-Send-Pr-Version: 3.2

>Number:         28947
>Category:       misc
>Synopsis:       dup2 closing pthread file descriptor
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    maxim
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Jul 13 07:00:01 PDT 2001
>Closed-Date:    Thu Feb 21 03:27:46 PST 2002
>Last-Modified:  Thu Feb 21 03:28:24 PST 2002
>Originator:     Stephane D'Alu
>Release:        FreeBSD 4.2-RELEASE i386
>Organization:
>Environment:

	

>Description:

when using pthread 2 file descriptors are used for pipes (see lsof output),
these file descriptors can't be closed by close(), but dup2() manage to 
duplicate other fd at these positions (fcntl/F_DUPFD cannot)

the real problem occurs when doing an exec, when presumably the
pthread is doing some cleanup and closes its files descriptor,
unfortunatelly these descriptors are not its own anymore but the
copy done but dup2.

would have expected dup2() to fail
(the problem is perhaps more with the pthread librairy than the kernel)



>How-To-Repeat:

-- a ------------------------
#include <pthread.h>

int main() {
    if (close(3) < 0)
	perror("close");
    if (dup2(0, 3) < 0) 
	perror("dup2");
}
-----------------------------

gcc -o a a.c -pthread

a         71332 sdalu    0u  VCHR          5,14     0t4766    8311 /dev/ttype
a         71332 sdalu    1u  VCHR          5,14     0t4766    8311 /dev/ttype
a         71332 sdalu    2u  VCHR          5,14     0t4766    8311 /dev/ttype
a         71332 sdalu    3u  PIPE    0xd734f260      16384         ->0xd734f300
a         71332 sdalu    4u  PIPE    0xd734f300      16384         ->0xd734f260

./a
close: Bad file descriptor


>Fix:



>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->jasone 
Responsible-Changed-By: jasone 
Responsible-Changed-When: Mon Dec 17 12:41:01 PST 2001 
Responsible-Changed-Why:  
I'll look at this. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=28947 

From: Maxim Konovalov <maxim@macomnet.ru>
To: "Stephane D'Alu" <sdalu@loria.fr>
Cc: jasone@freebsd.org, <freebsd-gnats-submit@freebsd.org>
Subject: Re: kern/28947: dup2 closing pthread file descriptor
Date: Wed, 13 Feb 2002 21:22:34 +0300 (MSK)

 Could you please try the patch below:
 
 Index: uthread_dup2.c
 ===================================================================
 RCS file: /home/ncvs/src/lib/libc_r/uthread/uthread_dup2.c,v
 retrieving revision 1.11
 diff -u -r1.11 uthread_dup2.c
 --- uthread_dup2.c	10 Apr 2001 04:19:20 -0000	1.11
 +++ uthread_dup2.c	13 Feb 2002 18:12:40 -0000
 @@ -45,7 +45,8 @@
  	int		newfd_opened;
 
  	/* Check if the file descriptor is out of range: */
 -	if (newfd < 0 || newfd >= _thread_dtablesize) {
 +	if (newfd < 0 || newfd >= _thread_dtablesize ||
 +	    newfd == _thread_kern_pipe[0] || newfd == _thread_kern_pipe[1]) {
  		/* Return a bad file descriptor error: */
  		errno = EBADF;
  		ret = -1;
 
 -- 
 Maxim Konovalov, MAcomnet, Internet-Intranet Dept., system engineer
 phone: +7 (095) 796-9079, mailto:maxim@macomnet.ru
 

From: "Stephane D'Alu" <sdalu@loria.fr>
To: Maxim Konovalov <maxim@macomnet.ru>
Cc: jasone@freebsd.org, freebsd-gnats-submit@freebsd.org
Subject: Re: kern/28947: dup2 closing pthread file descriptor
Date: Wed, 13 Feb 2002 23:43:49 +0100

 On Wed, Feb 13, 2002 at 09:22:34PM +0300, Maxim Konovalov wrote:
 > 
 > Could you please try the patch below:
 
 Tested on FreeBSD 4.4-RELEASE i386.
 
 Works fine.
 
 Would it be reasonable to say that file descriptors internally used
 by the thread library should not be visible to programmer, and
 move them to other places when there is a clash with legitimate 
 "syscall" requests like dup2? Not sure the effort is worth it anyway.
 
 > 
 > Index: uthread_dup2.c
 > ===================================================================
 > RCS file: /home/ncvs/src/lib/libc_r/uthread/uthread_dup2.c,v
 > retrieving revision 1.11
 > diff -u -r1.11 uthread_dup2.c
 > --- uthread_dup2.c	10 Apr 2001 04:19:20 -0000	1.11
 > +++ uthread_dup2.c	13 Feb 2002 18:12:40 -0000
 > @@ -45,7 +45,8 @@
 >  	int		newfd_opened;
 > 
 >  	/* Check if the file descriptor is out of range: */
 > -	if (newfd < 0 || newfd >= _thread_dtablesize) {
 > +	if (newfd < 0 || newfd >= _thread_dtablesize ||
 > +	    newfd == _thread_kern_pipe[0] || newfd == _thread_kern_pipe[1]) {
 >  		/* Return a bad file descriptor error: */
 >  		errno = EBADF;
 >  		ret = -1;
 > 
 > -- 
 > Maxim Konovalov, MAcomnet, Internet-Intranet Dept., system engineer
 > phone: +7 (095) 796-9079, mailto:maxim@macomnet.ru
 
 -- 
 Stephane D'Alu
Responsible-Changed-From-To: jasone->maxim 
Responsible-Changed-By: maxim 
Responsible-Changed-When: Thu Feb 14 07:22:08 PST 2002 
Responsible-Changed-Why:  
I have a patch for -current. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=28947 
State-Changed-From-To: open->analyzed 
State-Changed-By: maxim 
State-Changed-When: Thu Feb 14 07:27:20 PST 2002 
State-Changed-Why:  
The fix is committed to -current. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=28947 
State-Changed-From-To: analyzed->closed 
State-Changed-By: maxim 
State-Changed-When: Thu Feb 21 03:27:46 PST 2002 
State-Changed-Why:  
The fix committed to -current and -stable. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=28947 
>Unformatted:
