Skip to content

Commit b087498

Browse files
Ulrich Dreppertorvalds
authored andcommitted
flag parameters: eventfd
This patch adds the new eventfd2 syscall. It extends the old eventfd syscall by one parameter which is meant to hold a flag value. In this patch the only flag support is EFD_CLOEXEC which causes the close-on-exec flag for the returned file descriptor to be set. A new name EFD_CLOEXEC is introduced which in this implementation must have the same value as O_CLOEXEC. The following test must be adjusted for architectures other than x86 and x86-64 and in case the syscall numbers changed. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #include <fcntl.h> #include <stdio.h> #include <unistd.h> #include <sys/syscall.h> #ifndef __NR_eventfd2 # ifdef __x86_64__ # define __NR_eventfd2 290 # elif defined __i386__ # define __NR_eventfd2 328 # else # error "need __NR_eventfd2" # endif #endif #define EFD_CLOEXEC O_CLOEXEC int main (void) { int fd = syscall (__NR_eventfd2, 1, 0); if (fd == -1) { puts ("eventfd2(0) failed"); return 1; } int coe = fcntl (fd, F_GETFD); if (coe == -1) { puts ("fcntl failed"); return 1; } if (coe & FD_CLOEXEC) { puts ("eventfd2(0) sets close-on-exec flag"); return 1; } close (fd); fd = syscall (__NR_eventfd2, 1, EFD_CLOEXEC); if (fd == -1) { puts ("eventfd2(EFD_CLOEXEC) failed"); return 1; } coe = fcntl (fd, F_GETFD); if (coe == -1) { puts ("fcntl failed"); return 1; } if ((coe & FD_CLOEXEC) == 0) { puts ("eventfd2(EFD_CLOEXEC) does not set close-on-exec flag"); return 1; } close (fd); puts ("OK"); return 0; } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [akpm@linux-foundation.org: add sys_ni stub] Signed-off-by: Ulrich Drepper <drepper@redhat.com> Acked-by: Davide Libenzi <davidel@xmailserver.org> Cc: Michael Kerrisk <mtk.manpages@googlemail.com> Cc: <linux-arch@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 9deb27b commit b087498

File tree

8 files changed

+24
-2
lines changed

8 files changed

+24
-2
lines changed

arch/x86/ia32/ia32entry.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,4 +827,5 @@ ia32_sys_call_table:
827827
.quad compat_sys_timerfd_settime /* 325 */
828828
.quad compat_sys_timerfd_gettime
829829
.quad compat_sys_signalfd4
830+
.quad sys_eventfd2
830831
ia32_syscall_end:

arch/x86/kernel/syscall_table_32.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,4 @@ ENTRY(sys_call_table)
327327
.long sys_timerfd_settime /* 325 */
328328
.long sys_timerfd_gettime
329329
.long sys_signalfd4
330+
.long sys_eventfd2

fs/eventfd.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,14 @@ struct file *eventfd_fget(int fd)
198198
return file;
199199
}
200200

201-
asmlinkage long sys_eventfd(unsigned int count)
201+
asmlinkage long sys_eventfd2(unsigned int count, int flags)
202202
{
203203
int fd;
204204
struct eventfd_ctx *ctx;
205205

206+
if (flags & ~EFD_CLOEXEC)
207+
return -EINVAL;
208+
206209
ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
207210
if (!ctx)
208211
return -ENOMEM;
@@ -214,9 +217,15 @@ asmlinkage long sys_eventfd(unsigned int count)
214217
* When we call this, the initialization must be complete, since
215218
* anon_inode_getfd() will install the fd.
216219
*/
217-
fd = anon_inode_getfd("[eventfd]", &eventfd_fops, ctx, 0);
220+
fd = anon_inode_getfd("[eventfd]", &eventfd_fops, ctx,
221+
flags & O_CLOEXEC);
218222
if (fd < 0)
219223
kfree(ctx);
220224
return fd;
221225
}
222226

227+
asmlinkage long sys_eventfd(unsigned int count)
228+
{
229+
return sys_eventfd2(count, 0);
230+
}
231+

include/asm-x86/unistd_32.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@
333333
#define __NR_timerfd_settime 325
334334
#define __NR_timerfd_gettime 326
335335
#define __NR_signalfd4 327
336+
#define __NR_eventfd2 328
336337

337338
#ifdef __KERNEL__
338339

include/asm-x86/unistd_64.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,8 @@ __SYSCALL(__NR_timerfd_gettime, sys_timerfd_gettime)
643643
__SYSCALL(__NR_paccept, sys_paccept)
644644
#define __NR_signalfd4 289
645645
__SYSCALL(__NR_signalfd4, sys_signalfd4)
646+
#define __NR_eventfd2 290
647+
__SYSCALL(__NR_eventfd2, sys_eventfd2)
646648

647649

648650
#ifndef __NO_STUBS

include/linux/eventfd.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010

1111
#ifdef CONFIG_EVENTFD
1212

13+
/* For O_CLOEXEC */
14+
#include <linux/fcntl.h>
15+
16+
/* Flags for eventfd2. */
17+
#define EFD_CLOEXEC O_CLOEXEC
18+
1319
struct file *eventfd_fget(int fd);
1420
int eventfd_signal(struct file *file, int n);
1521

include/linux/syscalls.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ asmlinkage long sys_timerfd_settime(int ufd, int flags,
617617
struct itimerspec __user *otmr);
618618
asmlinkage long sys_timerfd_gettime(int ufd, struct itimerspec __user *otmr);
619619
asmlinkage long sys_eventfd(unsigned int count);
620+
asmlinkage long sys_eventfd2(unsigned int count, int flags);
620621
asmlinkage long sys_fallocate(int fd, int mode, loff_t offset, loff_t len);
621622

622623
int kernel_execve(const char *filename, char *const argv[], char *const envp[]);

kernel/sys_ni.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,4 @@ cond_syscall(sys_timerfd_gettime);
164164
cond_syscall(compat_sys_timerfd_settime);
165165
cond_syscall(compat_sys_timerfd_gettime);
166166
cond_syscall(sys_eventfd);
167+
cond_syscall(sys_eventfd2);

0 commit comments

Comments
 (0)