diff options
author | jhb <jhb@FreeBSD.org> | 2009-06-15 20:38:55 +0000 |
---|---|---|
committer | jhb <jhb@FreeBSD.org> | 2009-06-15 20:38:55 +0000 |
commit | 447d980cd05483b9af4e91000999997f5ba018e7 (patch) | |
tree | 5e2b271455a043dbcc3a4a6aa9fd155c1966ebb4 /sys/kern | |
parent | df30f1c9f2ef0498e2aa537d4aec3aab759dc6c3 (diff) | |
download | FreeBSD-src-447d980cd05483b9af4e91000999997f5ba018e7.zip FreeBSD-src-447d980cd05483b9af4e91000999997f5ba018e7.tar.gz |
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
Diffstat (limited to 'sys/kern')
-rw-r--r-- | sys/kern/kern_descrip.c | 36 | ||||
-rw-r--r-- | sys/kern/syscalls.master | 1 |
2 files changed, 37 insertions, 0 deletions
diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index 1221825..30bb84a 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -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. diff --git a/sys/kern/syscalls.master b/sys/kern/syscalls.master index ab5981e..466d4e3 100644 --- a/sys/kern/syscalls.master +++ b/sys/kern/syscalls.master @@ -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 |