aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAmir Goldstein <amir73il@gmail.com>2018-10-03 17:25:38 -0400
committerJan Kara <jack@suse.cz>2018-10-08 07:48:45 -0400
commitd0a6a87e40da49cfc7954c491d3065a25a641b29 (patch)
tree49e34224bcda4f0a436beb3b4a4e0214606dfaa1
parentbdd5a46fe30653cb4d26c7c787a22159bf79eed9 (diff)
fanotify: support reporting thread id instead of process id
In order to identify which thread triggered the event in a multi-threaded program, add the FAN_REPORT_TID flag in fanotify_init to opt-in for reporting the event creator's thread id information. Signed-off-by: nixiaoming <nixiaoming@huawei.com> Signed-off-by: Amir Goldstein <amir73il@gmail.com> Signed-off-by: Jan Kara <jack@suse.cz>
-rw-r--r--fs/notify/fanotify/fanotify.c9
-rw-r--r--fs/notify/fanotify/fanotify.h2
-rw-r--r--fs/notify/fanotify/fanotify_user.c4
-rw-r--r--include/linux/fanotify.h1
-rw-r--r--include/uapi/linux/fanotify.h3
5 files changed, 13 insertions, 6 deletions
diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c
index 361e3a0a445c..5769cf3ff035 100644
--- a/fs/notify/fanotify/fanotify.c
+++ b/fs/notify/fanotify/fanotify.c
@@ -25,7 +25,7 @@ static bool should_merge(struct fsnotify_event *old_fsn,
25 old = FANOTIFY_E(old_fsn); 25 old = FANOTIFY_E(old_fsn);
26 new = FANOTIFY_E(new_fsn); 26 new = FANOTIFY_E(new_fsn);
27 27
28 if (old_fsn->inode == new_fsn->inode && old->tgid == new->tgid && 28 if (old_fsn->inode == new_fsn->inode && old->pid == new->pid &&
29 old->path.mnt == new->path.mnt && 29 old->path.mnt == new->path.mnt &&
30 old->path.dentry == new->path.dentry) 30 old->path.dentry == new->path.dentry)
31 return true; 31 return true;
@@ -171,7 +171,10 @@ struct fanotify_event_info *fanotify_alloc_event(struct fsnotify_group *group,
171 goto out; 171 goto out;
172init: __maybe_unused 172init: __maybe_unused
173 fsnotify_init_event(&event->fse, inode, mask); 173 fsnotify_init_event(&event->fse, inode, mask);
174 event->tgid = get_pid(task_tgid(current)); 174 if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
175 event->pid = get_pid(task_pid(current));
176 else
177 event->pid = get_pid(task_tgid(current));
175 if (path) { 178 if (path) {
176 event->path = *path; 179 event->path = *path;
177 path_get(&event->path); 180 path_get(&event->path);
@@ -270,7 +273,7 @@ static void fanotify_free_event(struct fsnotify_event *fsn_event)
270 273
271 event = FANOTIFY_E(fsn_event); 274 event = FANOTIFY_E(fsn_event);
272 path_put(&event->path); 275 path_put(&event->path);
273 put_pid(event->tgid); 276 put_pid(event->pid);
274 if (fanotify_is_perm_event(fsn_event->mask)) { 277 if (fanotify_is_perm_event(fsn_event->mask)) {
275 kmem_cache_free(fanotify_perm_event_cachep, 278 kmem_cache_free(fanotify_perm_event_cachep,
276 FANOTIFY_PE(fsn_event)); 279 FANOTIFY_PE(fsn_event));
diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h
index 88a8290a61cb..ea05b8a401e7 100644
--- a/fs/notify/fanotify/fanotify.h
+++ b/fs/notify/fanotify/fanotify.h
@@ -19,7 +19,7 @@ struct fanotify_event_info {
19 * during this object's lifetime 19 * during this object's lifetime
20 */ 20 */
21 struct path path; 21 struct path path;
22 struct pid *tgid; 22 struct pid *pid;
23}; 23};
24 24
25/* 25/*
diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
index 14594e491d2b..e03be5071362 100644
--- a/fs/notify/fanotify/fanotify_user.c
+++ b/fs/notify/fanotify/fanotify_user.c
@@ -132,7 +132,7 @@ static int fill_event_metadata(struct fsnotify_group *group,
132 metadata->vers = FANOTIFY_METADATA_VERSION; 132 metadata->vers = FANOTIFY_METADATA_VERSION;
133 metadata->reserved = 0; 133 metadata->reserved = 0;
134 metadata->mask = fsn_event->mask & FANOTIFY_OUTGOING_EVENTS; 134 metadata->mask = fsn_event->mask & FANOTIFY_OUTGOING_EVENTS;
135 metadata->pid = pid_vnr(event->tgid); 135 metadata->pid = pid_vnr(event->pid);
136 if (unlikely(fsn_event->mask & FAN_Q_OVERFLOW)) 136 if (unlikely(fsn_event->mask & FAN_Q_OVERFLOW))
137 metadata->fd = FAN_NOFD; 137 metadata->fd = FAN_NOFD;
138 else { 138 else {
@@ -944,7 +944,7 @@ COMPAT_SYSCALL_DEFINE6(fanotify_mark,
944 */ 944 */
945static int __init fanotify_user_setup(void) 945static int __init fanotify_user_setup(void)
946{ 946{
947 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 6); 947 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 7);
948 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9); 948 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
949 949
950 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, 950 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
diff --git a/include/linux/fanotify.h b/include/linux/fanotify.h
index caf55c67fc6c..a5a60691e48b 100644
--- a/include/linux/fanotify.h
+++ b/include/linux/fanotify.h
@@ -19,6 +19,7 @@
19 FAN_CLASS_PRE_CONTENT) 19 FAN_CLASS_PRE_CONTENT)
20 20
21#define FANOTIFY_INIT_FLAGS (FANOTIFY_CLASS_BITS | \ 21#define FANOTIFY_INIT_FLAGS (FANOTIFY_CLASS_BITS | \
22 FAN_REPORT_TID | \
22 FAN_CLOEXEC | FAN_NONBLOCK | \ 23 FAN_CLOEXEC | FAN_NONBLOCK | \
23 FAN_UNLIMITED_QUEUE | FAN_UNLIMITED_MARKS) 24 FAN_UNLIMITED_QUEUE | FAN_UNLIMITED_MARKS)
24 25
diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.h
index d0c05de670ef..b86740d1c50a 100644
--- a/include/uapi/linux/fanotify.h
+++ b/include/uapi/linux/fanotify.h
@@ -40,6 +40,9 @@
40#define FAN_UNLIMITED_MARKS 0x00000020 40#define FAN_UNLIMITED_MARKS 0x00000020
41#define FAN_ENABLE_AUDIT 0x00000040 41#define FAN_ENABLE_AUDIT 0x00000040
42 42
43/* Flags to determine fanotify event format */
44#define FAN_REPORT_TID 0x00000100 /* event->pid is thread id */
45
43/* Deprecated - do not use this in programs and do not add new flags here! */ 46/* Deprecated - do not use this in programs and do not add new flags here! */
44#define FAN_ALL_INIT_FLAGS (FAN_CLOEXEC | FAN_NONBLOCK | \ 47#define FAN_ALL_INIT_FLAGS (FAN_CLOEXEC | FAN_NONBLOCK | \
45 FAN_ALL_CLASS_BITS | FAN_UNLIMITED_QUEUE |\ 48 FAN_ALL_CLASS_BITS | FAN_UNLIMITED_QUEUE |\