blob: ee9436c7b84bf8eb47c63b7e35fc91f882a2beff [file] [log] [blame]
subrata_modak73e6ce72007-05-24 10:00:45 +00001/*
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
Wanlong Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
subrata_modak73e6ce72007-05-24 10:00:45 +000022 *
23 * Started by Andrew Vagin <avagin@sw.ru>
24 *
subrata_modak73e6ce72007-05-24 10:00:45 +000025 * DESCRIPTION
26 * Check that inotify work for a file
27 *
28 * ALGORITHM
29 * Execute sequence file's operation and check return events
subrata_modak73e6ce72007-05-24 10:00:45 +000030 */
subrata_modak86faed42008-12-29 12:11:50 +000031#include "config.h"
subrata_modak73e6ce72007-05-24 10:00:45 +000032
subrata_modak73e6ce72007-05-24 10:00:45 +000033#include <stdio.h>
34#include <sys/stat.h>
35#include <sys/types.h>
36#include <sys/fcntl.h>
37#include <errno.h>
38#include <string.h>
subrata_modakc59422e2007-06-04 10:39:59 +000039#include <sys/syscall.h>
subrata_modak73e6ce72007-05-24 10:00:45 +000040#include "test.h"
Garrett Cooper53740502010-12-16 00:04:01 -080041#include "linux_syscall_numbers.h"
Garrett Cooper3bf3cb12012-05-20 10:51:55 -070042#include "inotify.h"
subrata_modak73e6ce72007-05-24 10:00:45 +000043
Garrett Cooper53740502010-12-16 00:04:01 -080044#if defined(HAVE_SYS_INOTIFY_H)
subrata_modak3e9f0942008-08-22 21:32:50 +000045#include <sys/inotify.h>
subrata_modakc59422e2007-06-04 10:39:59 +000046
subrata_modak73e6ce72007-05-24 10:00:45 +000047#define EVENT_MAX 1024
48/* size of the event structure, not counting name */
49#define EVENT_SIZE (sizeof (struct inotify_event))
50/* reasonable guess as to size of 1024 events */
51#define EVENT_BUF_LEN (EVENT_MAX * (EVENT_SIZE + 16))
52
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020053char *TCID = "inotify01";
54int TST_TOTAL = 7;
subrata_modak73e6ce72007-05-24 10:00:45 +000055
Cyril Hrubis26aa0a82013-08-08 14:58:42 +020056static void setup(void);
57static void cleanup(void);
58
subrata_modak73e6ce72007-05-24 10:00:45 +000059#define BUF_SIZE 256
Cyril Hrubis26aa0a82013-08-08 14:58:42 +020060static char fname[BUF_SIZE];
61static char buf[BUF_SIZE];
62static int fd, fd_notify;
Cyril Hrubis04f21772014-12-09 14:26:23 +010063static int wd, reap_wd;
subrata_modak73e6ce72007-05-24 10:00:45 +000064
Cyril Hrubis26aa0a82013-08-08 14:58:42 +020065static int event_set[EVENT_MAX];
subrata_modak73e6ce72007-05-24 10:00:45 +000066
Cyril Hrubis26aa0a82013-08-08 14:58:42 +020067static char event_buf[EVENT_BUF_LEN];
vapier03b05a02008-04-07 18:33:46 +000068
subrata_modak56207ce2009-03-23 13:35:39 +000069int main(int ac, char **av)
70{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020071 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020072 const char *msg;
subrata_modak73e6ce72007-05-24 10:00:45 +000073
Garrett Cooper45e285d2010-11-22 12:19:25 -080074 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080075 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modak73e6ce72007-05-24 10:00:45 +000076
subrata_modak56207ce2009-03-23 13:35:39 +000077 setup();
subrata_modak73e6ce72007-05-24 10:00:45 +000078
subrata_modak56207ce2009-03-23 13:35:39 +000079 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080080 tst_count = 0;
subrata_modak73e6ce72007-05-24 10:00:45 +000081
subrata_modak56207ce2009-03-23 13:35:39 +000082 /*
83 * generate sequence of events
84 */
85 if (chmod(fname, 0755) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080086 tst_brkm(TBROK | TERRNO, cleanup,
Garrett Cooper53740502010-12-16 00:04:01 -080087 "chmod(%s, 0755) failed", fname);
subrata_modak56207ce2009-03-23 13:35:39 +000088 }
Caspar Zhangd59a6592013-03-07 14:59:12 +080089 event_set[tst_count] = IN_ATTRIB;
90 tst_count++;
subrata_modak73e6ce72007-05-24 10:00:45 +000091
subrata_modak56207ce2009-03-23 13:35:39 +000092 if ((fd = open(fname, O_RDONLY)) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080093 tst_brkm(TBROK | TERRNO, cleanup,
94 "open(%s, O_RDWR|O_CREAT,0700) failed", fname);
subrata_modak56207ce2009-03-23 13:35:39 +000095 }
Caspar Zhangd59a6592013-03-07 14:59:12 +080096 event_set[tst_count] = IN_OPEN;
97 tst_count++;
subrata_modak73e6ce72007-05-24 10:00:45 +000098
subrata_modak56207ce2009-03-23 13:35:39 +000099 if (read(fd, buf, BUF_SIZE) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800100 tst_brkm(TBROK | TERRNO, cleanup,
101 "read(%d, buf, %d) failed", fd, BUF_SIZE);
subrata_modak56207ce2009-03-23 13:35:39 +0000102 }
Caspar Zhangd59a6592013-03-07 14:59:12 +0800103 event_set[tst_count] = IN_ACCESS;
104 tst_count++;
subrata_modak73e6ce72007-05-24 10:00:45 +0000105
subrata_modak56207ce2009-03-23 13:35:39 +0000106 if (close(fd) == -1) {
Garrett Cooper53740502010-12-16 00:04:01 -0800107 tst_brkm(TBROK, cleanup, "close(%s) failed", fname);
subrata_modak56207ce2009-03-23 13:35:39 +0000108 }
Caspar Zhangd59a6592013-03-07 14:59:12 +0800109 event_set[tst_count] = IN_CLOSE_NOWRITE;
110 tst_count++;
subrata_modak73e6ce72007-05-24 10:00:45 +0000111
subrata_modak56207ce2009-03-23 13:35:39 +0000112 if ((fd = open(fname, O_RDWR | O_CREAT, 0700)) == -1) {
113 tst_brkm(TBROK, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800114 "open(%s, O_RDWR|O_CREAT,0700) failed", fname);
subrata_modak56207ce2009-03-23 13:35:39 +0000115 }
Caspar Zhangd59a6592013-03-07 14:59:12 +0800116 event_set[tst_count] = IN_OPEN;
117 tst_count++;
subrata_modak73e6ce72007-05-24 10:00:45 +0000118
subrata_modak56207ce2009-03-23 13:35:39 +0000119 if (write(fd, buf, BUF_SIZE) == -1) {
120 tst_brkm(TBROK, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800121 "write(%d, %s, 1) failed", fd, fname);
subrata_modak56207ce2009-03-23 13:35:39 +0000122 }
Caspar Zhangd59a6592013-03-07 14:59:12 +0800123 event_set[tst_count] = IN_MODIFY;
124 tst_count++;
subrata_modak73e6ce72007-05-24 10:00:45 +0000125
subrata_modak56207ce2009-03-23 13:35:39 +0000126 if (close(fd) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800127 tst_brkm(TBROK, cleanup, "close(%s) failed", fname);
subrata_modak56207ce2009-03-23 13:35:39 +0000128 }
Caspar Zhangd59a6592013-03-07 14:59:12 +0800129 event_set[tst_count] = IN_CLOSE_WRITE;
130 tst_count++;
subrata_modak73e6ce72007-05-24 10:00:45 +0000131
Caspar Zhangd59a6592013-03-07 14:59:12 +0800132 if (TST_TOTAL != tst_count) {
subrata_modak56207ce2009-03-23 13:35:39 +0000133 tst_brkm(TBROK, cleanup,
Caspar Zhangd59a6592013-03-07 14:59:12 +0800134 "TST_TOTAL and tst_count are not equal");
subrata_modak56207ce2009-03-23 13:35:39 +0000135 }
Caspar Zhangd59a6592013-03-07 14:59:12 +0800136 tst_count = 0;
subrata_modak73e6ce72007-05-24 10:00:45 +0000137
subrata_modak56207ce2009-03-23 13:35:39 +0000138 /*
139 * get list on events
140 */
141 int len, i = 0, test_num = 0;
142 if ((len = read(fd_notify, event_buf, EVENT_BUF_LEN)) < 0) {
143 tst_brkm(TBROK, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800144 "read(%d, buf, %zu) failed",
145 fd_notify, EVENT_BUF_LEN);
subrata_modak73e6ce72007-05-24 10:00:45 +0000146
subrata_modak56207ce2009-03-23 13:35:39 +0000147 }
subrata_modak73e6ce72007-05-24 10:00:45 +0000148
subrata_modak56207ce2009-03-23 13:35:39 +0000149 /*
150 * check events
151 */
152 while (i < len) {
153 struct inotify_event *event;
154 event = (struct inotify_event *)&event_buf[i];
155 if (test_num >= TST_TOTAL) {
156 tst_resm(TFAIL,
157 "get unnecessary event: wd=%d mask=%x "
158 "cookie=%u len=%u",
159 event->wd, event->mask,
160 event->cookie, event->len);
161 } else if (event_set[test_num] == event->mask) {
Jan Kara1ea28212014-02-20 18:16:17 +0100162 if (event->cookie != 0) {
163 tst_resm(TFAIL,
164 "get event: wd=%d mask=%x "
165 "cookie=%u (expected 0) len=%u",
166 event->wd, event->mask,
167 event->cookie, event->len);
168 } else {
169 tst_resm(TPASS, "get event: wd=%d "
170 "mask=%x cookie=%u len=%u",
171 event->wd, event->mask,
172 event->cookie, event->len);
173 }
subrata_modak73e6ce72007-05-24 10:00:45 +0000174
subrata_modak56207ce2009-03-23 13:35:39 +0000175 } else {
176 tst_resm(TFAIL, "get event: wd=%d mask=%x "
177 "(expected %x) cookie=%u len=%u",
178 event->wd, event->mask,
179 event_set[test_num],
180 event->cookie, event->len);
181 }
182 test_num++;
183 i += EVENT_SIZE + event->len;
184 }
185 for (; test_num < TST_TOTAL; test_num++) {
Garrett Cooper53740502010-12-16 00:04:01 -0800186 tst_resm(TFAIL, "didn't get event: mask=%x",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800187 event_set[test_num]);
subrata_modak73e6ce72007-05-24 10:00:45 +0000188
subrata_modak56207ce2009-03-23 13:35:39 +0000189 }
subrata_modak73e6ce72007-05-24 10:00:45 +0000190
Garrett Cooper2c282152010-12-16 00:55:50 -0800191 }
subrata_modak56207ce2009-03-23 13:35:39 +0000192
subrata_modak56207ce2009-03-23 13:35:39 +0000193 cleanup();
Garrett Cooper766f33c2010-11-22 14:53:00 -0800194 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800195}
subrata_modak73e6ce72007-05-24 10:00:45 +0000196
Cyril Hrubis26aa0a82013-08-08 14:58:42 +0200197static void setup(void)
subrata_modak56207ce2009-03-23 13:35:39 +0000198{
Garrett Cooper2c282152010-12-16 00:55:50 -0800199
subrata_modak56207ce2009-03-23 13:35:39 +0000200 tst_sig(NOFORK, DEF_HANDLER, cleanup);
subrata_modak73e6ce72007-05-24 10:00:45 +0000201
subrata_modak56207ce2009-03-23 13:35:39 +0000202 TEST_PAUSE;
subrata_modak73e6ce72007-05-24 10:00:45 +0000203
subrata_modak56207ce2009-03-23 13:35:39 +0000204 tst_tmpdir();
subrata_modak73e6ce72007-05-24 10:00:45 +0000205
subrata_modak56207ce2009-03-23 13:35:39 +0000206 sprintf(fname, "tfile_%d", getpid());
207 if ((fd = open(fname, O_RDWR | O_CREAT, 0700)) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800208 tst_brkm(TBROK | TERRNO, cleanup,
209 "open(%s, O_RDWR|O_CREAT,0700) failed", fname);
subrata_modak56207ce2009-03-23 13:35:39 +0000210 }
211 if ((write(fd, fname, 1)) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800212 tst_brkm(TBROK | TERRNO, cleanup, "write(%d, %s, 1) failed",
213 fd, fname);
subrata_modak56207ce2009-03-23 13:35:39 +0000214 }
subrata_modak73e6ce72007-05-24 10:00:45 +0000215
subrata_modak56207ce2009-03-23 13:35:39 +0000216 /* close the file we have open */
217 if (close(fd) == -1) {
Garrett Cooper53740502010-12-16 00:04:01 -0800218 tst_brkm(TBROK, cleanup, "close(%s) failed", fname);
subrata_modak56207ce2009-03-23 13:35:39 +0000219 }
220 if ((fd_notify = myinotify_init()) < 0) {
221 if (errno == ENOSYS) {
Garrett Cooper766f33c2010-11-22 14:53:00 -0800222 tst_brkm(TCONF, cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000223 "inotify is not configured in this kernel.");
subrata_modak56207ce2009-03-23 13:35:39 +0000224 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800225 tst_brkm(TBROK | TERRNO, cleanup,
226 "inotify_init failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000227 }
228 }
subrata_modak73e6ce72007-05-24 10:00:45 +0000229
subrata_modak56207ce2009-03-23 13:35:39 +0000230 if ((wd = myinotify_add_watch(fd_notify, fname, IN_ALL_EVENTS)) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800231 tst_brkm(TBROK | TERRNO, cleanup,
232 "inotify_add_watch (%d, %s, IN_ALL_EVENTS) failed",
233 fd_notify, fname);
Cyril Hrubis04f21772014-12-09 14:26:23 +0100234 reap_wd = 1;
subrata_modak56207ce2009-03-23 13:35:39 +0000235 };
subrata_modak73e6ce72007-05-24 10:00:45 +0000236
Garrett Cooper2c282152010-12-16 00:55:50 -0800237}
subrata_modak73e6ce72007-05-24 10:00:45 +0000238
Cyril Hrubis26aa0a82013-08-08 14:58:42 +0200239static void cleanup(void)
subrata_modak56207ce2009-03-23 13:35:39 +0000240{
Cyril Hrubis04f21772014-12-09 14:26:23 +0100241 if (reap_wd && myinotify_rm_watch(fd_notify, wd) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800242 tst_resm(TWARN | TERRNO, "inotify_rm_watch (%d, %d) failed",
Garrett Cooper766f33c2010-11-22 14:53:00 -0800243 fd_notify, wd);
subrata_modak73e6ce72007-05-24 10:00:45 +0000244
subrata_modak56207ce2009-03-23 13:35:39 +0000245 }
subrata_modak73e6ce72007-05-24 10:00:45 +0000246
Cyril Hrubis04f21772014-12-09 14:26:23 +0100247 if (fd_notify > 0 && close(fd_notify))
Garrett Cooper766f33c2010-11-22 14:53:00 -0800248 tst_resm(TWARN, "close(%d) failed", fd_notify);
subrata_modak73e6ce72007-05-24 10:00:45 +0000249
subrata_modak56207ce2009-03-23 13:35:39 +0000250 tst_rmdir();
Garrett Cooper2c282152010-12-16 00:55:50 -0800251}
subrata_modakc59422e2007-06-04 10:39:59 +0000252
253#else
254
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200255char *TCID = "inotify01";
256int TST_TOTAL = 0;
subrata_modakc59422e2007-06-04 10:39:59 +0000257
Garrett Cooper766f33c2010-11-22 14:53:00 -0800258int main(void)
subrata_modakc59422e2007-06-04 10:39:59 +0000259{
Garrett Cooper53740502010-12-16 00:04:01 -0800260 tst_brkm(TCONF, NULL, "system doesn't have required inotify support");
subrata_modakc59422e2007-06-04 10:39:59 +0000261}
262
Markos Chandrasf4539c62012-01-03 09:41:10 +0000263#endif