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