svn commit: r194262 - in head: include lib/libc/sys sys/compat/freebsd32 sys/kern tools/regression/file/closefrom

John Baldwin jhb at FreeBSD.org
Mon Jun 15 20:38:56 UTC 2009


Author: jhb
Date: Mon Jun 15 20:38:55 2009
New Revision: 194262
URL: http://svn.freebsd.org/changeset/base/194262

Log:
  Add a new 'void closefrom(int lowfd)' system call.  When called, it closes
  any open file descriptors >= 'lowfd'.  It is largely identical to the same
  function on other operating systems such as Solaris, DFly, NetBSD, and
  OpenBSD.  One difference from other *BSD is that this closefrom() does not
  fail with any errors.  In practice, while the manpages for NetBSD and
  OpenBSD claim that they return EINTR, they ignore internal errors from
  close() and never return EINTR.  DFly does return EINTR, but for the common
  use case (closing fd's prior to execve()), the caller really wants all
  fd's closed and returning EINTR just forces callers to call closefrom() in
  a loop until it stops failing.
  
  Note that this implementation of closefrom(2) does not make any effort to
  resolve userland races with open(2) in other threads.  As such, it is not
  multithread safe.
  
  Submitted by:	rwatson (initial version)
  Reviewed by:	rwatson
  MFC after:	2 weeks

Added:
  head/lib/libc/sys/closefrom.2   (contents, props changed)
  head/tools/regression/file/closefrom/
  head/tools/regression/file/closefrom/Makefile   (contents, props changed)
  head/tools/regression/file/closefrom/closefrom.c   (contents, props changed)
  head/tools/regression/file/closefrom/closefrom.t   (contents, props changed)
Modified:
  head/include/unistd.h
  head/lib/libc/sys/Makefile.inc
  head/lib/libc/sys/Symbol.map
  head/lib/libc/sys/close.2
  head/sys/compat/freebsd32/syscalls.master
  head/sys/kern/kern_descrip.c
  head/sys/kern/syscalls.master

Modified: head/include/unistd.h
==============================================================================
--- head/include/unistd.h	Mon Jun 15 20:12:27 2009	(r194261)
+++ head/include/unistd.h	Mon Jun 15 20:38:55 2009	(r194262)
@@ -326,6 +326,7 @@ unsigned int	 alarm(unsigned int);
 int	 chdir(const char *);
 int	 chown(const char *, uid_t, gid_t);
 int	 close(int);
+void	 closefrom(int);
 int	 dup(int);
 int	 dup2(int, int);
 int	 execl(const char *, const char *, ...);

Modified: head/lib/libc/sys/Makefile.inc
==============================================================================
--- head/lib/libc/sys/Makefile.inc	Mon Jun 15 20:12:27 2009	(r194261)
+++ head/lib/libc/sys/Makefile.inc	Mon Jun 15 20:38:55 2009	(r194262)
@@ -63,7 +63,7 @@ MAN+=	abort2.2 accept.2 access.2 acct.2 
 	aio_cancel.2 aio_error.2 aio_read.2 aio_return.2 \
 	aio_suspend.2 aio_waitcomplete.2 aio_write.2 \
 	bind.2 brk.2 chdir.2 chflags.2 \
-	chmod.2 chown.2 chroot.2 clock_gettime.2 close.2 \
+	chmod.2 chown.2 chroot.2 clock_gettime.2 close.2 closefrom.2 \
 	connect.2 cpuset.2 cpuset_getaffinity.2 dup.2 execve.2 _exit.2 \
 	extattr_get_file.2 fcntl.2 fhopen.2 flock.2 fork.2 fsync.2 \
 	getdirentries.2 getdtablesize.2 \

Modified: head/lib/libc/sys/Symbol.map
==============================================================================
--- head/lib/libc/sys/Symbol.map	Mon Jun 15 20:12:27 2009	(r194261)
+++ head/lib/libc/sys/Symbol.map	Mon Jun 15 20:38:55 2009	(r194262)
@@ -333,6 +333,7 @@ FBSD_1.0 {
 };
 
 FBSD_1.1 {
+	closefrom;
 	cpuset;
 	cpuset_getid;
 	cpuset_setid;
@@ -472,6 +473,8 @@ FBSDprivate_1.0 {
 	__sys_clock_settime;
 	_close;
 	__sys_close;
+	_closefrom;
+	__sys_closefrom;
 	_connect;
 	__sys_connect;
 	_cpuset;

Modified: head/lib/libc/sys/close.2
==============================================================================
--- head/lib/libc/sys/close.2	Mon Jun 15 20:12:27 2009	(r194261)
+++ head/lib/libc/sys/close.2	Mon Jun 15 20:38:55 2009	(r194262)
@@ -120,6 +120,7 @@ before all pending data was delivered.
 .El
 .Sh SEE ALSO
 .Xr accept 2 ,
+.Xr closefrom 2 ,
 .Xr execve 2 ,
 .Xr fcntl 2 ,
 .Xr flock 2 ,

Added: head/lib/libc/sys/closefrom.2
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/lib/libc/sys/closefrom.2	Mon Jun 15 20:38:55 2009	(r194262)
@@ -0,0 +1,53 @@
+.\" Copyright (c) 2009 Advanced Computing Technologies LLC
+.\" Written by: John H. Baldwin <jhb at FreeBSD.org>
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd June 12, 2009
+.Dt CLOSEFROM 2
+.Os
+.Sh NAME
+.Nm closefrom
+.Nd delete open file descriptors
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.In unistd.h
+.Ft void
+.Fn closefrom "int lowfd"
+.Sh DESCRIPTION
+The
+.Fn closefrom
+system call deletes all open file descriptors greater than or equal to
+.Fa lowfd
+from the per-process object reference table.
+Any errors encountered while closing file descriptors are ignored.
+.Sh SEE ALSO
+.Xr close 2
+.Sh HISTORY
+The
+.Fn closefrom
+function first appeared in
+.Fx 8.0 .

Modified: head/sys/compat/freebsd32/syscalls.master
==============================================================================
--- head/sys/compat/freebsd32/syscalls.master	Mon Jun 15 20:12:27 2009	(r194261)
+++ head/sys/compat/freebsd32/syscalls.master	Mon Jun 15 20:38:55 2009	(r194262)
@@ -889,3 +889,4 @@
 507	AUE_NULL	STD	{ int freebsd32_jail_set(struct iovec32 *iovp, \
 				    unsigned int iovcnt, int flags); }
 508	AUE_NULL	NOPROTO	{ int jail_remove(int jid); }
+509	AUE_CLOSEFROM	NOPROTO	{ int closefrom(int lowfd); }

Modified: head/sys/kern/kern_descrip.c
==============================================================================
--- head/sys/kern/kern_descrip.c	Mon Jun 15 20:12:27 2009	(r194261)
+++ head/sys/kern/kern_descrip.c	Mon Jun 15 20:38:55 2009	(r194262)
@@ -1128,6 +1128,42 @@ kern_close(td, fd)
 	return (error);
 }
 
+/*
+ * Close open file descriptors.
+ */
+#ifndef _SYS_SYSPROTO_H_
+struct closefrom_args {
+	int	lowfd;
+};
+#endif
+/* ARGSUSED */
+int
+closefrom(struct thread *td, struct closefrom_args *uap)
+{
+	struct filedesc *fdp;
+	int fd;
+
+	fdp = td->td_proc->p_fd;
+	AUDIT_ARG(fd, uap->lowfd);
+
+	/*
+	 * Treat negative starting file descriptor values identical to
+	 * closefrom(0) which closes all files.
+	 */
+	if (uap->lowfd < 0)
+		uap->lowfd = 0;
+	FILEDESC_SLOCK(fdp);
+	for (fd = uap->lowfd; fd < fdp->fd_nfiles; fd++) {
+		if (fdp->fd_ofiles[fd] != NULL) {
+			FILEDESC_SUNLOCK(fdp);
+			(void)kern_close(td, fd);
+			FILEDESC_SLOCK(fdp);
+		}
+	}
+	FILEDESC_SUNLOCK(fdp);
+	return (0);
+}
+
 #if defined(COMPAT_43)
 /*
  * Return status information about a file descriptor.

Modified: head/sys/kern/syscalls.master
==============================================================================
--- head/sys/kern/syscalls.master	Mon Jun 15 20:12:27 2009	(r194261)
+++ head/sys/kern/syscalls.master	Mon Jun 15 20:38:55 2009	(r194262)
@@ -902,5 +902,6 @@
 507	AUE_NULL	STD	{ int jail_set(struct iovec *iovp, \
 				    unsigned int iovcnt, int flags); }
 508	AUE_NULL	STD	{ int jail_remove(int jid); }
+509	AUE_CLOSEFROM	STD	{ int closefrom(int lowfd); }
 ; Please copy any additions and changes to the following compatability tables:
 ; sys/compat/freebsd32/syscalls.master

Added: head/tools/regression/file/closefrom/Makefile
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/tools/regression/file/closefrom/Makefile	Mon Jun 15 20:38:55 2009	(r194262)
@@ -0,0 +1,9 @@
+# $FreeBSD$
+
+PROG=	closefrom
+MAN=
+WARNS?=	6
+
+LDADD=	-lutil
+
+.include <bsd.prog.mk>

Added: head/tools/regression/file/closefrom/closefrom.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/tools/regression/file/closefrom/closefrom.c	Mon Jun 15 20:38:55 2009	(r194262)
@@ -0,0 +1,274 @@
+/*-
+ * Copyright (c) 2009 Advanced Computing Technologies LLC
+ * Written by: John H. Baldwin <jhb at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+/*
+ * Regression tests for the closefrom(2) system call.
+ */
+
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/user.h>
+#include <sys/wait.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <libutil.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+struct shared_info {
+	int	failed;
+	char	tag[64];
+	char	message[0];
+};
+
+static int test = 1;
+
+static void
+ok(const char *descr)
+{
+
+	printf("ok %d - %s\n", test, descr);
+	test++;
+}
+
+static void
+fail(const char *descr, const char *fmt, ...)
+{
+	va_list ap;
+
+	printf("not ok %d - %s", test, descr);
+	test++;
+	if (fmt) {
+		va_start(ap, fmt);
+		printf(" # ");
+		vprintf(fmt, ap);
+		va_end(ap);
+	}
+	printf("\n");
+	exit(1);
+}
+
+#define	fail_err(descr)		fail((descr), "%s", strerror(errno))
+
+static void
+cok(struct shared_info *info, const char *descr)
+{
+
+	info->failed = 0;
+	strlcpy(info->tag, descr, sizeof(info->tag));
+	exit(0);
+}
+
+static void
+cfail(struct shared_info *info, const char *descr, const char *fmt, ...)
+{
+	va_list ap;
+
+	info->failed = 1;
+	strlcpy(info->tag, descr, sizeof(info->tag));
+	if (fmt) {
+		va_start(ap, fmt);
+		vsprintf(info->message, fmt, ap);
+		va_end(ap);
+	}
+	exit(0);
+}
+
+#define	cfail_err(info, descr)	cfail((info), (descr), "%s", strerror(errno))
+
+/*
+ * Use kinfo_getfile() to fetch the list of file descriptors and figure out
+ * the highest open file descriptor.
+ */
+static int
+highest_fd(void)
+{
+	struct kinfo_file *kif;
+	int cnt, i, highest;
+
+	kif = kinfo_getfile(getpid(), &cnt);
+	if (kif == NULL)
+		fail_err("kinfo_getfile");
+	highest = INT_MIN;
+	for (i = 0; i < cnt; i++)
+		if (kif[i].kf_fd > highest)
+			highest = kif[i].kf_fd;
+	free(kif);
+	return (highest);
+}
+
+static int
+devnull(void)
+{
+	int fd;
+
+	fd = open("/dev/null", O_RDONLY);
+	if (fd < 0)
+		fail_err("open(\"/dev/null\")");
+	return (fd);
+}
+
+int
+main(int __unused argc, char __unused *argv[])
+{
+	struct shared_info *info;
+	pid_t pid;
+	int fd, i;
+
+	printf("1..15\n");
+
+	/* We better start up with fd's 0, 1, and 2 open. */
+	fd = devnull();
+	if (fd != 3)
+		fail("open", "bad descriptor %d", fd);
+	ok("open");
+
+	/* Make sure highest_fd() works. */
+	fd = highest_fd();
+	if (fd != 3)
+		fail("highest_fd", "bad descriptor %d", fd);
+	ok("highest_fd");
+
+	/* Try to use closefrom() for just closing fd 3. */
+	closefrom(3);
+	fd = highest_fd();
+	if (fd != 2)
+		fail("closefrom", "highest fd %d", fd);
+	ok("closefrom");
+
+	/* Eat up 16 descriptors. */
+	for (i = 0; i < 16; i++)
+		(void)devnull();
+	fd = highest_fd();
+	if (fd != 18)
+		fail("open 16", "highest fd %d", fd);
+	ok("open 16");
+
+	/* Close half of them. */
+	closefrom(11);
+	fd = highest_fd();
+	if (fd != 10)
+		fail("closefrom", "highest fd %d", fd);
+	ok("closefrom");
+
+	/* Explicitly close descriptors 6 and 8 to create holes. */
+	if (close(6) < 0 || close(8) < 0)
+		fail_err("close2 ");
+	ok("close 2");
+
+	/* Verify that close on 6 and 8 fails with EBADF. */
+	if (close(6) == 0)
+		fail("close(6)", "did not fail");
+	if (errno != EBADF)
+		fail_err("close(6)");
+	ok("close(6)");
+	if (close(8) == 0)
+		fail("close(8)", "did not fail");
+	if (errno != EBADF)
+		fail_err("close(8)");
+	ok("close(8)");
+
+	/* Close from 4 on. */
+	closefrom(4);
+	fd = highest_fd();
+	if (fd != 3)
+		fail("closefrom", "highest fd %d", fd);
+	ok("closefrom");
+
+	/* Allocate a small SHM region for IPC with our child. */
+	info = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_ANON |
+	    MAP_SHARED, -1, 0);
+	if (info == MAP_FAILED)
+		fail_err("mmap");
+	ok("mmap");
+
+	/* Fork a child process to test closefrom(0). */
+	pid = fork();
+	if (pid < 0)
+		fail_err("fork");
+	if (pid == 0) {
+		/* Child. */
+		closefrom(0);
+		fd = highest_fd();
+		if (fd >= 0)
+			cfail(info, "closefrom(0)", "highest fd %d", fd);
+		cok(info, "closefrom(0)");
+	}
+	if (wait(NULL) < 0)
+		fail_err("wait");
+	if (info->failed)
+		fail(info->tag, "%s", info->message);
+	ok(info->tag);
+
+	/* Fork a child process to test closefrom(-1). */
+	pid = fork();
+	if (pid < 0)
+		fail_err("fork");
+	if (pid == 0) {
+		/* Child. */
+		closefrom(-1);
+		fd = highest_fd();
+		if (fd >= 0)
+			cfail(info, "closefrom(-1)", "highest fd %d", fd);
+		cok(info, "closefrom(-1)");
+	}
+	if (wait(NULL) < 0)
+		fail_err("wait");
+	if (info->failed)
+		fail(info->tag, "%s", info->message);
+	ok(info->tag);
+
+	/* Dup stdout to 6. */
+	if (dup2(1, 6) < 0)
+		fail_err("dup2");
+	fd = highest_fd();
+	if (fd != 6)
+		fail("dup2", "highest fd %d", fd);
+	ok("dup2");
+
+	/* Do a closefrom() starting in a hole. */
+	closefrom(4);
+	fd = highest_fd();
+	if (fd != 3)
+		fail("closefrom", "highest fd %d", fd);
+	ok("closefrom");
+
+	/* Do a closefrom() beyond our highest open fd. */
+	closefrom(32);
+	fd = highest_fd();
+	if (fd != 3)
+		fail("closefrom", "highest fd %d", fd);
+	ok("closefrom");
+	
+	return (0);
+}

Added: head/tools/regression/file/closefrom/closefrom.t
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/tools/regression/file/closefrom/closefrom.t	Mon Jun 15 20:38:55 2009	(r194262)
@@ -0,0 +1,10 @@
+#!/bin/sh
+# $FreeBSD$
+
+cd `dirname $0`
+
+executable=`basename $0 .t`
+
+make $executable 2>&1 > /dev/null
+
+exec ./$executable


More information about the svn-src-head mailing list