1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <signal.h>
#include <string.h>
#include "zdtmtst.h"
const char *test_doc = "Inotify on symlink should be checked";
#ifndef NODEL
char filename[] = "file";
char linkname[] = "file.lnk";
const char *inot_dir = "./inotify";
#else
char filename[] = "file.nodel";
char linkname[] = "file.nodel.lnk";
const char *inot_dir = "./inotify.nodel";
#endif
#ifdef __NR_inotify_init
#include <sys/inotify.h>
#ifndef IN_DONT_FOLLOW
/* Missed in SLES 10 header */
#define IN_DONT_FOLLOW 0x02000000
#endif
#define EVENT_MAX 1024
/* size of the event structure, not counting name */
#define EVENT_SIZE (sizeof (struct inotify_event))
/* reasonable guess as to size of 1024 events */
#define EVENT_BUF_LEN (EVENT_MAX * (EVENT_SIZE + 16))
#define BUF_SIZE 256
#define min_value(a,b) (a<b) ? a : b
#define handle_event(MASK) (MASK == IN_ACCESS) ? "IN_ACCESS" : \
(MASK == IN_MODIFY) ? "IN_MODIFY" : \
(MASK == IN_ATTRIB) ? "IN_ATTRIB" : \
(MASK == IN_CLOSE) ? "IN_CLOSE" : \
(MASK == IN_CLOSE_WRITE) ? "IN_CLOSE_WRITE" : \
(MASK == IN_CLOSE_NOWRITE) ? "IN_CLOSE_NOWRITE" : \
(MASK == IN_OPEN) ? "IN_OPEN" : \
(MASK == IN_MOVED_FROM) ? "IN_MOVED_FROM" : \
(MASK == IN_MOVED_TO) ? "IN_MOVED_TO" : \
(MASK == IN_DELETE) ? "IN_DELETE" : \
(MASK == IN_CREATE) ? "IN_CREATE" : \
(MASK == IN_DELETE_SELF) ? "IN_DELETE_SELF" : \
(MASK == IN_MOVE_SELF) ? "IN_MOVE_SELF" : \
(MASK == IN_UNMOUNT) ? "IN_UNMOUNT" : \
(MASK == IN_Q_OVERFLOW) ? "IN_Q_OVERFLOW" : \
(MASK == IN_IGNORED) ? "IN_IGNORED" : \
"UNKNOWN"
#include <unistd.h>
#include <fcntl.h>
typedef struct {
int inot;
int file;
int link;
int dir;
} desc;
void do_wait() {
test_daemon();
test_waitsig();
}
int createFiles(char *path, char *target, char *link) {
int fd;
fd = open(path,O_CREAT, 0644);
if (fd < 0) {
pr_perror("can't open %s", path);
return -1;
}
close(fd);
if (symlink(target, link) < 0) {
pr_perror("can't symlink %s to %s", path, link);
return -1;
}
return 0;
}
int addWatcher(int fd, const char *path) {
int wd;
wd = inotify_add_watch(fd, path, IN_ALL_EVENTS | IN_DONT_FOLLOW);
if (wd < 0) {
pr_perror("inotify_add_watch(%d, %s, IN_ALL_EVENTS) Failed, %s",
fd, path, strerror(errno));
return -1;
}
return wd;
}
int fChmod(char *path) {
if (chmod(path, 0755) < 0) {
pr_perror("chmod(%s, 0755) Failed, %s",
path, strerror(errno));
return -1;
}
return 0;
}
int fWriteClose(char *path) {
int fd = open(path, O_RDWR | O_CREAT, 0700);
if (fd == -1) {
pr_perror("open(%s, O_RDWR|O_CREAT,0700) Failed, %s",
path, strerror(errno));
return -1;
}
if (write(fd, "string", 7) == -1) {
pr_perror("write(%d, %s, 1) Failed, %s", fd, path, strerror(errno));
return -1;
}
if (close(fd) == -1) {
pr_perror("close(%s) Failed, %s", path, strerror(errno));
return -1;
}
return 0;
}
int fNoWriteClose(char *path) {
char buf[BUF_SIZE];
int fd = open(path, O_RDONLY);
if ( fd < 0 ) {
pr_perror("open(%s, O_RDONLY) Failed, %s",
path, strerror(errno));
return -1;
}
if (read(fd, buf, BUF_SIZE) == -1) {
pr_perror("read error: %s", strerror(errno));
close(fd);
return -1;
}
if (close(fd) == -1) {
pr_perror("close(%s) Failed, %s", path, strerror(errno));
return -1;
}
return 0;
}
int fMove(char *from, char *to) {
if (rename(from, to) == -1) {
pr_perror("rename error (from: %s to: %s) : %s",
from, to, strerror(errno));
return -1;
}
return 0;
}
desc init_env(const char *dir, char *file_path, char *link_path) {
desc in_desc = {-1, -1, -1, -1};
if (mkdir(dir, 0777) < 0) {
pr_perror("error in creating directory: %s, %s",
dir, strerror(errno));
return in_desc;
}
in_desc.inot = inotify_init();
if (in_desc.inot < 0) {
pr_perror("inotify_init () Failed, %s", strerror(errno));
rmdir(dir);
return in_desc;
}
if (snprintf(file_path, BUF_SIZE, "%s/%s", dir, filename) >= BUF_SIZE) {
pr_perror("filename %s is too long", filename);
rmdir(dir);
return in_desc;
}
if (snprintf(link_path, BUF_SIZE, "%s/%s", dir, linkname) >= BUF_SIZE) {
pr_perror("filename %s is too long", linkname);
rmdir(dir);
return in_desc;
}
in_desc.dir = addWatcher(in_desc.inot, dir);
if (createFiles(file_path, filename, link_path)) {
return in_desc;
}
in_desc.link = addWatcher(in_desc.inot, link_path);
in_desc.file = addWatcher(in_desc.inot, file_path);
return in_desc;
}
int fDelete(char *path) {
if (unlink(path) != 0) {
pr_perror("unlink: (%s)", strerror(errno));
return -1;
}
return 0;
}
int fRemDir(const char *target) {
if(rmdir(target)) {
pr_perror("rmdir: (%s)", strerror(errno));
return -1;
}
return 0;
}
int test_actions(const char *dir, char *file_path, char *link_path) {
if (
fChmod(link_path) == 0 &&
fWriteClose(link_path) == 0 &&
fNoWriteClose(link_path) == 0 &&
fMove(file_path, filename) == 0 &&
fMove(filename, file_path) == 0
#ifndef NODEL
&& fDelete(file_path) == 0 &&
fDelete(link_path) == 0 &&
fRemDir(dir) == 0
#endif
)
{
return 0;
}
return -1;
}
void dump_events(char *buf, int len) {
int marker = 0;
struct inotify_event *event;
while (marker < len) {
event = (struct inotify_event *) &buf[marker];
test_msg("\t%s (%x mask, %d len", handle_event(event->mask), event->mask, event->len);
if (event->len)
test_msg(", '%s' name", event->name);
test_msg(")\n");
marker += EVENT_SIZE + event->len;
}
}
int harmless(int mask)
{
switch (mask) {
case IN_CLOSE_NOWRITE:
case IN_ATTRIB:
return 1;
}
return 0;
}
int errors(int exp_len, int len, char *etalon_buf, char *buf) {
int marker=0;
int error=0;
while (marker < len){
struct inotify_event *event;
struct inotify_event *exp_event;
event = (struct inotify_event *) &buf[marker];
/* It's OK if some additional events are recevived */
if (marker < exp_len)
exp_event = (struct inotify_event *) &etalon_buf[marker];
else {
if (!harmless(event->mask)) {
fail("got unexpected event %s (%x mask)\n",
handle_event(event->mask), event->mask);
error++;
}
goto next_event;
}
if (event->mask != exp_event->mask) {
fail("Handled %s (%x mask), expected %s (%x mask)",
handle_event(event->mask), event->mask,
handle_event(exp_event->mask),
exp_event->mask);
error++;
}
if (event->len != exp_event->len) {
fail("Incorrect length of field name.");
error++;
break;
}
else if (event->len && strncmp(event->name, exp_event->name, event->len)) {
fail("Handled file name %s, expected %s",
event->name,
exp_event->name);
error++;
}
next_event:
marker += EVENT_SIZE + event->len;
}
return error;
}
int read_set(int inot_fd, char *event_set) {
int len;
if ((len = read(inot_fd, event_set, EVENT_BUF_LEN)) < 0) {
pr_perror("read(%d, buf, %lu) Failed, errno=%d",
inot_fd, (unsigned long)EVENT_BUF_LEN, errno);
return -1;
}
return len;
}
void common_close(desc *descr) {
if (descr->inot > 0) {
close(descr->inot);
descr->inot=-1;
descr->file=-1;
descr->dir=-1;
descr->link=-1;
}
}
int get_event_set(char *event_set, int wait) {
int len;
char link_path[BUF_SIZE];
char file_path[BUF_SIZE];
desc common_desc;
common_desc = init_env(inot_dir, file_path, link_path);
if ((common_desc.inot < 0) || (common_desc.file < 0) || \
(common_desc.dir < 0) || (common_desc.link < 0)) {
common_close(&common_desc);
return -1;
}
if(test_actions(inot_dir, file_path, link_path) < 0) {
common_close(&common_desc);
return -1;
}
if (wait) {
do_wait();
}
len = read_set(common_desc.inot, event_set);
common_close(&common_desc);
#ifdef NODEL
if (! (fDelete(file_path) == 0 &&
fDelete(link_path) == 0 &&
fRemDir(inot_dir) == 0))
return -1;
#endif
return len;
}
int check(int len, char *event_set, int exp_len, char *etalon_event_set) {
if ((exp_len < 0) || (len < 0)){
fail("Error in preparing event sets.");
return -1;
}
if (len < exp_len) {
fail("Events are lost. Read: %d, Expected: %d", len, exp_len);
test_msg("expected events\n");
dump_events(etalon_event_set, exp_len);
test_msg("real events\n");
dump_events(event_set, len);
return -1;
}
if (errors(exp_len, len, etalon_event_set, event_set) == 0) {
pass();
return 0;
}
return -1;
}
int main(int argc, char ** argv)
{
int exp_len=-1, len=-1;
char etalon_event_set[EVENT_BUF_LEN];
char event_set[EVENT_BUF_LEN];
test_init(argc, argv);
exp_len = get_event_set(etalon_event_set, 0);
len = get_event_set(event_set, 1);
if (check(len, event_set, exp_len, etalon_event_set)) {
return 1;
}
return 0;
}
#else
int main(int argc, char ** argv)
{
test_init(argc, argv);
skip("Inotify not supported.");
return 0;
}
#endif //__NR_inotify_init
|