eventfd, signalfd, timerfd, epoll_create w/flags
Subsystems (4)
PATCH v2 1/1 eventfd, signalfd, timerfd, epoll_create w/flags
This is the same patch with only epoll_create2 as the new syscall name, as per Davide's request.
Again, it applies on top of the socket patch.
arch/x86/ia32/ia32entry.S | 3 +++ arch/x86/kernel/syscall_table_32.S | 3 +++ b/include/asm-x86/unistd_64.h | 6 ++++++ b/include/linux/syscalls.h | 3 +++ fs/anon_inodes.c | 9 +++++---- fs/compat.c | 14 ++++++++++---- fs/eventfd.c | 19 +++++++++++++++++-- fs/eventpoll.c | 19 +++++++++++++++++-- fs/signalfd.c | 19 +++++++++++++++++-- fs/timerfd.c | 13 ++++++++++--- include/asm-x86/unistd_32.h | 3 +++ include/linux/anon_inodes.h | 2 +- include/linux/eventfd.h | 2 ++ include/linux/eventpoll.h | 2 ++ include/linux/signalfd.h | 3 +++ include/linux/timerfd.h | 4 +++- 16 files changed, 105 insertions(+), 19 deletions(-)
include/asm-x86/unistd_64.h +6 -0
@@ -641,6 +641,12 @@ __SYSCALL(__NR_timerfd_gettime, sys_timerfd_gettime)#define __NR_paccept 288 __SYSCALL(__NR_paccept, sys_paccept) +#define __NR_signalfd4 289 +__SYSCALL(__NR_signalfd4, sys_signalfd4) +#define __NR_eventfd2 290 +__SYSCALL(__NR_eventfd2, sys_eventfd2) +#define __NR_epoll_create2 291 +__SYSCALL(__NR_epoll_create2, sys_epoll_create2)#ifndef __NO_STUBS
include/linux/syscalls.h +3 -0
@@ -428,6 +428,7 @@asmlinkage long sys_select (int n,fd_set __user *inp,fd_set __user *outp,fd_set __user *exp,struct timeval __user *tvp);asmlinkage long sys_epoll_create (int size); +asmlinkage long sys_epoll_create2 (int size,int flags);asmlinkage long sys_epoll_ctl (int epfd,int op,int fd,struct epoll_event __user *event); asmlinkage long sys_epoll_wait(int epfd, struct epoll_event __user *events,
@@ -608,12 +609,14 @@ size_t len);asmlinkage long sys_getcpu (unsigned __user *cpu,unsigned __user *node,struct getcpu_cache __user *cache);asmlinkage long sys_signalfd (int ufd,sigset_t __user *user_mask,size_t sizemask); +asmlinkage long sys_signalfd4 (int ufd,sigset_t __user *user_mask,size_t sizemask,int flags);asmlinkage long sys_timerfd_create (int clockid,int flags);asmlinkage long sys_timerfd_settime (int ufd,int flags,const struct itimerspec __user *utmr,struct itimerspec __user *otmr);asmlinkage long sys_timerfd_gettime (int ufd,struct itimerspec __user *otmr);asmlinkage long sys_eventfd (unsigned int count); +asmlinkage long sys_eventfd2 (unsigned int count,int flags);asmlinkage long sys_fallocate (int fd,int mode,loff_t offset,loff_t len);int kernel_execve (const char *filename,char *const argv[],char *const envp[]);
arch/x86/ia32/ia32entry.S +3 -0
@@ -731,4 +731,7 @@ ia32_sys_call_table: .quad sys32_fallocate .quad compat_sys_timerfd_settime /* 325 */ .quad compat_sys_timerfd_gettime + .quad compat_sys_signalfd4 + .quad sys_eventfd2 + .quad sys_epoll_create2 ia32_syscall_end:
arch/x86/kernel/syscall_table_32.S +3 -0
@@ -326,3 +326,6 @@ ENTRY(sys_call_table) .long sys_fallocate .long sys_timerfd_settime /* 325 */ .long sys_timerfd_gettime + .long sys_signalfd4 + .long sys_eventfd2 + .long sys_epoll_create2
fs/anon_inodes.c +5 -4
@@ -61,8 +61,9 @@ static struct dentry_operations anon_inodefs_dentry_operations = {* @dpinode: [out] pointer to the inode * @pfile: [out] pointer to the file struct * @name: [in] name of the "class" of the new file -* @fops [in] file operations for the new file -* @priv [in] private data for the new file (will be file's private_data) +* @fops: [in] file operations for the new file +* @priv: [in] private data for the new file (will be file's private_data) +* @flags: [in] flags * * Creates a new file by hooking it on a single inode. This is useful for files * that do not need to have a full-fledged inode in order to operate correctly.
@@ -72,7 +73,7 @@ static struct dentry_operations anon_inodefs_dentry_operations = {*/ int anon_inode_getfd (int *pfd,struct inode **pinode,struct file **pfile,const char *name,const struct file_operations *fops, -void *priv) +void *priv,int flags) {struct qstr this;struct dentry *dentry;
@@ -82,7 +83,7 @@ int anon_inode_getfd(int *pfd, struct inode **pinode, struct file **pfile,if (IS_ERR (anon_inode_inode))return -ENODEV ; - error =get_unused_fd (); + error =get_unused_fd_flags (flags);if (error <0 )return error; fd = error;
fs/compat.c +10 -4
@@ -2130,9 +2130,9 @@ asmlinkage long compat_sys_epoll_pwait(int epfd,#ifdef CONFIG_SIGNALFD -asmlinkage long compat_sys_signalfd (int ufd, -const compat_sigset_t __user *sigmask, -compat_size_t sigsetsize) +asmlinkage long compat_sys_signalfd4 (int ufd, +const compat_sigset_t __user *sigmask, +compat_size_t sigsetsize,int flags) {compat_sigset_t ss32;sigset_t tmp;
@@ -2147,9 +2147,15 @@ asmlinkage long compat_sys_signalfd(int ufd,if (copy_to_user (ksigmask, &tmp,sizeof (sigset_t)))return -EFAULT ; - return sys_signalfd(ufd, ksigmask, sizeof(sigset_t)); + return sys_signalfd(ufd, ksigmask, sizeof(sigset_t), flags); } +asmlinkage long compat_sys_signalfd (int ufd, +const compat_sigset_t __user *sigmask, +compat_size_t sigsetsize) +{ +return compat_sys_signalfd4 (ufd, sigmask, sigsetsize,0 ); +}#endif /* CONFIG_SIGNALFD */ #ifdef CONFIG_TIMERFD
fs/eventfd.c +17 -2
@@ -198,12 +198,22 @@ struct file *eventfd_fget(int fd)return file; } -asmlinkage long sys_eventfd (unsigned int count) +asmlinkage long sys_eventfd2 (unsigned int count,int flags) {int error, fd;struct eventfd_ctx *ctx;struct file *file;struct inode *inode; +int fflags =0 ; + +if (flags) { +if ((flags &EFD_CLOEXEC ) !=0 ) { + fflags |=O_CLOEXEC ; + flags &= ~EFD_CLOEXEC ; + } +if (flags) +return -EINVAL ; + } ctx =kmalloc (sizeof (*ctx),GFP_KERNEL );if (!ctx)
@@ -217,7 +227,7 @@ asmlinkage long sys_eventfd(unsigned int count)* anon_inode_getfd() will install the fd. */ error =anon_inode_getfd (&fd, &inode, &file,"[eventfd]" , - &eventfd_fops, ctx); + &eventfd_fops, ctx, fflags);if (!error)return fd;
@@ -225,3 +235,8 @@ asmlinkage long sys_eventfd(unsigned int count)return error; } +asmlinkage long sys_eventfd (unsigned int count) +{ +return sys_eventfd2 (count,0 ); +} +
fs/eventpoll.c +17 -2
@@ -1067,12 +1067,22 @@ retry:* RB tree. With the current implementation, the "size" parameter is ignored * (besides sanity checks). */ -asmlinkage long sys_epoll_create (int size) +asmlinkage long sys_epoll_create2 (int size,int flags) {int error, fd =-1 ;struct eventpoll *ep;struct inode *inode;struct file *file; +int fflags =0 ; + +if (flags) { +if ((flags &EPOLL_CLOEXEC ) !=0 ) { + fflags |=O_CLOEXEC ; + flags &= ~EPOLL_CLOEXEC ; + } +if (flags) +return -EINVAL ; + }DNPRINTK (3 , (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d)\n" , current, size));
@@ -1090,7 +1100,7 @@ asmlinkage long sys_epoll_create(int size)* a file structure, and inode and a free file descriptor. */ error =anon_inode_getfd (&fd, &inode, &file,"[eventpoll]" , - &eventpoll_fops, ep); + &eventpoll_fops, ep, fflags);if (error) goto error_free;
@@ -1107,6 +1117,11 @@ error_return:return error; } +asmlinkage long sys_epoll_create (int size) +{ +return sys_epoll_create2 (size,0 ); +} +/* * The following function implements the controller interface for * the eventpoll file that enables the insertion/removal/change of
fs/signalfd.c +17 -2
@@ -205,13 +205,24 @@ static const struct file_operations signalfd_fops = { .read = signalfd_read, }; -asmlinkage long sys_signalfd (int ufd,sigset_t __user *user_mask,size_t sizemask) +asmlinkage long sys_signalfd4 (int ufd,sigset_t __user *user_mask, +size_t sizemask,int flags) {int error;sigset_t sigmask;struct signalfd_ctx *ctx;struct file *file;struct inode *inode; +int fflags =0 ; + +if (flags) { +if ((flags &SFD_CLOEXEC ) !=0 && ufd ==-1 ) { + fflags |=O_CLOEXEC ; + flags &= ~SFD_CLOEXEC ; + } +if (flags) +return -EINVAL ; + }if (sizemask !=sizeof (sigset_t) ||copy_from_user (&sigmask, user_mask,sizeof (sigmask)))
@@ -231,7 +242,7 @@ asmlinkage long sys_signalfd(int ufd, sigset_t __user *user_mask, size_t sizemas* anon_inode_getfd() will install the fd. */ error =anon_inode_getfd (&ufd, &inode, &file,"[signalfd]" , - &signalfd_fops, ctx); + &signalfd_fops, ctx, fflags);if (error) goto err_fdalloc; }else {
@@ -258,3 +269,7 @@ err_fdalloc:return error; } +asmlinkage long sys_signalfd (int ufd,sigset_t __user *user_mask,size_t sizemask) +{ +return sys_signalfd4 (ufd, user_mask, sizemask,0 ); +}
fs/timerfd.c +10 -3
@@ -184,9 +184,16 @@ asmlinkage long sys_timerfd_create(int clockid, int flags)struct timerfd_ctx *ctx;struct file *file;struct inode *inode; +int fflags =0 ; -if (flags) -return -EINVAL ; +if (flags) { +if ((flags &TFD_CLOEXEC ) !=0 ) { + fflags |=O_CLOEXEC ; + flags &= ~TFD_CLOEXEC ; + } +if (flags) +return -EINVAL ; + }if (clockid !=CLOCK_MONOTONIC && clockid !=CLOCK_REALTIME )return -EINVAL ;
@@ -200,7 +207,7 @@ asmlinkage long sys_timerfd_create(int clockid, int flags)hrtimer_init (&ctx->tmr, clockid,HRTIMER_MODE_ABS ); error =anon_inode_getfd (&ufd, &inode, &file,"[timerfd]" , - &timerfd_fops, ctx); + &timerfd_fops, ctx, fflags);if (error) {kfree (ctx);return error;
include/asm-x86/unistd_32.h +3 -0
@@ -332,6 +332,9 @@#define __NR_fallocate 324#define __NR_timerfd_settime 325#define __NR_timerfd_gettime 326 +#define __NR_signalfd4 327 +#define __NR_eventfd2 328 +#define __NR_epoll_create2 329#ifdef __KERNEL__
include/linux/anon_inodes.h +1 -1
@@ -10,7 +10,7 @@int anon_inode_getfd (int *pfd,struct inode **pinode,struct file **pfile,const char *name,const struct file_operations *fops, -void *priv); +void *priv,int flags);#endif /* _LINUX_ANON_INODES_H */
include/linux/eventfd.h +2 -0
@@ -8,6 +8,8 @@#ifndef _LINUX_EVENTFD_H#define _LINUX_EVENTFD_H +/* Flags for eventfd2. */ +#define EFD_CLOEXEC (1 << 0)#ifdef __KERNEL__
include/linux/eventpoll.h +2 -0
@@ -16,6 +16,8 @@#include <linux/types.h> +/* Flags for epoll_create2. */ +#define EPOLL_CLOEXEC (1 << 0)/* Valid opcodes to issue to sys_epoll_ctl() */ #define EPOLL_CTL_ADD 1
include/linux/signalfd.h +3 -0
@@ -9,6 +9,9 @@#define _LINUX_SIGNALFD_H +/* Flags for signalfd4. */ +#define SFD_CLOEXEC (1 << 0) +struct signalfd_siginfo {__u32 ssi_signo;__s32 ssi_errno;
include/linux/timerfd.h +3 -1
@@ -8,9 +8,11 @@#ifndef _LINUX_TIMERFD_H#define _LINUX_TIMERFD_H - +/* Flags for timerfd_settime. */ #define TFD_TIMER_ABSTIME (1 << 0) +/* Flags for timerfd_create. */ +#define TFD_CLOEXEC (1 << 0)#endif /* _LINUX_TIMERFD_H */
Quoted 10 lines · Ulrich Drepper
fs/eventfd.c +10
-0+int fflags =0 ; + +if (flags) { +if ((flags &EFD_CLOEXEC ) !=0 ) { + fflags |=O_CLOEXEC ; + flags &= ~EFD_CLOEXEC ; + } +if (flags) +return -EINVAL ; + }
...
Quoted 10 lines
fs/eventpoll.c +10
-0+int fflags =0 ; + +if (flags) { +if ((flags &EPOLL_CLOEXEC ) !=0 ) { + fflags |=O_CLOEXEC ; + flags &= ~EPOLL_CLOEXEC ; + } +if (flags) +return -EINVAL ; + }
...
Quoted 10 lines
fs/signalfd.c +10
-0+int fflags =0 ; + +if (flags) { +if ((flags &SFD_CLOEXEC ) !=0 && ufd ==-1 ) { + fflags |=O_CLOEXEC ; + flags &= ~SFD_CLOEXEC ; + } +if (flags) +return -EINVAL ; + }
...
fs/timerfd.c asmlinkage long sys_timerfd_create(int clockid, int flags) · line 196
Quoted 12 lines
fs/timerfd.c +9
-2+int fflags =0 ; -if (flags) -return -EINVAL ; +if (flags) { +if ((flags &TFD_CLOEXEC ) !=0 ) { + fflags |=O_CLOEXEC ; + flags &= ~TFD_CLOEXEC ; + } +if (flags) +return -EINVAL ; + }
Maybe something like:
struct oflags_rmap {
int f, of;
};unsigned int oflags_remap(const struct oflags_rmap *m, int n,
int f, int *rf)
{
int i;
for (i = 0, *rf = 0; i < n; i++, m++)
if (f & m->f) {
*rf |= m->of;
f &= ~m->f;
}
return f ? -EINVAL: 0;
}Then, in all the cases above (modulo different "struct oflags_rmap" fillings):
const struct oflags_rmap ormap[] = {
{ TFD_CLOEXEC, O_CLOEXEC }
};error = oflags_remap(ormap, ARRAY_SIZE(ormap), flags, &fflags);
- Davide
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Quoted 11 lines · Davide Libenzi
unsigned int oflags_remap(const struct
oflags_rmap*m, int n,
int f, int *rf){ int i; for (i = 0, *rf = 0; i < n; i++, m++) if (f & m->f) { *rf |= m->of; f &= ~m->f; } return f ? -EINVAL: 0; }
This will be more expensive in terms of code and runtime. Plus: who knows whether there will really be other flags which can be used like this? I think it's overkill to do this. If at some point there are more flags which are handled the same way one could do something like this but I don't think it's necessary now.
- --
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖ -----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org
iEYEARECAAYFAkgVY+QACgkQ2ijCOnn/RHTN9gCdG6HxUENiLQqmSFPc5LMAcHna Nc0AnAhma85jwTCS+XTsQ/MrNngQAT+i
=3vVs
-----END PGP SIGNATURE-----
Quoted 16 lines · Ulrich Drepper
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1Quoted 11 lines · Davide Libenzi
unsigned int oflags_remap(const struct
oflags_rmap*m, int n,
int f, int *rf){ int i; for (i = 0, *rf = 0; i < n; i++, m++) if (f & m->f) { *rf |= m->of; f &= ~m->f; } return f ? -EINVAL: 0; }
Quoted 5 lines
This will be more expensive in terms of code and runtime. Plus: who knows whether there will really be other flags which can be used like this? I think it's overkill to do this. If at some point there are more flags which are handled the same way one could do something like this but I don't think it's necessary now.
There are already, not one, not two, not three, but four call sites where an aweful cut&paste is used to basically do the same thing. And probably there's another one you likely did for the sockets.
- Davide