1 /*
2 * Copyright (c) 2007 SWSoft. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 * Started by Andrew Vagin <avagin@sw.ru>
24 *
25 * DESCRIPTION
26 * Check that inotify work for a directory
27 *
28 * ALGORITHM
29 * Execute sequence file's operation and check return events
30 */
31
32 #include "config.h"
33
34 #include <stdio.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <fcntl.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <sys/syscall.h>
41 #include <limits.h>
42 #include "test.h"
43 #include "safe_macros.h"
44 #include "lapi/syscalls.h"
45 #include "inotify.h"
46
47 #if defined(HAVE_SYS_INOTIFY_H)
48 #include <sys/inotify.h>
49
50 #ifndef IN_MOVE_SELF
51 #define IN_MOVE_SELF 0x00000800
52 #endif
53
54 #define EVENT_MAX 1024
55 /* size of the event structure, not counting name */
56 #define EVENT_SIZE (sizeof (struct inotify_event))
57 /* reasonable guess as to size of 1024 events */
58 #define EVENT_BUF_LEN (EVENT_MAX * (EVENT_SIZE + 16))
59
60 static void setup(void);
61 static void cleanup(void);
62
63 char *TCID = "inotify02";
64 int TST_TOTAL = 9;
65
66 #define BUF_SIZE 256
67 static char fname1[BUF_SIZE], fname2[BUF_SIZE], fname3[BUF_SIZE];
68 static int fd, fd_notify, reap_wd;
69 static int wd;
70
71 struct event_t {
72 char name[BUF_SIZE];
73 unsigned int mask;
74 };
75 #define FILE_NAME1 "test_file1"
76 #define FILE_NAME2 "test_file2"
77
78 static struct event_t event_set[EVENT_MAX];
79
80 static char event_buf[EVENT_BUF_LEN];
81
main(int ac,char ** av)82 int main(int ac, char **av)
83 {
84 int lc;
85 unsigned int stored_cookie = UINT_MAX;
86
87 tst_parse_opts(ac, av, NULL, NULL);
88
89 setup();
90
91 for (lc = 0; TEST_LOOPING(lc); lc++) {
92
93 tst_count = 0;
94
95 /*
96 * generate sequence of events
97 */
98 SAFE_CHMOD(cleanup, ".", 0755);
99 event_set[tst_count].mask = IN_ISDIR | IN_ATTRIB;
100 strcpy(event_set[tst_count].name, "");
101 tst_count++;
102
103 if ((fd = creat(FILE_NAME1, 0755)) == -1) {
104 tst_brkm(TBROK | TERRNO, cleanup,
105 "creat(\"%s\", 755) failed", FILE_NAME1);
106 }
107
108 event_set[tst_count].mask = IN_CREATE;
109 strcpy(event_set[tst_count].name, FILE_NAME1);
110 tst_count++;
111 event_set[tst_count].mask = IN_OPEN;
112 strcpy(event_set[tst_count].name, FILE_NAME1);
113 tst_count++;
114
115 SAFE_CLOSE(cleanup, fd);
116 event_set[tst_count].mask = IN_CLOSE_WRITE;
117 strcpy(event_set[tst_count].name, FILE_NAME1);
118 tst_count++;
119
120 SAFE_RENAME(cleanup, FILE_NAME1, FILE_NAME2);
121 event_set[tst_count].mask = IN_MOVED_FROM;
122 strcpy(event_set[tst_count].name, FILE_NAME1);
123 tst_count++;
124 event_set[tst_count].mask = IN_MOVED_TO;
125 strcpy(event_set[tst_count].name, FILE_NAME2);
126 tst_count++;
127
128 if (getcwd(fname1, BUF_SIZE) == NULL) {
129 tst_brkm(TBROK | TERRNO, cleanup,
130 "getcwd(%p, %d) failed", fname1, BUF_SIZE);
131 }
132
133 snprintf(fname2, BUF_SIZE, "%s.rename1", fname1);
134 SAFE_RENAME(cleanup, fname1, fname2);
135 event_set[tst_count].mask = IN_MOVE_SELF;
136 strcpy(event_set[tst_count].name, "");
137 tst_count++;
138
139 SAFE_UNLINK(cleanup, FILE_NAME2);
140 event_set[tst_count].mask = IN_DELETE;
141 strcpy(event_set[tst_count].name, FILE_NAME2);
142 tst_count++;
143
144 /*
145 * test that duplicate events will be coalesced into
146 * a single event. This test case should be last, that
147 * we can correct determine kernel bug which exist before
148 * 2.6.25. See comment below.
149 */
150 snprintf(fname3, BUF_SIZE, "%s.rename2", fname1);
151 SAFE_RENAME(cleanup, fname2, fname3);
152
153 SAFE_RENAME(cleanup, fname3, fname1);
154 event_set[tst_count].mask = IN_MOVE_SELF;
155 strcpy(event_set[tst_count].name, "");
156 tst_count++;
157
158 if (tst_count != TST_TOTAL) {
159 tst_brkm(TBROK, cleanup,
160 "tst_count and TST_TOTAL are not equal");
161 }
162
163 tst_count = 0;
164
165 int len, i = 0, test_num = 0;
166 if ((len = read(fd_notify, event_buf, EVENT_BUF_LEN)) == -1) {
167 tst_brkm(TBROK | TERRNO, cleanup,
168 "read(%d, buf, %zu) failed",
169 fd_notify, EVENT_BUF_LEN);
170
171 }
172
173 while (i < len) {
174 struct inotify_event *event;
175 event = (struct inotify_event *)&event_buf[i];
176 if (test_num >= TST_TOTAL) {
177 if (tst_kvercmp(2, 6, 25) < 0
178 && event_set[TST_TOTAL - 1].mask ==
179 event->mask)
180 tst_resm(TWARN,
181 "This may be kernel bug. "
182 "Before kernel 2.6.25, a kernel bug "
183 "meant that the kernel code that was "
184 "intended to coalesce successive identical "
185 "events (i.e., the two most recent "
186 "events could potentially be coalesced "
187 "if the older had not yet been read) "
188 "instead checked if the most recent event "
189 "could be coalesced with the oldest "
190 "unread event. This has been fixed by commit"
191 "1c17d18e3775485bf1e0ce79575eb637a94494a2.");
192 tst_resm(TFAIL,
193 "get unnecessary event: "
194 "wd=%d mask=%x cookie=%u len=%u"
195 "name=\"%.*s\"", event->wd, event->mask,
196 event->cookie, event->len, event->len,
197 event->name);
198
199 } else if ((event_set[test_num].mask == event->mask)
200 &&
201 (!strncmp
202 (event_set[test_num].name, event->name,
203 event->len))) {
204 int fail = 0;
205
206 if (event->mask == IN_MOVED_FROM) {
207 if (event->cookie == 0)
208 fail = 1;
209 else
210 stored_cookie = event->cookie;
211 } else if (event->mask == IN_MOVED_TO) {
212 if (event->cookie != stored_cookie)
213 fail = 1;
214 else
215 stored_cookie = UINT_MAX;
216 } else {
217 if (event->cookie != 0)
218 fail = 1;
219 }
220 if (!fail) {
221 tst_resm(TPASS,
222 "get event: wd=%d mask=%x "
223 "cookie=%u len=%u name=\"%.*s\"",
224 event->wd, event->mask,
225 event->cookie, event->len,
226 event->len, event->name);
227 } else {
228 tst_resm(TFAIL,
229 "get event: wd=%d mask=%x "
230 "cookie=%u (wrong) len=%u "
231 "name=\"%s\"",
232 event->wd, event->mask,
233 event->cookie, event->len,
234 event->name);
235 }
236 } else {
237 tst_resm(TFAIL, "get event: wd=%d mask=%x "
238 "(expected %x) cookie=%u len=%u "
239 "name=\"%s\" (expected \"%s\") %d",
240 event->wd, event->mask,
241 event_set[test_num].mask,
242 event->cookie, event->len, event->name,
243 event_set[test_num].name,
244 strcmp(event_set[test_num].name,
245 event->name));
246 }
247 test_num++;
248 i += EVENT_SIZE + event->len;
249 }
250
251 for (; test_num < TST_TOTAL; test_num++) {
252 tst_resm(TFAIL, "didn't get event: mask=%x ",
253 event_set[test_num].mask);
254 }
255 }
256
257 cleanup();
258 tst_exit();
259 }
260
setup(void)261 static void setup(void)
262 {
263
264 tst_sig(NOFORK, DEF_HANDLER, cleanup);
265
266 TEST_PAUSE;
267
268 tst_tmpdir();
269
270 if ((fd_notify = myinotify_init()) < 0) {
271 if (errno == ENOSYS) {
272 tst_brkm(TCONF, cleanup,
273 "inotify is not configured in this kernel.");
274 } else {
275 tst_brkm(TBROK | TERRNO, cleanup,
276 "inotify_init () failed");
277 }
278 }
279
280 if ((wd = myinotify_add_watch(fd_notify, ".", IN_ALL_EVENTS)) < 0) {
281 tst_brkm(TBROK | TERRNO, cleanup,
282 "inotify_add_watch (%d, \".\", IN_ALL_EVENTS) failed",
283 fd_notify);
284 reap_wd = 1;
285 };
286
287 }
288
cleanup(void)289 static void cleanup(void)
290 {
291 if (reap_wd && myinotify_rm_watch(fd_notify, wd) < 0) {
292 tst_resm(TWARN,
293 "inotify_rm_watch (%d, %d) failed,", fd_notify, wd);
294
295 }
296
297 if (fd_notify > 0 && close(fd_notify))
298 tst_resm(TWARN, "close(%d) failed", fd_notify);
299
300 tst_rmdir();
301 }
302
303 #else
304
305 char *TCID = "inotify02";
306 int TST_TOTAL = 0;
307
main(void)308 int main(void)
309 {
310 tst_brkm(TCONF, NULL, "system doesn't have required inotify support");
311 }
312
313 #endif
314