Commit d0a6a87e authored by Amir Goldstein's avatar Amir Goldstein Committed by Jan Kara

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: default avatarnixiaoming <nixiaoming@huawei.com>
Signed-off-by: default avatarAmir Goldstein <amir73il@gmail.com>
Signed-off-by: default avatarJan Kara <jack@suse.cz>
parent bdd5a46f
...@@ -25,7 +25,7 @@ static bool should_merge(struct fsnotify_event *old_fsn, ...@@ -25,7 +25,7 @@ static bool should_merge(struct fsnotify_event *old_fsn,
old = FANOTIFY_E(old_fsn); old = FANOTIFY_E(old_fsn);
new = FANOTIFY_E(new_fsn); new = FANOTIFY_E(new_fsn);
if (old_fsn->inode == new_fsn->inode && old->tgid == new->tgid && if (old_fsn->inode == new_fsn->inode && old->pid == new->pid &&
old->path.mnt == new->path.mnt && old->path.mnt == new->path.mnt &&
old->path.dentry == new->path.dentry) old->path.dentry == new->path.dentry)
return true; return true;
...@@ -171,7 +171,10 @@ struct fanotify_event_info *fanotify_alloc_event(struct fsnotify_group *group, ...@@ -171,7 +171,10 @@ struct fanotify_event_info *fanotify_alloc_event(struct fsnotify_group *group,
goto out; goto out;
init: __maybe_unused init: __maybe_unused
fsnotify_init_event(&event->fse, inode, mask); fsnotify_init_event(&event->fse, inode, mask);
event->tgid = get_pid(task_tgid(current)); if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
event->pid = get_pid(task_pid(current));
else
event->pid = get_pid(task_tgid(current));
if (path) { if (path) {
event->path = *path; event->path = *path;
path_get(&event->path); path_get(&event->path);
...@@ -270,7 +273,7 @@ static void fanotify_free_event(struct fsnotify_event *fsn_event) ...@@ -270,7 +273,7 @@ static void fanotify_free_event(struct fsnotify_event *fsn_event)
event = FANOTIFY_E(fsn_event); event = FANOTIFY_E(fsn_event);
path_put(&event->path); path_put(&event->path);
put_pid(event->tgid); put_pid(event->pid);
if (fanotify_is_perm_event(fsn_event->mask)) { if (fanotify_is_perm_event(fsn_event->mask)) {
kmem_cache_free(fanotify_perm_event_cachep, kmem_cache_free(fanotify_perm_event_cachep,
FANOTIFY_PE(fsn_event)); FANOTIFY_PE(fsn_event));
......
...@@ -19,7 +19,7 @@ struct fanotify_event_info { ...@@ -19,7 +19,7 @@ struct fanotify_event_info {
* during this object's lifetime * during this object's lifetime
*/ */
struct path path; struct path path;
struct pid *tgid; struct pid *pid;
}; };
/* /*
......
...@@ -132,7 +132,7 @@ static int fill_event_metadata(struct fsnotify_group *group, ...@@ -132,7 +132,7 @@ static int fill_event_metadata(struct fsnotify_group *group,
metadata->vers = FANOTIFY_METADATA_VERSION; metadata->vers = FANOTIFY_METADATA_VERSION;
metadata->reserved = 0; metadata->reserved = 0;
metadata->mask = fsn_event->mask & FANOTIFY_OUTGOING_EVENTS; metadata->mask = fsn_event->mask & FANOTIFY_OUTGOING_EVENTS;
metadata->pid = pid_vnr(event->tgid); metadata->pid = pid_vnr(event->pid);
if (unlikely(fsn_event->mask & FAN_Q_OVERFLOW)) if (unlikely(fsn_event->mask & FAN_Q_OVERFLOW))
metadata->fd = FAN_NOFD; metadata->fd = FAN_NOFD;
else { else {
...@@ -944,7 +944,7 @@ COMPAT_SYSCALL_DEFINE6(fanotify_mark, ...@@ -944,7 +944,7 @@ COMPAT_SYSCALL_DEFINE6(fanotify_mark,
*/ */
static int __init fanotify_user_setup(void) static int __init fanotify_user_setup(void)
{ {
BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 6); BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 7);
BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9); BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
FAN_CLASS_PRE_CONTENT) FAN_CLASS_PRE_CONTENT)
#define FANOTIFY_INIT_FLAGS (FANOTIFY_CLASS_BITS | \ #define FANOTIFY_INIT_FLAGS (FANOTIFY_CLASS_BITS | \
FAN_REPORT_TID | \
FAN_CLOEXEC | FAN_NONBLOCK | \ FAN_CLOEXEC | FAN_NONBLOCK | \
FAN_UNLIMITED_QUEUE | FAN_UNLIMITED_MARKS) FAN_UNLIMITED_QUEUE | FAN_UNLIMITED_MARKS)
......
...@@ -40,6 +40,9 @@ ...@@ -40,6 +40,9 @@
#define FAN_UNLIMITED_MARKS 0x00000020 #define FAN_UNLIMITED_MARKS 0x00000020
#define FAN_ENABLE_AUDIT 0x00000040 #define FAN_ENABLE_AUDIT 0x00000040
/* Flags to determine fanotify event format */
#define FAN_REPORT_TID 0x00000100 /* event->pid is thread id */
/* Deprecated - do not use this in programs and do not add new flags here! */ /* Deprecated - do not use this in programs and do not add new flags here! */
#define FAN_ALL_INIT_FLAGS (FAN_CLOEXEC | FAN_NONBLOCK | \ #define FAN_ALL_INIT_FLAGS (FAN_CLOEXEC | FAN_NONBLOCK | \
FAN_ALL_CLASS_BITS | FAN_UNLIMITED_QUEUE |\ FAN_ALL_CLASS_BITS | FAN_UNLIMITED_QUEUE |\
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment