From simon@comsys.ntu-kpi.kiev.ua  Wed Sep 23 10:32:16 2009
Return-Path: <simon@comsys.ntu-kpi.kiev.ua>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id B3677106568F
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 23 Sep 2009 10:32:16 +0000 (UTC)
	(envelope-from simon@comsys.ntu-kpi.kiev.ua)
Received: from comsys.ntu-kpi.kiev.ua (comsys.ntu-kpi.kiev.ua [77.47.192.42])
	by mx1.freebsd.org (Postfix) with ESMTP id 3F0CA8FC13
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 23 Sep 2009 10:32:16 +0000 (UTC)
Received: from pm513-1.comsys.ntu-kpi.kiev.ua (pm513-1.comsys.ntu-kpi.kiev.ua [10.18.52.101])
	(authenticated bits=0)
	by comsys.ntu-kpi.kiev.ua (8.13.7/8.13.7) with ESMTP id n8NAXZej020833
	(version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO)
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 23 Sep 2009 13:33:35 +0300 (EEST)
Received: by pm513-1.comsys.ntu-kpi.kiev.ua (Postfix, from userid 1001)
	id 4757E1CC38; Wed, 23 Sep 2009 13:32:14 +0300 (EEST)
Message-Id: <20090923103214.GA1306@pm513-1.comsys.ntu-kpi.kiev.ua>
Date: Wed, 23 Sep 2009 13:32:14 +0300
From: Andrey Simonenko <simon@comsys.ntu-kpi.kiev.ua>
To: FreeBSD-gnats-submit@freebsd.org
Subject: [libc] [patch] closelog() can close negative file descriptor

>Number:         139080
>Category:       kern
>Synopsis:       [libc] [patch] closelog() can close negative file descriptor
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    eadler
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Sep 23 10:40:01 UTC 2009
>Closed-Date:    Wed Jun 27 06:44:21 UTC 2012
>Last-Modified:  Wed Jun 27 06:55:08 UTC 2012
>Originator:     Andrey Simonenko
>Release:        FreeBSD 8.0-RC1 amd64
>Organization:
>Environment:
>Description:

The closelog() function can close negative file descriptor.
This is not a problem, but makes some noise in debugging
(valgrind for example).

>How-To-Repeat:

Simple program, that calls openlog() without LOG_NDELAY:

--------------------------------------------------------------------
#include <syslog.h>
#include <stdarg.h>

int
main(void)
{
	openlog("something", LOG_PID, LOG_LOCAL0);
	closelog();
	return (0);
}
--------------------------------------------------------------------

Run this program under valgrind:

....
==36309== Warning: invalid file descriptor -1 in syscall close()
....

>Fix:
--- syslog.c.orig	2009-09-04 14:05:03.000000000 +0300
+++ syslog.c	2009-09-23 12:45:58.000000000 +0300
@@ -413,8 +413,10 @@ void
 closelog(void)
 {
 	THREAD_LOCK();
-	(void)_close(LogFile);
-	LogFile = -1;
+	if (LogFile >= 0) {
+		(void)_close(LogFile);
+		LogFile = -1;
+	}
 	LogTag = NULL;
 	status = NOCONN;
 	THREAD_UNLOCK();

>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: gnats-admin->freebsd-bugs 
Responsible-Changed-By: gavin 
Responsible-Changed-When: Wed Sep 23 13:37:17 UTC 2009 
Responsible-Changed-Why:  
Rescue from pending 

http://www.freebsd.org/cgi/query-pr.cgi?pr=139080 
Responsible-Changed-From-To: freebsd-bugs->eadler 
Responsible-Changed-By: eadler 
Responsible-Changed-When: Thu May 10 18:25:49 UTC 2012 
Responsible-Changed-Why:  
I'll take it. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=139080 
State-Changed-From-To: open->analyzed 
State-Changed-By: eadler 
State-Changed-When: Mon Jun 18 04:31:45 UTC 2012 
State-Changed-Why:  
awaiting approval 

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

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/139080: commit references a PR
Date: Wed, 20 Jun 2012 06:38:57 +0000 (UTC)

 Author: eadler
 Date: Wed Jun 20 06:38:41 2012
 New Revision: 237286
 URL: http://svn.freebsd.org/changeset/base/237286
 
 Log:
   Don't close an uninitialized descriptor. [1]
   Add a sanity check for the validity of the passed fd.
   
   PR:		kern/139080 [1]
   Submitted by:	Andrey Simonenko <simon@comsys.ntu-kpi.kiev.ua> [1]
   Reviewed by:	pjd (briefly)
   Approved by:	cperciva
   MFC after:	1 week
 
 Modified:
   head/lib/libc/gen/syslog.c
 
 Modified: head/lib/libc/gen/syslog.c
 ==============================================================================
 --- head/lib/libc/gen/syslog.c	Wed Jun 20 04:11:34 2012	(r237285)
 +++ head/lib/libc/gen/syslog.c	Wed Jun 20 06:38:41 2012	(r237286)
 @@ -41,6 +41,7 @@ __FBSDID("$FreeBSD$");
  #include <sys/un.h>
  #include <netdb.h>
  
 +#include <assert.h>
  #include <errno.h>
  #include <fcntl.h>
  #include <paths.h>
 @@ -413,8 +414,11 @@ void
  closelog(void)
  {
  	THREAD_LOCK();
 -	(void)_close(LogFile);
 -	LogFile = -1;
 +	assert(LogFile >= -1);
 +	if (LogFile != -1) {
 +		(void)_close(LogFile);
 +		LogFile = -1;
 +	}
  	LogTag = NULL;
  	status = NOCONN;
  	THREAD_UNLOCK();
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: analyzed->patched 
State-Changed-By: eadler 
State-Changed-When: Wed Jun 20 06:42:09 UTC 2012 
State-Changed-Why:  
committed in r237286 

http://www.freebsd.org/cgi/query-pr.cgi?pr=139080 
State-Changed-From-To: patched->closed 
State-Changed-By: eadler 
State-Changed-When: Wed Jun 27 06:44:19 UTC 2012 
State-Changed-Why:  
MFCed 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/167302: commit references a PR
Date: Wed, 27 Jun 2012 06:41:25 +0000 (UTC)

 Author: eadler
 Date: Wed Jun 27 06:41:10 2012
 New Revision: 237633
 URL: http://svn.freebsd.org/changeset/base/237633
 
 Log:
   MFC r237286, r237523:
   	Don't close an uninitialized descriptor.
   
   PR:		bin/167302
   Approved by:	cperciva (implicit)
 
 Modified:
   stable/7/lib/libc/gen/syslog.c
 Directory Properties:
   stable/7/lib/libc/   (props changed)
 
 Modified: stable/7/lib/libc/gen/syslog.c
 ==============================================================================
 --- stable/7/lib/libc/gen/syslog.c	Wed Jun 27 06:40:59 2012	(r237632)
 +++ stable/7/lib/libc/gen/syslog.c	Wed Jun 27 06:41:10 2012	(r237633)
 @@ -413,8 +413,10 @@ void
  closelog(void)
  {
  	THREAD_LOCK();
 -	(void)_close(LogFile);
 -	LogFile = -1;
 +	if (LogFile != -1) {
 +		(void)_close(LogFile);
 +		LogFile = -1;
 +	}
  	LogTag = NULL;
  	status = NOCONN;
  	THREAD_UNLOCK();
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/167302: commit references a PR
Date: Wed, 27 Jun 2012 06:41:49 +0000 (UTC)

 Author: eadler
 Date: Wed Jun 27 06:41:14 2012
 New Revision: 237634
 URL: http://svn.freebsd.org/changeset/base/237634
 
 Log:
   MFC r237286, r237523:
   	Don't close an uninitialized descriptor.
   
   PR:		bin/167302
   Approved by:	cperciva (implicit)
 
 Modified:
   stable/8/lib/libc/gen/syslog.c
 Directory Properties:
   stable/8/lib/libc/   (props changed)
 
 Modified: stable/8/lib/libc/gen/syslog.c
 ==============================================================================
 --- stable/8/lib/libc/gen/syslog.c	Wed Jun 27 06:41:10 2012	(r237633)
 +++ stable/8/lib/libc/gen/syslog.c	Wed Jun 27 06:41:14 2012	(r237634)
 @@ -413,8 +413,10 @@ void
  closelog(void)
  {
  	THREAD_LOCK();
 -	(void)_close(LogFile);
 -	LogFile = -1;
 +	if (LogFile != -1) {
 +		(void)_close(LogFile);
 +		LogFile = -1;
 +	}
  	LogTag = NULL;
  	status = NOCONN;
  	THREAD_UNLOCK();
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"

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