Subject: bin/32747: powerd leaks file descriptor
To: None <gnats-admin@netbsd.org, netbsd-bugs@netbsd.org>
From: None <mlelstv@serpens.de>
List: netbsd-bugs
Date: 02/05/2006 13:35:00
>Number:         32747
>Category:       bin
>Synopsis:       powerd leaks filedescriptor
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sun Feb 05 13:35:00 +0000 2006
>Originator:     Michael van Elst
>Release:        NetBSD 3.0_STABLE
>Organization:
-- 
                                Michael van Elst
Internet: mlelstv@serpens.de
                                "A potential Snark may lurk in every tree."
>Environment:
	
	
System: NetBSD henery 3.0_STABLE NetBSD 3.0_STABLE (HENERY) #26: Sat Jan 28 20:13:18 CET 2006 src@henery:/usr/obj/home/src/sys/arch/i386/compile/HENERY i386
Architecture: i386
Machine: i386
>Description:
powerd can run scripts for certain power related events. In my
scripts I stop ntpd when my laptop runs on battery.

I noticed that in this scenario I couldn't restart powerd because
/dev/power was busy.

The reason is that powerd passes the open file descriptor to
the scripts and commands started from the script inherit that
descriptor. Any daemon started from the script therefore keeps
the filedescriptor open for a long time.

>How-To-Repeat:
Install powerd scripts that start a daemon.
Trigger that script.
Watch "/etc/rc.d/powerd restart" fail.

>Fix:

Index: powerd.c
===================================================================
RCS file: /cvsroot/src/usr.sbin/powerd/powerd.c,v
retrieving revision 1.4
diff -u -r1.4 powerd.c
--- powerd.c	3 May 2004 07:45:37 -0000	1.4
+++ powerd.c	5 Feb 2006 13:30:20 -0000
@@ -114,6 +114,7 @@
 		syslog(LOG_ERR, "open %s: %m", _PATH_DEV_POWER);
 		exit(EX_OSERR);
 	}
+	(void)fcntl(fd, F_SETFD, 1);
 
 	if (ioctl(fd, POWER_IOC_GET_TYPE, &power_type) < 0) {
 		syslog(LOG_ERR, "POWER_IOC_GET_TYPE: %m");

>Unformatted: