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