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) 2014 SUSE Linux. All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker * Author: Jan Kara <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * Check that inotify overflow event is properly generated.
11*49cdfc7eSAndroid Build Coastguard Worker */
12*49cdfc7eSAndroid Build Coastguard Worker
13*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
14*49cdfc7eSAndroid Build Coastguard Worker
15*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <sys/syscall.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
23*49cdfc7eSAndroid Build Coastguard Worker #include "inotify.h"
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker #if defined(HAVE_SYS_INOTIFY_H)
26*49cdfc7eSAndroid Build Coastguard Worker #include <sys/inotify.h>
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker /* size of the event structure, not counting name */
29*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_SIZE (sizeof(struct inotify_event))
30*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_BUF_LEN (EVENT_SIZE * 16)
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker #define BUF_SIZE 256
33*49cdfc7eSAndroid Build Coastguard Worker static char fname[BUF_SIZE];
34*49cdfc7eSAndroid Build Coastguard Worker static char buf[BUF_SIZE];
35*49cdfc7eSAndroid Build Coastguard Worker static int fd, fd_notify;
36*49cdfc7eSAndroid Build Coastguard Worker static int wd;
37*49cdfc7eSAndroid Build Coastguard Worker static int max_events;
38*49cdfc7eSAndroid Build Coastguard Worker
39*49cdfc7eSAndroid Build Coastguard Worker static char event_buf[EVENT_BUF_LEN];
40*49cdfc7eSAndroid Build Coastguard Worker
verify_inotify(void)41*49cdfc7eSAndroid Build Coastguard Worker void verify_inotify(void)
42*49cdfc7eSAndroid Build Coastguard Worker {
43*49cdfc7eSAndroid Build Coastguard Worker int i;
44*49cdfc7eSAndroid Build Coastguard Worker int len, stop;
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker /*
47*49cdfc7eSAndroid Build Coastguard Worker * generate events
48*49cdfc7eSAndroid Build Coastguard Worker */
49*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(fname, O_RDWR);
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < max_events; i++) {
52*49cdfc7eSAndroid Build Coastguard Worker SAFE_LSEEK(fd, 0, SEEK_SET);
53*49cdfc7eSAndroid Build Coastguard Worker SAFE_READ(1, fd, buf, BUF_SIZE);
54*49cdfc7eSAndroid Build Coastguard Worker SAFE_LSEEK(fd, 0, SEEK_SET);
55*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, BUF_SIZE);
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker stop = 0;
61*49cdfc7eSAndroid Build Coastguard Worker while (!stop) {
62*49cdfc7eSAndroid Build Coastguard Worker /*
63*49cdfc7eSAndroid Build Coastguard Worker * get list on events
64*49cdfc7eSAndroid Build Coastguard Worker */
65*49cdfc7eSAndroid Build Coastguard Worker len = read(fd_notify, event_buf, EVENT_BUF_LEN);
66*49cdfc7eSAndroid Build Coastguard Worker if (len < 0) {
67*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO,
68*49cdfc7eSAndroid Build Coastguard Worker "read(%d, buf, %zu) failed",
69*49cdfc7eSAndroid Build Coastguard Worker fd_notify, EVENT_BUF_LEN);
70*49cdfc7eSAndroid Build Coastguard Worker }
71*49cdfc7eSAndroid Build Coastguard Worker
72*49cdfc7eSAndroid Build Coastguard Worker /*
73*49cdfc7eSAndroid Build Coastguard Worker * check events
74*49cdfc7eSAndroid Build Coastguard Worker */
75*49cdfc7eSAndroid Build Coastguard Worker i = 0;
76*49cdfc7eSAndroid Build Coastguard Worker while (i < len) {
77*49cdfc7eSAndroid Build Coastguard Worker struct inotify_event *event;
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker event = (struct inotify_event *)&event_buf[i];
80*49cdfc7eSAndroid Build Coastguard Worker if (event->mask != IN_ACCESS &&
81*49cdfc7eSAndroid Build Coastguard Worker event->mask != IN_MODIFY &&
82*49cdfc7eSAndroid Build Coastguard Worker event->mask != IN_OPEN &&
83*49cdfc7eSAndroid Build Coastguard Worker event->mask != IN_Q_OVERFLOW) {
84*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL,
85*49cdfc7eSAndroid Build Coastguard Worker "get event: wd=%d mask=%x "
86*49cdfc7eSAndroid Build Coastguard Worker "cookie=%u (expected 0) len=%u",
87*49cdfc7eSAndroid Build Coastguard Worker event->wd, event->mask,
88*49cdfc7eSAndroid Build Coastguard Worker event->cookie, event->len);
89*49cdfc7eSAndroid Build Coastguard Worker stop = 1;
90*49cdfc7eSAndroid Build Coastguard Worker break;
91*49cdfc7eSAndroid Build Coastguard Worker }
92*49cdfc7eSAndroid Build Coastguard Worker if (event->mask == IN_Q_OVERFLOW) {
93*49cdfc7eSAndroid Build Coastguard Worker if (event->len != 0 ||
94*49cdfc7eSAndroid Build Coastguard Worker event->cookie != 0 ||
95*49cdfc7eSAndroid Build Coastguard Worker event->wd != -1) {
96*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL,
97*49cdfc7eSAndroid Build Coastguard Worker "invalid overflow event: "
98*49cdfc7eSAndroid Build Coastguard Worker "wd=%d mask=%x "
99*49cdfc7eSAndroid Build Coastguard Worker "cookie=%u len=%u",
100*49cdfc7eSAndroid Build Coastguard Worker event->wd, event->mask,
101*49cdfc7eSAndroid Build Coastguard Worker event->cookie,
102*49cdfc7eSAndroid Build Coastguard Worker event->len);
103*49cdfc7eSAndroid Build Coastguard Worker stop = 1;
104*49cdfc7eSAndroid Build Coastguard Worker break;
105*49cdfc7eSAndroid Build Coastguard Worker }
106*49cdfc7eSAndroid Build Coastguard Worker if ((int)(i + EVENT_SIZE) != len) {
107*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL,
108*49cdfc7eSAndroid Build Coastguard Worker "overflow event is not last");
109*49cdfc7eSAndroid Build Coastguard Worker stop = 1;
110*49cdfc7eSAndroid Build Coastguard Worker break;
111*49cdfc7eSAndroid Build Coastguard Worker }
112*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "get event: wd=%d "
113*49cdfc7eSAndroid Build Coastguard Worker "mask=%x cookie=%u len=%u",
114*49cdfc7eSAndroid Build Coastguard Worker event->wd, event->mask,
115*49cdfc7eSAndroid Build Coastguard Worker event->cookie, event->len);
116*49cdfc7eSAndroid Build Coastguard Worker stop = 1;
117*49cdfc7eSAndroid Build Coastguard Worker break;
118*49cdfc7eSAndroid Build Coastguard Worker }
119*49cdfc7eSAndroid Build Coastguard Worker i += EVENT_SIZE + event->len;
120*49cdfc7eSAndroid Build Coastguard Worker }
121*49cdfc7eSAndroid Build Coastguard Worker }
122*49cdfc7eSAndroid Build Coastguard Worker }
123*49cdfc7eSAndroid Build Coastguard Worker
setup(void)124*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
125*49cdfc7eSAndroid Build Coastguard Worker {
126*49cdfc7eSAndroid Build Coastguard Worker sprintf(fname, "tfile_%d", getpid());
127*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700);
128*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, BUF_SIZE);
129*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
130*49cdfc7eSAndroid Build Coastguard Worker
131*49cdfc7eSAndroid Build Coastguard Worker fd_notify = SAFE_MYINOTIFY_INIT1(O_NONBLOCK);
132*49cdfc7eSAndroid Build Coastguard Worker
133*49cdfc7eSAndroid Build Coastguard Worker wd = SAFE_MYINOTIFY_ADD_WATCH(fd_notify, fname, IN_ALL_EVENTS);
134*49cdfc7eSAndroid Build Coastguard Worker
135*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_SCANF("/proc/sys/fs/inotify/max_queued_events",
136*49cdfc7eSAndroid Build Coastguard Worker "%d", &max_events);
137*49cdfc7eSAndroid Build Coastguard Worker }
138*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)139*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
140*49cdfc7eSAndroid Build Coastguard Worker {
141*49cdfc7eSAndroid Build Coastguard Worker if (fd_notify > 0 && myinotify_rm_watch(fd_notify, wd) == -1) {
142*49cdfc7eSAndroid Build Coastguard Worker tst_res(TWARN | TERRNO, "inotify_rm_watch (%d, %d) failed",
143*49cdfc7eSAndroid Build Coastguard Worker fd_notify, wd);
144*49cdfc7eSAndroid Build Coastguard Worker }
145*49cdfc7eSAndroid Build Coastguard Worker
146*49cdfc7eSAndroid Build Coastguard Worker if (fd_notify > 0)
147*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd_notify);
148*49cdfc7eSAndroid Build Coastguard Worker }
149*49cdfc7eSAndroid Build Coastguard Worker
150*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
151*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
152*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
153*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
154*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_inotify,
155*49cdfc7eSAndroid Build Coastguard Worker };
156*49cdfc7eSAndroid Build Coastguard Worker
157*49cdfc7eSAndroid Build Coastguard Worker #else
158*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF("system doesn't have required inotify support");
159*49cdfc7eSAndroid Build Coastguard Worker #endif
160