Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Amir Goldstein and committed by
Jan Kara
d0a6a87e bdd5a46f

+13 -6
+6 -3
fs/notify/fanotify/fanotify.c
··· 25 25 old = FANOTIFY_E(old_fsn); 26 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 29 old->path.mnt == new->path.mnt && 30 30 old->path.dentry == new->path.dentry) 31 31 return true; ··· 171 171 goto out; 172 172 init: __maybe_unused 173 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 178 if (path) { 176 179 event->path = *path; 177 180 path_get(&event->path); ··· 273 270 274 271 event = FANOTIFY_E(fsn_event); 275 272 path_put(&event->path); 276 - put_pid(event->tgid); 273 + put_pid(event->pid); 277 274 if (fanotify_is_perm_event(fsn_event->mask)) { 278 275 kmem_cache_free(fanotify_perm_event_cachep, 279 276 FANOTIFY_PE(fsn_event));
+1 -1
fs/notify/fanotify/fanotify.h
··· 19 19 * during this object's lifetime 20 20 */ 21 21 struct path path; 22 - struct pid *tgid; 22 + struct pid *pid; 23 23 }; 24 24 25 25 /*
+2 -2
fs/notify/fanotify/fanotify_user.c
··· 132 132 metadata->vers = FANOTIFY_METADATA_VERSION; 133 133 metadata->reserved = 0; 134 134 metadata->mask = fsn_event->mask & FANOTIFY_OUTGOING_EVENTS; 135 - metadata->pid = pid_vnr(event->tgid); 135 + metadata->pid = pid_vnr(event->pid); 136 136 if (unlikely(fsn_event->mask & FAN_Q_OVERFLOW)) 137 137 metadata->fd = FAN_NOFD; 138 138 else { ··· 944 944 */ 945 945 static 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 948 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9); 949 949 950 950 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
+1
include/linux/fanotify.h
··· 19 19 FAN_CLASS_PRE_CONTENT) 20 20 21 21 #define FANOTIFY_INIT_FLAGS (FANOTIFY_CLASS_BITS | \ 22 + FAN_REPORT_TID | \ 22 23 FAN_CLOEXEC | FAN_NONBLOCK | \ 23 24 FAN_UNLIMITED_QUEUE | FAN_UNLIMITED_MARKS) 24 25
+3
include/uapi/linux/fanotify.h
··· 40 40 #define FAN_UNLIMITED_MARKS 0x00000020 41 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 46 /* Deprecated - do not use this in programs and do not add new flags here! */ 44 47 #define FAN_ALL_INIT_FLAGS (FAN_CLOEXEC | FAN_NONBLOCK | \ 45 48 FAN_ALL_CLASS_BITS | FAN_UNLIMITED_QUEUE |\