1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2018 Huawei. All Rights Reserved.
4 *
5 * Started by nixiaoming <nixiaoming@huawei.com>
6 *
7 * DESCRIPTION
8 * After fanotify_init adds flags FAN_REPORT_TID,
9 * check whether the program can accurately identify which thread id
10 * in the multithreaded program triggered the event.
11 *
12 */
13 #define _GNU_SOURCE
14 #include "config.h"
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <fcntl.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/syscall.h>
26 #include <pthread.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <linux/limits.h>
30 #include "tst_test.h"
31 #include "tst_safe_pthread.h"
32 #include "fanotify.h"
33
34 #if defined(HAVE_SYS_FANOTIFY_H)
35 #include <sys/fanotify.h>
36
37 #define gettid() syscall(SYS_gettid)
38 static int tid;
39
thread_create_file(void * arg LTP_ATTRIBUTE_UNUSED)40 void *thread_create_file(void *arg LTP_ATTRIBUTE_UNUSED)
41 {
42 char tid_file[64] = {0};
43
44 tid = gettid();
45 snprintf(tid_file, sizeof(tid_file), "test_tid_%d", tid);
46 SAFE_FILE_PRINTF(tid_file, "1");
47
48 pthread_exit(0);
49 }
50
51 static unsigned int tcases[] = {
52 FAN_CLASS_NOTIF,
53 FAN_CLASS_NOTIF | FAN_REPORT_TID
54 };
55
test01(unsigned int i)56 void test01(unsigned int i)
57 {
58 int ret;
59 pthread_t p_id;
60 struct fanotify_event_metadata event;
61 int fd_notify;
62 int tgid = getpid();
63
64 tst_res(TINFO, "Test #%u: %s FAN_REPORT_TID: tgid=%d, tid=%d, event.pid=%d",
65 i, (tcases[i] & FAN_REPORT_TID) ? "with" : "without",
66 tgid, tid, event.pid);
67
68 fd_notify = fanotify_init(tcases[i], 0);
69 if (fd_notify < 0) {
70 if (errno == EINVAL && (tcases[i] & FAN_REPORT_TID)) {
71 tst_res(TCONF,
72 "FAN_REPORT_TID not supported in kernel?");
73 return;
74 }
75 tst_brk(TBROK | TERRNO, "fanotify_init(0x%x, 0) failed",
76 tcases[i]);
77 }
78
79 ret = fanotify_mark(fd_notify, FAN_MARK_ADD,
80 FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD, AT_FDCWD, ".");
81 if (ret != 0)
82 tst_brk(TBROK, "fanotify_mark FAN_MARK_ADD fail ret=%d", ret);
83
84 SAFE_PTHREAD_CREATE(&p_id, NULL, thread_create_file, NULL);
85
86 SAFE_READ(0, fd_notify, &event, sizeof(struct fanotify_event_metadata));
87
88 if ((tcases[i] & FAN_REPORT_TID) && event.pid == tid)
89 tst_res(TPASS, "event.pid == tid");
90 else if (!(tcases[i] & FAN_REPORT_TID) && event.pid == tgid)
91 tst_res(TPASS, "event.pid == tgid");
92 else
93 tst_res(TFAIL, "unexpected event.pid value");
94
95 if (event.fd != FAN_NOFD)
96 SAFE_CLOSE(event.fd);
97 SAFE_CLOSE(fd_notify);
98 SAFE_PTHREAD_JOIN(p_id, NULL);
99 }
100
setup(void)101 static void setup(void)
102 {
103 int fd;
104
105 fd = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF, O_RDONLY);
106 SAFE_CLOSE(fd);
107 }
108
109 static struct tst_test test = {
110 .setup = setup,
111 .test = test01,
112 .tcnt = ARRAY_SIZE(tcases),
113 .needs_tmpdir = 1,
114 .needs_root = 1,
115 };
116
117 #else
118 TST_TEST_TCONF("system doesn't have required fanotify support");
119 #endif
120