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