[LTP] [PATCH v3] inotify: modernize with SAFE_ wrappers

Jinseok Kim always.starving0@gmail.com
Sat Feb 21 09:21:37 CET 2026


Replace manual read/write with SAFE_READ/SAFE_WRITE for better stability
and consistency.

inotify12.c intentionally unchanged: raw read() + manual EAGAIN handling
is required to treat missing second event as IN_IGNORED (normal case).

Signed-off-by: Jinseok Kim <always.starving0@gmail.com>
---
 testcases/kernel/syscalls/inotify/inotify01.c | 17 +++--------------
 testcases/kernel/syscalls/inotify/inotify03.c | 14 ++------------
 testcases/kernel/syscalls/inotify/inotify04.c |  4 +---
 testcases/kernel/syscalls/inotify/inotify05.c |  7 +------
 testcases/kernel/syscalls/inotify/inotify07.c |  7 +------
 testcases/kernel/syscalls/inotify/inotify08.c |  7 +------
 testcases/kernel/syscalls/inotify/inotify10.c |  4 +---
 7 files changed, 10 insertions(+), 50 deletions(-)

diff --git a/testcases/kernel/syscalls/inotify/inotify01.c b/testcases/kernel/syscalls/inotify/inotify01.c
index 8671b594a..972b1025e 100644
--- a/testcases/kernel/syscalls/inotify/inotify01.c
+++ b/testcases/kernel/syscalls/inotify/inotify01.c
@@ -55,10 +55,7 @@ void verify_inotify(void)
 	event_set[test_cnt] = IN_OPEN;
 	test_cnt++;

-	if (read(fd, buf, BUF_SIZE) == -1) {
-		tst_brk(TBROK | TERRNO,
-			"read(%d, buf, %d) failed", fd, BUF_SIZE);
-	}
+	SAFE_READ(0, fd, buf, BUF_SIZE);
 	event_set[test_cnt] = IN_ACCESS;
 	test_cnt++;

@@ -70,10 +67,7 @@ void verify_inotify(void)
 	event_set[test_cnt] = IN_OPEN;
 	test_cnt++;

-	if (write(fd, buf, BUF_SIZE) == -1) {
-		tst_brk(TBROK,
-			"write(%d, %s, %d) failed", fd, fname, BUF_SIZE);
-	}
+	SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, BUF_SIZE);
 	event_set[test_cnt] = IN_MODIFY;
 	test_cnt++;

@@ -85,12 +79,7 @@ void verify_inotify(void)
 	 * get list of events
 	 */
 	int len, i = 0, test_num = 0;
-	if ((len = read(fd_notify, event_buf, EVENT_BUF_LEN)) < 0) {
-		tst_brk(TBROK,
-			"read(%d, buf, %zu) failed",
-			fd_notify, EVENT_BUF_LEN);
-
-	}
+	len = SAFE_READ(0, fd_notify, event_buf, EVENT_BUF_LEN);

 	/*
 	 * check events
diff --git a/testcases/kernel/syscalls/inotify/inotify03.c b/testcases/kernel/syscalls/inotify/inotify03.c
index 9bb95addb..a7974dd57 100644
--- a/testcases/kernel/syscalls/inotify/inotify03.c
+++ b/testcases/kernel/syscalls/inotify/inotify03.c
@@ -74,11 +74,7 @@ void verify_inotify(void)
 	}
 	mount_flag = 0;

-	len = read(fd_notify, event_buf, EVENT_BUF_LEN);
-	if (len < 0) {
-		tst_brk(TBROK | TERRNO,
-			"read(%d, buf, %zu) failed", fd_notify, EVENT_BUF_LEN);
-	}
+	len = SAFE_READ(0, fd_notify, event_buf, EVENT_BUF_LEN);

 	/* check events */
 	test_num = 0;
@@ -125,8 +121,6 @@ void verify_inotify(void)

 static void setup(void)
 {
-	int ret;
-
 	SAFE_MKDIR(mntpoint, DIR_MODE);

 	SAFE_MOUNT(tst_device->dev, mntpoint, tst_device->fs_type, 0, NULL);
@@ -135,11 +129,7 @@ static void setup(void)
 	sprintf(fname, "%s/tfile_%d", mntpoint, getpid());
 	fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700);

-	ret = write(fd, fname, 1);
-	if (ret == -1) {
-		tst_brk(TBROK | TERRNO,
-			 "write(%d, %s, 1) failed", fd, fname);
-	}
+	SAFE_WRITE(SAFE_WRITE_ALL, fd, fname, 1);

 	/* close the file we have open */
 	SAFE_CLOSE(fd);
diff --git a/testcases/kernel/syscalls/inotify/inotify04.c b/testcases/kernel/syscalls/inotify/inotify04.c
index 1db38ddf2..947623952 100644
--- a/testcases/kernel/syscalls/inotify/inotify04.c
+++ b/testcases/kernel/syscalls/inotify/inotify04.c
@@ -118,9 +118,7 @@ void verify_inotify(void)
 	strcpy(event_set[test_cnt].name, "");
 	test_cnt++;

-	len = read(fd_notify, event_buf, EVENT_BUF_LEN);
-	if (len == -1)
-		tst_brk(TBROK | TERRNO, "read failed");
+	len = SAFE_READ(0, fd_notify, event_buf, EVENT_BUF_LEN);

 	while (i < len) {
 		struct inotify_event *event;
diff --git a/testcases/kernel/syscalls/inotify/inotify05.c b/testcases/kernel/syscalls/inotify/inotify05.c
index d9bfb05f1..82a4c7bdc 100644
--- a/testcases/kernel/syscalls/inotify/inotify05.c
+++ b/testcases/kernel/syscalls/inotify/inotify05.c
@@ -60,12 +60,7 @@ void verify_inotify(void)
 		/*
 		 * get list on events
 		 */
-		len = read(fd_notify, event_buf, EVENT_BUF_LEN);
-		if (len < 0) {
-			tst_brk(TBROK | TERRNO,
-				"read(%d, buf, %zu) failed",
-				fd_notify, EVENT_BUF_LEN);
-		}
+		len = SAFE_READ(0, fd_notify, event_buf, EVENT_BUF_LEN);

 		/*
 		 * check events
diff --git a/testcases/kernel/syscalls/inotify/inotify07.c b/testcases/kernel/syscalls/inotify/inotify07.c
index 66a2f4d37..b4000f353 100644
--- a/testcases/kernel/syscalls/inotify/inotify07.c
+++ b/testcases/kernel/syscalls/inotify/inotify07.c
@@ -88,12 +88,7 @@ void verify_inotify(void)
 	strcpy(event_set[test_cnt].name, FILE_NAME);
 	test_cnt++;

-	int len = read(fd_notify, event_buf, EVENT_BUF_LEN);
-	if (len == -1 && errno != EAGAIN) {
-		tst_brk(TBROK | TERRNO,
-			"read(%d, buf, %zu) failed",
-			fd_notify, EVENT_BUF_LEN);
-	}
+	int len = SAFE_READ(0, fd_notify, event_buf, EVENT_BUF_LEN);

 	int i = 0, test_num = 0;
 	while (i < len) {
diff --git a/testcases/kernel/syscalls/inotify/inotify08.c b/testcases/kernel/syscalls/inotify/inotify08.c
index 4cbb16ce0..e0837cac3 100644
--- a/testcases/kernel/syscalls/inotify/inotify08.c
+++ b/testcases/kernel/syscalls/inotify/inotify08.c
@@ -86,12 +86,7 @@ void verify_inotify(void)
 	SAFE_TOUCH(OVL_LOWER"/"FILE_NAME, 0644, NULL);
 	SAFE_TOUCH(OVL_UPPER"/"FILE_NAME, 0644, NULL);

-	int len = read(fd_notify, event_buf, EVENT_BUF_LEN);
-	if (len == -1 && errno != EAGAIN) {
-		tst_brk(TBROK | TERRNO,
-			"read(%d, buf, %zu) failed",
-			fd_notify, EVENT_BUF_LEN);
-	}
+	int len = SAFE_READ(0, fd_notify, event_buf, EVENT_BUF_LEN);

 	int i = 0, test_num = 0;
 	while (i < len) {
diff --git a/testcases/kernel/syscalls/inotify/inotify10.c b/testcases/kernel/syscalls/inotify/inotify10.c
index a78572dff..4c3a1d116 100644
--- a/testcases/kernel/syscalls/inotify/inotify10.c
+++ b/testcases/kernel/syscalls/inotify/inotify10.c
@@ -143,9 +143,7 @@ static void verify_inotify(unsigned int n)
 		test_cnt++;
 	}

-	len = read(fd_notify, event_buf, EVENT_BUF_LEN);
-	if (len == -1)
-		tst_brk(TBROK | TERRNO, "read failed");
+	len = SAFE_READ(0, fd_notify, event_buf, EVENT_BUF_LEN);

 	while (i < len) {
 		struct event_t *expected = &event_set[test_num];
--
2.43.0


More information about the ltp mailing list