fanotify_user.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <linux/fcntl.h>
  2. #include <linux/fs.h>
  3. #include <linux/anon_inodes.h>
  4. #include <linux/fsnotify_backend.h>
  5. #include <linux/security.h>
  6. #include <linux/syscalls.h>
  7. #include "fanotify.h"
  8. static int fanotify_release(struct inode *ignored, struct file *file)
  9. {
  10. struct fsnotify_group *group = file->private_data;
  11. pr_debug("%s: file=%p group=%p\n", __func__, file, group);
  12. /* matches the fanotify_init->fsnotify_alloc_group */
  13. fsnotify_put_group(group);
  14. return 0;
  15. }
  16. static const struct file_operations fanotify_fops = {
  17. .poll = NULL,
  18. .read = NULL,
  19. .fasync = NULL,
  20. .release = fanotify_release,
  21. .unlocked_ioctl = NULL,
  22. .compat_ioctl = NULL,
  23. };
  24. /* fanotify syscalls */
  25. SYSCALL_DEFINE3(fanotify_init, unsigned int, flags, unsigned int, event_f_flags,
  26. unsigned int, priority)
  27. {
  28. struct fsnotify_group *group;
  29. int f_flags, fd;
  30. pr_debug("%s: flags=%d event_f_flags=%d priority=%d\n",
  31. __func__, flags, event_f_flags, priority);
  32. if (event_f_flags)
  33. return -EINVAL;
  34. if (priority)
  35. return -EINVAL;
  36. if (!capable(CAP_SYS_ADMIN))
  37. return -EACCES;
  38. if (flags & ~FAN_ALL_INIT_FLAGS)
  39. return -EINVAL;
  40. f_flags = (O_RDONLY | FMODE_NONOTIFY);
  41. if (flags & FAN_CLOEXEC)
  42. f_flags |= O_CLOEXEC;
  43. if (flags & FAN_NONBLOCK)
  44. f_flags |= O_NONBLOCK;
  45. /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
  46. group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
  47. if (IS_ERR(group))
  48. return PTR_ERR(group);
  49. fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
  50. if (fd < 0)
  51. goto out_put_group;
  52. return fd;
  53. out_put_group:
  54. fsnotify_put_group(group);
  55. return fd;
  56. }