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) 2021 CTERA Networks. All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * Started by Amir Goldstein <[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 *
11*49cdfc7eSAndroid Build Coastguard Worker * Check that dnotify DN_RENAME event is reported only on rename inside same parent.
12*49cdfc7eSAndroid Build Coastguard Worker */
13*49cdfc7eSAndroid Build Coastguard Worker
14*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
19*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard Worker #define TEST_DIR "test_dir"
22*49cdfc7eSAndroid Build Coastguard Worker #define TEST_DIR2 "test_dir2"
23*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE "test_file"
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker #define TEST_SIG (SIGRTMIN+1)
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker static int parent_fd, subdir_fd;
28*49cdfc7eSAndroid Build Coastguard Worker static int got_parent_event, got_subdir_event;
29*49cdfc7eSAndroid Build Coastguard Worker
dnotify_handler(int sig,siginfo_t * si,void * data LTP_ATTRIBUTE_UNUSED)30*49cdfc7eSAndroid Build Coastguard Worker static void dnotify_handler(int sig, siginfo_t *si, void *data LTP_ATTRIBUTE_UNUSED)
31*49cdfc7eSAndroid Build Coastguard Worker {
32*49cdfc7eSAndroid Build Coastguard Worker if (si->si_fd == parent_fd)
33*49cdfc7eSAndroid Build Coastguard Worker got_parent_event = 1;
34*49cdfc7eSAndroid Build Coastguard Worker else if (si->si_fd == subdir_fd)
35*49cdfc7eSAndroid Build Coastguard Worker got_subdir_event = 1;
36*49cdfc7eSAndroid Build Coastguard Worker else
37*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "Got unexpected signal %d with si_fd %d", sig, si->si_fd);
38*49cdfc7eSAndroid Build Coastguard Worker }
39*49cdfc7eSAndroid Build Coastguard Worker
setup_dnotify(int fd)40*49cdfc7eSAndroid Build Coastguard Worker static void setup_dnotify(int fd)
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker struct sigaction act;
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker act.sa_sigaction = dnotify_handler;
45*49cdfc7eSAndroid Build Coastguard Worker sigemptyset(&act.sa_mask);
46*49cdfc7eSAndroid Build Coastguard Worker act.sa_flags = SA_SIGINFO;
47*49cdfc7eSAndroid Build Coastguard Worker sigaction(TEST_SIG, &act, NULL);
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker TEST(fcntl(fd, F_SETSIG, TEST_SIG));
50*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != 0) {
51*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "F_SETSIG failed errno = %d : %s",
52*49cdfc7eSAndroid Build Coastguard Worker TST_ERR, strerror(TST_ERR));
53*49cdfc7eSAndroid Build Coastguard Worker }
54*49cdfc7eSAndroid Build Coastguard Worker
55*49cdfc7eSAndroid Build Coastguard Worker TEST(fcntl(fd, F_NOTIFY, DN_RENAME|DN_MULTISHOT));
56*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != 0) {
57*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "F_NOTIFY failed errno = %d : %s",
58*49cdfc7eSAndroid Build Coastguard Worker TST_ERR, strerror(TST_ERR));
59*49cdfc7eSAndroid Build Coastguard Worker }
60*49cdfc7eSAndroid Build Coastguard Worker }
61*49cdfc7eSAndroid Build Coastguard Worker
verify_dnotify(void)62*49cdfc7eSAndroid Build Coastguard Worker static void verify_dnotify(void)
63*49cdfc7eSAndroid Build Coastguard Worker {
64*49cdfc7eSAndroid Build Coastguard Worker parent_fd = SAFE_OPEN(".", O_RDONLY);
65*49cdfc7eSAndroid Build Coastguard Worker subdir_fd = SAFE_OPEN(TEST_DIR, O_RDONLY);
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker /* Watch renames inside ".", but not in and out of "." */
68*49cdfc7eSAndroid Build Coastguard Worker setup_dnotify(parent_fd);
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker /* Also watch for renames inside subdir, but not in and out of subdir */
71*49cdfc7eSAndroid Build Coastguard Worker setup_dnotify(subdir_fd);
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker /* Rename file from "." to subdir should not generate DN_RENAME on either */
74*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Testing no DN_RENAME on rename from parent to subdir");
75*49cdfc7eSAndroid Build Coastguard Worker SAFE_RENAME(TEST_FILE, TEST_DIR "/" TEST_FILE);
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker if (got_parent_event)
78*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Got unexpected event on parent");
79*49cdfc7eSAndroid Build Coastguard Worker else
80*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "No event on parent as expected");
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker if (got_subdir_event)
83*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Got unexpected event on subdir");
84*49cdfc7eSAndroid Build Coastguard Worker else
85*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "No event on subdir as expected");
86*49cdfc7eSAndroid Build Coastguard Worker
87*49cdfc7eSAndroid Build Coastguard Worker /* Rename subdir itself should generate DN_RENAME on ".", but not on itself */
88*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Testing DN_RENAME on rename of subdir itself");
89*49cdfc7eSAndroid Build Coastguard Worker SAFE_RENAME(TEST_DIR, TEST_DIR2);
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker if (got_parent_event)
92*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Got event on parent as expected");
93*49cdfc7eSAndroid Build Coastguard Worker else
94*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Missing event on parent");
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker if (got_subdir_event)
97*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Got unexpected event on subdir");
98*49cdfc7eSAndroid Build Coastguard Worker else
99*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "No event on subdir as expected");
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(parent_fd);
102*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(subdir_fd);
103*49cdfc7eSAndroid Build Coastguard Worker
104*49cdfc7eSAndroid Build Coastguard Worker /* Cleanup before rerun */
105*49cdfc7eSAndroid Build Coastguard Worker SAFE_RENAME(TEST_DIR2 "/" TEST_FILE, TEST_FILE);
106*49cdfc7eSAndroid Build Coastguard Worker SAFE_RENAME(TEST_DIR2, TEST_DIR);
107*49cdfc7eSAndroid Build Coastguard Worker got_parent_event = 0;
108*49cdfc7eSAndroid Build Coastguard Worker got_subdir_event = 0;
109*49cdfc7eSAndroid Build Coastguard Worker }
110*49cdfc7eSAndroid Build Coastguard Worker
setup(void)111*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
112*49cdfc7eSAndroid Build Coastguard Worker {
113*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(TEST_DIR, 00700);
114*49cdfc7eSAndroid Build Coastguard Worker SAFE_TOUCH(TEST_FILE, 0666, NULL);
115*49cdfc7eSAndroid Build Coastguard Worker }
116*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)117*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
118*49cdfc7eSAndroid Build Coastguard Worker {
119*49cdfc7eSAndroid Build Coastguard Worker if (parent_fd > 0)
120*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(parent_fd);
121*49cdfc7eSAndroid Build Coastguard Worker
122*49cdfc7eSAndroid Build Coastguard Worker if (subdir_fd > 0)
123*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(subdir_fd);
124*49cdfc7eSAndroid Build Coastguard Worker }
125*49cdfc7eSAndroid Build Coastguard Worker
126*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
127*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
128*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
129*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
130*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_dnotify,
131*49cdfc7eSAndroid Build Coastguard Worker .needs_kconfigs = (const char *[]) { "CONFIG_DNOTIFY=y", NULL },
132*49cdfc7eSAndroid Build Coastguard Worker };
133