923a60
From 66d06bd0a577ddb2461e8d1e5c8c2fbf6845227d Mon Sep 17 00:00:00 2001
923a60
From: Lukas Nykryn <lnykryn@redhat.com>
923a60
Date: Wed, 19 Nov 2014 12:14:13 +0100
923a60
Subject: [PATCH] Revert "missing: remove fanotify"
923a60
923a60
This reverts commit c7e4a7bece7a5c4484d229dd5e8ff01a5d49c62e.
923a60
923a60
Conflicts:
923a60
	src/shared/missing.h
923a60
---
923a60
 Makefile.am                 |  1 +
923a60
 configure.ac                |  1 +
923a60
 src/shared/linux/fanotify.h | 98 +++++++++++++++++++++++++++++++++++++
923a60
 src/shared/missing.h        | 64 ++++++++++++++++++++++++
923a60
 4 files changed, 164 insertions(+)
923a60
 create mode 100644 src/shared/linux/fanotify.h
923a60
923a60
diff --git a/Makefile.am b/Makefile.am
923a60
index a734e9c486..70e4fbc6d4 100644
923a60
--- a/Makefile.am
923a60
+++ b/Makefile.am
923a60
@@ -749,6 +749,7 @@ libsystemd_shared_la_SOURCES = \
923a60
 	src/shared/capability.c \
923a60
 	src/shared/capability.h \
923a60
 	src/shared/linux/auto_dev-ioctl.h \
923a60
+	src/shared/linux/fanotify.h \
923a60
 	src/shared/ioprio.h \
923a60
 	src/shared/missing.h \
923a60
 	src/shared/initreq.h \
923a60
diff --git a/configure.ac b/configure.ac
923a60
index 97a29d63fd..3f50887a8d 100644
923a60
--- a/configure.ac
923a60
+++ b/configure.ac
923a60
@@ -310,6 +310,7 @@ RT_LIBS="$LIBS"
923a60
 AC_SUBST(RT_LIBS)
923a60
 LIBS="$save_LIBS"
923a60
 
923a60
+AC_CHECK_FUNCS([fanotify_init fanotify_mark])
923a60
 AC_CHECK_FUNCS([memfd_create])
923a60
 AC_CHECK_FUNCS([__secure_getenv secure_getenv])
923a60
 AC_CHECK_DECLS([gettid, pivot_root, name_to_handle_at, setns, getrandom, renameat2, kcmp, LO_FLAGS_PARTSCAN],
923a60
diff --git a/src/shared/linux/fanotify.h b/src/shared/linux/fanotify.h
923a60
new file mode 100644
923a60
index 0000000000..5cc1a7e676
923a60
--- /dev/null
923a60
+++ b/src/shared/linux/fanotify.h
923a60
@@ -0,0 +1,98 @@
923a60
+#ifndef _LINUX_FANOTIFY_H
923a60
+#define _LINUX_FANOTIFY_H
923a60
+
923a60
+#include <linux/types.h>
923a60
+
923a60
+/* the following events that user-space can register for */
923a60
+#define FAN_ACCESS      0x00000001  /* File was accessed */
923a60
+#define FAN_MODIFY      0x00000002  /* File was modified */
923a60
+#define FAN_CLOSE_WRITE     0x00000008  /* Unwrittable file closed */
923a60
+#define FAN_CLOSE_NOWRITE   0x00000010  /* Writtable file closed */
923a60
+#define FAN_OPEN        0x00000020  /* File was opened */
923a60
+
923a60
+#define FAN_EVENT_ON_CHILD  0x08000000  /* interested in child events */
923a60
+
923a60
+/* FIXME currently Q's have no limit.... */
923a60
+#define FAN_Q_OVERFLOW      0x00004000  /* Event queued overflowed */
923a60
+
923a60
+#define FAN_OPEN_PERM       0x00010000  /* File open in perm check */
923a60
+#define FAN_ACCESS_PERM     0x00020000  /* File accessed in perm check */
923a60
+
923a60
+/* helper events */
923a60
+#define FAN_CLOSE       (FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE) /* close */
923a60
+
923a60
+/* flags used for fanotify_init() */
923a60
+#define FAN_CLOEXEC     0x00000001
923a60
+#define FAN_NONBLOCK        0x00000002
923a60
+
923a60
+#define FAN_ALL_INIT_FLAGS  (FAN_CLOEXEC | FAN_NONBLOCK)
923a60
+
923a60
+/* flags used for fanotify_modify_mark() */
923a60
+#define FAN_MARK_ADD        0x00000001
923a60
+#define FAN_MARK_REMOVE     0x00000002
923a60
+#define FAN_MARK_DONT_FOLLOW    0x00000004
923a60
+#define FAN_MARK_ONLYDIR    0x00000008
923a60
+#define FAN_MARK_MOUNT      0x00000010
923a60
+#define FAN_MARK_IGNORED_MASK   0x00000020
923a60
+#define FAN_MARK_IGNORED_SURV_MODIFY    0x00000040
923a60
+#define FAN_MARK_FLUSH      0x00000080
923a60
+
923a60
+#define FAN_ALL_MARK_FLAGS  (FAN_MARK_ADD |\
923a60
+                 FAN_MARK_REMOVE |\
923a60
+                 FAN_MARK_DONT_FOLLOW |\
923a60
+                 FAN_MARK_ONLYDIR |\
923a60
+                 FAN_MARK_MOUNT |\
923a60
+                 FAN_MARK_IGNORED_MASK |\
923a60
+                 FAN_MARK_IGNORED_SURV_MODIFY)
923a60
+
923a60
+/*
923a60
+ * All of the events - we build the list by hand so that we can add flags in
923a60
+ * the future and not break backward compatibility.  Apps will get only the
923a60
+ * events that they originally wanted.  Be sure to add new events here!
923a60
+ */
923a60
+#define FAN_ALL_EVENTS (FAN_ACCESS |\
923a60
+            FAN_MODIFY |\
923a60
+            FAN_CLOSE |\
923a60
+            FAN_OPEN)
923a60
+
923a60
+/*
923a60
+ * All events which require a permission response from userspace
923a60
+ */
923a60
+#define FAN_ALL_PERM_EVENTS (FAN_OPEN_PERM |\
923a60
+                 FAN_ACCESS_PERM)
923a60
+
923a60
+#define FAN_ALL_OUTGOING_EVENTS (FAN_ALL_EVENTS |\
923a60
+                 FAN_ALL_PERM_EVENTS |\
923a60
+                 FAN_Q_OVERFLOW)
923a60
+
923a60
+#define FANOTIFY_METADATA_VERSION   2
923a60
+
923a60
+struct fanotify_event_metadata {
923a60
+    __u32 event_len;
923a60
+    __u32 vers;
923a60
+    __u64 mask;
923a60
+    __s32 fd;
923a60
+    __s32 pid;
923a60
+} __attribute__ ((packed));
923a60
+
923a60
+struct fanotify_response {
923a60
+    __s32 fd;
923a60
+    __u32 response;
923a60
+} __attribute__ ((packed));
923a60
+
923a60
+/* Legit userspace responses to a _PERM event */
923a60
+#define FAN_ALLOW   0x01
923a60
+#define FAN_DENY    0x02
923a60
+
923a60
+/* Helper functions to deal with fanotify_event_metadata buffers */
923a60
+#define FAN_EVENT_METADATA_LEN (sizeof(struct fanotify_event_metadata))
923a60
+
923a60
+#define FAN_EVENT_NEXT(meta, len) ((len) -= (meta)->event_len, \
923a60
+                   (struct fanotify_event_metadata*)(((char *)(meta)) + \
923a60
+                   (meta)->event_len))
923a60
+
923a60
+#define FAN_EVENT_OK(meta, len) ((long)(len) >= (long)FAN_EVENT_METADATA_LEN && \
923a60
+                (long)(meta)->event_len >= (long)FAN_EVENT_METADATA_LEN && \
923a60
+                (long)(meta)->event_len <= (long)(len))
923a60
+
923a60
+#endif /* _LINUX_FANOTIFY_H */
923a60
diff --git a/src/shared/missing.h b/src/shared/missing.h
923a60
index b33a70cb2c..06a55769a4 100644
923a60
--- a/src/shared/missing.h
923a60
+++ b/src/shared/missing.h
923a60
@@ -156,6 +156,70 @@ static inline int pivot_root(const char *new_root, const char *put_old) {
923a60
 #  endif
923a60
 #endif
923a60
 
923a60
+#ifdef __x86_64__
923a60
+#  ifndef __NR_fanotify_init
923a60
+#    define __NR_fanotify_init 300
923a60
+#  endif
923a60
+#  ifndef __NR_fanotify_mark
923a60
+#    define __NR_fanotify_mark 301
923a60
+#  endif
923a60
+#elif defined _MIPS_SIM
923a60
+#  if _MIPS_SIM == _MIPS_SIM_ABI32
923a60
+#    ifndef __NR_fanotify_init
923a60
+#      define __NR_fanotify_init 4336
923a60
+#    endif
923a60
+#    ifndef __NR_fanotify_mark
923a60
+#      define __NR_fanotify_mark 4337
923a60
+#    endif
923a60
+#  elif _MIPS_SIM == _MIPS_SIM_NABI32
923a60
+#    ifndef __NR_fanotify_init
923a60
+#      define __NR_fanotify_init 6300
923a60
+#    endif
923a60
+#    ifndef __NR_fanotify_mark
923a60
+#      define __NR_fanotify_mark 6301
923a60
+#    endif
923a60
+#  elif _MIPS_SIM == _MIPS_SIM_ABI64
923a60
+#    ifndef __NR_fanotify_init
923a60
+#      define __NR_fanotify_init 5295
923a60
+#    endif
923a60
+#    ifndef __NR_fanotify_mark
923a60
+#      define __NR_fanotify_mark 5296
923a60
+#    endif
923a60
+#  endif
923a60
+#else
923a60
+#  ifndef __NR_fanotify_init
923a60
+#    define __NR_fanotify_init 338
923a60
+#  endif
923a60
+#  ifndef __NR_fanotify_mark
923a60
+#    define __NR_fanotify_mark 339
923a60
+#  endif
923a60
+#endif
923a60
+
923a60
+#ifndef HAVE_FANOTIFY_INIT
923a60
+static inline int fanotify_init(unsigned int flags, unsigned int event_f_flags) {
923a60
+        return syscall(__NR_fanotify_init, flags, event_f_flags);
923a60
+}
923a60
+#endif
923a60
+
923a60
+#ifndef HAVE_FANOTIFY_MARK
923a60
+static inline int fanotify_mark(int fanotify_fd, unsigned int flags, uint64_t mask,
923a60
+                                int dfd, const char *pathname) {
923a60
+#if defined _MIPS_SIM && _MIPS_SIM == _MIPS_SIM_ABI32 || defined __powerpc__ && !defined __powerpc64__ \
923a60
+    || defined __arm__ && !defined __aarch64__
923a60
+        union {
923a60
+                uint64_t _64;
923a60
+                uint32_t _32[2];
923a60
+        } _mask;
923a60
+        _mask._64 = mask;
923a60
+
923a60
+        return syscall(__NR_fanotify_mark, fanotify_fd, flags,
923a60
+                       _mask._32[0], _mask._32[1], dfd, pathname);
923a60
+#else
923a60
+        return syscall(__NR_fanotify_mark, fanotify_fd, flags, mask, dfd, pathname);
923a60
+#endif
923a60
+}
923a60
+#endif
923a60
+
923a60
 #ifndef HAVE_MEMFD_CREATE
923a60
 static inline int memfd_create(const char *name, unsigned int flags) {
923a60
         return syscall(__NR_memfd_create, name, flags);