1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2007 SWSoft.  All Rights Reserved.
4f08c3bdfSopenharmony_ci * Author: Andrew Vagin <avagin@sw.ru>
5f08c3bdfSopenharmony_ci *
6f08c3bdfSopenharmony_ci * DESCRIPTION
7f08c3bdfSopenharmony_ci *     Check that inotify work for a file
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * ALGORITHM
10f08c3bdfSopenharmony_ci *     Execute sequence file's operation and check return events
11f08c3bdfSopenharmony_ci */
12f08c3bdfSopenharmony_ci#include "config.h"
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include <stdio.h>
15f08c3bdfSopenharmony_ci#include <sys/stat.h>
16f08c3bdfSopenharmony_ci#include <sys/types.h>
17f08c3bdfSopenharmony_ci#include <fcntl.h>
18f08c3bdfSopenharmony_ci#include <errno.h>
19f08c3bdfSopenharmony_ci#include <string.h>
20f08c3bdfSopenharmony_ci#include <sys/syscall.h>
21f08c3bdfSopenharmony_ci#include "tst_test.h"
22f08c3bdfSopenharmony_ci#include "inotify.h"
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci#if defined(HAVE_SYS_INOTIFY_H)
25f08c3bdfSopenharmony_ci#include <sys/inotify.h>
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci#define EVENT_MAX 1024
28f08c3bdfSopenharmony_ci/* size of the event structure, not counting name */
29f08c3bdfSopenharmony_ci#define EVENT_SIZE  (sizeof (struct inotify_event))
30f08c3bdfSopenharmony_ci/* reasonable guess as to size of 1024 events */
31f08c3bdfSopenharmony_ci#define EVENT_BUF_LEN        (EVENT_MAX * (EVENT_SIZE + 16))
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci#define BUF_SIZE 256
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_cistatic char fname[BUF_SIZE];
36f08c3bdfSopenharmony_cistatic char buf[BUF_SIZE];
37f08c3bdfSopenharmony_cistatic int fd, fd_notify;
38f08c3bdfSopenharmony_cistatic int wd, reap_wd;
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_cistatic unsigned int event_set[EVENT_MAX];
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_cistatic char event_buf[EVENT_BUF_LEN];
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_civoid verify_inotify(void)
45f08c3bdfSopenharmony_ci{
46f08c3bdfSopenharmony_ci	int test_cnt = 0;
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	/*
49f08c3bdfSopenharmony_ci	 * generate sequence of events
50f08c3bdfSopenharmony_ci	 */
51f08c3bdfSopenharmony_ci	SAFE_CHMOD(fname, 0755);
52f08c3bdfSopenharmony_ci	event_set[test_cnt] = IN_ATTRIB;
53f08c3bdfSopenharmony_ci	test_cnt++;
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(fname, O_RDONLY);
56f08c3bdfSopenharmony_ci	event_set[test_cnt] = IN_OPEN;
57f08c3bdfSopenharmony_ci	test_cnt++;
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci	if (read(fd, buf, BUF_SIZE) == -1) {
60f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO,
61f08c3bdfSopenharmony_ci			"read(%d, buf, %d) failed", fd, BUF_SIZE);
62f08c3bdfSopenharmony_ci	}
63f08c3bdfSopenharmony_ci	event_set[test_cnt] = IN_ACCESS;
64f08c3bdfSopenharmony_ci	test_cnt++;
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
67f08c3bdfSopenharmony_ci	event_set[test_cnt] = IN_CLOSE_NOWRITE;
68f08c3bdfSopenharmony_ci	test_cnt++;
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700);
71f08c3bdfSopenharmony_ci	event_set[test_cnt] = IN_OPEN;
72f08c3bdfSopenharmony_ci	test_cnt++;
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	if (write(fd, buf, BUF_SIZE) == -1) {
75f08c3bdfSopenharmony_ci		tst_brk(TBROK,
76f08c3bdfSopenharmony_ci			"write(%d, %s, 1) failed", fd, fname);
77f08c3bdfSopenharmony_ci	}
78f08c3bdfSopenharmony_ci	event_set[test_cnt] = IN_MODIFY;
79f08c3bdfSopenharmony_ci	test_cnt++;
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
82f08c3bdfSopenharmony_ci	event_set[test_cnt] = IN_CLOSE_WRITE;
83f08c3bdfSopenharmony_ci	test_cnt++;
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_ci	/*
86f08c3bdfSopenharmony_ci	 * get list of events
87f08c3bdfSopenharmony_ci	 */
88f08c3bdfSopenharmony_ci	int len, i = 0, test_num = 0;
89f08c3bdfSopenharmony_ci	if ((len = read(fd_notify, event_buf, EVENT_BUF_LEN)) < 0) {
90f08c3bdfSopenharmony_ci		tst_brk(TBROK,
91f08c3bdfSopenharmony_ci			"read(%d, buf, %zu) failed",
92f08c3bdfSopenharmony_ci			fd_notify, EVENT_BUF_LEN);
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ci	}
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci	/*
97f08c3bdfSopenharmony_ci	 * check events
98f08c3bdfSopenharmony_ci	 */
99f08c3bdfSopenharmony_ci	while (i < len) {
100f08c3bdfSopenharmony_ci		struct inotify_event *event;
101f08c3bdfSopenharmony_ci		event = (struct inotify_event *)&event_buf[i];
102f08c3bdfSopenharmony_ci		if (test_num >= test_cnt) {
103f08c3bdfSopenharmony_ci			tst_res(TFAIL,
104f08c3bdfSopenharmony_ci				"get unnecessary event: wd=%d mask=%02x "
105f08c3bdfSopenharmony_ci				"cookie=%u len=%u",
106f08c3bdfSopenharmony_ci				event->wd, event->mask,
107f08c3bdfSopenharmony_ci				event->cookie, event->len);
108f08c3bdfSopenharmony_ci		} else if (event_set[test_num] == event->mask) {
109f08c3bdfSopenharmony_ci			if (event->cookie != 0) {
110f08c3bdfSopenharmony_ci				tst_res(TFAIL,
111f08c3bdfSopenharmony_ci					"get event: wd=%d mask=%02x "
112f08c3bdfSopenharmony_ci					"cookie=%u (expected 0) len=%u",
113f08c3bdfSopenharmony_ci					event->wd, event->mask,
114f08c3bdfSopenharmony_ci					event->cookie, event->len);
115f08c3bdfSopenharmony_ci			} else {
116f08c3bdfSopenharmony_ci				tst_res(TPASS, "get event: wd=%d "
117f08c3bdfSopenharmony_ci					"mask=%02x cookie=%u len=%u",
118f08c3bdfSopenharmony_ci					event->wd, event->mask,
119f08c3bdfSopenharmony_ci					event->cookie, event->len);
120f08c3bdfSopenharmony_ci			}
121f08c3bdfSopenharmony_ci
122f08c3bdfSopenharmony_ci		} else {
123f08c3bdfSopenharmony_ci			tst_res(TFAIL, "get event: wd=%d mask=%02x "
124f08c3bdfSopenharmony_ci				"(expected %x) cookie=%u len=%u",
125f08c3bdfSopenharmony_ci				event->wd, event->mask,
126f08c3bdfSopenharmony_ci				event_set[test_num],
127f08c3bdfSopenharmony_ci				event->cookie, event->len);
128f08c3bdfSopenharmony_ci		}
129f08c3bdfSopenharmony_ci		test_num++;
130f08c3bdfSopenharmony_ci		i += EVENT_SIZE + event->len;
131f08c3bdfSopenharmony_ci	}
132f08c3bdfSopenharmony_ci	for (; test_num < test_cnt; test_num++) {
133f08c3bdfSopenharmony_ci		tst_res(TFAIL, "didn't get event: mask=%02x",
134f08c3bdfSopenharmony_ci			event_set[test_num]);
135f08c3bdfSopenharmony_ci
136f08c3bdfSopenharmony_ci	}
137f08c3bdfSopenharmony_ci}
138f08c3bdfSopenharmony_ci
139f08c3bdfSopenharmony_cistatic void setup(void)
140f08c3bdfSopenharmony_ci{
141f08c3bdfSopenharmony_ci	sprintf(fname, "tfile_%d", getpid());
142f08c3bdfSopenharmony_ci
143f08c3bdfSopenharmony_ci	SAFE_FILE_PRINTF(fname, "%s", fname);
144f08c3bdfSopenharmony_ci
145f08c3bdfSopenharmony_ci	fd_notify = SAFE_MYINOTIFY_INIT();
146f08c3bdfSopenharmony_ci
147f08c3bdfSopenharmony_ci	wd = SAFE_MYINOTIFY_ADD_WATCH(fd_notify, fname, IN_ALL_EVENTS);
148f08c3bdfSopenharmony_ci	reap_wd = 1;
149f08c3bdfSopenharmony_ci}
150f08c3bdfSopenharmony_ci
151f08c3bdfSopenharmony_cistatic void cleanup(void)
152f08c3bdfSopenharmony_ci{
153f08c3bdfSopenharmony_ci	if (reap_wd && myinotify_rm_watch(fd_notify, wd) < 0) {
154f08c3bdfSopenharmony_ci		tst_res(TWARN | TERRNO, "inotify_rm_watch (%d, %d) failed",
155f08c3bdfSopenharmony_ci			fd_notify, wd);
156f08c3bdfSopenharmony_ci	}
157f08c3bdfSopenharmony_ci
158f08c3bdfSopenharmony_ci	if (fd_notify > 0)
159f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd_notify);
160f08c3bdfSopenharmony_ci}
161f08c3bdfSopenharmony_ci
162f08c3bdfSopenharmony_cistatic struct tst_test test = {
163f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
164f08c3bdfSopenharmony_ci	.setup = setup,
165f08c3bdfSopenharmony_ci	.cleanup = cleanup,
166f08c3bdfSopenharmony_ci	.test_all = verify_inotify,
167f08c3bdfSopenharmony_ci};
168f08c3bdfSopenharmony_ci
169f08c3bdfSopenharmony_ci#else
170f08c3bdfSopenharmony_ci	TST_TEST_TCONF("system doesn't have required inotify support");
171f08c3bdfSopenharmony_ci#endif
172