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) 2020 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 * DESCRIPTION
8*49cdfc7eSAndroid Build Coastguard Worker * Check that dnotify event is reported to both parent and subdir
9*49cdfc7eSAndroid Build Coastguard Worker */
10*49cdfc7eSAndroid Build Coastguard Worker
11*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
12*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
15*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
16*49cdfc7eSAndroid Build Coastguard Worker
17*49cdfc7eSAndroid Build Coastguard Worker #define TEST_DIR "test_dir"
18*49cdfc7eSAndroid Build Coastguard Worker
19*49cdfc7eSAndroid Build Coastguard Worker #define TEST_SIG SIGRTMIN+1
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard Worker static int parent_fd, subdir_fd;
22*49cdfc7eSAndroid Build Coastguard Worker static int got_parent_event, got_subdir_event;
23*49cdfc7eSAndroid Build Coastguard Worker
dnotify_handler(int sig,siginfo_t * si,void * data)24*49cdfc7eSAndroid Build Coastguard Worker static void dnotify_handler(int sig, siginfo_t *si, void *data __attribute__((unused)))
25*49cdfc7eSAndroid Build Coastguard Worker {
26*49cdfc7eSAndroid Build Coastguard Worker if (si->si_fd == parent_fd)
27*49cdfc7eSAndroid Build Coastguard Worker got_parent_event = 1;
28*49cdfc7eSAndroid Build Coastguard Worker else if (si->si_fd == subdir_fd)
29*49cdfc7eSAndroid Build Coastguard Worker got_subdir_event = 1;
30*49cdfc7eSAndroid Build Coastguard Worker else
31*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "Got unexpected signal %d with si_fd %d", sig, si->si_fd);
32*49cdfc7eSAndroid Build Coastguard Worker }
33*49cdfc7eSAndroid Build Coastguard Worker
setup_dnotify(int fd)34*49cdfc7eSAndroid Build Coastguard Worker static void setup_dnotify(int fd)
35*49cdfc7eSAndroid Build Coastguard Worker {
36*49cdfc7eSAndroid Build Coastguard Worker struct sigaction act;
37*49cdfc7eSAndroid Build Coastguard Worker
38*49cdfc7eSAndroid Build Coastguard Worker act.sa_sigaction = dnotify_handler;
39*49cdfc7eSAndroid Build Coastguard Worker sigemptyset(&act.sa_mask);
40*49cdfc7eSAndroid Build Coastguard Worker act.sa_flags = SA_SIGINFO;
41*49cdfc7eSAndroid Build Coastguard Worker sigaction(TEST_SIG, &act, NULL);
42*49cdfc7eSAndroid Build Coastguard Worker
43*49cdfc7eSAndroid Build Coastguard Worker TEST(fcntl(fd, F_SETSIG, TEST_SIG));
44*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != 0) {
45*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "F_SETSIG failed errno = %d : %s",
46*49cdfc7eSAndroid Build Coastguard Worker TST_ERR, strerror(TST_ERR));
47*49cdfc7eSAndroid Build Coastguard Worker }
48*49cdfc7eSAndroid Build Coastguard Worker TEST(fcntl(fd, F_NOTIFY, DN_ATTRIB|DN_MULTISHOT));
49*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != 0) {
50*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "F_NOTIFY failed errno = %d : %s",
51*49cdfc7eSAndroid Build Coastguard Worker TST_ERR, strerror(TST_ERR));
52*49cdfc7eSAndroid Build Coastguard Worker }
53*49cdfc7eSAndroid Build Coastguard Worker }
54*49cdfc7eSAndroid Build Coastguard Worker
verify_dnotify(void)55*49cdfc7eSAndroid Build Coastguard Worker static void verify_dnotify(void)
56*49cdfc7eSAndroid Build Coastguard Worker {
57*49cdfc7eSAndroid Build Coastguard Worker parent_fd = SAFE_OPEN(".", O_RDONLY);
58*49cdfc7eSAndroid Build Coastguard Worker subdir_fd = SAFE_OPEN(TEST_DIR, O_RDONLY);
59*49cdfc7eSAndroid Build Coastguard Worker /* Watch "." and its children for changes */
60*49cdfc7eSAndroid Build Coastguard Worker setup_dnotify(parent_fd);
61*49cdfc7eSAndroid Build Coastguard Worker /* Also watch subdir itself for changes */
62*49cdfc7eSAndroid Build Coastguard Worker setup_dnotify(subdir_fd);
63*49cdfc7eSAndroid Build Coastguard Worker /* Generate DN_ATTRIB event on subdir that should send a signal on both fds */
64*49cdfc7eSAndroid Build Coastguard Worker SAFE_CHMOD(TEST_DIR, 0755);
65*49cdfc7eSAndroid Build Coastguard Worker if (got_parent_event)
66*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Got event on parent as expected");
67*49cdfc7eSAndroid Build Coastguard Worker else
68*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Missing event on parent");
69*49cdfc7eSAndroid Build Coastguard Worker if (got_subdir_event)
70*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Got event on subdir as expected");
71*49cdfc7eSAndroid Build Coastguard Worker else
72*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Missing event on subdir");
73*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(parent_fd);
74*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(subdir_fd);
75*49cdfc7eSAndroid Build Coastguard Worker }
76*49cdfc7eSAndroid Build Coastguard Worker
setup(void)77*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
78*49cdfc7eSAndroid Build Coastguard Worker {
79*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(TEST_DIR, 00700);
80*49cdfc7eSAndroid Build Coastguard Worker }
81*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)82*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
83*49cdfc7eSAndroid Build Coastguard Worker {
84*49cdfc7eSAndroid Build Coastguard Worker if (parent_fd > 0)
85*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(parent_fd);
86*49cdfc7eSAndroid Build Coastguard Worker if (subdir_fd > 0)
87*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(subdir_fd);
88*49cdfc7eSAndroid Build Coastguard Worker }
89*49cdfc7eSAndroid Build Coastguard Worker
90*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
91*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
92*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
93*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
94*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_dnotify,
95*49cdfc7eSAndroid Build Coastguard Worker .needs_kconfigs = (const char *[]) { "CONFIG_DNOTIFY=y", NULL },
96*49cdfc7eSAndroid Build Coastguard Worker };
97