Linux Test Project
 help / color / mirror / Atom feed
* [LTP] [PATCH v2 1/2] lib: tst_rtctime: close RTC fd on the ioctl() error path
@ 2026-07-17 10:19 Kuba Pawlak via ltp
  2026-07-17 10:20 ` [LTP] [PATCH v2 2/2] rtc02: skip (TCONF) on read-only RTCs that reject RTC_SET_TIME Kuba Pawlak via ltp
  2026-07-17 11:26 ` [LTP] lib: tst_rtctime: close RTC fd on the ioctl() error path linuxtestproject.agent
  0 siblings, 2 replies; 3+ messages in thread
From: Kuba Pawlak via ltp @ 2026-07-17 10:19 UTC (permalink / raw)
  To: ltp

tst_rtc_ioctl() opens the RTC device with SAFE_OPEN() but returns -1 on
an ioctl() failure without closing the descriptor, leaking it on every
failed call. The RTC character device is exclusive-open, so a leaked fd
makes the next tst_rtc_ioctl() open fail with EBUSY.

This was mostly latent because the failure path was rarely taken, but it
is now hit routinely on RTCs that reject RTC_SET_TIME (e.g. Qualcomm PMIC
RTCs returning -ENODEV). Close the fd on the error path and preserve
errno so callers can still inspect the ioctl() failure reason.

Signed-off-by: Kuba Pawlak <kuba.pawlak@canonical.com>
---
 lib/tst_rtctime.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/lib/tst_rtctime.c b/lib/tst_rtctime.c
index c62ac73..cc0cb4e 100644
--- a/lib/tst_rtctime.c
+++ b/lib/tst_rtctime.c
@@ -7,6 +7,7 @@
 
 #include <stdbool.h>
 #include <limits.h>
+#include <errno.h>
 #define TST_NO_DEFAULT_MAIN
 #include "tst_test.h"
 #include "tst_rtctime.h"
@@ -117,11 +118,15 @@ int tst_rtc_ioctl(const char *rtc_dev, unsigned long request,
 
 	ret = ioctl(rtc_fd, request, rtc_tm);
 
-	if (ret != 0)
-		return -1;
+	if (ret != 0) {
+		int saved_errno = errno;
 
-	if (rtc_fd > 0)
 		SAFE_CLOSE(rtc_fd);
+		errno = saved_errno;
+		return -1;
+	}
+
+	SAFE_CLOSE(rtc_fd);
 
 	return 0;
 }
-- 
2.43.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-07-17 11:27 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-17 10:19 [LTP] [PATCH v2 1/2] lib: tst_rtctime: close RTC fd on the ioctl() error path Kuba Pawlak via ltp
2026-07-17 10:20 ` [LTP] [PATCH v2 2/2] rtc02: skip (TCONF) on read-only RTCs that reject RTC_SET_TIME Kuba Pawlak via ltp
2026-07-17 11:26 ` [LTP] lib: tst_rtctime: close RTC fd on the ioctl() error path linuxtestproject.agent

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox