Commit 4006553b06306b34054529477b06b68a1c66249b

Authored by Ulrich Drepper
Committed by Linus Torvalds
1 parent ed8cae8ba0

flag parameters: inotify_init

This patch introduces the new syscall inotify_init1 (note: the 1 stands for
the one parameter the syscall takes, as opposed to no parameter before).  The
values accepted for this parameter are function-specific and defined in the
inotify.h header.  Here the values must match the O_* flags, though.  In this
patch CLOEXEC support is introduced.

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_inotify_init1
# ifdef __x86_64__
#  define __NR_inotify_init1 294
# elif defined __i386__
#  define __NR_inotify_init1 332
# else
#  error "need __NR_inotify_init1"
# endif
#endif

#define IN_CLOEXEC O_CLOEXEC

int
main (void)
{
  int fd;
  fd = syscall (__NR_inotify_init1, 0);
  if (fd == -1)
    {
      puts ("inotify_init1(0) failed");
      return 1;
    }
  int coe = fcntl (fd, F_GETFD);
  if (coe == -1)
    {
      puts ("fcntl failed");
      return 1;
    }
  if (coe & FD_CLOEXEC)
    {
      puts ("inotify_init1(0) set close-on-exit");
      return 1;
    }
  close (fd);

  fd = syscall (__NR_inotify_init1, IN_CLOEXEC);
  if (fd == -1)
    {
      puts ("inotify_init1(IN_CLOEXEC) failed");
      return 1;
    }
  coe = fcntl (fd, F_GETFD);
  if (coe == -1)
    {
      puts ("fcntl failed");
      return 1;
    }
  if ((coe & FD_CLOEXEC) == 0)
    {
      puts ("inotify_init1(O_CLOEXEC) does not set close-on-exit");
      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>

Showing 8 changed files with 22 additions and 2 deletions Side-by-side Diff

arch/x86/ia32/ia32entry.S
... ... @@ -831,5 +831,6 @@
831 831 .quad sys_epoll_create2
832 832 .quad sys_dup3 /* 330 */
833 833 .quad sys_pipe2
  834 + .quad sys_inotify_init1
834 835 ia32_syscall_end:
arch/x86/kernel/syscall_table_32.S
... ... @@ -331,4 +331,5 @@
331 331 .long sys_epoll_create2
332 332 .long sys_dup3 /* 330 */
333 333 .long sys_pipe2
  334 + .long sys_inotify_init1
... ... @@ -566,7 +566,7 @@
566 566 .destroy_watch = free_inotify_user_watch,
567 567 };
568 568  
569   -asmlinkage long sys_inotify_init(void)
  569 +asmlinkage long sys_inotify_init1(int flags)
570 570 {
571 571 struct inotify_device *dev;
572 572 struct inotify_handle *ih;
... ... @@ -574,7 +574,10 @@
574 574 struct file *filp;
575 575 int fd, ret;
576 576  
577   - fd = get_unused_fd();
  577 + if (flags & ~IN_CLOEXEC)
  578 + return -EINVAL;
  579 +
  580 + fd = get_unused_fd_flags(flags & O_CLOEXEC);
578 581 if (fd < 0)
579 582 return fd;
580 583  
... ... @@ -636,6 +639,11 @@
636 639 out_put_fd:
637 640 put_unused_fd(fd);
638 641 return ret;
  642 +}
  643 +
  644 +asmlinkage long sys_inotify_init(void)
  645 +{
  646 + return sys_inotify_init1(0);
639 647 }
640 648  
641 649 asmlinkage long sys_inotify_add_watch(int fd, const char __user *path, u32 mask)
include/asm-x86/unistd_32.h
... ... @@ -337,6 +337,7 @@
337 337 #define __NR_epoll_create2 329
338 338 #define __NR_dup3 330
339 339 #define __NR_pipe2 331
  340 +#define __NR_inotify_init1 332
340 341  
341 342 #ifdef __KERNEL__
342 343  
include/asm-x86/unistd_64.h
... ... @@ -651,6 +651,8 @@
651 651 __SYSCALL(__NR_dup3, sys_dup3)
652 652 #define __NR_pipe2 293
653 653 __SYSCALL(__NR_pipe2, sys_pipe2)
  654 +#define __NR_inotify_init1 294
  655 +__SYSCALL(__NR_inotify_init1, sys_inotify_init1)
654 656  
655 657  
656 658 #ifndef __NO_STUBS
include/linux/inotify.h
... ... @@ -7,6 +7,8 @@
7 7 #ifndef _LINUX_INOTIFY_H
8 8 #define _LINUX_INOTIFY_H
9 9  
  10 +/* For O_CLOEXEC */
  11 +#include <linux/fcntl.h>
10 12 #include <linux/types.h>
11 13  
12 14 /*
... ... @@ -62,6 +64,9 @@
62 64 IN_CLOSE_NOWRITE | IN_OPEN | IN_MOVED_FROM | \
63 65 IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF | \
64 66 IN_MOVE_SELF)
  67 +
  68 +/* Flags for sys_inotify_init1. */
  69 +#define IN_CLOEXEC O_CLOEXEC
65 70  
66 71 #ifdef __KERNEL__
67 72  
include/linux/syscalls.h
... ... @@ -547,6 +547,7 @@
547 547 unsigned long addr, unsigned long flags);
548 548  
549 549 asmlinkage long sys_inotify_init(void);
  550 +asmlinkage long sys_inotify_init1(int flags);
550 551 asmlinkage long sys_inotify_add_watch(int fd, const char __user *path,
551 552 u32 mask);
552 553 asmlinkage long sys_inotify_rm_watch(int fd, u32 wd);
... ... @@ -96,6 +96,7 @@
96 96 cond_syscall(compat_sys_keyctl);
97 97 cond_syscall(compat_sys_socketcall);
98 98 cond_syscall(sys_inotify_init);
  99 +cond_syscall(sys_inotify_init1);
99 100 cond_syscall(sys_inotify_add_watch);
100 101 cond_syscall(sys_inotify_rm_watch);
101 102 cond_syscall(sys_migrate_pages);