Skip to contentSkip to conversation

lib: Add safe timerfd macros

PATCH New Petr Vorel 2 participants 4 messages Mar 4, 2020 – Mar 5, 2020 Library code

lore
Share & tools
lore

Petr Vorel

author
#0 lore raw

PATCH 1/2 lib: Add safe timerfd macros

SAFE_TIMERFD_CREATE(), SAFE_TIMERFD_GETTIME() and SAFE_TIMERFD_SETTIME()

Added only to new C API.

---
Changes v1->v2:
* patch based on 1st and 2nd patch from Cyril's patchset "[01/12] lib:
* Move tst_clock_name() to tst_clock.c" (now posted only these 2)
https://patchwork.ozlabs.org/project/ltp/list/?series=162390&state=*
* Move implementation code to C file
* drop version check. BTW I was wrong, timerfd_create() requires flags to be zero (I had code in timerfd_settime()). And this is correctly handled in old tests (old tests which use non-zero flag already require correctly 2.6.27 instead of 2.6.25).
* check return for == -1 (instead of < 0)

NOTE: I decided ignore errno reset (and not use TEST()), as I agree with Cyril ("Generally the syscalls in libc have single macro definition that is used everywhere to copy the error from the errno variable. If that piece of code is buggy half of the test in LTP would fail anyway.")

Kind regards,
Petr

 include/tst_safe_timerfd.h | 32 +++++++++++++++++++++
 lib/tst_safe_timerfd.c     | 59 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 91 insertions(+)
 create mode 100644 include/tst_safe_timerfd.h
 create mode 100644 lib/tst_safe_timerfd.c
include/tst_safe_timerfd.h new file +32 -0
new file mode 100644
index 000000000..526f12838
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 Petr Vorel <[email protected]>
+ */
+
+#ifndef TST_SAFE_TIMERFD_H__
+#define TST_SAFE_TIMERFD_H__
+
+#include "lapi/timerfd.h"
+
+int safe_timerfd_create(const char *file, const int lineno,
+				      int clockid, int flags);
+
+#define SAFE_TIMERFD_CREATE(clockid, flags)\
+	safe_timerfd_create(__FILE__, __LINE__, (clockid), (flags))
+
+int safe_timerfd_gettime(const char *file, const int lineno,
+				int fd, struct itimerspec *curr_value);
+
+#define SAFE_TIMERFD_GETTIME(fd, curr_value)\
+	safe_timerfd_gettime(__FILE__, __LINE__, (fd), (curr_value))
+
+int safe_timerfd_settime(const char *file, const int lineno,
+				int fd, int flags,
+				const struct itimerspec *new_value,
+				struct itimerspec *old_value);
+
+#define SAFE_TIMERFD_SETTIME(fd, flags, new_value, old_value)\
+	safe_timerfd_settime(__FILE__, __LINE__, (fd), (flags), (new_value), \
+						 (old_value))
+
+#endif /* SAFE_TIMERFD_H__ */
lib/tst_safe_timerfd.c new file +59 -0

59 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 1/2 lib: Add safe timerfd macros

Hi,

I'm sorry I forget to add v2 in subject.

Quoted 4 lines

Changes v1->v2:
* patch based on 1st and 2nd patch from Cyril's patchset "[01/12] lib:
* Move tst_clock_name() to tst_clock.c" (now posted only these 2)
https://patchwork.ozlabs.org/project/ltp/list/?series=162390&state=*

And the reason was to make use of tst_clock_name() as Cyril suggested.

Kind regards,
Petr

author
#2 lore raw

PATCH 2/2 timerfd: Use safe macros

In reply to Petr Vorel

SAFE_TIMERFD_CREATE() and SAFE_TIMERFD_SETTIME()

---
Changes v1->v2: NONE

 testcases/kernel/syscalls/timerfd/timerfd01.c        |  9 +++------
 .../kernel/syscalls/timerfd/timerfd_settime02.c      | 12 ++----------
 2 files changed, 5 insertions(+), 16 deletions(-)
testcases/kernel/syscalls/timerfd/timerfd01.c +3 -6
index af19b4399..7bbde7283 100644
@@ -19,7 +19,7 @@
 #include <poll.h>
 #include "tst_test.h"
 #include "tst_timer.h"
-#include "lapi/timerfd.h"
+#include "tst_safe_timerfd.h"
 
 static struct tcase {
 	int id;
@@ -47,8 +47,7 @@ static void settime(int tfd, struct itimerspec *tmr, int tflags,
 	tmr->it_value = tst_us_to_timespec(tvalue);
 	tmr->it_interval = tst_us_to_timespec(tinterval);
 
-	if (timerfd_settime(tfd, tflags, tmr, NULL))
-		tst_brk(TBROK | TERRNO, "timerfd_settime() failed");
+	SAFE_TIMERFD_SETTIME(tfd, tflags, tmr, NULL);
 }
 
 static void waittmr(int tfd, unsigned int exp_ticks)
@@ -87,9 +86,7 @@ static void run(unsigned int n)
 
 	tst_res(TINFO, "testing %s", clks->name);
 
-	tfd = timerfd_create(clks->id, 0);
-	if (tfd == -1)
-		tst_brk(TFAIL | TERRNO, "timerfd_create() failed");
+	tfd = SAFE_TIMERFD_CREATE(clks->id, 0);
 
 	tst_res(TINFO, "relative timer (100 ms)");
 	settime(tfd, &tmr, 0, 100 * 1000, 0);
testcases/kernel/syscalls/timerfd/timerfd_settime02.c +2 -10
index 7fa26de62..0565802f4 100644
@@ -15,7 +15,7 @@
  *  timerfd: Protect the might cancel mechanism proper
  */
 #include <unistd.h>
-#include <lapi/timerfd.h>
+#include "tst_safe_timerfd.h"
 #include "tst_test.h"
 #include "tst_fuzzy_sync.h"
 #include "tst_taint.h"
@@ -32,16 +32,8 @@ static struct tst_fzsync_pair fzsync_pair;
 
 static void setup(void)
 {
-	int ttype;
-
 	tst_taint_init(TST_TAINT_W | TST_TAINT_D);
-	errno = 0;
-	fd = timerfd_create(CLOCK_REALTIME, 0);
-
-	if (fd < 0) {
-		ttype = (errno == ENOTSUP ? TCONF : TBROK);
-		tst_brk(ttype | TERRNO, "Cannot create timer");
-	}
+	fd = SAFE_TIMERFD_CREATE(CLOCK_REALTIME, 0);
 
 	fzsync_pair.exec_loops = 1000000;
 	tst_fzsync_pair_init(&fzsync_pair);
Signature · 1 line
2.25.1
#3 lore raw

PATCH 1/2 lib: Add safe timerfd macros

In reply to Petr Vorel

Hi!

Quoted 50 lines
 include/tst_safe_timerfd.h | 32 +++++++++++++++++++++
 lib/tst_safe_timerfd.c     | 59 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 91 insertions(+)
 create mode 100644 include/tst_safe_timerfd.h
 create mode 100644 lib/tst_safe_timerfd.c
include/tst_safe_timerfd.h new file +32 -0
new file mode 100644
index 000000000..526f12838
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 Petr Vorel <[email protected]>
+ */
+
+#ifndef TST_SAFE_TIMERFD_H__
+#define TST_SAFE_TIMERFD_H__
+
+#include "lapi/timerfd.h"
+
+int safe_timerfd_create(const char *file, const int lineno,
+				      int clockid, int flags);
+
+#define SAFE_TIMERFD_CREATE(clockid, flags)\
+	safe_timerfd_create(__FILE__, __LINE__, (clockid), (flags))
+
+int safe_timerfd_gettime(const char *file, const int lineno,
+				int fd, struct itimerspec *curr_value);
+
+#define SAFE_TIMERFD_GETTIME(fd, curr_value)\
+	safe_timerfd_gettime(__FILE__, __LINE__, (fd), (curr_value))
+
+int safe_timerfd_settime(const char *file, const int lineno,
+				int fd, int flags,
+				const struct itimerspec *new_value,
+				struct itimerspec *old_value);
+
+#define SAFE_TIMERFD_SETTIME(fd, flags, new_value, old_value)\
+	safe_timerfd_settime(__FILE__, __LINE__, (fd), (flags), (new_value), \
+						 (old_value))
+
+#endif /* SAFE_TIMERFD_H__ */
lib/tst_safe_timerfd.c new file +0 -0
new file mode 100644
index 000000000..80de87ad3

lib/tst_safe_timerfd.c line 6

Quoted 6 lines
lib/tst_safe_timerfd.c new file +6 -0
@@ -0,0 +1,59 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 Petr Vorel <[email protected]>
+ */
+
+#include <errno.h>

Errno is include in tst_test.h.

Quoted 6 lines
lib/tst_safe_timerfd.c new file +6 -0
+#include "tst_safe_timerfd.h"
+#include "lapi/timerfd.h"
+#include "tst_clocks.h"
+#define TST_NO_DEFAULT_MAIN
+#include "tst_test.h"
+

lib/tst_safe_timerfd.c line 23

Quoted 10 lines
lib/tst_safe_timerfd.c new file +10 -0
+#define TTYPE (errno == ENOTSUP ? TCONF : TBROK)
+
+int safe_timerfd_create(const char *file, const int lineno,
+				      int clockid, int flags)
+{
+	int fd;
+
+	fd = timerfd_create(clockid, flags);
+
+	if (fd == -1) {

See the other email about the return values.

Other than that this looks good.

Quoted 39 lines
lib/tst_safe_timerfd.c new file +36 -0
+		tst_brk(TTYPE | TERRNO, "%s:%d timerfd_create(%s) failed",
+			file, lineno, tst_clock_name(clockid));
+	}
+
+	return fd;
+}
+
+int safe_timerfd_gettime(const char *file, const int lineno,
+				int fd, struct itimerspec *curr_value)
+{
+	int rval;
+
+	rval = timerfd_gettime(fd, curr_value);
+	if (rval == -1) {
+		tst_brk(TTYPE | TERRNO, "%s:%d timerfd_gettime() failed",
+			file, lineno);
+	}
+
+	return rval;
+}
+
+int safe_timerfd_settime(const char *file, const int lineno,
+				int fd, int flags,
+				const struct itimerspec *new_value,
+				struct itimerspec *old_value)
+{
+	int rval;
+
+	rval = timerfd_settime(fd, flags, new_value, old_value);
+	if (rval == -1) {
+		tst_brk(TTYPE | TERRNO, "%s:%d timerfd_settime() failed",
+			file, lineno);
+	}
+
+	return rval;
+}
Signature · 1 line
2.25.1
Signature · 2 lines
Cyril Hrubis
[email protected]