1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2012 Linux Test Project. All Rights Reserved.
4 * Ngie Cooper, April 2012
5 *
6 * DESCRIPTION
7 * verify that IN_DELETE_SELF functions as expected
8 *
9 * ALGORITHM
10 * This testcase creates a temporary directory, then add watches to a
11 * predefined file and subdirectory, and delete the file and directory to
12 * ensure that the IN_DELETE_SELF event is captured properly.
13 *
14 * Because of how the inotify(7) API is designed, we also need to catch the
15 * IN_ATTRIB and IN_IGNORED events.
16 */
17
18 #include "config.h"
19
20 #if defined(HAVE_SYS_INOTIFY_H)
21 # include <sys/inotify.h>
22 #endif
23 #include <errno.h>
24 #include <string.h>
25 #include "tst_test.h"
26 #include "inotify.h"
27
28 #if defined(HAVE_SYS_INOTIFY_H)
29
30 #define EVENT_MAX 1024
31 /* size of the event structure, not counting name */
32 #define EVENT_SIZE (sizeof(struct inotify_event))
33 /* reasonable guess as to size of 1024 events */
34 #define EVENT_BUF_LEN (EVENT_MAX * (EVENT_SIZE + 16))
35
36
37 #define BUF_SIZE 256
38
39 struct event_t {
40 char name[BUF_SIZE];
41 unsigned int mask;
42 };
43
44 #define TEST_DIR "test_dir"
45 #define TEST_FILE "test_file"
46
47 struct event_t event_set[EVENT_MAX];
48
49 char event_buf[EVENT_BUF_LEN];
50
51 int fd_notify, reap_wd_file, reap_wd_dir, wd_dir, wd_file;
52
53 static struct tst_kern_exv kvers[] = {
54 { "RHEL5", "2.6.18-132" },
55 { NULL, NULL },
56 };
57
cleanup(void)58 static void cleanup(void)
59 {
60 if (reap_wd_dir && myinotify_rm_watch(fd_notify, wd_dir) == -1)
61 tst_res(TWARN,
62 "inotify_rm_watch(%d, %d) [1] failed", fd_notify,
63 wd_dir);
64
65 if (reap_wd_file && myinotify_rm_watch(fd_notify, wd_file) == -1)
66 tst_res(TWARN,
67 "inotify_rm_watch(%d, %d) [2] failed", fd_notify,
68 wd_file);
69
70 if (fd_notify > 0)
71 SAFE_CLOSE(fd_notify);
72 }
73
setup(void)74 static void setup(void)
75 {
76 fd_notify = SAFE_MYINOTIFY_INIT();
77 }
78
verify_inotify(void)79 void verify_inotify(void)
80 {
81 int i = 0, test_num = 0, len;
82 int test_cnt = 0;
83
84 SAFE_MKDIR(TEST_DIR, 00700);
85 close(SAFE_CREAT(TEST_FILE, 00600));
86
87 wd_dir = SAFE_MYINOTIFY_ADD_WATCH(fd_notify, TEST_DIR, IN_ALL_EVENTS);
88 reap_wd_dir = 1;
89
90 wd_file = SAFE_MYINOTIFY_ADD_WATCH(fd_notify, TEST_FILE, IN_ALL_EVENTS);
91 reap_wd_file = 1;
92
93 SAFE_RMDIR(TEST_DIR);
94 reap_wd_dir = 0;
95
96 event_set[test_cnt].mask = IN_DELETE_SELF;
97 strcpy(event_set[test_cnt].name, "");
98 test_cnt++;
99 event_set[test_cnt].mask = IN_IGNORED;
100 strcpy(event_set[test_cnt].name, "");
101 test_cnt++;
102
103 SAFE_UNLINK(TEST_FILE);
104 reap_wd_file = 0;
105
106 /*
107 * When a file is unlinked, the link count is reduced by 1, and when it
108 * hits 0 the file is removed.
109 *
110 * This isn't well documented in inotify(7), but it's intuitive if you
111 * understand how Unix works.
112 */
113 if (tst_kvercmp2(2, 6, 25, kvers) >= 0) {
114 event_set[test_cnt].mask = IN_ATTRIB;
115 strcpy(event_set[test_cnt].name, "");
116 test_cnt++;
117 }
118
119 event_set[test_cnt].mask = IN_DELETE_SELF;
120 strcpy(event_set[test_cnt].name, TEST_FILE);
121 test_cnt++;
122 event_set[test_cnt].mask = IN_IGNORED;
123 strcpy(event_set[test_cnt].name, "");
124 test_cnt++;
125
126 len = read(fd_notify, event_buf, EVENT_BUF_LEN);
127 if (len == -1)
128 tst_brk(TBROK | TERRNO, "read failed");
129
130 while (i < len) {
131 struct inotify_event *event;
132 event = (struct inotify_event *)&event_buf[i];
133 if (test_num >= test_cnt) {
134 if (tst_kvercmp(2, 6, 25) < 0
135 && event_set[test_cnt - 1].mask == event->mask)
136 tst_res(TWARN,
137 "This may be kernel bug. "
138 "Before kernel 2.6.25, a kernel bug "
139 "meant that the kernel code that was "
140 "intended to coalesce successive identical "
141 "events (i.e., the two most recent "
142 "events could potentially be coalesced "
143 "if the older had not yet been read) "
144 "instead checked if the most recent event "
145 "could be coalesced with the oldest "
146 "unread event. This has been fixed by commit"
147 "1c17d18e3775485bf1e0ce79575eb637a94494a2.");
148 tst_res(TFAIL,
149 "got unnecessary event: "
150 "wd=%d mask=%04x cookie=%u len=%u "
151 "name=\"%.*s\"", event->wd, event->mask,
152 event->cookie, event->len, event->len, event->name);
153
154 } else if ((event_set[test_num].mask == event->mask)
155 &&
156 (!strncmp
157 (event_set[test_num].name, event->name,
158 event->len))) {
159 tst_res(TPASS,
160 "got event: wd=%d mask=%04x "
161 "cookie=%u len=%u name=\"%.*s\"",
162 event->wd, event->mask, event->cookie,
163 event->len, event->len, event->name);
164
165 } else {
166 tst_res(TFAIL, "got event: wd=%d mask=%04x "
167 "(expected %x) cookie=%u len=%u "
168 "name=\"%.*s\" (expected \"%s\") %d",
169 event->wd, event->mask,
170 event_set[test_num].mask,
171 event->cookie, event->len,
172 event->len, event->name,
173 event_set[test_num].name,
174 strncmp(event_set[test_num].name, event->name, event->len));
175 }
176 test_num++;
177 i += EVENT_SIZE + event->len;
178 }
179
180 for (; test_num < test_cnt; test_num++) {
181 tst_res(TFAIL, "didn't get event: mask=%04x ",
182 event_set[test_num].mask);
183 }
184
185 }
186
187 static struct tst_test test = {
188 .needs_tmpdir = 1,
189 .setup = setup,
190 .cleanup = cleanup,
191 .test_all = verify_inotify,
192 };
193
194 #else
195 TST_TEST_TCONF("system doesn't have required inotify support");
196 #endif /* defined(HAVE_SYS_INOTIFY_H) */
197