blob: bce5d8b43587494547e4ad84f79f30524b0e1e0f [file] [log] [blame]
Jan Kara9ec33a02014-02-24 16:15:10 +01001/*
2 * Copyright (c) 2014 SUSE Linux. 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 * Started by Jan Kara <jack@suse.cz>
20 *
21 * DESCRIPTION
22 * Check that inotify overflow event is properly generated
23 *
24 * ALGORITHM
25 * Generate enough events without reading them and check that overflow
26 * event is generated.
27 */
28#include "config.h"
29
30#include <stdio.h>
31#include <sys/stat.h>
32#include <sys/types.h>
33#include <sys/fcntl.h>
34#include <errno.h>
35#include <string.h>
36#include <sys/syscall.h>
37#include "test.h"
Jan Kara9ec33a02014-02-24 16:15:10 +010038#include "linux_syscall_numbers.h"
39#include "inotify.h"
40#include "safe_macros.h"
41
42char *TCID = "inotify05";
43int TST_TOTAL = 1;
44
45#if defined(HAVE_SYS_INOTIFY_H)
46#include <sys/inotify.h>
47
48/* size of the event structure, not counting name */
49#define EVENT_SIZE (sizeof(struct inotify_event))
50#define EVENT_BUF_LEN (EVENT_SIZE * 16)
51
52static void setup(void);
53static void cleanup(void);
54
55#define BUF_SIZE 256
56static char fname[BUF_SIZE];
57static char buf[BUF_SIZE];
58static int fd, fd_notify;
59static int wd;
60static int max_events;
61
62static char event_buf[EVENT_BUF_LEN];
63
64int main(int ac, char **av)
65{
66 int lc, i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020067 const char *msg;
Jan Kara9ec33a02014-02-24 16:15:10 +010068 int len, stop;
69
70 msg = parse_opts(ac, av, NULL, NULL);
71 if (msg != NULL)
72 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
73
74 setup();
75
76 for (lc = 0; TEST_LOOPING(lc); lc++) {
77 /*
78 * generate events
79 */
80 fd = SAFE_OPEN(cleanup, fname, O_RDWR);
81
82 for (i = 0; i < max_events; i++) {
83 SAFE_LSEEK(cleanup, fd, 0, SEEK_SET);
84 SAFE_READ(cleanup, 1, fd, buf, BUF_SIZE);
85 SAFE_LSEEK(cleanup, fd, 0, SEEK_SET);
86 SAFE_WRITE(cleanup, 1, fd, buf, BUF_SIZE);
87 }
88
89 SAFE_CLOSE(cleanup, fd);
90
91 stop = 0;
92 while (!stop) {
93 /*
94 * get list on events
95 */
96 len = read(fd_notify, event_buf, EVENT_BUF_LEN);
97 if (len < 0) {
98 tst_brkm(TBROK | TERRNO, cleanup,
99 "read(%d, buf, %zu) failed",
100 fd_notify, EVENT_BUF_LEN);
101 }
102
103 /*
104 * check events
105 */
106 i = 0;
107 while (i < len) {
108 struct inotify_event *event;
109
110 event = (struct inotify_event *)&event_buf[i];
111 if (event->mask != IN_ACCESS &&
112 event->mask != IN_MODIFY &&
113 event->mask != IN_OPEN &&
114 event->mask != IN_Q_OVERFLOW) {
115 tst_resm(TFAIL,
116 "get event: wd=%d mask=%x "
117 "cookie=%u (expected 0) len=%u",
118 event->wd, event->mask,
119 event->cookie, event->len);
120 stop = 1;
121 break;
122 }
123 if (event->mask == IN_Q_OVERFLOW) {
124 if (event->len != 0 ||
125 event->cookie != 0 ||
126 event->wd != -1) {
127 tst_resm(TFAIL,
128 "invalid overflow event: "
129 "wd=%d mask=%x "
130 "cookie=%u len=%u",
131 event->wd, event->mask,
132 event->cookie,
133 event->len);
134 stop = 1;
135 break;
136 }
137 if ((int)(i + EVENT_SIZE) != len) {
138 tst_resm(TFAIL,
139 "overflow event is not last");
140 stop = 1;
141 break;
142 }
143 tst_resm(TPASS, "get event: wd=%d "
144 "mask=%x cookie=%u len=%u",
145 event->wd, event->mask,
146 event->cookie, event->len);
147 stop = 1;
148 break;
149 }
150 i += EVENT_SIZE + event->len;
151 }
152 }
153 }
154
155 cleanup();
156 tst_exit();
157}
158
159static void setup(void)
160{
161 tst_sig(NOFORK, DEF_HANDLER, cleanup);
162
163 TEST_PAUSE;
164
165 tst_tmpdir();
166
167 sprintf(fname, "tfile_%d", getpid());
168 fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0700);
169 SAFE_WRITE(cleanup, 1, fd, buf, BUF_SIZE);
170 SAFE_CLOSE(cleanup, fd);
171
172 fd_notify = syscall(__NR_inotify_init1, O_NONBLOCK);
173 if (fd_notify < 0) {
174 if (errno == ENOSYS) {
175 tst_brkm(TCONF, cleanup,
176 "inotify is not configured in this kernel.");
177 } else {
178 tst_brkm(TBROK | TERRNO, cleanup,
179 "inotify_init failed");
180 }
181 }
182
183 wd = myinotify_add_watch(fd_notify, fname, IN_ALL_EVENTS);
184 if (wd < 0) {
185 tst_brkm(TBROK | TERRNO, cleanup,
186 "inotify_add_watch (%d, %s, IN_ALL_EVENTS) failed",
187 fd_notify, fname);
188 };
189
190 SAFE_FILE_SCANF(cleanup, "/proc/sys/fs/inotify/max_queued_events",
191 "%d", &max_events);
192}
193
194static void cleanup(void)
195{
196 if (fd_notify > 0 && myinotify_rm_watch(fd_notify, wd) == -1) {
197 tst_resm(TWARN | TERRNO, "inotify_rm_watch (%d, %d) failed",
198 fd_notify, wd);
199
200 }
201
202 if (fd_notify > 0 && close(fd_notify) == -1)
203 tst_resm(TWARN, "close(%d) failed", fd_notify);
204
Jan Kara9ec33a02014-02-24 16:15:10 +0100205 tst_rmdir();
206}
207
208#else
209
210int main(void)
211{
212 tst_brkm(TCONF, NULL, "system doesn't have required inotify support");
213}
214
215#endif