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