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