All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH 1/2] syscalls/clock_settime01.c: create syscall clock_settime test
@ 2018-12-05 19:30 Rafael David Tinoco
  2018-12-05 19:30 ` [LTP] [PATCH 2/2] timers/clock_settime: remove clock_settime tests Rafael David Tinoco
  2018-12-06 13:03 ` [LTP] [PATCH 1/2] syscalls/clock_settime01.c: create syscall clock_settime test Cyril Hrubis
  0 siblings, 2 replies; 37+ messages in thread
From: Rafael David Tinoco @ 2018-12-05 19:30 UTC (permalink / raw)
  To: ltp

Fixes: 343

clock_settime01 creates a new test, using new API, based on existing and
older kernel/timers/clock_settime tests. It includes tests from files
clock_settime02 and clock_settime03, which will be deleted in next
commits.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 runtest/syscalls                              |   2 +
 .../kernel/syscalls/clock_settime/.gitignore  |   1 +
 .../kernel/syscalls/clock_settime/Makefile    |   8 +
 .../syscalls/clock_settime/clock_settime01.c  | 187 ++++++++++++++++++
 4 files changed, 198 insertions(+)
 create mode 100644 testcases/kernel/syscalls/clock_settime/.gitignore
 create mode 100644 testcases/kernel/syscalls/clock_settime/Makefile
 create mode 100644 testcases/kernel/syscalls/clock_settime/clock_settime01.c

diff --git a/runtest/syscalls b/runtest/syscalls
index ac1d2d2cd..4cbc13209 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -79,6 +79,8 @@ clock_nanosleep01 clock_nanosleep01
 clock_nanosleep02 clock_nanosleep02
 clock_nanosleep2_01 clock_nanosleep2_01
 
+clock_settime01 clock_settime01
+
 clone01 clone01
 clone02 clone02
 clone03 clone03
diff --git a/testcases/kernel/syscalls/clock_settime/.gitignore b/testcases/kernel/syscalls/clock_settime/.gitignore
new file mode 100644
index 000000000..fcbb9fecc
--- /dev/null
+++ b/testcases/kernel/syscalls/clock_settime/.gitignore
@@ -0,0 +1 @@
+clock_settime01
diff --git a/testcases/kernel/syscalls/clock_settime/Makefile b/testcases/kernel/syscalls/clock_settime/Makefile
new file mode 100644
index 000000000..e6674a6b2
--- /dev/null
+++ b/testcases/kernel/syscalls/clock_settime/Makefile
@@ -0,0 +1,8 @@
+# Copyright (c) 2018 - Linaro Limited. All rights reserved.
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/clock_settime/clock_settime01.c b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
new file mode 100644
index 000000000..591e5c723
--- /dev/null
+++ b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
@@ -0,0 +1,187 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+/*
+ * Basic tests for clock_settime(2) on different clock types
+ */
+
+#include "config.h"
+#include "tst_test.h"
+#include "lapi/syscalls.h"
+
+#define NSEC_PER_SEC (1000000000L)
+#define MAX_CLOCKS 16
+
+static struct timespec clock_realtime_saved;
+
+struct test_case {
+	clockid_t type;
+	struct timespec newtime;
+	int exp_ret;
+	int exp_err;
+	int replace;
+};
+
+struct test_case tc[] = {
+	{				/* case 01: REALTIME		      */
+	 .type = CLOCK_REALTIME,
+	 .exp_ret = 0,
+	 },
+	{				/* case 02: REALTIME: timespec NULL   */
+	 .type = CLOCK_REALTIME,
+	 .newtime.tv_sec = -2,
+	 .exp_ret = -1,
+	 .exp_err = EFAULT,
+	 .replace = 1,
+	 },
+	{				/* case 03: REALTIME: tv_sec = -1     */
+	 .type = CLOCK_REALTIME,
+	 .newtime.tv_sec = -1,
+	 .exp_ret = -1,
+	 .exp_err = EINVAL,
+	 .replace = 1,
+	 },
+	{				/* case 04: REALTIME: tv_nsec = -1    */
+	 .type = CLOCK_REALTIME,
+	 .newtime.tv_nsec = -1,
+	 .exp_ret = -1,
+	 .exp_err = EINVAL,
+	 .replace = 1,
+	 },
+	{				/* case 05: REALTIME: tv_nsec = 1s+1  */
+	 .type = CLOCK_REALTIME,
+	 .newtime.tv_nsec = NSEC_PER_SEC + 1,
+	 .exp_ret = -1,
+	 .exp_err = EINVAL,
+	 .replace = 1,
+	 },
+	{				/* case 06: MONOTONIC		      */
+	 .type = CLOCK_MONOTONIC,
+	 .exp_ret = -1,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 07: MAXCLOCK		      */
+	 .type = MAX_CLOCKS,
+	 .exp_ret = -1,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 08: MAXCLOCK+1		      */
+	 .type = MAX_CLOCKS + 1,
+	 .exp_ret = -1,
+	 .exp_err = EINVAL,
+	 },
+	/* Linux specific */
+	{				/* case 09: CLOCK_MONOTONIC_COARSE    */
+	 .type = CLOCK_MONOTONIC_COARSE,
+	 .exp_ret = -1,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 10: CLOCK_MONOTONIC_RAW       */
+	 .type = CLOCK_MONOTONIC_RAW,
+	 .exp_ret = -1,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 11: CLOCK_BOOTTIME	      */
+	 .type = CLOCK_BOOTTIME,
+	 .exp_ret = -1,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 12: CLOCK_PROCESS_CPUTIME_ID  */
+	 .type = CLOCK_PROCESS_CPUTIME_ID,
+	 .exp_ret = -1,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 13: CLOCK_THREAD_CPUTIME_ID   */
+	 .type = CLOCK_THREAD_CPUTIME_ID,
+	 .exp_ret = -1,
+	 .exp_err = EINVAL,
+	 },
+};
+
+static int sys_clock_settime(clockid_t clk_id, struct timespec *tp)
+{
+	return tst_syscall(__NR_clock_settime, clk_id, tp);
+}
+
+static int sys_clock_gettime(clockid_t clk_id, struct timespec *tp)
+{
+	return tst_syscall(__NR_clock_gettime, clk_id, tp);
+}
+
+static void cleanup(void)
+{
+	/* restore realtime clock */
+
+	if (sys_clock_settime(CLOCK_REALTIME, &clock_realtime_saved) < 0)
+		tst_res(TBROK | TTERRNO, "clock_settime(2): could not set "
+				"current time back");
+}
+
+static void setup(void)
+{
+	if (sys_clock_gettime(CLOCK_REALTIME, &clock_realtime_saved) < 0)
+		tst_res(TBROK | TTERRNO, "clock_gettime(2): could not get "
+				"current time");
+}
+
+static void verify_clock_settime(unsigned int i)
+{
+	struct timespec spec, *specptr;
+
+	if (tc[i].replace == 0) {
+
+		/* add 1 sec to test clock */
+
+		specptr = &spec;
+		specptr->tv_sec = clock_realtime_saved.tv_sec + 1;
+		specptr->tv_nsec = clock_realtime_saved.tv_nsec;
+
+	} else {
+
+		/* bad pointer case */
+
+		if (tc[i].newtime.tv_sec == -2)
+			specptr = tst_get_bad_addr(cleanup);
+
+		/* use given values */
+
+		else {
+			specptr = &spec;
+			specptr->tv_sec = tc[i].newtime.tv_sec;
+			specptr->tv_nsec = tc[i].newtime.tv_nsec;
+		}
+	}
+
+	TEST(sys_clock_settime(tc[i].type, specptr));
+
+	if (tc[i].exp_ret == TST_RET) {
+
+		if (TST_RET >= 0)
+			tst_res(TPASS, "clock_settime(2): worked as expected");
+
+		else {
+			if (tc[i].exp_err == TST_ERR)
+				tst_res(TPASS, "clock_settime(2): failed as "
+						"expected");
+			else
+				tst_res(TFAIL | TTERRNO, "clock_settime(2): "
+						"failed with different error");
+		}
+
+		return;
+	}
+
+	tst_res(TFAIL | TTERRNO, "clock_settime(2): clock type %d failed",
+			tc[i].type);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test = verify_clock_settime,
+	.cleanup = cleanup,
+	.tcnt = ARRAY_SIZE(tc),
+	.needs_root = 1,
+};
-- 
2.20.0.rc1


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

* [LTP] [PATCH 2/2] timers/clock_settime: remove clock_settime tests
  2018-12-05 19:30 [LTP] [PATCH 1/2] syscalls/clock_settime01.c: create syscall clock_settime test Rafael David Tinoco
@ 2018-12-05 19:30 ` Rafael David Tinoco
  2018-12-06 13:03 ` [LTP] [PATCH 1/2] syscalls/clock_settime01.c: create syscall clock_settime test Cyril Hrubis
  1 sibling, 0 replies; 37+ messages in thread
From: Rafael David Tinoco @ 2018-12-05 19:30 UTC (permalink / raw)
  To: ltp

Fixes: 343

clock_settime01 creates a new test, using new API, based on existing and
older kernel/timers/clock_settime tests. It includes tests from files
clock_settime02 and clock_settime03.

This commit deletes timers/clock_settime/* tests.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 runtest/timers                                |   2 -
 .../kernel/timers/clock_settime/.gitignore    |   2 -
 .../kernel/timers/clock_settime/Makefile      |  27 ---
 .../timers/clock_settime/clock_settime02.c    | 128 -------------
 .../timers/clock_settime/clock_settime03.c    | 173 ------------------
 5 files changed, 332 deletions(-)
 delete mode 100644 testcases/kernel/timers/clock_settime/.gitignore
 delete mode 100644 testcases/kernel/timers/clock_settime/Makefile
 delete mode 100644 testcases/kernel/timers/clock_settime/clock_settime02.c
 delete mode 100644 testcases/kernel/timers/clock_settime/clock_settime03.c

diff --git a/runtest/timers b/runtest/timers
index a58ac57fc..618d2cb0c 100644
--- a/runtest/timers
+++ b/runtest/timers
@@ -1,8 +1,6 @@
 #DESCRIPTION:Posix Timer Tests
 clock_gettime02 clock_gettime02
 clock_gettime03 clock_gettime03
-clock_settime02 clock_settime02
-clock_settime03 clock_settime03
 timer_create02 timer_create02
 timer_create03 timer_create03
 timer_create04 timer_create04
diff --git a/testcases/kernel/timers/clock_settime/.gitignore b/testcases/kernel/timers/clock_settime/.gitignore
deleted file mode 100644
index 957c5ac26..000000000
--- a/testcases/kernel/timers/clock_settime/.gitignore
+++ /dev/null
@@ -1,2 +0,0 @@
-/clock_settime02
-/clock_settime03
diff --git a/testcases/kernel/timers/clock_settime/Makefile b/testcases/kernel/timers/clock_settime/Makefile
deleted file mode 100644
index 8de247075..000000000
--- a/testcases/kernel/timers/clock_settime/Makefile
+++ /dev/null
@@ -1,27 +0,0 @@
-#
-#  Copyright (c) International Business Machines  Corp., 2001
-#
-#  This program is free software;  you can redistribute it and/or modify
-#  it under the terms of the GNU General Public License as published by
-#  the Free Software Foundation; either version 2 of the License, or
-#  (at your option) any later version.
-#
-#  This program is distributed in the hope that it will be useful,
-#  but WITHOUT ANY WARRANTY;  without even the implied warranty of
-#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
-#  the GNU General Public License for more details.
-#
-#  You should have received a copy of the GNU General Public License
-#  along with this program;  if not, write to the Free Software
-#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
-#
-
-top_srcdir		?= ../../../..
-
-include $(top_srcdir)/include/mk/testcases.mk
-
-CPPFLAGS		+= -D_GNU_SOURCE -I$(abs_srcdir)/../include
-
-LDLIBS			+= -lpthread -lrt
-
-include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/timers/clock_settime/clock_settime02.c b/testcases/kernel/timers/clock_settime/clock_settime02.c
deleted file mode 100644
index 65721c1ae..000000000
--- a/testcases/kernel/timers/clock_settime/clock_settime02.c
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
- * Copyright (c) Wipro Technologies Ltd, 2003.  All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- */
-/**************************************************************************
- *
- *    TEST IDENTIFIER	: clock_settime02
- *
- *    EXECUTED BY	: root / superuser
- *
- *    TEST TITLE	: Basic test for clock_settime(2)
- *
- *    TEST CASE TOTAL	: 1
- *
- *    AUTHOR		: Aniruddha Marathe <aniruddha.marathe@wipro.com>
- *
- *    SIGNALS
- * 	Uses SIGUSR1 to pause before test if option set.
- * 	(See the parse_opts(3) man page).
- *
- *    DESCRIPTION
- *     This is a Phase I test for the clock_settime(2) system call.
- *     It is intended to provide a limited exposure of the system call.
- *
- * 	Setup:
- *	  Setup signal handling.
- *	  Pause for SIGUSR1 if option specified.
- *
- * 	Test:
- *	 Loop if the proper options are given.
- *	 Set the parameters of timespec struct
- *	 Execute system call
- *	 Check return code, if system call failed (return=-1)
- *		Log the errno and Issue a FAIL message.
- *	 Otherwise, Issue a PASS message.
- *
- * 	Cleanup:
- * 	  Print errno log and/or timing stats if options given
- *
- * USAGE:  <for command-line>
- * clock_settime02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-p]
- * where:
- * 	-c n : Run n copies simultaneously.
- *	-e   : Turn on errno logging.
- *	-i n : Execute test n times.
- *	-I x : Execute test for x seconds.
- *	-p   : Pause for SIGUSR1 before starting
- *	-P x : Pause for x seconds between iterations.
- *	-t   : Turn on syscall timing.
- *
- *RESTRICTIONS:
- * None
- *****************************************************************************/
-
-#include <stdlib.h>
-#include <errno.h>
-#include <time.h>
-#include <signal.h>
-
-#include "test.h"
-#include "common_timers.h"
-
-static void setup(void);
-static void cleanup(void);
-
-char *TCID = "clock_settime02";
-int TST_TOTAL = 1;
-static struct timespec saved;
-
-int main(int ac, char **av)
-{
-	int lc;
-	struct timespec spec;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		spec.tv_sec = saved.tv_sec + 1;
-		spec.tv_nsec = 0;
-
-		TEST(ltp_syscall(__NR_clock_settime, CLOCK_REALTIME, &spec));
-		tst_resm((TEST_RETURN < 0 ? TFAIL | TTERRNO : TPASS),
-			 "clock_settime %s",
-			 (TEST_RETURN == 0 ? "passed" : "failed"));
-	}
-
-	cleanup();
-	tst_exit();
-}
-
-static void setup(void)
-{
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	tst_require_root();
-
-	/* Save the current time specifications */
-	if (ltp_syscall(__NR_clock_gettime, CLOCK_REALTIME, &saved) < 0)
-		tst_brkm(TBROK, NULL, "Could not save the current time");
-
-	TEST_PAUSE;
-}
-
-static void cleanup(void)
-{
-	/* Set the saved time */
-	if (clock_settime(CLOCK_REALTIME, &saved) < 0) {
-		tst_resm(TWARN, "FATAL COULD NOT RESET THE CLOCK");
-		tst_resm(TFAIL, "Error Setting Time, errno=%d", errno);
-	}
-}
diff --git a/testcases/kernel/timers/clock_settime/clock_settime03.c b/testcases/kernel/timers/clock_settime/clock_settime03.c
deleted file mode 100644
index 38b41d025..000000000
--- a/testcases/kernel/timers/clock_settime/clock_settime03.c
+++ /dev/null
@@ -1,173 +0,0 @@
-/*
- * Copyright (c) Wipro Technologies Ltd, 2003.  All Rights Reserved.
- * Copyright (c) 2011 Cyril Hrubis <chrubis@suse.cz>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- */
-
-#include <errno.h>
-#include <time.h>
-#include <pwd.h>
-#include <unistd.h>
-
-#include "test.h"
-#include "safe_macros.h"
-#include "common_timers.h"
-
-static void setup(void);
-static void cleanup(void);
-static int setup_test(int option);
-
-clockid_t clocks[] = {
-	CLOCK_REALTIME,
-	CLOCK_MONOTONIC,
-	MAX_CLOCKS,
-	MAX_CLOCKS + 1,
-	CLOCK_REALTIME,
-	CLOCK_REALTIME,
-	CLOCK_REALTIME,
-	CLOCK_PROCESS_CPUTIME_ID,
-	CLOCK_THREAD_CPUTIME_ID
-};
-
-int testcases[] = {
-	EFAULT,			/* tp bad               */
-	EINVAL,			/* CLOCK_MONOTONIC      */
-	EINVAL,			/* MAX_CLOCKS           */
-	EINVAL,			/* MAX_CLOCKS + 1       */
-	EINVAL,			/* Invalid timespec     */
-	EINVAL,			/* NSEC_PER_SEC + 1     */
-	EPERM,			/* non-root user        */
-	EINVAL,			/* PROCESS_CPUTIME_ID	*/
-	EINVAL,			/* THREAD_CPUTIME_ID	*/
-};
-
-char *TCID = "clock_settime03";
-int TST_TOTAL = ARRAY_SIZE(testcases);
-
-char nobody_uid[] = "nobody";
-struct passwd *ltpuser;
-static struct timespec spec, *temp, saved;
-
-int main(int ac, char **av)
-{
-	int lc, i;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++) {
-
-			if (setup_test(i) < 0)
-				continue;
-
-			TEST(ltp_syscall(__NR_clock_settime, clocks[i], temp));
-
-			/* Change the UID back to root */
-			if (i == TST_TOTAL - 1) {
-				SAFE_SETEUID(cleanup, 0);
-			}
-
-			/* check return code */
-			if (TEST_RETURN == -1 && TEST_ERRNO == testcases[i]) {
-				tst_resm(TPASS | TTERRNO,
-					 "clock_settime(2) got expected "
-					 "failure.");
-			} else {
-				tst_resm(TFAIL | TTERRNO,
-					 "clock_settime(2) failed to produce "
-					 "expected error (return code = %ld)",
-					 TEST_RETURN);
-				/* Restore the clock to its previous state. */
-				if (TEST_RETURN == 0) {
-					if (ltp_syscall(__NR_clock_settime,
-						    CLOCK_REALTIME,
-						    &saved) < 0) {
-						tst_resm(TWARN | TERRNO,
-							 "FATAL: could not set "
-							 "the clock!");
-					}
-				}
-			}
-
-		}
-
-	}
-
-	cleanup();
-	tst_exit();
-}
-
-static int setup_test(int option)
-{
-	/* valid timespec */
-	spec = saved;
-	temp = &spec;
-
-	/* error sceanrios */
-	switch (option) {
-	case 0:
-		/* Make tp argument bad pointer */
-		temp = (struct timespec *)-1;
-		break;
-	case 4:
-		/* Make the parameter of timespec invalid */
-		spec.tv_nsec = -1;
-		break;
-	case 5:
-		/* Make the parameter of timespec invalid */
-		spec.tv_nsec = NSEC_PER_SEC + 1;
-		break;
-	case 6:
-		/* change the User to non-root */
-		spec.tv_nsec = 0;
-		if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
-			tst_resm(TWARN, "user \"nobody\" not present; "
-				 "skipping test");
-			return -1;
-		}
-		if (seteuid(ltpuser->pw_uid) == -1) {
-			tst_resm(TWARN | TERRNO,
-				 "seteuid failed to set the effective "
-				 "uid to %d (nobody)", ltpuser->pw_uid);
-			return -1;
-		}
-		break;
-	}
-	return 0;
-}
-
-static void setup(void)
-{
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	tst_require_root();
-
-	if (ltp_syscall(__NR_clock_gettime, CLOCK_REALTIME, &saved) < 0)
-		tst_brkm(TBROK, NULL, "Clock gettime failed");
-
-	spec.tv_sec = 1;
-	spec.tv_nsec = 0;
-
-	TEST_PAUSE;
-}
-
-static void cleanup(void)
-{
-}
-- 
2.20.0.rc1


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

* [LTP] [PATCH 1/2] syscalls/clock_settime01.c: create syscall clock_settime test
  2018-12-05 19:30 [LTP] [PATCH 1/2] syscalls/clock_settime01.c: create syscall clock_settime test Rafael David Tinoco
  2018-12-05 19:30 ` [LTP] [PATCH 2/2] timers/clock_settime: remove clock_settime tests Rafael David Tinoco
@ 2018-12-06 13:03 ` Cyril Hrubis
  2018-12-06 14:49   ` Rafael David Tinoco
  2018-12-06 19:07   ` [LTP] [PATCH v2 1/2] syscalls/clock_settime01.c: create syscall clock_settime Rafael David Tinoco
  1 sibling, 2 replies; 37+ messages in thread
From: Cyril Hrubis @ 2018-12-06 13:03 UTC (permalink / raw)
  To: ltp

Hi!
> ---
>  runtest/syscalls                              |   2 +
>  .../kernel/syscalls/clock_settime/.gitignore  |   1 +
>  .../kernel/syscalls/clock_settime/Makefile    |   8 +
>  .../syscalls/clock_settime/clock_settime01.c  | 187 ++++++++++++++++++
>  4 files changed, 198 insertions(+)
>  create mode 100644 testcases/kernel/syscalls/clock_settime/.gitignore
>  create mode 100644 testcases/kernel/syscalls/clock_settime/Makefile
>  create mode 100644 testcases/kernel/syscalls/clock_settime/clock_settime01.c
> 
> diff --git a/runtest/syscalls b/runtest/syscalls
> index ac1d2d2cd..4cbc13209 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -79,6 +79,8 @@ clock_nanosleep01 clock_nanosleep01
>  clock_nanosleep02 clock_nanosleep02
>  clock_nanosleep2_01 clock_nanosleep2_01
>  
> +clock_settime01 clock_settime01
> +
>  clone01 clone01
>  clone02 clone02
>  clone03 clone03
> diff --git a/testcases/kernel/syscalls/clock_settime/.gitignore b/testcases/kernel/syscalls/clock_settime/.gitignore
> new file mode 100644
> index 000000000..fcbb9fecc
> --- /dev/null
> +++ b/testcases/kernel/syscalls/clock_settime/.gitignore
> @@ -0,0 +1 @@
> +clock_settime01
> diff --git a/testcases/kernel/syscalls/clock_settime/Makefile b/testcases/kernel/syscalls/clock_settime/Makefile
> new file mode 100644
> index 000000000..e6674a6b2
> --- /dev/null
> +++ b/testcases/kernel/syscalls/clock_settime/Makefile
> @@ -0,0 +1,8 @@
> +# Copyright (c) 2018 - Linaro Limited. All rights reserved.
> +# SPDX-License-Identifier: GPL-2.0-or-later
> +
> +top_srcdir		?= ../../../..
> +
> +include $(top_srcdir)/include/mk/testcases.mk
> +
> +include $(top_srcdir)/include/mk/generic_leaf_target.mk
> diff --git a/testcases/kernel/syscalls/clock_settime/clock_settime01.c b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
> new file mode 100644
> index 000000000..591e5c723
> --- /dev/null
> +++ b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
> @@ -0,0 +1,187 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2018 Linaro Limited. All rights reserved.
> + * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
> + */
> +
> +/*
> + * Basic tests for clock_settime(2) on different clock types
> + */
> +
> +#include "config.h"
> +#include "tst_test.h"
> +#include "lapi/syscalls.h"
> +
> +#define NSEC_PER_SEC (1000000000L)
> +#define MAX_CLOCKS 16
> +
> +static struct timespec clock_realtime_saved;
> +
> +struct test_case {
> +	clockid_t type;
> +	struct timespec newtime;
> +	int exp_ret;
> +	int exp_err;
> +	int replace;
> +};
> +
> +struct test_case tc[] = {
> +	{				/* case 01: REALTIME		      */
> +	 .type = CLOCK_REALTIME,
> +	 .exp_ret = 0,
> +	 },

What about we separate the only positive case to a separate test, then
we can also do more a few more sanity checks there. I would probably go
for sequence of something as:

* get the realtime time
* increasing it by some value in seconds
* set the realtime time
* get the realtime time
* check that the value is aprox the first value increased by the value
* decrease by the value
* set it again
* check it again

That would cover both cases of setting time forward and backward and at
the end we would end up with the correct time as a bonus.

> +	{				/* case 02: REALTIME: timespec NULL   */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_sec = -2,
> +	 .exp_ret = -1,
> +	 .exp_err = EFAULT,
> +	 .replace = 1,
> +	 },
> +	{				/* case 03: REALTIME: tv_sec = -1     */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_sec = -1,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 .replace = 1,
> +	 },
> +	{				/* case 04: REALTIME: tv_nsec = -1    */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_nsec = -1,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 .replace = 1,
> +	 },
> +	{				/* case 05: REALTIME: tv_nsec = 1s+1  */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_nsec = NSEC_PER_SEC + 1,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 .replace = 1,
> +	 },
> +	{				/* case 06: MONOTONIC		      */
> +	 .type = CLOCK_MONOTONIC,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 07: MAXCLOCK		      */
> +	 .type = MAX_CLOCKS,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 08: MAXCLOCK+1		      */
> +	 .type = MAX_CLOCKS + 1,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 },
> +	/* Linux specific */
> +	{				/* case 09: CLOCK_MONOTONIC_COARSE    */
> +	 .type = CLOCK_MONOTONIC_COARSE,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 10: CLOCK_MONOTONIC_RAW       */
> +	 .type = CLOCK_MONOTONIC_RAW,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 11: CLOCK_BOOTTIME	      */
> +	 .type = CLOCK_BOOTTIME,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 12: CLOCK_PROCESS_CPUTIME_ID  */
> +	 .type = CLOCK_PROCESS_CPUTIME_ID,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 13: CLOCK_THREAD_CPUTIME_ID   */
> +	 .type = CLOCK_THREAD_CPUTIME_ID,
> +	 .exp_ret = -1,
> +	 .exp_err = EINVAL,
> +	 },
> +};
> +
> +static int sys_clock_settime(clockid_t clk_id, struct timespec *tp)
> +{
> +	return tst_syscall(__NR_clock_settime, clk_id, tp);
> +}
> +
> +static int sys_clock_gettime(clockid_t clk_id, struct timespec *tp)
> +{
> +	return tst_syscall(__NR_clock_gettime, clk_id, tp);
> +}

Any reason why we avoid the clock_settime and clock_gettime libc
functions here?

> +static void cleanup(void)
> +{
> +	/* restore realtime clock */
> +
> +	if (sys_clock_settime(CLOCK_REALTIME, &clock_realtime_saved) < 0)
> +		tst_res(TBROK | TTERRNO, "clock_settime(2): could not set "
> +				"current time back");
> +}
> +
> +static void setup(void)
> +{
> +	if (sys_clock_gettime(CLOCK_REALTIME, &clock_realtime_saved) < 0)
> +		tst_res(TBROK | TTERRNO, "clock_gettime(2): could not get "
> +				"current time");
> +}
> +
> +static void verify_clock_settime(unsigned int i)
> +{
> +	struct timespec spec, *specptr;
> +
> +	if (tc[i].replace == 0) {
> +
> +		/* add 1 sec to test clock */
> +
> +		specptr = &spec;
> +		specptr->tv_sec = clock_realtime_saved.tv_sec + 1;
> +		specptr->tv_nsec = clock_realtime_saved.tv_nsec;
> +
> +	} else {
> +
> +		/* bad pointer case */
> +
> +		if (tc[i].newtime.tv_sec == -2)
> +			specptr = tst_get_bad_addr(cleanup);
> +
> +		/* use given values */
> +
> +		else {
> +			specptr = &spec;
> +			specptr->tv_sec = tc[i].newtime.tv_sec;
> +			specptr->tv_nsec = tc[i].newtime.tv_nsec;
> +		}
> +	}
> +
> +	TEST(sys_clock_settime(tc[i].type, specptr));
> +
> +	if (tc[i].exp_ret == TST_RET) {
> +
> +		if (TST_RET >= 0)
> +			tst_res(TPASS, "clock_settime(2): worked as expected");
> +
> +		else {
> +			if (tc[i].exp_err == TST_ERR)
> +				tst_res(TPASS, "clock_settime(2): failed as "
> +						"expected");
> +			else
> +				tst_res(TFAIL | TTERRNO, "clock_settime(2): "
> +						"failed with different error");
> +		}


LKML coding style prefers curly braces around both blocks if they are
around one of them, but that's a minor one.

> +		return;
> +	}
> +
> +	tst_res(TFAIL | TTERRNO, "clock_settime(2): clock type %d failed",
> +			tc[i].type);
> +}
> +
> +static struct tst_test test = {
> +	.setup = setup,
> +	.test = verify_clock_settime,
> +	.cleanup = cleanup,
> +	.tcnt = ARRAY_SIZE(tc),
> +	.needs_root = 1,
> +};
> -- 
> 2.20.0.rc1
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH 1/2] syscalls/clock_settime01.c: create syscall clock_settime test
  2018-12-06 13:03 ` [LTP] [PATCH 1/2] syscalls/clock_settime01.c: create syscall clock_settime test Cyril Hrubis
@ 2018-12-06 14:49   ` Rafael David Tinoco
  2018-12-06 19:07   ` [LTP] [PATCH v2 1/2] syscalls/clock_settime01.c: create syscall clock_settime Rafael David Tinoco
  1 sibling, 0 replies; 37+ messages in thread
From: Rafael David Tinoco @ 2018-12-06 14:49 UTC (permalink / raw)
  To: ltp

On 12/6/18 11:03 AM, Cyril Hrubis wrote:
> Hi!
>> ---
>>  runtest/syscalls                              |   2 +
>>  .../kernel/syscalls/clock_settime/.gitignore  |   1 +
>>  .../kernel/syscalls/clock_settime/Makefile    |   8 +
>>  .../syscalls/clock_settime/clock_settime01.c  | 187 ++++++++++++++++++
>>  4 files changed, 198 insertions(+)
>>  create mode 100644 testcases/kernel/syscalls/clock_settime/.gitignore
>>  create mode 100644 testcases/kernel/syscalls/clock_settime/Makefile
>>  create mode 100644 testcases/kernel/syscalls/clock_settime/clock_settime01.c
>>
>> diff --git a/runtest/syscalls b/runtest/syscalls
>> index ac1d2d2cd..4cbc13209 100644
>> --- a/runtest/syscalls
>> +++ b/runtest/syscalls
>> @@ -79,6 +79,8 @@ clock_nanosleep01 clock_nanosleep01
>>  clock_nanosleep02 clock_nanosleep02
>>  clock_nanosleep2_01 clock_nanosleep2_01
>>  
>> +clock_settime01 clock_settime01
>> +
>>  clone01 clone01
>>  clone02 clone02
>>  clone03 clone03
>> diff --git a/testcases/kernel/syscalls/clock_settime/.gitignore b/testcases/kernel/syscalls/clock_settime/.gitignore
>> new file mode 100644
>> index 000000000..fcbb9fecc
>> --- /dev/null
>> +++ b/testcases/kernel/syscalls/clock_settime/.gitignore
>> @@ -0,0 +1 @@
>> +clock_settime01
>> diff --git a/testcases/kernel/syscalls/clock_settime/Makefile b/testcases/kernel/syscalls/clock_settime/Makefile
>> new file mode 100644
>> index 000000000..e6674a6b2
>> --- /dev/null
>> +++ b/testcases/kernel/syscalls/clock_settime/Makefile
>> @@ -0,0 +1,8 @@
>> +# Copyright (c) 2018 - Linaro Limited. All rights reserved.
>> +# SPDX-License-Identifier: GPL-2.0-or-later
>> +
>> +top_srcdir		?= ../../../..
>> +
>> +include $(top_srcdir)/include/mk/testcases.mk
>> +
>> +include $(top_srcdir)/include/mk/generic_leaf_target.mk
>> diff --git a/testcases/kernel/syscalls/clock_settime/clock_settime01.c b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
>> new file mode 100644
>> index 000000000..591e5c723
>> --- /dev/null
>> +++ b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
>> @@ -0,0 +1,187 @@
>> +// SPDX-License-Identifier: GPL-2.0-or-later
>> +/*
>> + * Copyright (c) 2018 Linaro Limited. All rights reserved.
>> + * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
>> + */
>> +
>> +/*
>> + * Basic tests for clock_settime(2) on different clock types
>> + */
>> +
>> +#include "config.h"
>> +#include "tst_test.h"
>> +#include "lapi/syscalls.h"
>> +
>> +#define NSEC_PER_SEC (1000000000L)
>> +#define MAX_CLOCKS 16
>> +
>> +static struct timespec clock_realtime_saved;
>> +
>> +struct test_case {
>> +	clockid_t type;
>> +	struct timespec newtime;
>> +	int exp_ret;
>> +	int exp_err;
>> +	int replace;
>> +};
>> +
>> +struct test_case tc[] = {
>> +	{				/* case 01: REALTIME		      */
>> +	 .type = CLOCK_REALTIME,
>> +	 .exp_ret = 0,
>> +	 },
> 
> What about we separate the only positive case to a separate test, then
> we can also do more a few more sanity checks there. I would probably go
> for sequence of something as:
> 
> * get the realtime time
> * increasing it by some value in seconds
> * set the realtime time
> * get the realtime time
> * check that the value is aprox the first value increased by the value
> * decrease by the value
> * set it again
> * check it again
> 
> That would cover both cases of setting time forward and backward and at
> the end we would end up with the correct time as a bonus.
> 

Alright. Will do.

>> +	{				/* case 02: REALTIME: timespec NULL   */
>> +	 .type = CLOCK_REALTIME,
>> +	 .newtime.tv_sec = -2,
>> +	 .exp_ret = -1,
>> +	 .exp_err = EFAULT,
>> +	 .replace = 1,
>> +	 },
>> +	{				/* case 03: REALTIME: tv_sec = -1     */
>> +	 .type = CLOCK_REALTIME,
>> +	 .newtime.tv_sec = -1,
>> +	 .exp_ret = -1,
>> +	 .exp_err = EINVAL,
>> +	 .replace = 1,
>> +	 },
>> +	{				/* case 04: REALTIME: tv_nsec = -1    */
>> +	 .type = CLOCK_REALTIME,
>> +	 .newtime.tv_nsec = -1,
>> +	 .exp_ret = -1,
>> +	 .exp_err = EINVAL,
>> +	 .replace = 1,
>> +	 },
>> +	{				/* case 05: REALTIME: tv_nsec = 1s+1  */
>> +	 .type = CLOCK_REALTIME,
>> +	 .newtime.tv_nsec = NSEC_PER_SEC + 1,
>> +	 .exp_ret = -1,
>> +	 .exp_err = EINVAL,
>> +	 .replace = 1,
>> +	 },
>> +	{				/* case 06: MONOTONIC		      */
>> +	 .type = CLOCK_MONOTONIC,
>> +	 .exp_ret = -1,
>> +	 .exp_err = EINVAL,
>> +	 },
>> +	{				/* case 07: MAXCLOCK		      */
>> +	 .type = MAX_CLOCKS,
>> +	 .exp_ret = -1,
>> +	 .exp_err = EINVAL,
>> +	 },
>> +	{				/* case 08: MAXCLOCK+1		      */
>> +	 .type = MAX_CLOCKS + 1,
>> +	 .exp_ret = -1,
>> +	 .exp_err = EINVAL,
>> +	 },
>> +	/* Linux specific */
>> +	{				/* case 09: CLOCK_MONOTONIC_COARSE    */
>> +	 .type = CLOCK_MONOTONIC_COARSE,
>> +	 .exp_ret = -1,
>> +	 .exp_err = EINVAL,
>> +	 },
>> +	{				/* case 10: CLOCK_MONOTONIC_RAW       */
>> +	 .type = CLOCK_MONOTONIC_RAW,
>> +	 .exp_ret = -1,
>> +	 .exp_err = EINVAL,
>> +	 },
>> +	{				/* case 11: CLOCK_BOOTTIME	      */
>> +	 .type = CLOCK_BOOTTIME,
>> +	 .exp_ret = -1,
>> +	 .exp_err = EINVAL,
>> +	 },
>> +	{				/* case 12: CLOCK_PROCESS_CPUTIME_ID  */
>> +	 .type = CLOCK_PROCESS_CPUTIME_ID,
>> +	 .exp_ret = -1,
>> +	 .exp_err = EINVAL,
>> +	 },
>> +	{				/* case 13: CLOCK_THREAD_CPUTIME_ID   */
>> +	 .type = CLOCK_THREAD_CPUTIME_ID,
>> +	 .exp_ret = -1,
>> +	 .exp_err = EINVAL,
>> +	 },
>> +};
>> +
>> +static int sys_clock_settime(clockid_t clk_id, struct timespec *tp)
>> +{
>> +	return tst_syscall(__NR_clock_settime, clk_id, tp);
>> +}
>> +
>> +static int sys_clock_gettime(clockid_t clk_id, struct timespec *tp)
>> +{
>> +	return tst_syscall(__NR_clock_gettime, clk_id, tp);
>> +}
> 
> Any reason why we avoid the clock_settime and clock_gettime libc
> functions here?

Nope. I guess I could use libc one.

> 
>> +static void cleanup(void)
>> +{
>> +	/* restore realtime clock */
>> +
>> +	if (sys_clock_settime(CLOCK_REALTIME, &clock_realtime_saved) < 0)
>> +		tst_res(TBROK | TTERRNO, "clock_settime(2): could not set "
>> +				"current time back");
>> +}
>> +
>> +static void setup(void)
>> +{
>> +	if (sys_clock_gettime(CLOCK_REALTIME, &clock_realtime_saved) < 0)
>> +		tst_res(TBROK | TTERRNO, "clock_gettime(2): could not get "
>> +				"current time");
>> +}
>> +
>> +static void verify_clock_settime(unsigned int i)
>> +{
>> +	struct timespec spec, *specptr;
>> +
>> +	if (tc[i].replace == 0) {
>> +
>> +		/* add 1 sec to test clock */
>> +
>> +		specptr = &spec;
>> +		specptr->tv_sec = clock_realtime_saved.tv_sec + 1;
>> +		specptr->tv_nsec = clock_realtime_saved.tv_nsec;
>> +
>> +	} else {
>> +
>> +		/* bad pointer case */
>> +
>> +		if (tc[i].newtime.tv_sec == -2)
>> +			specptr = tst_get_bad_addr(cleanup);
>> +
>> +		/* use given values */
>> +
>> +		else {
>> +			specptr = &spec;
>> +			specptr->tv_sec = tc[i].newtime.tv_sec;
>> +			specptr->tv_nsec = tc[i].newtime.tv_nsec;
>> +		}
>> +	}
>> +
>> +	TEST(sys_clock_settime(tc[i].type, specptr));
>> +
>> +	if (tc[i].exp_ret == TST_RET) {
>> +
>> +		if (TST_RET >= 0)
>> +			tst_res(TPASS, "clock_settime(2): worked as expected");
>> +
>> +		else {
>> +			if (tc[i].exp_err == TST_ERR)
>> +				tst_res(TPASS, "clock_settime(2): failed as "
>> +						"expected");
>> +			else
>> +				tst_res(TFAIL | TTERRNO, "clock_settime(2): "
>> +						"failed with different error");
>> +		}
> 
> 
> LKML coding style prefers curly braces around both blocks if they are
> around one of them, but that's a minor one.

Will fix.

> 
>> +		return;
>> +	}
>> +
>> +	tst_res(TFAIL | TTERRNO, "clock_settime(2): clock type %d failed",
>> +			tc[i].type);
>> +}
>> +
>> +static struct tst_test test = {
>> +	.setup = setup,
>> +	.test = verify_clock_settime,
>> +	.cleanup = cleanup,
>> +	.tcnt = ARRAY_SIZE(tc),
>> +	.needs_root = 1,
>> +};
>> -- 
>> 2.20.0.rc1
>>
>>
>> -- 
>> Mailing list info: https://lists.linux.it/listinfo/ltp
> 

Thanks for reviewing it.

-- 
Rafael D. Tinoco
Linaro - Kernel Validation

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

* [LTP] [PATCH v2 1/2] syscalls/clock_settime01.c: create syscall clock_settime
  2018-12-06 13:03 ` [LTP] [PATCH 1/2] syscalls/clock_settime01.c: create syscall clock_settime test Cyril Hrubis
  2018-12-06 14:49   ` Rafael David Tinoco
@ 2018-12-06 19:07   ` Rafael David Tinoco
  2018-12-06 19:07     ` [LTP] [PATCH v2 2/2] timers/clock_settime: remove clock_settime tests Rafael David Tinoco
                       ` (2 more replies)
  1 sibling, 3 replies; 37+ messages in thread
From: Rafael David Tinoco @ 2018-12-06 19:07 UTC (permalink / raw)
  To: ltp

Fixes: 343

clock_settime01 creates a new test, using new API, based on existing and
older kernel/timers/clock_settime tests. It includes tests from files
clock_settime02 and clock_settime03, which will be deleted in next
commits.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 runtest/syscalls                              |   3 +
 .../kernel/syscalls/clock_settime/.gitignore  |   2 +
 .../kernel/syscalls/clock_settime/Makefile    |   8 +
 .../syscalls/clock_settime/clock_settime01.c  | 122 +++++++++++++
 .../syscalls/clock_settime/clock_settime02.c  | 171 ++++++++++++++++++
 5 files changed, 306 insertions(+)
 create mode 100644 testcases/kernel/syscalls/clock_settime/.gitignore
 create mode 100644 testcases/kernel/syscalls/clock_settime/Makefile
 create mode 100644 testcases/kernel/syscalls/clock_settime/clock_settime01.c
 create mode 100644 testcases/kernel/syscalls/clock_settime/clock_settime02.c

diff --git a/runtest/syscalls b/runtest/syscalls
index ac1d2d2cd..2e38ab37e 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -79,6 +79,9 @@ clock_nanosleep01 clock_nanosleep01
 clock_nanosleep02 clock_nanosleep02
 clock_nanosleep2_01 clock_nanosleep2_01
 
+clock_settime01 clock_settime01
+clock_settime02 clock_settime02
+
 clone01 clone01
 clone02 clone02
 clone03 clone03
diff --git a/testcases/kernel/syscalls/clock_settime/.gitignore b/testcases/kernel/syscalls/clock_settime/.gitignore
new file mode 100644
index 000000000..281217550
--- /dev/null
+++ b/testcases/kernel/syscalls/clock_settime/.gitignore
@@ -0,0 +1,2 @@
+clock_settime01
+clock_settime02
diff --git a/testcases/kernel/syscalls/clock_settime/Makefile b/testcases/kernel/syscalls/clock_settime/Makefile
new file mode 100644
index 000000000..e6674a6b2
--- /dev/null
+++ b/testcases/kernel/syscalls/clock_settime/Makefile
@@ -0,0 +1,8 @@
+# Copyright (c) 2018 - Linaro Limited. All rights reserved.
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/clock_settime/clock_settime01.c b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
new file mode 100644
index 000000000..97d720fa2
--- /dev/null
+++ b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
@@ -0,0 +1,122 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+/*
+ * Basic test for clock_settime(2) on REALTIME clock:
+ *
+ *      1) advance DELTA_SEC seconds
+ *      2) go backwards DELTA_SEC seconds
+ *
+ * Accept DELTA_PER deviation on both (specially going backwards).
+ */
+
+#include "config.h"
+#include "tst_test.h"
+#include "lapi/syscalls.h"
+
+#define DELTA_SEC 10	/* 10 seconds delta    */
+#define DELTA_PER 0.1	/* 1 percent deviation */
+
+static struct timespec real_begin, mono_begin, mono_end;
+
+static void clock_elapsed(struct timespec *begin, struct timespec *end,
+		struct timespec *elapsed)
+{
+	elapsed->tv_sec = end->tv_sec - begin->tv_sec;
+	elapsed->tv_nsec = end->tv_nsec - begin->tv_nsec;
+}
+
+static void clock_return(void)
+{
+	static struct timespec elapsed, adjust;
+
+	clock_elapsed(&mono_begin, &mono_end, &elapsed);
+
+	adjust.tv_sec = real_begin.tv_sec + elapsed.tv_sec;
+	adjust.tv_nsec = real_begin.tv_nsec + elapsed.tv_nsec;
+
+	if (clock_settime(CLOCK_REALTIME, &adjust) != 0)
+		tst_res(TBROK | TTERRNO, "could restore realtime clock");
+}
+
+static void clock_fixnow(void)
+{
+	if (clock_gettime(CLOCK_MONOTONIC_RAW, &mono_end) != 0)
+		tst_res(TBROK | TTERRNO, "could not get elapsed time");
+
+	clock_return();
+}
+
+static void setup(void)
+{
+	/* save initial monotonic time to restore it when needed */
+
+	if (clock_gettime(CLOCK_REALTIME, &real_begin) != 0)
+		tst_res(TBROK | TTERRNO, "could not get initial real time");
+
+	if (clock_gettime(CLOCK_MONOTONIC_RAW, &mono_begin) != 0)
+		tst_res(TBROK | TTERRNO, "couldn't get initial monotonic time");
+}
+
+static void cleanup(void)
+{
+	clock_fixnow();
+}
+
+static void verify_clock_settime(void)
+{
+	static struct timespec begin, change, end, elapsed;
+
+	/* test 01: move forward */
+
+	if (clock_gettime(CLOCK_REALTIME, &begin) != 0)
+		tst_res(TBROK | TTERRNO, "could not get realtime at the begin");
+
+	change.tv_sec = begin.tv_sec + DELTA_SEC;
+
+	if (clock_settime(CLOCK_REALTIME, &change) != 0)
+		tst_res(TBROK | TTERRNO, "could not set realtime change");
+
+	if (clock_gettime(CLOCK_REALTIME, &end) != 0)
+		tst_res(TBROK | TTERRNO, "could not get realtime after change");
+
+	clock_elapsed(&begin, &end, &elapsed);
+
+	if (elapsed.tv_sec < (float) (DELTA_SEC - (DELTA_SEC * DELTA_PER)))
+		tst_res(TFAIL, "clock_settime(2): could not advance time");
+	else
+		tst_res(TPASS, "clock_settime(2): was able to advance time");
+
+	/* test 02: move backward */
+
+	if (clock_gettime(CLOCK_REALTIME, &begin) != 0)
+		tst_res(TBROK | TTERRNO, "could not get realtime@the begin");
+
+	change.tv_sec = begin.tv_sec - DELTA_SEC;
+
+	if (clock_settime(CLOCK_REALTIME, &change) != 0)
+		tst_res(TBROK | TTERRNO, "could not set realtime change");
+
+	if (clock_gettime(CLOCK_REALTIME, &end) != 0)
+		tst_res(TBROK | TTERRNO, "could not get realtime after change");
+
+	clock_elapsed(&begin, &end, &elapsed);
+
+	elapsed.tv_sec = ~elapsed.tv_sec;
+	elapsed.tv_nsec = ~elapsed.tv_nsec;
+
+	if (elapsed.tv_sec < (float) (DELTA_SEC - (DELTA_SEC * DELTA_PER)))
+		tst_res(TFAIL, "clock_settime(2): could not recede time");
+	else
+		tst_res(TPASS, "clock_settime(2): was able to recede time");
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = verify_clock_settime,
+	.cleanup = cleanup,
+	.needs_root = 1,
+};
diff --git a/testcases/kernel/syscalls/clock_settime/clock_settime02.c b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
new file mode 100644
index 000000000..710f37219
--- /dev/null
+++ b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
@@ -0,0 +1,171 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+/*
+ * Basic tests for errors of clock_settime(2) on different clock types.
+ */
+
+#include "config.h"
+#include "tst_test.h"
+#include "lapi/syscalls.h"
+
+#define DELTA_SEC 10
+#define NSEC_PER_SEC (1000000000L)
+#define MAX_CLOCKS 16
+
+static struct timespec clock_realtime_saved;
+
+struct test_case {
+	clockid_t type;
+	struct timespec newtime;
+	int exp_err;
+	int replace;
+};
+
+struct test_case tc[] = {
+	{				/* case 01: REALTIME: timespec NULL   */
+	 .type = CLOCK_REALTIME,
+	 .newtime.tv_sec = -2,
+	 .exp_err = EFAULT,
+	 .replace = 1,
+	 },
+	{				/* case 02: REALTIME: tv_sec = -1     */
+	 .type = CLOCK_REALTIME,
+	 .newtime.tv_sec = -1,
+	 .exp_err = EINVAL,
+	 .replace = 1,
+	 },
+	{				/* case 03: REALTIME: tv_nsec = -1    */
+	 .type = CLOCK_REALTIME,
+	 .newtime.tv_nsec = -1,
+	 .exp_err = EINVAL,
+	 .replace = 1,
+	 },
+	{				/* case 04: REALTIME: tv_nsec = 1s+1  */
+	 .type = CLOCK_REALTIME,
+	 .newtime.tv_nsec = NSEC_PER_SEC + 1,
+	 .exp_err = EINVAL,
+	 .replace = 1,
+	 },
+	{				/* case 05: MONOTONIC		      */
+	 .type = CLOCK_MONOTONIC,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 06: MAXCLOCK		      */
+	 .type = MAX_CLOCKS,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 07: MAXCLOCK+1		      */
+	 .type = MAX_CLOCKS + 1,
+	 .exp_err = EINVAL,
+	 },
+	/* Linux specific */
+	{				/* case 08: CLOCK_MONOTONIC_COARSE    */
+	 .type = CLOCK_MONOTONIC_COARSE,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 09: CLOCK_MONOTONIC_RAW       */
+	 .type = CLOCK_MONOTONIC_RAW,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 10: CLOCK_BOOTTIME	      */
+	 .type = CLOCK_BOOTTIME,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 11: CLOCK_PROCESS_CPUTIME_ID  */
+	 .type = CLOCK_PROCESS_CPUTIME_ID,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 12: CLOCK_THREAD_CPUTIME_ID   */
+	 .type = CLOCK_THREAD_CPUTIME_ID,
+	 .exp_err = EINVAL,
+	 },
+};
+
+/*
+ * Some tests may cause libc to segfault when passing bad arguments.
+ */
+static int sys_clock_settime(clockid_t clk_id, struct timespec *tp)
+{
+	return tst_syscall(__NR_clock_settime, clk_id, tp);
+}
+
+static int sys_clock_gettime(clockid_t clk_id, struct timespec *tp)
+{
+	return tst_syscall(__NR_clock_gettime, clk_id, tp);
+}
+
+static void cleanup(void)
+{
+	/* restore realtime clock */
+
+	if (sys_clock_settime(CLOCK_REALTIME, &clock_realtime_saved) < 0)
+		tst_res(TBROK | TTERRNO, "clock_settime(2): could not set "
+				"current time back");
+}
+
+static void setup(void)
+{
+	if (sys_clock_gettime(CLOCK_REALTIME, &clock_realtime_saved) < 0)
+		tst_res(TBROK | TTERRNO, "clock_gettime(2): could not get "
+				"current time");
+}
+
+static void verify_clock_settime(unsigned int i)
+{
+	struct timespec spec, *specptr;
+
+	if (tc[i].replace == 0) {
+
+		/* add 1 sec to test clock */
+
+		specptr = &spec;
+		specptr->tv_sec = clock_realtime_saved.tv_sec + 1;
+		specptr->tv_nsec = clock_realtime_saved.tv_nsec;
+
+	} else {
+
+		/* bad pointer case */
+
+		if (tc[i].newtime.tv_sec == -2)
+			specptr = tst_get_bad_addr(cleanup);
+
+		/* use given values */
+
+		else {
+			specptr = &spec;
+			specptr->tv_sec = tc[i].newtime.tv_sec;
+			specptr->tv_nsec = tc[i].newtime.tv_nsec;
+		}
+	}
+
+	TEST(sys_clock_settime(tc[i].type, specptr));
+
+	if (TST_RET == -1) {
+
+		if (tc[i].exp_err == TST_ERR) {
+
+			tst_res(TPASS, "clock_settime(2): failed as expected");
+
+		} else {
+			tst_res(TFAIL | TTERRNO, "clock_settime(2): "
+					"failed with different error");
+		}
+
+		return;
+	}
+
+	tst_res(TFAIL | TTERRNO, "clock_settime(2): clock type %d failed",
+			tc[i].type);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test = verify_clock_settime,
+	.cleanup = cleanup,
+	.tcnt = ARRAY_SIZE(tc),
+	.needs_root = 1,
+};
-- 
2.20.0.rc1


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

* [LTP] [PATCH v2 2/2] timers/clock_settime: remove clock_settime tests
  2018-12-06 19:07   ` [LTP] [PATCH v2 1/2] syscalls/clock_settime01.c: create syscall clock_settime Rafael David Tinoco
@ 2018-12-06 19:07     ` Rafael David Tinoco
  2018-12-06 19:11     ` [LTP] [PATCH v2 1/2] syscalls/clock_settime01.c: create syscall clock_settime Rafael David Tinoco
  2018-12-11 14:27     ` Cyril Hrubis
  2 siblings, 0 replies; 37+ messages in thread
From: Rafael David Tinoco @ 2018-12-06 19:07 UTC (permalink / raw)
  To: ltp

Fixes: 343

clock_settime01 creates a new test, using new API, based on existing and
older kernel/timers/clock_settime tests. It includes tests from files
clock_settime02 and clock_settime03.

This commit deletes timers/clock_settime/* tests.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 runtest/timers                                |   2 -
 .../kernel/timers/clock_settime/.gitignore    |   2 -
 .../kernel/timers/clock_settime/Makefile      |  27 ---
 .../timers/clock_settime/clock_settime02.c    | 128 -------------
 .../timers/clock_settime/clock_settime03.c    | 173 ------------------
 5 files changed, 332 deletions(-)
 delete mode 100644 testcases/kernel/timers/clock_settime/.gitignore
 delete mode 100644 testcases/kernel/timers/clock_settime/Makefile
 delete mode 100644 testcases/kernel/timers/clock_settime/clock_settime02.c
 delete mode 100644 testcases/kernel/timers/clock_settime/clock_settime03.c

diff --git a/runtest/timers b/runtest/timers
index a58ac57fc..618d2cb0c 100644
--- a/runtest/timers
+++ b/runtest/timers
@@ -1,8 +1,6 @@
 #DESCRIPTION:Posix Timer Tests
 clock_gettime02 clock_gettime02
 clock_gettime03 clock_gettime03
-clock_settime02 clock_settime02
-clock_settime03 clock_settime03
 timer_create02 timer_create02
 timer_create03 timer_create03
 timer_create04 timer_create04
diff --git a/testcases/kernel/timers/clock_settime/.gitignore b/testcases/kernel/timers/clock_settime/.gitignore
deleted file mode 100644
index 957c5ac26..000000000
--- a/testcases/kernel/timers/clock_settime/.gitignore
+++ /dev/null
@@ -1,2 +0,0 @@
-/clock_settime02
-/clock_settime03
diff --git a/testcases/kernel/timers/clock_settime/Makefile b/testcases/kernel/timers/clock_settime/Makefile
deleted file mode 100644
index 8de247075..000000000
--- a/testcases/kernel/timers/clock_settime/Makefile
+++ /dev/null
@@ -1,27 +0,0 @@
-#
-#  Copyright (c) International Business Machines  Corp., 2001
-#
-#  This program is free software;  you can redistribute it and/or modify
-#  it under the terms of the GNU General Public License as published by
-#  the Free Software Foundation; either version 2 of the License, or
-#  (at your option) any later version.
-#
-#  This program is distributed in the hope that it will be useful,
-#  but WITHOUT ANY WARRANTY;  without even the implied warranty of
-#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
-#  the GNU General Public License for more details.
-#
-#  You should have received a copy of the GNU General Public License
-#  along with this program;  if not, write to the Free Software
-#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
-#
-
-top_srcdir		?= ../../../..
-
-include $(top_srcdir)/include/mk/testcases.mk
-
-CPPFLAGS		+= -D_GNU_SOURCE -I$(abs_srcdir)/../include
-
-LDLIBS			+= -lpthread -lrt
-
-include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/timers/clock_settime/clock_settime02.c b/testcases/kernel/timers/clock_settime/clock_settime02.c
deleted file mode 100644
index 65721c1ae..000000000
--- a/testcases/kernel/timers/clock_settime/clock_settime02.c
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
- * Copyright (c) Wipro Technologies Ltd, 2003.  All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- */
-/**************************************************************************
- *
- *    TEST IDENTIFIER	: clock_settime02
- *
- *    EXECUTED BY	: root / superuser
- *
- *    TEST TITLE	: Basic test for clock_settime(2)
- *
- *    TEST CASE TOTAL	: 1
- *
- *    AUTHOR		: Aniruddha Marathe <aniruddha.marathe@wipro.com>
- *
- *    SIGNALS
- * 	Uses SIGUSR1 to pause before test if option set.
- * 	(See the parse_opts(3) man page).
- *
- *    DESCRIPTION
- *     This is a Phase I test for the clock_settime(2) system call.
- *     It is intended to provide a limited exposure of the system call.
- *
- * 	Setup:
- *	  Setup signal handling.
- *	  Pause for SIGUSR1 if option specified.
- *
- * 	Test:
- *	 Loop if the proper options are given.
- *	 Set the parameters of timespec struct
- *	 Execute system call
- *	 Check return code, if system call failed (return=-1)
- *		Log the errno and Issue a FAIL message.
- *	 Otherwise, Issue a PASS message.
- *
- * 	Cleanup:
- * 	  Print errno log and/or timing stats if options given
- *
- * USAGE:  <for command-line>
- * clock_settime02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-p]
- * where:
- * 	-c n : Run n copies simultaneously.
- *	-e   : Turn on errno logging.
- *	-i n : Execute test n times.
- *	-I x : Execute test for x seconds.
- *	-p   : Pause for SIGUSR1 before starting
- *	-P x : Pause for x seconds between iterations.
- *	-t   : Turn on syscall timing.
- *
- *RESTRICTIONS:
- * None
- *****************************************************************************/
-
-#include <stdlib.h>
-#include <errno.h>
-#include <time.h>
-#include <signal.h>
-
-#include "test.h"
-#include "common_timers.h"
-
-static void setup(void);
-static void cleanup(void);
-
-char *TCID = "clock_settime02";
-int TST_TOTAL = 1;
-static struct timespec saved;
-
-int main(int ac, char **av)
-{
-	int lc;
-	struct timespec spec;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		spec.tv_sec = saved.tv_sec + 1;
-		spec.tv_nsec = 0;
-
-		TEST(ltp_syscall(__NR_clock_settime, CLOCK_REALTIME, &spec));
-		tst_resm((TEST_RETURN < 0 ? TFAIL | TTERRNO : TPASS),
-			 "clock_settime %s",
-			 (TEST_RETURN == 0 ? "passed" : "failed"));
-	}
-
-	cleanup();
-	tst_exit();
-}
-
-static void setup(void)
-{
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	tst_require_root();
-
-	/* Save the current time specifications */
-	if (ltp_syscall(__NR_clock_gettime, CLOCK_REALTIME, &saved) < 0)
-		tst_brkm(TBROK, NULL, "Could not save the current time");
-
-	TEST_PAUSE;
-}
-
-static void cleanup(void)
-{
-	/* Set the saved time */
-	if (clock_settime(CLOCK_REALTIME, &saved) < 0) {
-		tst_resm(TWARN, "FATAL COULD NOT RESET THE CLOCK");
-		tst_resm(TFAIL, "Error Setting Time, errno=%d", errno);
-	}
-}
diff --git a/testcases/kernel/timers/clock_settime/clock_settime03.c b/testcases/kernel/timers/clock_settime/clock_settime03.c
deleted file mode 100644
index 38b41d025..000000000
--- a/testcases/kernel/timers/clock_settime/clock_settime03.c
+++ /dev/null
@@ -1,173 +0,0 @@
-/*
- * Copyright (c) Wipro Technologies Ltd, 2003.  All Rights Reserved.
- * Copyright (c) 2011 Cyril Hrubis <chrubis@suse.cz>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- */
-
-#include <errno.h>
-#include <time.h>
-#include <pwd.h>
-#include <unistd.h>
-
-#include "test.h"
-#include "safe_macros.h"
-#include "common_timers.h"
-
-static void setup(void);
-static void cleanup(void);
-static int setup_test(int option);
-
-clockid_t clocks[] = {
-	CLOCK_REALTIME,
-	CLOCK_MONOTONIC,
-	MAX_CLOCKS,
-	MAX_CLOCKS + 1,
-	CLOCK_REALTIME,
-	CLOCK_REALTIME,
-	CLOCK_REALTIME,
-	CLOCK_PROCESS_CPUTIME_ID,
-	CLOCK_THREAD_CPUTIME_ID
-};
-
-int testcases[] = {
-	EFAULT,			/* tp bad               */
-	EINVAL,			/* CLOCK_MONOTONIC      */
-	EINVAL,			/* MAX_CLOCKS           */
-	EINVAL,			/* MAX_CLOCKS + 1       */
-	EINVAL,			/* Invalid timespec     */
-	EINVAL,			/* NSEC_PER_SEC + 1     */
-	EPERM,			/* non-root user        */
-	EINVAL,			/* PROCESS_CPUTIME_ID	*/
-	EINVAL,			/* THREAD_CPUTIME_ID	*/
-};
-
-char *TCID = "clock_settime03";
-int TST_TOTAL = ARRAY_SIZE(testcases);
-
-char nobody_uid[] = "nobody";
-struct passwd *ltpuser;
-static struct timespec spec, *temp, saved;
-
-int main(int ac, char **av)
-{
-	int lc, i;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++) {
-
-			if (setup_test(i) < 0)
-				continue;
-
-			TEST(ltp_syscall(__NR_clock_settime, clocks[i], temp));
-
-			/* Change the UID back to root */
-			if (i == TST_TOTAL - 1) {
-				SAFE_SETEUID(cleanup, 0);
-			}
-
-			/* check return code */
-			if (TEST_RETURN == -1 && TEST_ERRNO == testcases[i]) {
-				tst_resm(TPASS | TTERRNO,
-					 "clock_settime(2) got expected "
-					 "failure.");
-			} else {
-				tst_resm(TFAIL | TTERRNO,
-					 "clock_settime(2) failed to produce "
-					 "expected error (return code = %ld)",
-					 TEST_RETURN);
-				/* Restore the clock to its previous state. */
-				if (TEST_RETURN == 0) {
-					if (ltp_syscall(__NR_clock_settime,
-						    CLOCK_REALTIME,
-						    &saved) < 0) {
-						tst_resm(TWARN | TERRNO,
-							 "FATAL: could not set "
-							 "the clock!");
-					}
-				}
-			}
-
-		}
-
-	}
-
-	cleanup();
-	tst_exit();
-}
-
-static int setup_test(int option)
-{
-	/* valid timespec */
-	spec = saved;
-	temp = &spec;
-
-	/* error sceanrios */
-	switch (option) {
-	case 0:
-		/* Make tp argument bad pointer */
-		temp = (struct timespec *)-1;
-		break;
-	case 4:
-		/* Make the parameter of timespec invalid */
-		spec.tv_nsec = -1;
-		break;
-	case 5:
-		/* Make the parameter of timespec invalid */
-		spec.tv_nsec = NSEC_PER_SEC + 1;
-		break;
-	case 6:
-		/* change the User to non-root */
-		spec.tv_nsec = 0;
-		if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
-			tst_resm(TWARN, "user \"nobody\" not present; "
-				 "skipping test");
-			return -1;
-		}
-		if (seteuid(ltpuser->pw_uid) == -1) {
-			tst_resm(TWARN | TERRNO,
-				 "seteuid failed to set the effective "
-				 "uid to %d (nobody)", ltpuser->pw_uid);
-			return -1;
-		}
-		break;
-	}
-	return 0;
-}
-
-static void setup(void)
-{
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	tst_require_root();
-
-	if (ltp_syscall(__NR_clock_gettime, CLOCK_REALTIME, &saved) < 0)
-		tst_brkm(TBROK, NULL, "Clock gettime failed");
-
-	spec.tv_sec = 1;
-	spec.tv_nsec = 0;
-
-	TEST_PAUSE;
-}
-
-static void cleanup(void)
-{
-}
-- 
2.20.0.rc1


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

* [LTP] [PATCH v2 1/2] syscalls/clock_settime01.c: create syscall clock_settime
  2018-12-06 19:07   ` [LTP] [PATCH v2 1/2] syscalls/clock_settime01.c: create syscall clock_settime Rafael David Tinoco
  2018-12-06 19:07     ` [LTP] [PATCH v2 2/2] timers/clock_settime: remove clock_settime tests Rafael David Tinoco
@ 2018-12-06 19:11     ` Rafael David Tinoco
  2018-12-11 14:27     ` Cyril Hrubis
  2 siblings, 0 replies; 37+ messages in thread
From: Rafael David Tinoco @ 2018-12-06 19:11 UTC (permalink / raw)
  To: ltp

On 12/6/18 5:07 PM, Rafael David Tinoco wrote:
> Fixes: 343
> 
> clock_settime01 creates a new test, using new API, based on existing and
> older kernel/timers/clock_settime tests. It includes tests from files
> clock_settime02 and clock_settime03, which will be deleted in next
> commits.
> 
> Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
> ---

Cyril,

Please if you accept this patch, amend the description to:

"""
clock_settime01 creates a new test, using new API, based on existing
and older kernel/timers/clock_settime02 test. clock_settime02 creates
another test based on older kernel/timers/clock_settime03 test. Both
will be deleted in the next commits.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
"""

I forgot to change description before sending v2. Sorry.

About the first test, best way to check good results is:

$ date ; time sudo $(find . -name clock_settime01) -I 10 2>&1 >
/dev/null 2>&1 ; date

Thu Dec  6 17:09:24 -02 2018

real	0m10.032s
user	0m2.710s
sys	0m11.911s

Thu Dec  6 17:09:34 -02 2018

Using CLOCK_MONOTONIC_RAW we can make sure to restore a good time to
CLOCK_REALTIME after the test has run (independently of how many seconds
it took or iterations it had).

-- 
Rafael D. Tinoco
Linaro - Kernel Validation

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

* [LTP] [PATCH v2 1/2] syscalls/clock_settime01.c: create syscall clock_settime
  2018-12-06 19:07   ` [LTP] [PATCH v2 1/2] syscalls/clock_settime01.c: create syscall clock_settime Rafael David Tinoco
  2018-12-06 19:07     ` [LTP] [PATCH v2 2/2] timers/clock_settime: remove clock_settime tests Rafael David Tinoco
  2018-12-06 19:11     ` [LTP] [PATCH v2 1/2] syscalls/clock_settime01.c: create syscall clock_settime Rafael David Tinoco
@ 2018-12-11 14:27     ` Cyril Hrubis
  2018-12-11 16:05       ` Rafael David Tinoco
  2018-12-12 20:37       ` [LTP] [PATCH v3 1/6] tst_timer.h: add tst_timespect_rem_us() function Rafael David Tinoco
  2 siblings, 2 replies; 37+ messages in thread
From: Cyril Hrubis @ 2018-12-11 14:27 UTC (permalink / raw)
  To: ltp

Hi!
> new file mode 100644
> index 000000000..97d720fa2
> --- /dev/null
> +++ b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
> @@ -0,0 +1,122 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2018 Linaro Limited. All rights reserved.
> + * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
> + */
> +
> +/*
> + * Basic test for clock_settime(2) on REALTIME clock:
> + *
> + *      1) advance DELTA_SEC seconds
> + *      2) go backwards DELTA_SEC seconds
> + *
> + * Accept DELTA_PER deviation on both (specially going backwards).
> + */
> +
> +#include "config.h"
> +#include "tst_test.h"
> +#include "lapi/syscalls.h"
> +
> +#define DELTA_SEC 10	/* 10 seconds delta    */
> +#define DELTA_PER 0.1	/* 1 percent deviation */
> +
> +static struct timespec real_begin, mono_begin, mono_end;
> +
> +static void clock_elapsed(struct timespec *begin, struct timespec *end,
> +		struct timespec *elapsed)
> +{
> +	elapsed->tv_sec = end->tv_sec - begin->tv_sec;
> +	elapsed->tv_nsec = end->tv_nsec - begin->tv_nsec;

If you do this the elapsed may end up in non-normalized state, i.e.
elapsed->tv_nsec may end up negative.

We do have all kinds of inline functions for conversion and arimetic in
tst_timer.h so there is no point of rolling your own here.

> +}
> +
> +static void clock_return(void)
                      ^
		      I would have named this restore, but that is very
		      minor.

> +{
> +	static struct timespec elapsed, adjust;
> +
> +	clock_elapsed(&mono_begin, &mono_end, &elapsed);
> +
> +	adjust.tv_sec = real_begin.tv_sec + elapsed.tv_sec;
> +	adjust.tv_nsec = real_begin.tv_nsec + elapsed.tv_nsec;

We should normalize the addition here as well, i.e. carry over to
seconds if the number of nanoseconds gets greater than 1s.

Ideally we should add a function to add two timespec structures into the
tst_timer.h header (in a separate patch).

> +	if (clock_settime(CLOCK_REALTIME, &adjust) != 0)
> +		tst_res(TBROK | TTERRNO, "could restore realtime clock");
> +}
> +
> +static void clock_fixnow(void)
> +{
> +	if (clock_gettime(CLOCK_MONOTONIC_RAW, &mono_end) != 0)
> +		tst_res(TBROK | TTERRNO, "could not get elapsed time");

We should make sure here that both real_begin and mono_begin were set
before we attempt to restore the system time. A restore_time flag se to
1 after we successfully read both will do.

> +	clock_return();
> +}
> +
> +static void setup(void)
> +{
> +	/* save initial monotonic time to restore it when needed */
> +
> +	if (clock_gettime(CLOCK_REALTIME, &real_begin) != 0)
> +		tst_res(TBROK | TTERRNO, "could not get initial real time");
> +
> +	if (clock_gettime(CLOCK_MONOTONIC_RAW, &mono_begin) != 0)
> +		tst_res(TBROK | TTERRNO, "couldn't get initial monotonic time");
                  ^
		  This should have been tst_brk() doing tst_res() with
		  TBROK does not make much sense.

Also we do have tst_safe_clocks.h, if you include that you can use
SAFE_CLOCK_GETTIME() instead.

Overall the idea of restoring wall clock using monotonic timer is a good
one, maybe we should even move this code to a library so that all tests
that change wall clock would need just set restore_wallclock flag in the
tst_test structure...

> +}
> +
> +static void cleanup(void)
> +{
> +	clock_fixnow();
> +}
> +
> +static void verify_clock_settime(void)
> +{
> +	static struct timespec begin, change, end, elapsed;
> +
> +	/* test 01: move forward */
> +
> +	if (clock_gettime(CLOCK_REALTIME, &begin) != 0)
> +		tst_res(TBROK | TTERRNO, "could not get realtime at the begin");
> +
> +	change.tv_sec = begin.tv_sec + DELTA_SEC;
> +
> +	if (clock_settime(CLOCK_REALTIME, &change) != 0)
> +		tst_res(TBROK | TTERRNO, "could not set realtime change");
> +
> +	if (clock_gettime(CLOCK_REALTIME, &end) != 0)
> +		tst_res(TBROK | TTERRNO, "could not get realtime after change");

Here as well.

> +	clock_elapsed(&begin, &end, &elapsed);
> +
> +	if (elapsed.tv_sec < (float) (DELTA_SEC - (DELTA_SEC * DELTA_PER)))

It would be much easier to just use tst_timespec_diff_ms() and check
that the result is in reasonable range. I.e. check lower bound as well.

> +		tst_res(TFAIL, "clock_settime(2): could not advance time");
> +	else
> +		tst_res(TPASS, "clock_settime(2): was able to advance time");
> +
> +	/* test 02: move backward */
> +
> +	if (clock_gettime(CLOCK_REALTIME, &begin) != 0)
> +		tst_res(TBROK | TTERRNO, "could not get realtime at the begin");
> +
> +	change.tv_sec = begin.tv_sec - DELTA_SEC;
> +
> +	if (clock_settime(CLOCK_REALTIME, &change) != 0)
> +		tst_res(TBROK | TTERRNO, "could not set realtime change");
> +
> +	if (clock_gettime(CLOCK_REALTIME, &end) != 0)
> +		tst_res(TBROK | TTERRNO, "could not get realtime after change");
> +
> +	clock_elapsed(&begin, &end, &elapsed);
> +
> +	elapsed.tv_sec = ~elapsed.tv_sec;
> +	elapsed.tv_nsec = ~elapsed.tv_nsec;
> +
> +	if (elapsed.tv_sec < (float) (DELTA_SEC - (DELTA_SEC * DELTA_PER)))
> +		tst_res(TFAIL, "clock_settime(2): could not recede time");
> +	else
> +		tst_res(TPASS, "clock_settime(2): was able to recede time");

Here as well.

> +}
> +
> +static struct tst_test test = {
> +	.setup = setup,
> +	.test_all = verify_clock_settime,
> +	.cleanup = cleanup,
> +	.needs_root = 1,
> +};
>
> diff --git a/testcases/kernel/syscalls/clock_settime/clock_settime02.c b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
> new file mode 100644
> index 000000000..710f37219
> --- /dev/null
> +++ b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
> @@ -0,0 +1,171 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2018 Linaro Limited. All rights reserved.
> + * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
> + */
> +
> +/*
> + * Basic tests for errors of clock_settime(2) on different clock types.
> + */
> +
> +#include "config.h"
> +#include "tst_test.h"
> +#include "lapi/syscalls.h"
> +
> +#define DELTA_SEC 10
> +#define NSEC_PER_SEC (1000000000L)
> +#define MAX_CLOCKS 16
> +
> +static struct timespec clock_realtime_saved;
> +
> +struct test_case {
> +	clockid_t type;
> +	struct timespec newtime;
> +	int exp_err;
> +	int replace;
> +};
> +
> +struct test_case tc[] = {
> +	{				/* case 01: REALTIME: timespec NULL   */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_sec = -2,
> +	 .exp_err = EFAULT,
> +	 .replace = 1,
> +	 },
> +	{				/* case 02: REALTIME: tv_sec = -1     */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_sec = -1,
> +	 .exp_err = EINVAL,
> +	 .replace = 1,
> +	 },
> +	{				/* case 03: REALTIME: tv_nsec = -1    */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_nsec = -1,
> +	 .exp_err = EINVAL,
> +	 .replace = 1,
> +	 },
> +	{				/* case 04: REALTIME: tv_nsec = 1s+1  */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_nsec = NSEC_PER_SEC + 1,
> +	 .exp_err = EINVAL,
> +	 .replace = 1,
> +	 },
> +	{				/* case 05: MONOTONIC		      */
> +	 .type = CLOCK_MONOTONIC,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 06: MAXCLOCK		      */
> +	 .type = MAX_CLOCKS,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 07: MAXCLOCK+1		      */
> +	 .type = MAX_CLOCKS + 1,
> +	 .exp_err = EINVAL,
> +	 },
> +	/* Linux specific */
> +	{				/* case 08: CLOCK_MONOTONIC_COARSE    */
> +	 .type = CLOCK_MONOTONIC_COARSE,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 09: CLOCK_MONOTONIC_RAW       */
> +	 .type = CLOCK_MONOTONIC_RAW,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 10: CLOCK_BOOTTIME	      */
> +	 .type = CLOCK_BOOTTIME,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 11: CLOCK_PROCESS_CPUTIME_ID  */
> +	 .type = CLOCK_PROCESS_CPUTIME_ID,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 12: CLOCK_THREAD_CPUTIME_ID   */
> +	 .type = CLOCK_THREAD_CPUTIME_ID,
> +	 .exp_err = EINVAL,
> +	 },
> +};
> +
> +/*
> + * Some tests may cause libc to segfault when passing bad arguments.
> + */
> +static int sys_clock_settime(clockid_t clk_id, struct timespec *tp)
> +{
> +	return tst_syscall(__NR_clock_settime, clk_id, tp);
> +}
> +
> +static int sys_clock_gettime(clockid_t clk_id, struct timespec *tp)
> +{
> +	return tst_syscall(__NR_clock_gettime, clk_id, tp);
> +}
> +
> +static void cleanup(void)
> +{
> +	/* restore realtime clock */
> +
> +	if (sys_clock_settime(CLOCK_REALTIME, &clock_realtime_saved) < 0)
> +		tst_res(TBROK | TTERRNO, "clock_settime(2): could not set "
> +				"current time back");
> +}
> +
> +static void setup(void)
> +{
> +	if (sys_clock_gettime(CLOCK_REALTIME, &clock_realtime_saved) < 0)
> +		tst_res(TBROK | TTERRNO, "clock_gettime(2): could not get "
> +				"current time");
> +}
> +
> +static void verify_clock_settime(unsigned int i)
> +{
> +	struct timespec spec, *specptr;
> +
> +	if (tc[i].replace == 0) {
> +
> +		/* add 1 sec to test clock */
> +
> +		specptr = &spec;
> +		specptr->tv_sec = clock_realtime_saved.tv_sec + 1;
> +		specptr->tv_nsec = clock_realtime_saved.tv_nsec;
> +
> +	} else {
> +
> +		/* bad pointer case */
> +
> +		if (tc[i].newtime.tv_sec == -2)
> +			specptr = tst_get_bad_addr(cleanup);
> +
> +		/* use given values */
> +
> +		else {

Still curly braces missing here, but that is minor.

And maybe we just need to turn the newtime into a pointer in the tcases
structure. Then you can initialize global variable to current time +
epsion and use pointer to it or even initialize it inline as:

static struct timespec valid_time;

struct test_case tc[] = {
	{
	 .type = CLOCK_REALTIME,
	 .exp_err = EFAULT,
	},
	{
	 .type = CLOCK_REALTIME;
	 .newtime = &valid_time;
	 .exp_err = EINVAL,
	{
	 .type = CLOCK_REALTIME,
	 .newtime = &(struct timespec){},
	 .exp_err = EINVAL,
	 .replace = 1,
	},
	...
};

And we can loop in the test setup and set NULL address to the result of
tst_get_bad_addr() as well.

> +			specptr = &spec;
> +			specptr->tv_sec = tc[i].newtime.tv_sec;
> +			specptr->tv_nsec = tc[i].newtime.tv_nsec;
> +		}
> +	}
> +
> +	TEST(sys_clock_settime(tc[i].type, specptr));
> +
> +	if (TST_RET == -1) {
> +
> +		if (tc[i].exp_err == TST_ERR) {
> +
> +			tst_res(TPASS, "clock_settime(2): failed as expected");
> +
> +		} else {
> +			tst_res(TFAIL | TTERRNO, "clock_settime(2): "
> +					"failed with different error");
> +		}
> +
> +		return;
> +	}
> +
> +	tst_res(TFAIL | TTERRNO, "clock_settime(2): clock type %d failed",
> +			tc[i].type);
> +}
> +
> +static struct tst_test test = {
> +	.setup = setup,
> +	.test = verify_clock_settime,
> +	.cleanup = cleanup,
> +	.tcnt = ARRAY_SIZE(tc),
> +	.needs_root = 1,
> +};
> -- 
> 2.20.0.rc1
> 

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v2 1/2] syscalls/clock_settime01.c: create syscall clock_settime
  2018-12-11 14:27     ` Cyril Hrubis
@ 2018-12-11 16:05       ` Rafael David Tinoco
  2018-12-12 20:37       ` [LTP] [PATCH v3 1/6] tst_timer.h: add tst_timespect_rem_us() function Rafael David Tinoco
  1 sibling, 0 replies; 37+ messages in thread
From: Rafael David Tinoco @ 2018-12-11 16:05 UTC (permalink / raw)
  To: ltp

On 12/11/18 12:27 PM, Cyril Hrubis wrote:
> Hi!
>> new file mode 100644
>> index 000000000..97d720fa2
>> --- /dev/null
>> +++ b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
>> @@ -0,0 +1,122 @@
>> +// SPDX-License-Identifier: GPL-2.0-or-later
>> +/*
>> + * Copyright (c) 2018 Linaro Limited. All rights reserved.
>> + * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
>> + */
>> +
>> +/*
>> + * Basic test for clock_settime(2) on REALTIME clock:
>> + *
>> + *      1) advance DELTA_SEC seconds
>> + *      2) go backwards DELTA_SEC seconds
>> + *
>> + * Accept DELTA_PER deviation on both (specially going backwards).
>> + */
>> +
>> +#include "config.h"
>> +#include "tst_test.h"
>> +#include "lapi/syscalls.h"
>> +
>> +#define DELTA_SEC 10	/* 10 seconds delta    */
>> +#define DELTA_PER 0.1	/* 1 percent deviation */
>> +
>> +static struct timespec real_begin, mono_begin, mono_end;
>> +
>> +static void clock_elapsed(struct timespec *begin, struct timespec *end,
>> +		struct timespec *elapsed)
>> +{
>> +	elapsed->tv_sec = end->tv_sec - begin->tv_sec;
>> +	elapsed->tv_nsec = end->tv_nsec - begin->tv_nsec;
> 
> If you do this the elapsed may end up in non-normalized state, i.e.
> elapsed->tv_nsec may end up negative.

#)... definitely, will stick to what tst_timer.h has.

> 
> We do have all kinds of inline functions for conversion and arimetic in
> tst_timer.h so there is no point of rolling your own here.
> 
>> +}
>> +
>> +static void clock_return(void)
>                       ^
> 		      I would have named this restore, but that is very
> 		      minor.
> 
>> +{
>> +	static struct timespec elapsed, adjust;
>> +
>> +	clock_elapsed(&mono_begin, &mono_end, &elapsed);
>> +
>> +	adjust.tv_sec = real_begin.tv_sec + elapsed.tv_sec;
>> +	adjust.tv_nsec = real_begin.tv_nsec + elapsed.tv_nsec;
> 
> We should normalize the addition here as well, i.e. carry over to
> seconds if the number of nanoseconds gets greater than 1s.

definitely, stupid mistake on my side, will fix.

> 
> Ideally we should add a function to add two timespec structures into the
> tst_timer.h header (in a separate patch).

Alright.

> 
>> +	if (clock_settime(CLOCK_REALTIME, &adjust) != 0)
>> +		tst_res(TBROK | TTERRNO, "could restore realtime clock");
>> +}
>> +
>> +static void clock_fixnow(void)
>> +{
>> +	if (clock_gettime(CLOCK_MONOTONIC_RAW, &mono_end) != 0)
>> +		tst_res(TBROK | TTERRNO, "could not get elapsed time");
> 
> We should make sure here that both real_begin and mono_begin were set
> before we attempt to restore the system time. A restore_time flag se to
> 1 after we successfully read both will do.

Alright.

> 
>> +	clock_return();
>> +}
>> +
>> +static void setup(void)
>> +{
>> +	/* save initial monotonic time to restore it when needed */
>> +
>> +	if (clock_gettime(CLOCK_REALTIME, &real_begin) != 0)
>> +		tst_res(TBROK | TTERRNO, "could not get initial real time");
>> +
>> +	if (clock_gettime(CLOCK_MONOTONIC_RAW, &mono_begin) != 0)
>> +		tst_res(TBROK | TTERRNO, "couldn't get initial monotonic time");
>                   ^
> 		  This should have been tst_brk() doing tst_res() with
> 		  TBROK does not make much sense.
> 
> Also we do have tst_safe_clocks.h, if you include that you can use
> SAFE_CLOCK_GETTIME() instead.
> 
> Overall the idea of restoring wall clock using monotonic timer is a good
> one, maybe we should even move this code to a library so that all tests
> that change wall clock would need just set restore_wallclock flag in the
> tst_test structure...

Will look into that.

> 
>> +}
>> +
>> +static void cleanup(void)
>> +{
>> +	clock_fixnow();
>> +}
>> +
>> +static void verify_clock_settime(void)
>> +{
>> +	static struct timespec begin, change, end, elapsed;
>> +
>> +	/* test 01: move forward */
>> +
>> +	if (clock_gettime(CLOCK_REALTIME, &begin) != 0)
>> +		tst_res(TBROK | TTERRNO, "could not get realtime at the begin");
>> +
>> +	change.tv_sec = begin.tv_sec + DELTA_SEC;
>> +
>> +	if (clock_settime(CLOCK_REALTIME, &change) != 0)
>> +		tst_res(TBROK | TTERRNO, "could not set realtime change");
>> +
>> +	if (clock_gettime(CLOCK_REALTIME, &end) != 0)
>> +		tst_res(TBROK | TTERRNO, "could not get realtime after change");
> 
> Here as well.
> 
>> +	clock_elapsed(&begin, &end, &elapsed);
>> +
>> +	if (elapsed.tv_sec < (float) (DELTA_SEC - (DELTA_SEC * DELTA_PER)))
> 
> It would be much easier to just use tst_timespec_diff_ms() and check
> that the result is in reasonable range. I.e. check lower bound as well.

Yep, I missed tst_timer.h entirely it seems =o), won't reinvent the wheel.

> 
>> +		tst_res(TFAIL, "clock_settime(2): could not advance time");
>> +	else
>> +		tst_res(TPASS, "clock_settime(2): was able to advance time");
>> +
>> +	/* test 02: move backward */
>> +
>> +	if (clock_gettime(CLOCK_REALTIME, &begin) != 0)
>> +		tst_res(TBROK | TTERRNO, "could not get realtime at the begin");
>> +
>> +	change.tv_sec = begin.tv_sec - DELTA_SEC;
>> +
>> +	if (clock_settime(CLOCK_REALTIME, &change) != 0)
>> +		tst_res(TBROK | TTERRNO, "could not set realtime change");
>> +
>> +	if (clock_gettime(CLOCK_REALTIME, &end) != 0)
>> +		tst_res(TBROK | TTERRNO, "could not get realtime after change");
>> +
>> +	clock_elapsed(&begin, &end, &elapsed);
>> +
>> +	elapsed.tv_sec = ~elapsed.tv_sec;
>> +	elapsed.tv_nsec = ~elapsed.tv_nsec;
>> +
>> +	if (elapsed.tv_sec < (float) (DELTA_SEC - (DELTA_SEC * DELTA_PER)))
>> +		tst_res(TFAIL, "clock_settime(2): could not recede time");
>> +	else
>> +		tst_res(TPASS, "clock_settime(2): was able to recede time");
> 
> Here as well.
> 
>> +}
>> +
>> +static struct tst_test test = {
>> +	.setup = setup,
>> +	.test_all = verify_clock_settime,
>> +	.cleanup = cleanup,
>> +	.needs_root = 1,
>> +};
>>
>> diff --git a/testcases/kernel/syscalls/clock_settime/clock_settime02.c b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
>> new file mode 100644
>> index 000000000..710f37219
>> --- /dev/null
>> +++ b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
>> @@ -0,0 +1,171 @@
>> +// SPDX-License-Identifier: GPL-2.0-or-later
>> +/*
>> + * Copyright (c) 2018 Linaro Limited. All rights reserved.
>> + * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
>> + */
>> +
>> +/*
>> + * Basic tests for errors of clock_settime(2) on different clock types.
>> + */
>> +
>> +#include "config.h"
>> +#include "tst_test.h"
>> +#include "lapi/syscalls.h"
>> +
>> +#define DELTA_SEC 10
>> +#define NSEC_PER_SEC (1000000000L)
>> +#define MAX_CLOCKS 16
>> +
>> +static struct timespec clock_realtime_saved;
>> +
>> +struct test_case {
>> +	clockid_t type;
>> +	struct timespec newtime;
>> +	int exp_err;
>> +	int replace;
>> +};
>> +
>> +struct test_case tc[] = {
>> +	{				/* case 01: REALTIME: timespec NULL   */
>> +	 .type = CLOCK_REALTIME,
>> +	 .newtime.tv_sec = -2,
>> +	 .exp_err = EFAULT,
>> +	 .replace = 1,
>> +	 },
>> +	{				/* case 02: REALTIME: tv_sec = -1     */
>> +	 .type = CLOCK_REALTIME,
>> +	 .newtime.tv_sec = -1,
>> +	 .exp_err = EINVAL,
>> +	 .replace = 1,
>> +	 },
>> +	{				/* case 03: REALTIME: tv_nsec = -1    */
>> +	 .type = CLOCK_REALTIME,
>> +	 .newtime.tv_nsec = -1,
>> +	 .exp_err = EINVAL,
>> +	 .replace = 1,
>> +	 },
>> +	{				/* case 04: REALTIME: tv_nsec = 1s+1  */
>> +	 .type = CLOCK_REALTIME,
>> +	 .newtime.tv_nsec = NSEC_PER_SEC + 1,
>> +	 .exp_err = EINVAL,
>> +	 .replace = 1,
>> +	 },
>> +	{				/* case 05: MONOTONIC		      */
>> +	 .type = CLOCK_MONOTONIC,
>> +	 .exp_err = EINVAL,
>> +	 },
>> +	{				/* case 06: MAXCLOCK		      */
>> +	 .type = MAX_CLOCKS,
>> +	 .exp_err = EINVAL,
>> +	 },
>> +	{				/* case 07: MAXCLOCK+1		      */
>> +	 .type = MAX_CLOCKS + 1,
>> +	 .exp_err = EINVAL,
>> +	 },
>> +	/* Linux specific */
>> +	{				/* case 08: CLOCK_MONOTONIC_COARSE    */
>> +	 .type = CLOCK_MONOTONIC_COARSE,
>> +	 .exp_err = EINVAL,
>> +	 },
>> +	{				/* case 09: CLOCK_MONOTONIC_RAW       */
>> +	 .type = CLOCK_MONOTONIC_RAW,
>> +	 .exp_err = EINVAL,
>> +	 },
>> +	{				/* case 10: CLOCK_BOOTTIME	      */
>> +	 .type = CLOCK_BOOTTIME,
>> +	 .exp_err = EINVAL,
>> +	 },
>> +	{				/* case 11: CLOCK_PROCESS_CPUTIME_ID  */
>> +	 .type = CLOCK_PROCESS_CPUTIME_ID,
>> +	 .exp_err = EINVAL,
>> +	 },
>> +	{				/* case 12: CLOCK_THREAD_CPUTIME_ID   */
>> +	 .type = CLOCK_THREAD_CPUTIME_ID,
>> +	 .exp_err = EINVAL,
>> +	 },
>> +};
>> +
>> +/*
>> + * Some tests may cause libc to segfault when passing bad arguments.
>> + */
>> +static int sys_clock_settime(clockid_t clk_id, struct timespec *tp)
>> +{
>> +	return tst_syscall(__NR_clock_settime, clk_id, tp);
>> +}
>> +
>> +static int sys_clock_gettime(clockid_t clk_id, struct timespec *tp)
>> +{
>> +	return tst_syscall(__NR_clock_gettime, clk_id, tp);
>> +}
>> +
>> +static void cleanup(void)
>> +{
>> +	/* restore realtime clock */
>> +
>> +	if (sys_clock_settime(CLOCK_REALTIME, &clock_realtime_saved) < 0)
>> +		tst_res(TBROK | TTERRNO, "clock_settime(2): could not set "
>> +				"current time back");
>> +}
>> +
>> +static void setup(void)
>> +{
>> +	if (sys_clock_gettime(CLOCK_REALTIME, &clock_realtime_saved) < 0)
>> +		tst_res(TBROK | TTERRNO, "clock_gettime(2): could not get "
>> +				"current time");
>> +}
>> +
>> +static void verify_clock_settime(unsigned int i)
>> +{
>> +	struct timespec spec, *specptr;
>> +
>> +	if (tc[i].replace == 0) {
>> +
>> +		/* add 1 sec to test clock */
>> +
>> +		specptr = &spec;
>> +		specptr->tv_sec = clock_realtime_saved.tv_sec + 1;
>> +		specptr->tv_nsec = clock_realtime_saved.tv_nsec;
>> +
>> +	} else {
>> +
>> +		/* bad pointer case */
>> +
>> +		if (tc[i].newtime.tv_sec == -2)
>> +			specptr = tst_get_bad_addr(cleanup);
>> +
>> +		/* use given values */
>> +
>> +		else {
> 
> Still curly braces missing here, but that is minor.
> 
> And maybe we just need to turn the newtime into a pointer in the tcases
> structure. Then you can initialize global variable to current time +
> epsion and use pointer to it or even initialize it inline as:
> 
> static struct timespec valid_time;
> 
> struct test_case tc[] = {
> 	{
> 	 .type = CLOCK_REALTIME,
> 	 .exp_err = EFAULT,
> 	},
> 	{
> 	 .type = CLOCK_REALTIME;
> 	 .newtime = &valid_time;
> 	 .exp_err = EINVAL,
> 	{
> 	 .type = CLOCK_REALTIME,
> 	 .newtime = &(struct timespec){},
> 	 .exp_err = EINVAL,
> 	 .replace = 1,
> 	},
> 	...
> };
> 
> And we can loop in the test setup and set NULL address to the result of
> tst_get_bad_addr() as well.

Good idea.

> 
>> +			specptr = &spec;
>> +			specptr->tv_sec = tc[i].newtime.tv_sec;
>> +			specptr->tv_nsec = tc[i].newtime.tv_nsec;
>> +		}
>> +	}
>> +
>> +	TEST(sys_clock_settime(tc[i].type, specptr));
>> +
>> +	if (TST_RET == -1) {
>> +
>> +		if (tc[i].exp_err == TST_ERR) {
>> +
>> +			tst_res(TPASS, "clock_settime(2): failed as expected");
>> +
>> +		} else {
>> +			tst_res(TFAIL | TTERRNO, "clock_settime(2): "
>> +					"failed with different error");
>> +		}
>> +
>> +		return;
>> +	}
>> +
>> +	tst_res(TFAIL | TTERRNO, "clock_settime(2): clock type %d failed",
>> +			tc[i].type);
>> +}
>> +
>> +static struct tst_test test = {
>> +	.setup = setup,
>> +	.test = verify_clock_settime,
>> +	.cleanup = cleanup,
>> +	.tcnt = ARRAY_SIZE(tc),
>> +	.needs_root = 1,
>> +};
>> -- 
>> 2.20.0.rc1
>>
> 

Thanks for reviewing... will re-work it.

-- 
Rafael D. Tinoco
Linaro - Kernel Validation

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

* [LTP] [PATCH v3 1/6] tst_timer.h: add tst_timespect_rem_us() function
  2018-12-11 14:27     ` Cyril Hrubis
  2018-12-11 16:05       ` Rafael David Tinoco
@ 2018-12-12 20:37       ` Rafael David Tinoco
  2018-12-12 20:37         ` [LTP] [PATCH v3 2/6] lib: add tst_clock_settime() to tst_clocks.h Rafael David Tinoco
                           ` (5 more replies)
  1 sibling, 6 replies; 37+ messages in thread
From: Rafael David Tinoco @ 2018-12-12 20:37 UTC (permalink / raw)
  To: ltp

Expand tst_timer.h functionality by having a function to also remove
given microseconds from a given timespec.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 include/tst_timer.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/include/tst_timer.h b/include/tst_timer.h
index 577bc88ef..b1c9ceeba 100644
--- a/include/tst_timer.h
+++ b/include/tst_timer.h
@@ -151,6 +151,23 @@ static inline struct timespec tst_timespec_add_us(struct timespec t,
 	return t;
 }
 
+/*
+ * Removes us microseconds to t.
+ */
+static inline struct timespec tst_timespec_rem_us(struct timespec t,
+                                                  long long us)
+{
+	t.tv_sec -= us / 1000000;
+	t.tv_nsec -= (us % 1000000) * 1000;
+
+	if (t.tv_nsec < 0) {
+		t.tv_sec--;
+		t.tv_nsec += 1000000000;
+	}
+
+	return t;
+}
+
 /*
  * Returns difference between two timespec structures.
  */
-- 
2.20.0.rc1


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

* [LTP] [PATCH v3 2/6] lib: add tst_clock_settime() to tst_clocks.h
  2018-12-12 20:37       ` [LTP] [PATCH v3 1/6] tst_timer.h: add tst_timespect_rem_us() function Rafael David Tinoco
@ 2018-12-12 20:37         ` Rafael David Tinoco
  2018-12-12 20:37         ` [LTP] [PATCH v3 3/6] lib: include SAFE_CLOCK_SETTIME() macro Rafael David Tinoco
                           ` (4 subsequent siblings)
  5 siblings, 0 replies; 37+ messages in thread
From: Rafael David Tinoco @ 2018-12-12 20:37 UTC (permalink / raw)
  To: ltp

Adds tst_clock_settime() function to the lib.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 include/tst_clocks.h | 2 ++
 lib/tst_clocks.c     | 5 +++++
 2 files changed, 7 insertions(+)

diff --git a/include/tst_clocks.h b/include/tst_clocks.h
index ee2f645c7..90784a3fd 100644
--- a/include/tst_clocks.h
+++ b/include/tst_clocks.h
@@ -26,4 +26,6 @@ int tst_clock_getres(clockid_t clk_id, struct timespec *res);
 
 int tst_clock_gettime(clockid_t clk_id, struct timespec *ts);
 
+int tst_clock_settime(clockid_t clk_id, struct timespec *ts);
+
 #endif /* TST_CLOCKS__ */
diff --git a/lib/tst_clocks.c b/lib/tst_clocks.c
index 87413a339..35798a4aa 100644
--- a/lib/tst_clocks.c
+++ b/lib/tst_clocks.c
@@ -35,3 +35,8 @@ int tst_clock_gettime(clockid_t clk_id, struct timespec *ts)
 {
 	return syscall(SYS_clock_gettime, clk_id, ts);
 }
+
+int tst_clock_settime(clockid_t clk_id, struct timespec *ts)
+{
+	return syscall(SYS_clock_settime, clk_id, ts);
+}
-- 
2.20.0.rc1


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

* [LTP] [PATCH v3 3/6] lib: include SAFE_CLOCK_SETTIME() macro
  2018-12-12 20:37       ` [LTP] [PATCH v3 1/6] tst_timer.h: add tst_timespect_rem_us() function Rafael David Tinoco
  2018-12-12 20:37         ` [LTP] [PATCH v3 2/6] lib: add tst_clock_settime() to tst_clocks.h Rafael David Tinoco
@ 2018-12-12 20:37         ` Rafael David Tinoco
  2018-12-12 20:37         ` [LTP] [PATCH v3 4/6] lib: new restore_wallclock field to restore realtime clock Rafael David Tinoco
                           ` (3 subsequent siblings)
  5 siblings, 0 replies; 37+ messages in thread
From: Rafael David Tinoco @ 2018-12-12 20:37 UTC (permalink / raw)
  To: ltp

Adds SAFE_CLOCK_SETTIME() macro to tst_safe_clocks.h.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 include/tst_safe_clocks.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/include/tst_safe_clocks.h b/include/tst_safe_clocks.h
index 1b4d48543..553f8464a 100644
--- a/include/tst_safe_clocks.h
+++ b/include/tst_safe_clocks.h
@@ -30,9 +30,23 @@ static inline void safe_clock_gettime(const char *file, const int lineno,
 			"%s:%d clock_gettime() failed", file, lineno);
 }
 
+
+static inline void safe_clock_settime(const char *file, const int lineno,
+	clockid_t clk_id, struct timespec *tp)
+{
+	int rval;
+
+	rval = clock_settime(clk_id, tp);
+	if (rval != 0)
+		tst_brk(TBROK | TERRNO,
+			"%s:%d clock_gettime() failed", file, lineno);
+}
+
 #define SAFE_CLOCK_GETRES(clk_id, res)\
 	safe_clock_getres(__FILE__, __LINE__, (clk_id), (res))
 
 #define SAFE_CLOCK_GETTIME(clk_id, tp)\
 	safe_clock_gettime(__FILE__, __LINE__, (clk_id), (tp))
 
+#define SAFE_CLOCK_SETTIME(clk_id, tp)\
+	safe_clock_settime(__FILE__, __LINE__, (clk_id), (tp))
-- 
2.20.0.rc1


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

* [LTP] [PATCH v3 4/6] lib: new restore_wallclock field to restore realtime clock
  2018-12-12 20:37       ` [LTP] [PATCH v3 1/6] tst_timer.h: add tst_timespect_rem_us() function Rafael David Tinoco
  2018-12-12 20:37         ` [LTP] [PATCH v3 2/6] lib: add tst_clock_settime() to tst_clocks.h Rafael David Tinoco
  2018-12-12 20:37         ` [LTP] [PATCH v3 3/6] lib: include SAFE_CLOCK_SETTIME() macro Rafael David Tinoco
@ 2018-12-12 20:37         ` Rafael David Tinoco
  2019-01-24 15:12           ` Cyril Hrubis
  2018-12-12 20:37         ` [LTP] [PATCH v3 5/6] syscalls/clock_settime: create syscall clock_settime tests Rafael David Tinoco
                           ` (2 subsequent siblings)
  5 siblings, 1 reply; 37+ messages in thread
From: Rafael David Tinoco @ 2018-12-12 20:37 UTC (permalink / raw)
  To: ltp

Some tests around clocks need to restore the correct date and time after
the tests, including possible iterations, run.

This commit introduces a new field to tst_test called
"restore_wallclock", which makes the test to save current realtime clock
during setup phase, and, later, during cleanup, restore it to the
appropriate time using a monotonic raw clock difference.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 include/tst_test.h  |  1 +
 lib/tst_test.c      |  6 ++++++
 lib/tst_wallclock.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 51 insertions(+)
 create mode 100644 lib/tst_wallclock.c

diff --git a/include/tst_test.h b/include/tst_test.h
index 2ebf746eb..170bddc21 100644
--- a/include/tst_test.h
+++ b/include/tst_test.h
@@ -131,6 +131,7 @@ struct tst_test {
 	int needs_rofs:1;
 	int child_needs_reinit:1;
 	int needs_devfs:1;
+	int restore_wallclock:1;
 	/*
 	 * If set the test function will be executed for all available
 	 * filesystems and the current filesytem type would be set in the
diff --git a/lib/tst_test.c b/lib/tst_test.c
index 661fbbfce..aa3d674f0 100644
--- a/lib/tst_test.c
+++ b/lib/tst_test.c
@@ -868,6 +868,9 @@ static void do_setup(int argc, char *argv[])
 
 	if (tst_test->resource_files)
 		copy_resources();
+
+	if (tst_test->restore_wallclock)
+		tst_wallclock_save();
 }
 
 static void do_test_setup(void)
@@ -899,6 +902,9 @@ static void do_cleanup(void)
 		tst_sys_conf_restore(0);
 
 	cleanup_ipc();
+
+	if (tst_test->restore_wallclock)
+		tst_wallclock_restore();
 }
 
 static void run_tests(void)
diff --git a/lib/tst_wallclock.c b/lib/tst_wallclock.c
new file mode 100644
index 000000000..ef08e1dba
--- /dev/null
+++ b/lib/tst_wallclock.c
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+#include <errno.h>
+
+#define TST_NO_DEFAULT_MAIN
+
+#include "tst_test.h"
+#include "tst_timer.h"
+#include "tst_clocks.h"
+#include "lapi/posix_clocks.h"
+
+static struct timespec real_begin, mono_begin;
+
+void tst_wallclock_save(void)
+{
+	/* save initial monotonic time to restore it when needed */
+
+	if (tst_clock_gettime(CLOCK_REALTIME, &real_begin))
+		tst_brk(TBROK | TERRNO, "tst_clock_gettime() realtime failed");
+
+	if (tst_clock_gettime(CLOCK_MONOTONIC_RAW, &mono_begin))
+		tst_brk(TBROK | TERRNO, "tst_clock_gettime() monotonic failed");
+}
+
+void tst_wallclock_restore(void)
+{
+	static struct timespec mono_end, elapsed, adjust;
+
+	if (tst_clock_gettime(CLOCK_MONOTONIC_RAW, &mono_end))
+		tst_brk(TBROK | TERRNO, "tst_clock_gettime() monotonic failed");
+
+	elapsed = tst_timespec_diff(mono_end, mono_begin);
+
+	adjust = tst_timespec_add_us(real_begin, tst_timespec_to_us(elapsed));
+
+	/* restore realtime clock based on monotonic delta */
+
+	if (tst_clock_settime(CLOCK_REALTIME, &adjust))
+		tst_brk(TBROK | TERRNO, "tst_clock_settime() realtime failed");
+}
-- 
2.20.0.rc1


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

* [LTP] [PATCH v3 5/6] syscalls/clock_settime: create syscall clock_settime tests
  2018-12-12 20:37       ` [LTP] [PATCH v3 1/6] tst_timer.h: add tst_timespect_rem_us() function Rafael David Tinoco
                           ` (2 preceding siblings ...)
  2018-12-12 20:37         ` [LTP] [PATCH v3 4/6] lib: new restore_wallclock field to restore realtime clock Rafael David Tinoco
@ 2018-12-12 20:37         ` Rafael David Tinoco
  2018-12-12 20:46           ` Rafael David Tinoco
  2019-01-24 16:11           ` Cyril Hrubis
  2018-12-12 20:37         ` [LTP] [PATCH v3 6/6] timers/clock_settime: remove clock_settime tests Rafael David Tinoco
  2019-01-24 15:06         ` [LTP] [PATCH v3 1/6] tst_timer.h: add tst_timespect_rem_us() function Cyril Hrubis
  5 siblings, 2 replies; 37+ messages in thread
From: Rafael David Tinoco @ 2018-12-12 20:37 UTC (permalink / raw)
  To: ltp

Fixes: 343

clock_settime01 creates a new test, using new API, based on existing
and older kernel/timers/clock_settime02 test. clock_settime02 creates
another test based on older kernel/timers/clock_settime03 test. Both
will be deleted in the next commits.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 runtest/syscalls                              |   3 +
 .../kernel/syscalls/clock_settime/.gitignore  |   2 +
 .../kernel/syscalls/clock_settime/Makefile    |   8 +
 .../syscalls/clock_settime/clock_settime01.c  |  75 +++++++++
 .../syscalls/clock_settime/clock_settime02.c  | 150 ++++++++++++++++++
 5 files changed, 238 insertions(+)
 create mode 100644 testcases/kernel/syscalls/clock_settime/.gitignore
 create mode 100644 testcases/kernel/syscalls/clock_settime/Makefile
 create mode 100644 testcases/kernel/syscalls/clock_settime/clock_settime01.c
 create mode 100644 testcases/kernel/syscalls/clock_settime/clock_settime02.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 34b47f36b..dd03ad31a 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -79,6 +79,9 @@ clock_nanosleep01 clock_nanosleep01
 clock_nanosleep02 clock_nanosleep02
 clock_nanosleep2_01 clock_nanosleep2_01
 
+clock_settime01 clock_settime01
+clock_settime02 clock_settime02
+
 clone01 clone01
 clone02 clone02
 clone03 clone03
diff --git a/testcases/kernel/syscalls/clock_settime/.gitignore b/testcases/kernel/syscalls/clock_settime/.gitignore
new file mode 100644
index 000000000..281217550
--- /dev/null
+++ b/testcases/kernel/syscalls/clock_settime/.gitignore
@@ -0,0 +1,2 @@
+clock_settime01
+clock_settime02
diff --git a/testcases/kernel/syscalls/clock_settime/Makefile b/testcases/kernel/syscalls/clock_settime/Makefile
new file mode 100644
index 000000000..e6674a6b2
--- /dev/null
+++ b/testcases/kernel/syscalls/clock_settime/Makefile
@@ -0,0 +1,8 @@
+# Copyright (c) 2018 - Linaro Limited. All rights reserved.
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/clock_settime/clock_settime01.c b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
new file mode 100644
index 000000000..6e0ee47c4
--- /dev/null
+++ b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
@@ -0,0 +1,75 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+/*
+ * Basic test for clock_settime(2) on REALTIME clock:
+ *
+ *      1) advance DELTA_SEC seconds
+ *      2) go backwards DELTA_SEC seconds
+ *
+ * Restore wall clock at the end of test.
+ */
+
+#include "config.h"
+#include "tst_timer.h"
+#include "tst_safe_clocks.h"
+#include "tst_test.h"
+#include "lapi/syscalls.h"
+
+#define DELTA_SEC 10
+#define DELTA_SEC_US (long long) (DELTA_SEC * 1000000)
+#define DELTA_SEC_VAR_POS (long long) (DELTA_SEC_US * 1.10)
+#define DELTA_SEC_VAR_NEG (long long) (DELTA_SEC_US * 0.90)
+
+static void verify_clock_settime(void)
+{
+	long long elapsed;
+	struct timespec begin, change, end;
+
+	/* test 01: move forward */
+
+	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &begin);
+
+	change = tst_timespec_add_us(begin, DELTA_SEC_US);
+
+	if (clock_settime(CLOCK_REALTIME, &change) != 0)
+		tst_brk(TBROK | TTERRNO, "could not set realtime change");
+
+	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &end);
+
+	elapsed = tst_timespec_diff_us(end, begin);
+
+	if (elapsed > DELTA_SEC_US && elapsed < DELTA_SEC_VAR_POS) {
+		tst_res(TPASS, "clock_settime(2): was able to advance time");
+	} else {
+		tst_res(TFAIL, "clock_settime(2): could not advance time");
+	}
+
+	/* test 02: move backward */
+
+	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &begin);
+
+	change = tst_timespec_rem_us(begin, DELTA_SEC_US);
+
+	if (clock_settime(CLOCK_REALTIME, &change) != 0)
+		tst_brk(TBROK | TTERRNO, "could not set realtime change");
+
+	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &end);
+
+	elapsed = tst_timespec_diff_us(end, begin);
+
+	if (~(elapsed) > DELTA_SEC_VAR_NEG) {
+		tst_res(TPASS, "clock_settime(2): was able to recede time");
+	} else {
+		tst_res(TFAIL, "clock_settime(2): could not recede time");
+	}
+}
+
+static struct tst_test test = {
+	.test_all = verify_clock_settime,
+	.needs_root = 1,
+	.restore_wallclock = 1,
+};
diff --git a/testcases/kernel/syscalls/clock_settime/clock_settime02.c b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
new file mode 100644
index 000000000..25fcbfe09
--- /dev/null
+++ b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
@@ -0,0 +1,150 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+/*
+ * Basic tests for errors of clock_settime(2) on different clock types.
+ */
+
+#include "config.h"
+#include "tst_test.h"
+#include "lapi/syscalls.h"
+#include "tst_timer.h"
+#include "tst_safe_clocks.h"
+
+#define DELTA_SEC 10
+#define NSEC_PER_SEC (1000000000L)
+#define MAX_CLOCKS 16
+
+struct test_case {
+	clockid_t type;
+	struct timespec newtime;
+	int exp_err;
+	int replace;
+};
+
+struct test_case tc[] = {
+	{				/* case 01: REALTIME: timespec NULL   */
+	 .type = CLOCK_REALTIME,
+	 .newtime.tv_sec = -2,
+	 .exp_err = EFAULT,
+	 .replace = 1,
+	 },
+	{				/* case 02: REALTIME: tv_sec = -1     */
+	 .type = CLOCK_REALTIME,
+	 .newtime.tv_sec = -1,
+	 .exp_err = EINVAL,
+	 .replace = 1,
+	 },
+	{				/* case 03: REALTIME: tv_nsec = -1    */
+	 .type = CLOCK_REALTIME,
+	 .newtime.tv_nsec = -1,
+	 .exp_err = EINVAL,
+	 .replace = 1,
+	 },
+	{				/* case 04: REALTIME: tv_nsec = 1s+1  */
+	 .type = CLOCK_REALTIME,
+	 .newtime.tv_nsec = NSEC_PER_SEC + 1,
+	 .exp_err = EINVAL,
+	 .replace = 1,
+	 },
+	{				/* case 05: MONOTONIC		      */
+	 .type = CLOCK_MONOTONIC,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 06: MAXCLOCK		      */
+	 .type = MAX_CLOCKS,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 07: MAXCLOCK+1		      */
+	 .type = MAX_CLOCKS + 1,
+	 .exp_err = EINVAL,
+	 },
+	/* Linux specific */
+	{				/* case 08: CLOCK_MONOTONIC_COARSE    */
+	 .type = CLOCK_MONOTONIC_COARSE,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 09: CLOCK_MONOTONIC_RAW       */
+	 .type = CLOCK_MONOTONIC_RAW,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 10: CLOCK_BOOTTIME	      */
+	 .type = CLOCK_BOOTTIME,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 11: CLOCK_PROCESS_CPUTIME_ID  */
+	 .type = CLOCK_PROCESS_CPUTIME_ID,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 12: CLOCK_THREAD_CPUTIME_ID   */
+	 .type = CLOCK_THREAD_CPUTIME_ID,
+	 .exp_err = EINVAL,
+	 },
+};
+
+/*
+ * Some tests may cause libc to segfault when passing bad arguments.
+ */
+static int sys_clock_settime(clockid_t clk_id, struct timespec *tp)
+{
+	return tst_syscall(__NR_clock_settime, clk_id, tp);
+}
+
+static void verify_clock_settime(unsigned int i)
+{
+	struct timespec saved, spec, *specptr;
+
+	if (tc[i].replace == 0) {
+
+		SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &saved);
+
+		/* add 1 sec to test clock */
+		specptr = &spec;
+		specptr->tv_sec = saved.tv_sec + 1;
+		specptr->tv_nsec = saved.tv_nsec;
+
+	} else {
+
+
+		if (tc[i].newtime.tv_sec == -2) {
+
+			/* bad pointer case */
+			specptr = tst_get_bad_addr(NULL);
+		} else {
+
+			/* use given values */
+			specptr = &spec;
+			specptr->tv_sec = tc[i].newtime.tv_sec;
+			specptr->tv_nsec = tc[i].newtime.tv_nsec;
+		}
+	}
+
+	TEST(sys_clock_settime(tc[i].type, specptr));
+
+	if (TST_RET == -1) {
+
+		if (tc[i].exp_err == TST_ERR) {
+
+			tst_res(TPASS, "clock_settime(2): failed as expected");
+
+		} else {
+			tst_res(TFAIL | TTERRNO, "clock_settime(2): "
+					"failed with different error");
+		}
+
+		return;
+	}
+
+	tst_res(TFAIL | TTERRNO, "clock_settime(2): clock type %d failed",
+			tc[i].type);
+}
+
+static struct tst_test test = {
+	.test = verify_clock_settime,
+	.tcnt = ARRAY_SIZE(tc),
+	.needs_root = 1,
+	.restore_wallclock = 1,
+};
-- 
2.20.0.rc1


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

* [LTP] [PATCH v3 6/6] timers/clock_settime: remove clock_settime tests
  2018-12-12 20:37       ` [LTP] [PATCH v3 1/6] tst_timer.h: add tst_timespect_rem_us() function Rafael David Tinoco
                           ` (3 preceding siblings ...)
  2018-12-12 20:37         ` [LTP] [PATCH v3 5/6] syscalls/clock_settime: create syscall clock_settime tests Rafael David Tinoco
@ 2018-12-12 20:37         ` Rafael David Tinoco
  2019-01-08 12:04           ` Rafael David Tinoco
  2019-01-24 15:06         ` [LTP] [PATCH v3 1/6] tst_timer.h: add tst_timespect_rem_us() function Cyril Hrubis
  5 siblings, 1 reply; 37+ messages in thread
From: Rafael David Tinoco @ 2018-12-12 20:37 UTC (permalink / raw)
  To: ltp

Fixes: 343

clock_settime01 creates a new test, using new API, based on existing and
older kernel/timers/clock_settime tests. It includes tests from files
clock_settime02 and clock_settime03.

This commit deletes timers/clock_settime/* tests.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 runtest/timers                                |   2 -
 .../kernel/timers/clock_settime/.gitignore    |   2 -
 .../kernel/timers/clock_settime/Makefile      |  27 ---
 .../timers/clock_settime/clock_settime02.c    | 128 -------------
 .../timers/clock_settime/clock_settime03.c    | 173 ------------------
 5 files changed, 332 deletions(-)
 delete mode 100644 testcases/kernel/timers/clock_settime/.gitignore
 delete mode 100644 testcases/kernel/timers/clock_settime/Makefile
 delete mode 100644 testcases/kernel/timers/clock_settime/clock_settime02.c
 delete mode 100644 testcases/kernel/timers/clock_settime/clock_settime03.c

diff --git a/runtest/timers b/runtest/timers
index a58ac57fc..618d2cb0c 100644
--- a/runtest/timers
+++ b/runtest/timers
@@ -1,8 +1,6 @@
 #DESCRIPTION:Posix Timer Tests
 clock_gettime02 clock_gettime02
 clock_gettime03 clock_gettime03
-clock_settime02 clock_settime02
-clock_settime03 clock_settime03
 timer_create02 timer_create02
 timer_create03 timer_create03
 timer_create04 timer_create04
diff --git a/testcases/kernel/timers/clock_settime/.gitignore b/testcases/kernel/timers/clock_settime/.gitignore
deleted file mode 100644
index 957c5ac26..000000000
--- a/testcases/kernel/timers/clock_settime/.gitignore
+++ /dev/null
@@ -1,2 +0,0 @@
-/clock_settime02
-/clock_settime03
diff --git a/testcases/kernel/timers/clock_settime/Makefile b/testcases/kernel/timers/clock_settime/Makefile
deleted file mode 100644
index 8de247075..000000000
--- a/testcases/kernel/timers/clock_settime/Makefile
+++ /dev/null
@@ -1,27 +0,0 @@
-#
-#  Copyright (c) International Business Machines  Corp., 2001
-#
-#  This program is free software;  you can redistribute it and/or modify
-#  it under the terms of the GNU General Public License as published by
-#  the Free Software Foundation; either version 2 of the License, or
-#  (at your option) any later version.
-#
-#  This program is distributed in the hope that it will be useful,
-#  but WITHOUT ANY WARRANTY;  without even the implied warranty of
-#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
-#  the GNU General Public License for more details.
-#
-#  You should have received a copy of the GNU General Public License
-#  along with this program;  if not, write to the Free Software
-#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
-#
-
-top_srcdir		?= ../../../..
-
-include $(top_srcdir)/include/mk/testcases.mk
-
-CPPFLAGS		+= -D_GNU_SOURCE -I$(abs_srcdir)/../include
-
-LDLIBS			+= -lpthread -lrt
-
-include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/timers/clock_settime/clock_settime02.c b/testcases/kernel/timers/clock_settime/clock_settime02.c
deleted file mode 100644
index 65721c1ae..000000000
--- a/testcases/kernel/timers/clock_settime/clock_settime02.c
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
- * Copyright (c) Wipro Technologies Ltd, 2003.  All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- */
-/**************************************************************************
- *
- *    TEST IDENTIFIER	: clock_settime02
- *
- *    EXECUTED BY	: root / superuser
- *
- *    TEST TITLE	: Basic test for clock_settime(2)
- *
- *    TEST CASE TOTAL	: 1
- *
- *    AUTHOR		: Aniruddha Marathe <aniruddha.marathe@wipro.com>
- *
- *    SIGNALS
- * 	Uses SIGUSR1 to pause before test if option set.
- * 	(See the parse_opts(3) man page).
- *
- *    DESCRIPTION
- *     This is a Phase I test for the clock_settime(2) system call.
- *     It is intended to provide a limited exposure of the system call.
- *
- * 	Setup:
- *	  Setup signal handling.
- *	  Pause for SIGUSR1 if option specified.
- *
- * 	Test:
- *	 Loop if the proper options are given.
- *	 Set the parameters of timespec struct
- *	 Execute system call
- *	 Check return code, if system call failed (return=-1)
- *		Log the errno and Issue a FAIL message.
- *	 Otherwise, Issue a PASS message.
- *
- * 	Cleanup:
- * 	  Print errno log and/or timing stats if options given
- *
- * USAGE:  <for command-line>
- * clock_settime02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-p]
- * where:
- * 	-c n : Run n copies simultaneously.
- *	-e   : Turn on errno logging.
- *	-i n : Execute test n times.
- *	-I x : Execute test for x seconds.
- *	-p   : Pause for SIGUSR1 before starting
- *	-P x : Pause for x seconds between iterations.
- *	-t   : Turn on syscall timing.
- *
- *RESTRICTIONS:
- * None
- *****************************************************************************/
-
-#include <stdlib.h>
-#include <errno.h>
-#include <time.h>
-#include <signal.h>
-
-#include "test.h"
-#include "common_timers.h"
-
-static void setup(void);
-static void cleanup(void);
-
-char *TCID = "clock_settime02";
-int TST_TOTAL = 1;
-static struct timespec saved;
-
-int main(int ac, char **av)
-{
-	int lc;
-	struct timespec spec;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		spec.tv_sec = saved.tv_sec + 1;
-		spec.tv_nsec = 0;
-
-		TEST(ltp_syscall(__NR_clock_settime, CLOCK_REALTIME, &spec));
-		tst_resm((TEST_RETURN < 0 ? TFAIL | TTERRNO : TPASS),
-			 "clock_settime %s",
-			 (TEST_RETURN == 0 ? "passed" : "failed"));
-	}
-
-	cleanup();
-	tst_exit();
-}
-
-static void setup(void)
-{
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	tst_require_root();
-
-	/* Save the current time specifications */
-	if (ltp_syscall(__NR_clock_gettime, CLOCK_REALTIME, &saved) < 0)
-		tst_brkm(TBROK, NULL, "Could not save the current time");
-
-	TEST_PAUSE;
-}
-
-static void cleanup(void)
-{
-	/* Set the saved time */
-	if (clock_settime(CLOCK_REALTIME, &saved) < 0) {
-		tst_resm(TWARN, "FATAL COULD NOT RESET THE CLOCK");
-		tst_resm(TFAIL, "Error Setting Time, errno=%d", errno);
-	}
-}
diff --git a/testcases/kernel/timers/clock_settime/clock_settime03.c b/testcases/kernel/timers/clock_settime/clock_settime03.c
deleted file mode 100644
index 38b41d025..000000000
--- a/testcases/kernel/timers/clock_settime/clock_settime03.c
+++ /dev/null
@@ -1,173 +0,0 @@
-/*
- * Copyright (c) Wipro Technologies Ltd, 2003.  All Rights Reserved.
- * Copyright (c) 2011 Cyril Hrubis <chrubis@suse.cz>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- */
-
-#include <errno.h>
-#include <time.h>
-#include <pwd.h>
-#include <unistd.h>
-
-#include "test.h"
-#include "safe_macros.h"
-#include "common_timers.h"
-
-static void setup(void);
-static void cleanup(void);
-static int setup_test(int option);
-
-clockid_t clocks[] = {
-	CLOCK_REALTIME,
-	CLOCK_MONOTONIC,
-	MAX_CLOCKS,
-	MAX_CLOCKS + 1,
-	CLOCK_REALTIME,
-	CLOCK_REALTIME,
-	CLOCK_REALTIME,
-	CLOCK_PROCESS_CPUTIME_ID,
-	CLOCK_THREAD_CPUTIME_ID
-};
-
-int testcases[] = {
-	EFAULT,			/* tp bad               */
-	EINVAL,			/* CLOCK_MONOTONIC      */
-	EINVAL,			/* MAX_CLOCKS           */
-	EINVAL,			/* MAX_CLOCKS + 1       */
-	EINVAL,			/* Invalid timespec     */
-	EINVAL,			/* NSEC_PER_SEC + 1     */
-	EPERM,			/* non-root user        */
-	EINVAL,			/* PROCESS_CPUTIME_ID	*/
-	EINVAL,			/* THREAD_CPUTIME_ID	*/
-};
-
-char *TCID = "clock_settime03";
-int TST_TOTAL = ARRAY_SIZE(testcases);
-
-char nobody_uid[] = "nobody";
-struct passwd *ltpuser;
-static struct timespec spec, *temp, saved;
-
-int main(int ac, char **av)
-{
-	int lc, i;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++) {
-
-			if (setup_test(i) < 0)
-				continue;
-
-			TEST(ltp_syscall(__NR_clock_settime, clocks[i], temp));
-
-			/* Change the UID back to root */
-			if (i == TST_TOTAL - 1) {
-				SAFE_SETEUID(cleanup, 0);
-			}
-
-			/* check return code */
-			if (TEST_RETURN == -1 && TEST_ERRNO == testcases[i]) {
-				tst_resm(TPASS | TTERRNO,
-					 "clock_settime(2) got expected "
-					 "failure.");
-			} else {
-				tst_resm(TFAIL | TTERRNO,
-					 "clock_settime(2) failed to produce "
-					 "expected error (return code = %ld)",
-					 TEST_RETURN);
-				/* Restore the clock to its previous state. */
-				if (TEST_RETURN == 0) {
-					if (ltp_syscall(__NR_clock_settime,
-						    CLOCK_REALTIME,
-						    &saved) < 0) {
-						tst_resm(TWARN | TERRNO,
-							 "FATAL: could not set "
-							 "the clock!");
-					}
-				}
-			}
-
-		}
-
-	}
-
-	cleanup();
-	tst_exit();
-}
-
-static int setup_test(int option)
-{
-	/* valid timespec */
-	spec = saved;
-	temp = &spec;
-
-	/* error sceanrios */
-	switch (option) {
-	case 0:
-		/* Make tp argument bad pointer */
-		temp = (struct timespec *)-1;
-		break;
-	case 4:
-		/* Make the parameter of timespec invalid */
-		spec.tv_nsec = -1;
-		break;
-	case 5:
-		/* Make the parameter of timespec invalid */
-		spec.tv_nsec = NSEC_PER_SEC + 1;
-		break;
-	case 6:
-		/* change the User to non-root */
-		spec.tv_nsec = 0;
-		if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
-			tst_resm(TWARN, "user \"nobody\" not present; "
-				 "skipping test");
-			return -1;
-		}
-		if (seteuid(ltpuser->pw_uid) == -1) {
-			tst_resm(TWARN | TERRNO,
-				 "seteuid failed to set the effective "
-				 "uid to %d (nobody)", ltpuser->pw_uid);
-			return -1;
-		}
-		break;
-	}
-	return 0;
-}
-
-static void setup(void)
-{
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	tst_require_root();
-
-	if (ltp_syscall(__NR_clock_gettime, CLOCK_REALTIME, &saved) < 0)
-		tst_brkm(TBROK, NULL, "Clock gettime failed");
-
-	spec.tv_sec = 1;
-	spec.tv_nsec = 0;
-
-	TEST_PAUSE;
-}
-
-static void cleanup(void)
-{
-}
-- 
2.20.0.rc1


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

* [LTP] [PATCH v3 5/6] syscalls/clock_settime: create syscall clock_settime tests
  2018-12-12 20:37         ` [LTP] [PATCH v3 5/6] syscalls/clock_settime: create syscall clock_settime tests Rafael David Tinoco
@ 2018-12-12 20:46           ` Rafael David Tinoco
  2019-01-24 16:11           ` Cyril Hrubis
  1 sibling, 0 replies; 37+ messages in thread
From: Rafael David Tinoco @ 2018-12-12 20:46 UTC (permalink / raw)
  To: ltp

On 12/12/18 6:37 PM, Rafael David Tinoco wrote:
> Fixes: 343
> 
> clock_settime01 creates a new test, using new API, based on existing
> and older kernel/timers/clock_settime02 test. clock_settime02 creates
> another test based on older kernel/timers/clock_settime03 test. Both
> will be deleted in the next commits.
> 
> Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
> ---
>  runtest/syscalls                              |   3 +
>  .../kernel/syscalls/clock_settime/.gitignore  |   2 +
>  .../kernel/syscalls/clock_settime/Makefile    |   8 +
>  .../syscalls/clock_settime/clock_settime01.c  |  75 +++++++++
>  .../syscalls/clock_settime/clock_settime02.c  | 150 ++++++++++++++++++
>  5 files changed, 238 insertions(+)
>  create mode 100644 testcases/kernel/syscalls/clock_settime/.gitignore
>  create mode 100644 testcases/kernel/syscalls/clock_settime/Makefile
>  create mode 100644 testcases/kernel/syscalls/clock_settime/clock_settime01.c
>  create mode 100644 testcases/kernel/syscalls/clock_settime/clock_settime02.c
> 
> diff --git a/runtest/syscalls b/runtest/syscalls
> index 34b47f36b..dd03ad31a 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -79,6 +79,9 @@ clock_nanosleep01 clock_nanosleep01
>  clock_nanosleep02 clock_nanosleep02
>  clock_nanosleep2_01 clock_nanosleep2_01
>  
> +clock_settime01 clock_settime01
> +clock_settime02 clock_settime02
> +
>  clone01 clone01
>  clone02 clone02
>  clone03 clone03
> diff --git a/testcases/kernel/syscalls/clock_settime/.gitignore b/testcases/kernel/syscalls/clock_settime/.gitignore
> new file mode 100644
> index 000000000..281217550
> --- /dev/null
> +++ b/testcases/kernel/syscalls/clock_settime/.gitignore
> @@ -0,0 +1,2 @@
> +clock_settime01
> +clock_settime02
> diff --git a/testcases/kernel/syscalls/clock_settime/Makefile b/testcases/kernel/syscalls/clock_settime/Makefile
> new file mode 100644
> index 000000000..e6674a6b2
> --- /dev/null
> +++ b/testcases/kernel/syscalls/clock_settime/Makefile
> @@ -0,0 +1,8 @@
> +# Copyright (c) 2018 - Linaro Limited. All rights reserved.
> +# SPDX-License-Identifier: GPL-2.0-or-later
> +
> +top_srcdir		?= ../../../..
> +
> +include $(top_srcdir)/include/mk/testcases.mk
> +
> +include $(top_srcdir)/include/mk/generic_leaf_target.mk
> diff --git a/testcases/kernel/syscalls/clock_settime/clock_settime01.c b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
> new file mode 100644
> index 000000000..6e0ee47c4
> --- /dev/null
> +++ b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
> @@ -0,0 +1,75 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2018 Linaro Limited. All rights reserved.
> + * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
> + */
> +
> +/*
> + * Basic test for clock_settime(2) on REALTIME clock:
> + *
> + *      1) advance DELTA_SEC seconds
> + *      2) go backwards DELTA_SEC seconds
> + *
> + * Restore wall clock at the end of test.
> + */
> +
> +#include "config.h"
> +#include "tst_timer.h"
> +#include "tst_safe_clocks.h"
> +#include "tst_test.h"
> +#include "lapi/syscalls.h"
> +
> +#define DELTA_SEC 10
> +#define DELTA_SEC_US (long long) (DELTA_SEC * 1000000)
> +#define DELTA_SEC_VAR_POS (long long) (DELTA_SEC_US * 1.10)
> +#define DELTA_SEC_VAR_NEG (long long) (DELTA_SEC_US * 0.90)
> +
> +static void verify_clock_settime(void)
> +{
> +	long long elapsed;
> +	struct timespec begin, change, end;
> +
> +	/* test 01: move forward */
> +
> +	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &begin);
> +
> +	change = tst_timespec_add_us(begin, DELTA_SEC_US);
> +
> +	if (clock_settime(CLOCK_REALTIME, &change) != 0)
> +		tst_brk(TBROK | TTERRNO, "could not set realtime change");
> +
> +	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &end);
> +
> +	elapsed = tst_timespec_diff_us(end, begin);
> +
> +	if (elapsed > DELTA_SEC_US && elapsed < DELTA_SEC_VAR_POS) {
> +		tst_res(TPASS, "clock_settime(2): was able to advance time");
> +	} else {
> +		tst_res(TFAIL, "clock_settime(2): could not advance time");
> +	}
> +
> +	/* test 02: move backward */
> +
> +	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &begin);
> +
> +	change = tst_timespec_rem_us(begin, DELTA_SEC_US);
> +
> +	if (clock_settime(CLOCK_REALTIME, &change) != 0)
> +		tst_brk(TBROK | TTERRNO, "could not set realtime change");
> +
> +	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &end);
> +
> +	elapsed = tst_timespec_diff_us(end, begin);
> +
> +	if (~(elapsed) > DELTA_SEC_VAR_NEG) {
> +		tst_res(TPASS, "clock_settime(2): was able to recede time");
> +	} else {
> +		tst_res(TFAIL, "clock_settime(2): could not recede time");
> +	}
> +}
> +
> +static struct tst_test test = {
> +	.test_all = verify_clock_settime,
> +	.needs_root = 1,
> +	.restore_wallclock = 1,
> +};
> diff --git a/testcases/kernel/syscalls/clock_settime/clock_settime02.c b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
> new file mode 100644
> index 000000000..25fcbfe09
> --- /dev/null
> +++ b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
> @@ -0,0 +1,150 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2018 Linaro Limited. All rights reserved.
> + * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
> + */
> +
> +/*
> + * Basic tests for errors of clock_settime(2) on different clock types.
> + */
> +
> +#include "config.h"
> +#include "tst_test.h"
> +#include "lapi/syscalls.h"
> +#include "tst_timer.h"
> +#include "tst_safe_clocks.h"
> +
> +#define DELTA_SEC 10
> +#define NSEC_PER_SEC (1000000000L)
> +#define MAX_CLOCKS 16
> +
> +struct test_case {
> +	clockid_t type;
> +	struct timespec newtime;
> +	int exp_err;
> +	int replace;
> +};

Cyril, I haven't done a global timespec, having a pointer inside the
test_case structure, just because there are a few other tests, like when
having sec to -1, nsec to -1, etc.. and I wouldn't be able to set those
during compilation time.. I hope you don't mind, test is functional and
working good.

-- 
Rafael D. Tinoco
Linaro - Kernel Validation

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

* [LTP] [PATCH v3 6/6] timers/clock_settime: remove clock_settime tests
  2018-12-12 20:37         ` [LTP] [PATCH v3 6/6] timers/clock_settime: remove clock_settime tests Rafael David Tinoco
@ 2019-01-08 12:04           ` Rafael David Tinoco
  2019-01-08 12:42             ` Cyril Hrubis
  2019-01-24 12:58             ` Rafael David Tinoco
  0 siblings, 2 replies; 37+ messages in thread
From: Rafael David Tinoco @ 2019-01-08 12:04 UTC (permalink / raw)
  To: ltp

Ping ? =o)

> On 12 Dec 2018, at 18:37, Rafael David Tinoco <rafael.tinoco@linaro.org> wrote:
> 
> Fixes: 343
> 
> clock_settime01 creates a new test, using new API, based on existing and
> older kernel/timers/clock_settime tests. It includes tests from files
> clock_settime02 and clock_settime03.
> 
> This commit deletes timers/clock_settime/* tests.
> 
> Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>


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

* [LTP] [PATCH v3 6/6] timers/clock_settime: remove clock_settime tests
  2019-01-08 12:04           ` Rafael David Tinoco
@ 2019-01-08 12:42             ` Cyril Hrubis
  2019-01-08 12:46               ` Rafael David Tinoco
  2019-01-24 12:58             ` Rafael David Tinoco
  1 sibling, 1 reply; 37+ messages in thread
From: Cyril Hrubis @ 2019-01-08 12:42 UTC (permalink / raw)
  To: ltp

Hi!
> Ping ? =o)

We are in the middle of the pre-release freeze, I will get to you once
the release is finalized.

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v3 6/6] timers/clock_settime: remove clock_settime tests
  2019-01-08 12:42             ` Cyril Hrubis
@ 2019-01-08 12:46               ` Rafael David Tinoco
  0 siblings, 0 replies; 37+ messages in thread
From: Rafael David Tinoco @ 2019-01-08 12:46 UTC (permalink / raw)
  To: ltp

Cyril Hrubis <chrubis@suse.cz> wrote:

> Hi!
>> Ping ? =o)
> 
> We are in the middle of the pre-release freeze, I will get to you once
> the release is finalized.

Alright. Thanks! I wasn’t sure because of EOY!

Good luck! o/


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

* [LTP] [PATCH v3 6/6] timers/clock_settime: remove clock_settime tests
  2019-01-08 12:04           ` Rafael David Tinoco
  2019-01-08 12:42             ` Cyril Hrubis
@ 2019-01-24 12:58             ` Rafael David Tinoco
  2019-01-24 13:18               ` Cyril Hrubis
  1 sibling, 1 reply; 37+ messages in thread
From: Rafael David Tinoco @ 2019-01-24 12:58 UTC (permalink / raw)
  To: ltp

I know you guys are catching up after the freeze, just making sure you don’t
loose track of these 6 patches when you have time!

Thanks

Rafael

> On 8 Jan 2019, at 10:04, Rafael David Tinoco
> <rafael.tinoco@linaro.org> wrote:
> 
> Ping ? =o)
> 
>> On 12 Dec 2018, at 18:37, Rafael David Tinoco
>> <rafael.tinoco@linaro.org> wrote:
>> 
>> Fixes: 343
>> 
>> clock_settime01 creates a new test, using new API, based on existing
>> and older kernel/timers/clock_settime tests. It includes tests from
>> files clock_settime02 and clock_settime03.
>> 
>> This commit deletes timers/clock_settime/* tests.
>> 
>> Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>


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

* [LTP] [PATCH v3 6/6] timers/clock_settime: remove clock_settime tests
  2019-01-24 12:58             ` Rafael David Tinoco
@ 2019-01-24 13:18               ` Cyril Hrubis
  0 siblings, 0 replies; 37+ messages in thread
From: Cyril Hrubis @ 2019-01-24 13:18 UTC (permalink / raw)
  To: ltp

Hi!
> I know you guys are catching up after the freeze, just making sure you don???t
> loose track of these 6 patches when you have time!

Thanks for the ping :-).

I've just finished LTP GSoC application and was about to get to the
patch review.

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v3 1/6] tst_timer.h: add tst_timespect_rem_us() function
  2018-12-12 20:37       ` [LTP] [PATCH v3 1/6] tst_timer.h: add tst_timespect_rem_us() function Rafael David Tinoco
                           ` (4 preceding siblings ...)
  2018-12-12 20:37         ` [LTP] [PATCH v3 6/6] timers/clock_settime: remove clock_settime tests Rafael David Tinoco
@ 2019-01-24 15:06         ` Cyril Hrubis
  5 siblings, 0 replies; 37+ messages in thread
From: Cyril Hrubis @ 2019-01-24 15:06 UTC (permalink / raw)
  To: ltp

Hi!
> Expand tst_timer.h functionality by having a function to also remove
> given microseconds from a given timespec.
> 
> Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
> ---
>  include/tst_timer.h | 17 +++++++++++++++++
>  1 file changed, 17 insertions(+)
> 
> diff --git a/include/tst_timer.h b/include/tst_timer.h
> index 577bc88ef..b1c9ceeba 100644
> --- a/include/tst_timer.h
> +++ b/include/tst_timer.h
> @@ -151,6 +151,23 @@ static inline struct timespec tst_timespec_add_us(struct timespec t,
>  	return t;
>  }
>  
> +/*
> + * Removes us microseconds to t.
> + */
> +static inline struct timespec tst_timespec_rem_us(struct timespec t,
> +                                                  long long us)
                                               ^
					       This is very minor but
					       better name would be sub
					       as in substract

> +{
> +	t.tv_sec -= us / 1000000;
> +	t.tv_nsec -= (us % 1000000) * 1000;
> +
> +	if (t.tv_nsec < 0) {
> +		t.tv_sec--;
> +		t.tv_nsec += 1000000000;
> +	}
> +
> +	return t;
> +}
> +
>  /*
>   * Returns difference between two timespec structures.
>   */

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v3 4/6] lib: new restore_wallclock field to restore realtime clock
  2018-12-12 20:37         ` [LTP] [PATCH v3 4/6] lib: new restore_wallclock field to restore realtime clock Rafael David Tinoco
@ 2019-01-24 15:12           ` Cyril Hrubis
  0 siblings, 0 replies; 37+ messages in thread
From: Cyril Hrubis @ 2019-01-24 15:12 UTC (permalink / raw)
  To: ltp

Hi!
> Some tests around clocks need to restore the correct date and time after
> the tests, including possible iterations, run.
> 
> This commit introduces a new field to tst_test called
> "restore_wallclock", which makes the test to save current realtime clock
> during setup phase, and, later, during cleanup, restore it to the
> appropriate time using a monotonic raw clock difference.
> 
> Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
> ---
>  include/tst_test.h  |  1 +
>  lib/tst_test.c      |  6 ++++++
>  lib/tst_wallclock.c | 44 ++++++++++++++++++++++++++++++++++++++++++++

We need to add include/tst_wallclock.h for the function prototypes which
should be then included in the tst_test.c.

And we should definitelly add a paragraph about this functionality into
the doc/test-writing-guidelines.txt.

>  3 files changed, 51 insertions(+)
>  create mode 100644 lib/tst_wallclock.c
> 
> diff --git a/include/tst_test.h b/include/tst_test.h
> index 2ebf746eb..170bddc21 100644
> --- a/include/tst_test.h
> +++ b/include/tst_test.h
> @@ -131,6 +131,7 @@ struct tst_test {
>  	int needs_rofs:1;
>  	int child_needs_reinit:1;
>  	int needs_devfs:1;
> +	int restore_wallclock:1;
>  	/*
>  	 * If set the test function will be executed for all available
>  	 * filesystems and the current filesytem type would be set in the
> diff --git a/lib/tst_test.c b/lib/tst_test.c
> index 661fbbfce..aa3d674f0 100644
> --- a/lib/tst_test.c
> +++ b/lib/tst_test.c
> @@ -868,6 +868,9 @@ static void do_setup(int argc, char *argv[])
>  
>  	if (tst_test->resource_files)
>  		copy_resources();
> +
> +	if (tst_test->restore_wallclock)
> +		tst_wallclock_save();
>  }
>  
>  static void do_test_setup(void)
> @@ -899,6 +902,9 @@ static void do_cleanup(void)
>  		tst_sys_conf_restore(0);
>  
>  	cleanup_ipc();
> +
> +	if (tst_test->restore_wallclock)
> +		tst_wallclock_restore();
>  }
>  
>  static void run_tests(void)
> diff --git a/lib/tst_wallclock.c b/lib/tst_wallclock.c
> new file mode 100644
> index 000000000..ef08e1dba
> --- /dev/null
> +++ b/lib/tst_wallclock.c
> @@ -0,0 +1,44 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2018 Linaro Limited. All rights reserved.
> + * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
> + */
> +
> +#include <errno.h>
> +
> +#define TST_NO_DEFAULT_MAIN
> +
> +#include "tst_test.h"
> +#include "tst_timer.h"
> +#include "tst_clocks.h"
> +#include "lapi/posix_clocks.h"
> +
> +static struct timespec real_begin, mono_begin;
> +
> +void tst_wallclock_save(void)
> +{
> +	/* save initial monotonic time to restore it when needed */
> +
> +	if (tst_clock_gettime(CLOCK_REALTIME, &real_begin))
> +		tst_brk(TBROK | TERRNO, "tst_clock_gettime() realtime failed");
> +
> +	if (tst_clock_gettime(CLOCK_MONOTONIC_RAW, &mono_begin))
> +		tst_brk(TBROK | TERRNO, "tst_clock_gettime() monotonic failed");
> +}
> +
> +void tst_wallclock_restore(void)
> +{
> +	static struct timespec mono_end, elapsed, adjust;
> +
> +	if (tst_clock_gettime(CLOCK_MONOTONIC_RAW, &mono_end))
> +		tst_brk(TBROK | TERRNO, "tst_clock_gettime() monotonic failed");
> +
> +	elapsed = tst_timespec_diff(mono_end, mono_begin);
> +
> +	adjust = tst_timespec_add_us(real_begin, tst_timespec_to_us(elapsed));

It may be a bit cleaner to add tst_timespec_add() so that we do not have
to convert the value between ns and us back and forth, but that is very
minor as well.

> +	/* restore realtime clock based on monotonic delta */
> +
> +	if (tst_clock_settime(CLOCK_REALTIME, &adjust))
> +		tst_brk(TBROK | TERRNO, "tst_clock_settime() realtime failed");
> +}
> -- 
> 2.20.0.rc1
> 

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v3 5/6] syscalls/clock_settime: create syscall clock_settime tests
  2018-12-12 20:37         ` [LTP] [PATCH v3 5/6] syscalls/clock_settime: create syscall clock_settime tests Rafael David Tinoco
  2018-12-12 20:46           ` Rafael David Tinoco
@ 2019-01-24 16:11           ` Cyril Hrubis
  2019-01-29 17:36             ` [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h Rafael David Tinoco
  1 sibling, 1 reply; 37+ messages in thread
From: Cyril Hrubis @ 2019-01-24 16:11 UTC (permalink / raw)
  To: ltp

Hi!
> +#include "config.h"
> +#include "tst_timer.h"
> +#include "tst_safe_clocks.h"
> +#include "tst_test.h"
> +#include "lapi/syscalls.h"
> +
> +#define DELTA_SEC 10
> +#define DELTA_SEC_US (long long) (DELTA_SEC * 1000000)
             ^
	     Maybe just DELTA_US having both SEC and US in the name is
	     confusing.

> +#define DELTA_SEC_VAR_POS (long long) (DELTA_SEC_US * 1.10)
> +#define DELTA_SEC_VAR_NEG (long long) (DELTA_SEC_US * 0.90)

It would be probably more clear to just define epsilon here as

#define DELTA_EPS (DELTA_US * 0.1)

Then use it as DELTA_US + DELTA_EPS and DELTA_US - DELTA_EPS

> +static void verify_clock_settime(void)
> +{
> +	long long elapsed;
> +	struct timespec begin, change, end;
> +
> +	/* test 01: move forward */
> +
> +	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &begin);
> +
> +	change = tst_timespec_add_us(begin, DELTA_SEC_US);
> +
> +	if (clock_settime(CLOCK_REALTIME, &change) != 0)
> +		tst_brk(TBROK | TTERRNO, "could not set realtime change");
> +
> +	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &end);
> +
> +	elapsed = tst_timespec_diff_us(end, begin);
> +
> +	if (elapsed > DELTA_SEC_US && elapsed < DELTA_SEC_VAR_POS) {
> +		tst_res(TPASS, "clock_settime(2): was able to advance time");
> +	} else {
> +		tst_res(TFAIL, "clock_settime(2): could not advance time");
> +	}

The curly braces around these one line statements are useless here.

> +	/* test 02: move backward */
> +
> +	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &begin);
> +
> +	change = tst_timespec_rem_us(begin, DELTA_SEC_US);
> +
> +	if (clock_settime(CLOCK_REALTIME, &change) != 0)
> +		tst_brk(TBROK | TTERRNO, "could not set realtime change");
> +
> +	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &end);
> +
> +	elapsed = tst_timespec_diff_us(end, begin);
> +
> +	if (~(elapsed) > DELTA_SEC_VAR_NEG) {

We still does not check that the time wasn't set too much into history
so this should be adjusted just like the previous condition to:

	if (~(elapsed) < DELTA_US && ~(elapsed) > DELTA_US - DELTA_EPS)

> +		tst_res(TPASS, "clock_settime(2): was able to recede time");
> +	} else {
> +		tst_res(TFAIL, "clock_settime(2): could not recede time");
> +	}

Again useless curly braces.

> +}
> +
> +static struct tst_test test = {
> +	.test_all = verify_clock_settime,
> +	.needs_root = 1,
> +	.restore_wallclock = 1,
> +};
> diff --git a/testcases/kernel/syscalls/clock_settime/clock_settime02.c b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
> new file mode 100644
> index 000000000..25fcbfe09
> --- /dev/null
> +++ b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
> @@ -0,0 +1,150 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2018 Linaro Limited. All rights reserved.
> + * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
> + */
> +
> +/*
> + * Basic tests for errors of clock_settime(2) on different clock types.
> + */
> +
> +#include "config.h"
> +#include "tst_test.h"
> +#include "lapi/syscalls.h"
> +#include "tst_timer.h"
> +#include "tst_safe_clocks.h"
> +
> +#define DELTA_SEC 10
> +#define NSEC_PER_SEC (1000000000L)
> +#define MAX_CLOCKS 16
> +
> +struct test_case {
> +	clockid_t type;
> +	struct timespec newtime;
> +	int exp_err;
> +	int replace;
> +};
> +
> +struct test_case tc[] = {
> +	{				/* case 01: REALTIME: timespec NULL   */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_sec = -2,
> +	 .exp_err = EFAULT,
> +	 .replace = 1,
> +	 },
> +	{				/* case 02: REALTIME: tv_sec = -1     */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_sec = -1,
> +	 .exp_err = EINVAL,
> +	 .replace = 1,
> +	 },
> +	{				/* case 03: REALTIME: tv_nsec = -1    */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_nsec = -1,
> +	 .exp_err = EINVAL,
> +	 .replace = 1,
> +	 },
> +	{				/* case 04: REALTIME: tv_nsec = 1s+1  */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_nsec = NSEC_PER_SEC + 1,
> +	 .exp_err = EINVAL,
> +	 .replace = 1,
> +	 },
> +	{				/* case 05: MONOTONIC		      */
> +	 .type = CLOCK_MONOTONIC,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 06: MAXCLOCK		      */
> +	 .type = MAX_CLOCKS,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 07: MAXCLOCK+1		      */
> +	 .type = MAX_CLOCKS + 1,
> +	 .exp_err = EINVAL,
> +	 },
> +	/* Linux specific */
> +	{				/* case 08: CLOCK_MONOTONIC_COARSE    */
> +	 .type = CLOCK_MONOTONIC_COARSE,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 09: CLOCK_MONOTONIC_RAW       */
> +	 .type = CLOCK_MONOTONIC_RAW,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 10: CLOCK_BOOTTIME	      */
> +	 .type = CLOCK_BOOTTIME,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 11: CLOCK_PROCESS_CPUTIME_ID  */
> +	 .type = CLOCK_PROCESS_CPUTIME_ID,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 12: CLOCK_THREAD_CPUTIME_ID   */
> +	 .type = CLOCK_THREAD_CPUTIME_ID,
> +	 .exp_err = EINVAL,
> +	 },
> +};
> +
> +/*
> + * Some tests may cause libc to segfault when passing bad arguments.
> + */
> +static int sys_clock_settime(clockid_t clk_id, struct timespec *tp)
> +{
> +	return tst_syscall(__NR_clock_settime, clk_id, tp);
> +}
> +
> +static void verify_clock_settime(unsigned int i)
> +{
> +	struct timespec saved, spec, *specptr;


I would have initialized the specptr = &spec here and only replaced it
down there if we are hitting the EFAULT case.

> +	if (tc[i].replace == 0) {
> +
> +		SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &saved);
> +
> +		/* add 1 sec to test clock */
> +		specptr = &spec;
> +		specptr->tv_sec = saved.tv_sec + 1;
> +		specptr->tv_nsec = saved.tv_nsec;

And we don't need the saved here anymore, so we can really do
clock_gettime() on the specptr here, right?

> +	} else {
> +
> +
> +		if (tc[i].newtime.tv_sec == -2) {
> +
> +			/* bad pointer case */
> +			specptr = tst_get_bad_addr(NULL);

Maybe we can just look for EFAULT errno instead of having magic tv_sec
value.

> +		} else {
> +
> +			/* use given values */
> +			specptr = &spec;
> +			specptr->tv_sec = tc[i].newtime.tv_sec;
> +			specptr->tv_nsec = tc[i].newtime.tv_nsec;

You can do simple spec = tc[i].newtime here.

> +		}
> +	}



> +	TEST(sys_clock_settime(tc[i].type, specptr));
> +
> +	if (TST_RET == -1) {
> +
> +		if (tc[i].exp_err == TST_ERR) {
> +
> +			tst_res(TPASS, "clock_settime(2): failed as expected");
> +
> +		} else {
> +			tst_res(TFAIL | TTERRNO, "clock_settime(2): "
> +					"failed with different error");

I would be better to print the expected errno here as well something as:

	tst_res(TFAIL | TTERRNO, "clock_settime(%s, ...) expected %s",
	        clock_name(tc[i].type), tst_strerrno(tc[i].exp_err));
> +		}
> +
> +		return;
> +	}
> +
> +	tst_res(TFAIL | TTERRNO, "clock_settime(2): clock type %d failed",
> +			tc[i].type);

Shouldn't this say:

	tst_res(TFAIL, "clock_settime(%s, ...) passed unexpectedly, expected %s",
	        clock_name(tc[i].type), tst_strerrno(tc[i].exp_err));

> +}
> +
> +static struct tst_test test = {
> +	.test = verify_clock_settime,
> +	.tcnt = ARRAY_SIZE(tc),
> +	.needs_root = 1,
> +	.restore_wallclock = 1,
> +};
> -- 
> 2.20.0.rc1
> 

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h
  2019-01-24 16:11           ` Cyril Hrubis
@ 2019-01-29 17:36             ` Rafael David Tinoco
  2019-01-29 17:36               ` [LTP] [PATCH v4 2/8] lib: include SAFE_CLOCK_SETTIME() macro Rafael David Tinoco
                                 ` (7 more replies)
  0 siblings, 8 replies; 37+ messages in thread
From: Rafael David Tinoco @ 2019-01-29 17:36 UTC (permalink / raw)
  To: ltp

Adds tst_clock_settime() function to the lib.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 include/tst_clocks.h | 2 ++
 lib/tst_clocks.c     | 5 +++++
 2 files changed, 7 insertions(+)

diff --git a/include/tst_clocks.h b/include/tst_clocks.h
index ee2f645c7..90784a3fd 100644
--- a/include/tst_clocks.h
+++ b/include/tst_clocks.h
@@ -26,4 +26,6 @@ int tst_clock_getres(clockid_t clk_id, struct timespec *res);
 
 int tst_clock_gettime(clockid_t clk_id, struct timespec *ts);
 
+int tst_clock_settime(clockid_t clk_id, struct timespec *ts);
+
 #endif /* TST_CLOCKS__ */
diff --git a/lib/tst_clocks.c b/lib/tst_clocks.c
index 87413a339..35798a4aa 100644
--- a/lib/tst_clocks.c
+++ b/lib/tst_clocks.c
@@ -35,3 +35,8 @@ int tst_clock_gettime(clockid_t clk_id, struct timespec *ts)
 {
 	return syscall(SYS_clock_gettime, clk_id, ts);
 }
+
+int tst_clock_settime(clockid_t clk_id, struct timespec *ts)
+{
+	return syscall(SYS_clock_settime, clk_id, ts);
+}
-- 
2.20.1


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

* [LTP] [PATCH v4 2/8] lib: include SAFE_CLOCK_SETTIME() macro
  2019-01-29 17:36             ` [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h Rafael David Tinoco
@ 2019-01-29 17:36               ` Rafael David Tinoco
  2019-01-29 17:36               ` [LTP] [PATCH v4 3/8] tst_timer: Add tst_timespec_add() Rafael David Tinoco
                                 ` (6 subsequent siblings)
  7 siblings, 0 replies; 37+ messages in thread
From: Rafael David Tinoco @ 2019-01-29 17:36 UTC (permalink / raw)
  To: ltp

Adds SAFE_CLOCK_SETTIME() macro to tst_safe_clocks.h.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 include/tst_safe_clocks.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/include/tst_safe_clocks.h b/include/tst_safe_clocks.h
index 1b4d48543..553f8464a 100644
--- a/include/tst_safe_clocks.h
+++ b/include/tst_safe_clocks.h
@@ -30,9 +30,23 @@ static inline void safe_clock_gettime(const char *file, const int lineno,
 			"%s:%d clock_gettime() failed", file, lineno);
 }
 
+
+static inline void safe_clock_settime(const char *file, const int lineno,
+	clockid_t clk_id, struct timespec *tp)
+{
+	int rval;
+
+	rval = clock_settime(clk_id, tp);
+	if (rval != 0)
+		tst_brk(TBROK | TERRNO,
+			"%s:%d clock_gettime() failed", file, lineno);
+}
+
 #define SAFE_CLOCK_GETRES(clk_id, res)\
 	safe_clock_getres(__FILE__, __LINE__, (clk_id), (res))
 
 #define SAFE_CLOCK_GETTIME(clk_id, tp)\
 	safe_clock_gettime(__FILE__, __LINE__, (clk_id), (tp))
 
+#define SAFE_CLOCK_SETTIME(clk_id, tp)\
+	safe_clock_settime(__FILE__, __LINE__, (clk_id), (tp))
-- 
2.20.1


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

* [LTP] [PATCH v4 3/8] tst_timer: Add tst_timespec_add()
  2019-01-29 17:36             ` [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h Rafael David Tinoco
  2019-01-29 17:36               ` [LTP] [PATCH v4 2/8] lib: include SAFE_CLOCK_SETTIME() macro Rafael David Tinoco
@ 2019-01-29 17:36               ` Rafael David Tinoco
  2019-01-29 17:36               ` [LTP] [PATCH v4 4/8] lib: new restore_wallclock field to restore realtime clock Rafael David Tinoco
                                 ` (5 subsequent siblings)
  7 siblings, 0 replies; 37+ messages in thread
From: Rafael David Tinoco @ 2019-01-29 17:36 UTC (permalink / raw)
  To: ltp

This commit adds a tst_timespec_add() function that adds two given
timespec structs. It is needed in order to avoid unneeded ns <-> us
conversions because, so far, there is only tst_timespec_add_us()
available.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 include/tst_timer.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/include/tst_timer.h b/include/tst_timer.h
index 577bc88ef..b57adf7aa 100644
--- a/include/tst_timer.h
+++ b/include/tst_timer.h
@@ -151,6 +151,25 @@ static inline struct timespec tst_timespec_add_us(struct timespec t,
 	return t;
 }
 
+/*
+ * Adds two timespec structures.
+ */
+static inline struct timespec tst_timespec_add(struct timespec t1,
+                                               struct timespec t2)
+{
+	struct timespec res;
+
+	res.tv_sec = t1.tv_sec + t2.tv_sec;
+	res.tv_nsec = t1.tv_nsec + t2.tv_nsec;
+
+	if (res.tv_nsec >= 1000000000) {
+		res.tv_sec++;
+		res.tv_nsec -= 1000000000;
+	}
+
+	return res;
+}
+
 /*
  * Returns difference between two timespec structures.
  */
-- 
2.20.1


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

* [LTP] [PATCH v4 4/8] lib: new restore_wallclock field to restore realtime clock
  2019-01-29 17:36             ` [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h Rafael David Tinoco
  2019-01-29 17:36               ` [LTP] [PATCH v4 2/8] lib: include SAFE_CLOCK_SETTIME() macro Rafael David Tinoco
  2019-01-29 17:36               ` [LTP] [PATCH v4 3/8] tst_timer: Add tst_timespec_add() Rafael David Tinoco
@ 2019-01-29 17:36               ` Rafael David Tinoco
  2019-01-30 13:53                 ` Cyril Hrubis
  2019-01-29 17:36               ` [LTP] [PATCH v4 5/8] tst_timer: Add tst_timespec_sub_us() Rafael David Tinoco
                                 ` (4 subsequent siblings)
  7 siblings, 1 reply; 37+ messages in thread
From: Rafael David Tinoco @ 2019-01-29 17:36 UTC (permalink / raw)
  To: ltp

Some tests that change the system-wide clock need to have a way to
restore the correct time after their execution.

This commit introduces a new field to tst_test struct called
"restore_wallclock": it makes the test to save current realtime clock
during setup phase, and, later, during cleanup, restore it to the
appropriate time using a monotonic raw clock difference.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 doc/test-writing-guidelines.txt | 35 +++++++++++++++++++++++++
 include/tst_test.h              |  1 +
 include/tst_wallclock.h         | 15 +++++++++++
 lib/tst_test.c                  |  7 +++++
 lib/tst_wallclock.c             | 45 +++++++++++++++++++++++++++++++++
 5 files changed, 103 insertions(+)
 create mode 100644 include/tst_wallclock.h
 create mode 100644 lib/tst_wallclock.c

diff --git a/doc/test-writing-guidelines.txt b/doc/test-writing-guidelines.txt
index 731be7692..f2f72c4d6 100644
--- a/doc/test-writing-guidelines.txt
+++ b/doc/test-writing-guidelines.txt
@@ -1539,6 +1539,41 @@ static struct tst_test test = {
 };
 -------------------------------------------------------------------------------
 
+2.2.29 Changing the Wall Clock Time during test execution
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+There are some tests that, for different reasons, might need to change the
+system-wide clock time. Whenever this happens, it is imperative that the clock
+is restored, at the end of test's execution, taking in consideration the amount
+of time elapsed during that test.
+
+In order for that to happen, struct tst_test has a variable called
+"restore_wallclock" that should be set to "1" so LTP knows it should: (1)
+initialize a monotonic clock during test setup phase and (2) use that monotonic
+clock to fix the system-wide clock time at the test cleanup phase.
+
+[source,c]
+-------------------------------------------------------------------------------
+#include "tst_test.h"
+
+static void setup(void)
+{
+	...
+}
+
+static void run(void)
+{
+	...
+}
+
+sturct tst_test test = {
+	...
+	.setup = setup,
+	.test_all = run,
+	.restore_wallclock = 1,
+	...
+};
+-------------------------------------------------------------------------------
 
 2.3 Writing a testcase in shell
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/include/tst_test.h b/include/tst_test.h
index b37890959..12dda2e79 100644
--- a/include/tst_test.h
+++ b/include/tst_test.h
@@ -135,6 +135,7 @@ struct tst_test {
 	int needs_rofs:1;
 	int child_needs_reinit:1;
 	int needs_devfs:1;
+	int restore_wallclock:1;
 	/*
 	 * If set the test function will be executed for all available
 	 * filesystems and the current filesytem type would be set in the
diff --git a/include/tst_wallclock.h b/include/tst_wallclock.h
new file mode 100644
index 000000000..7d6723a7a
--- /dev/null
+++ b/include/tst_wallclock.h
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+
+#ifndef TST_WALLCLK_H__
+#define TST_WALLCLK_H__
+
+void tst_wallclock_save(void);
+
+void tst_wallclock_restore(void);
+
+#endif	/* TST_WALLCLK_H__ */
diff --git a/lib/tst_test.c b/lib/tst_test.c
index da3e0c8a0..7dd890b8d 100644
--- a/lib/tst_test.c
+++ b/lib/tst_test.c
@@ -35,6 +35,7 @@
 #include "tst_timer_test.h"
 #include "tst_clocks.h"
 #include "tst_timer.h"
+#include "tst_wallclock.h"
 #include "tst_sys_conf.h"
 #include "tst_kconfig.h"
 
@@ -872,6 +873,9 @@ static void do_setup(int argc, char *argv[])
 
 	if (tst_test->resource_files)
 		copy_resources();
+
+	if (tst_test->restore_wallclock)
+		tst_wallclock_save();
 }
 
 static void do_test_setup(void)
@@ -903,6 +907,9 @@ static void do_cleanup(void)
 		tst_sys_conf_restore(0);
 
 	cleanup_ipc();
+
+	if (tst_test->restore_wallclock)
+		tst_wallclock_restore();
 }
 
 static void run_tests(void)
diff --git a/lib/tst_wallclock.c b/lib/tst_wallclock.c
new file mode 100644
index 000000000..1513882f4
--- /dev/null
+++ b/lib/tst_wallclock.c
@@ -0,0 +1,45 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+#include <errno.h>
+
+#define TST_NO_DEFAULT_MAIN
+
+#include "tst_test.h"
+#include "tst_timer.h"
+#include "tst_clocks.h"
+#include "tst_wallclock.h"
+#include "lapi/posix_clocks.h"
+
+static struct timespec real_begin, mono_begin;
+
+void tst_wallclock_save(void)
+{
+	/* save initial monotonic time to restore it when needed */
+
+	if (tst_clock_gettime(CLOCK_REALTIME, &real_begin))
+		tst_brk(TBROK | TERRNO, "tst_clock_gettime() realtime failed");
+
+	if (tst_clock_gettime(CLOCK_MONOTONIC_RAW, &mono_begin))
+		tst_brk(TBROK | TERRNO, "tst_clock_gettime() monotonic failed");
+}
+
+void tst_wallclock_restore(void)
+{
+	static struct timespec mono_end, elapsed, adjust;
+
+	if (tst_clock_gettime(CLOCK_MONOTONIC_RAW, &mono_end))
+		tst_brk(TBROK | TERRNO, "tst_clock_gettime() monotonic failed");
+
+	elapsed = tst_timespec_diff(mono_end, mono_begin);
+
+	adjust = tst_timespec_add(real_begin, elapsed);
+
+	/* restore realtime clock based on monotonic delta */
+
+	if (tst_clock_settime(CLOCK_REALTIME, &adjust))
+		tst_brk(TBROK | TERRNO, "tst_clock_settime() realtime failed");
+}
-- 
2.20.1


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

* [LTP] [PATCH v4 5/8] tst_timer: Add tst_timespec_sub_us()
  2019-01-29 17:36             ` [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h Rafael David Tinoco
                                 ` (2 preceding siblings ...)
  2019-01-29 17:36               ` [LTP] [PATCH v4 4/8] lib: new restore_wallclock field to restore realtime clock Rafael David Tinoco
@ 2019-01-29 17:36               ` Rafael David Tinoco
  2019-01-29 17:36               ` [LTP] [PATCH v4 6/8] tst_timer: Turn clock_name() function public Rafael David Tinoco
                                 ` (3 subsequent siblings)
  7 siblings, 0 replies; 37+ messages in thread
From: Rafael David Tinoco @ 2019-01-29 17:36 UTC (permalink / raw)
  To: ltp

This commit adds a tst_timespec_sub_us() function that subtracts
microseconds from a given timespec struct.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 include/tst_timer.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/include/tst_timer.h b/include/tst_timer.h
index b57adf7aa..043b71460 100644
--- a/include/tst_timer.h
+++ b/include/tst_timer.h
@@ -170,6 +170,23 @@ static inline struct timespec tst_timespec_add(struct timespec t1,
 	return res;
 }
 
+/*
+ * Subtracts us microseconds from t.
+ */
+static inline struct timespec tst_timespec_sub_us(struct timespec t,
+                                                  long long us)
+{
+	t.tv_sec -= us / 1000000;
+	t.tv_nsec -= (us % 1000000) * 1000;
+
+	if (t.tv_nsec < 0) {
+		t.tv_sec--;
+		t.tv_nsec += 1000000000;
+	}
+
+	return t;
+}
+
 /*
  * Returns difference between two timespec structures.
  */
-- 
2.20.1


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

* [LTP] [PATCH v4 6/8] tst_timer: Turn clock_name() function public
  2019-01-29 17:36             ` [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h Rafael David Tinoco
                                 ` (3 preceding siblings ...)
  2019-01-29 17:36               ` [LTP] [PATCH v4 5/8] tst_timer: Add tst_timespec_sub_us() Rafael David Tinoco
@ 2019-01-29 17:36               ` Rafael David Tinoco
  2019-01-30 13:54                 ` Cyril Hrubis
  2019-01-29 17:36               ` [LTP] [PATCH v4 7/8] syscalls/clock_settime: create syscall clock_settime tests Rafael David Tinoco
                                 ` (2 subsequent siblings)
  7 siblings, 1 reply; 37+ messages in thread
From: Rafael David Tinoco @ 2019-01-29 17:36 UTC (permalink / raw)
  To: ltp

This commit exposes clock_name() function by removing its static
definition and creating a public function prototype. This function is
needed by clock/alarm tests and their error messages.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 include/tst_timer.h | 5 +++++
 lib/tst_timer.c     | 2 +-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/include/tst_timer.h b/include/tst_timer.h
index 043b71460..c66f630c4 100644
--- a/include/tst_timer.h
+++ b/include/tst_timer.h
@@ -332,4 +332,9 @@ static inline long long tst_timer_elapsed_us(void)
 	return tst_timespec_to_us(tst_timer_elapsed());
 }
 
+/*
+ * Returns a string containing given clock type name
+ */
+const char *clock_name(clockid_t);
+
 #endif /* TST_TIMER */
diff --git a/lib/tst_timer.c b/lib/tst_timer.c
index dffaba0cb..e83da7ff6 100644
--- a/lib/tst_timer.c
+++ b/lib/tst_timer.c
@@ -15,7 +15,7 @@
 static struct timespec start_time, stop_time;
 static clockid_t clock_id;
 
-static const char *clock_name(clockid_t clk_id)
+const char *clock_name(clockid_t clk_id)
 {
 	switch (clk_id) {
 	case CLOCK_REALTIME:
-- 
2.20.1


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

* [LTP] [PATCH v4 7/8] syscalls/clock_settime: create syscall clock_settime tests
  2019-01-29 17:36             ` [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h Rafael David Tinoco
                                 ` (4 preceding siblings ...)
  2019-01-29 17:36               ` [LTP] [PATCH v4 6/8] tst_timer: Turn clock_name() function public Rafael David Tinoco
@ 2019-01-29 17:36               ` Rafael David Tinoco
  2019-01-30 13:56                 ` Cyril Hrubis
  2019-01-29 17:36               ` [LTP] [PATCH v4 8/8] timers/clock_settime: remove " Rafael David Tinoco
  2019-01-30 13:50               ` [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h Cyril Hrubis
  7 siblings, 1 reply; 37+ messages in thread
From: Rafael David Tinoco @ 2019-01-29 17:36 UTC (permalink / raw)
  To: ltp

Fixes: 343

clock_settime01 creates a new test, using new API, based on existing and
older kernel/timers/clock_settime02 test. clock_settime02 creates
another test based on older kernel/timers/clock_settime03 test. Both
will be deleted in the next commits.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 runtest/syscalls                              |   3 +
 .../kernel/syscalls/clock_settime/.gitignore  |   2 +
 .../kernel/syscalls/clock_settime/Makefile    |   8 +
 .../syscalls/clock_settime/clock_settime01.c  |  72 +++++++++
 .../syscalls/clock_settime/clock_settime02.c  | 148 ++++++++++++++++++
 5 files changed, 233 insertions(+)
 create mode 100644 testcases/kernel/syscalls/clock_settime/.gitignore
 create mode 100644 testcases/kernel/syscalls/clock_settime/Makefile
 create mode 100644 testcases/kernel/syscalls/clock_settime/clock_settime01.c
 create mode 100644 testcases/kernel/syscalls/clock_settime/clock_settime02.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 45fcebdd9..668c87cd1 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -79,6 +79,9 @@ clock_nanosleep01 clock_nanosleep01
 clock_nanosleep02 clock_nanosleep02
 clock_nanosleep2_01 clock_nanosleep2_01
 
+clock_settime01 clock_settime01
+clock_settime02 clock_settime02
+
 clone01 clone01
 clone02 clone02
 clone03 clone03
diff --git a/testcases/kernel/syscalls/clock_settime/.gitignore b/testcases/kernel/syscalls/clock_settime/.gitignore
new file mode 100644
index 000000000..281217550
--- /dev/null
+++ b/testcases/kernel/syscalls/clock_settime/.gitignore
@@ -0,0 +1,2 @@
+clock_settime01
+clock_settime02
diff --git a/testcases/kernel/syscalls/clock_settime/Makefile b/testcases/kernel/syscalls/clock_settime/Makefile
new file mode 100644
index 000000000..e6674a6b2
--- /dev/null
+++ b/testcases/kernel/syscalls/clock_settime/Makefile
@@ -0,0 +1,8 @@
+# Copyright (c) 2018 - Linaro Limited. All rights reserved.
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/clock_settime/clock_settime01.c b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
new file mode 100644
index 000000000..227cfe38f
--- /dev/null
+++ b/testcases/kernel/syscalls/clock_settime/clock_settime01.c
@@ -0,0 +1,72 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+/*
+ * Basic test for clock_settime(2) on REALTIME clock:
+ *
+ *      1) advance DELTA_SEC seconds
+ *      2) go backwards DELTA_SEC seconds
+ *
+ * Restore wall clock at the end of test.
+ */
+
+#include "config.h"
+#include "tst_timer.h"
+#include "tst_safe_clocks.h"
+#include "tst_test.h"
+#include "lapi/syscalls.h"
+
+#define DELTA_SEC 10
+#define DELTA_US (long long) (DELTA_SEC * 1000000)
+#define DELTA_EPS (long long) (DELTA_US * 0.1)
+
+static void verify_clock_settime(void)
+{
+	long long elapsed;
+	struct timespec begin, change, end;
+
+	/* test 01: move forward */
+
+	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &begin);
+
+	change = tst_timespec_add_us(begin, DELTA_US);
+
+	if (clock_settime(CLOCK_REALTIME, &change) != 0)
+		tst_brk(TBROK | TTERRNO, "could not set realtime change");
+
+	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &end);
+
+	elapsed = tst_timespec_diff_us(end, begin);
+
+	if (elapsed > DELTA_US && elapsed < (DELTA_US + DELTA_EPS))
+		tst_res(TPASS, "clock_settime(2): was able to advance time");
+	else
+		tst_res(TFAIL, "clock_settime(2): could not advance time");
+
+	/* test 02: move backward */
+
+	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &begin);
+
+	change = tst_timespec_sub_us(begin, DELTA_US);
+
+	if (clock_settime(CLOCK_REALTIME, &change) != 0)
+		tst_brk(TBROK | TTERRNO, "could not set realtime change");
+
+	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &end);
+
+	elapsed = tst_timespec_diff_us(end, begin);
+
+	if (~(elapsed) < DELTA_US && ~(elapsed) > (DELTA_US - DELTA_EPS))
+		tst_res(TPASS, "clock_settime(2): was able to recede time");
+	else
+		tst_res(TFAIL, "clock_settime(2): could not recede time");
+}
+
+static struct tst_test test = {
+	.test_all = verify_clock_settime,
+	.needs_root = 1,
+	.restore_wallclock = 1,
+};
diff --git a/testcases/kernel/syscalls/clock_settime/clock_settime02.c b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
new file mode 100644
index 000000000..09c9dc4a4
--- /dev/null
+++ b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
@@ -0,0 +1,148 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 Linaro Limited. All rights reserved.
+ * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
+ */
+
+/*
+ * Basic tests for errors of clock_settime(2) on different clock types.
+ */
+
+#include "config.h"
+#include "tst_test.h"
+#include "lapi/syscalls.h"
+#include "tst_timer.h"
+#include "tst_safe_clocks.h"
+
+#define DELTA_SEC 10
+#define NSEC_PER_SEC (1000000000L)
+#define MAX_CLOCKS 16
+
+struct test_case {
+	clockid_t type;
+	struct timespec newtime;
+	int exp_err;
+	int replace;
+};
+
+struct test_case tc[] = {
+	{				/* case 01: REALTIME: timespec NULL   */
+	 .type = CLOCK_REALTIME,
+	 .exp_err = EFAULT,
+	 .replace = 1,
+	 },
+	{				/* case 02: REALTIME: tv_sec = -1     */
+	 .type = CLOCK_REALTIME,
+	 .newtime.tv_sec = -1,
+	 .exp_err = EINVAL,
+	 .replace = 1,
+	 },
+	{				/* case 03: REALTIME: tv_nsec = -1    */
+	 .type = CLOCK_REALTIME,
+	 .newtime.tv_nsec = -1,
+	 .exp_err = EINVAL,
+	 .replace = 1,
+	 },
+	{				/* case 04: REALTIME: tv_nsec = 1s+1  */
+	 .type = CLOCK_REALTIME,
+	 .newtime.tv_nsec = NSEC_PER_SEC + 1,
+	 .exp_err = EINVAL,
+	 .replace = 1,
+	 },
+	{				/* case 05: MONOTONIC		      */
+	 .type = CLOCK_MONOTONIC,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 06: MAXCLOCK		      */
+	 .type = MAX_CLOCKS,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 07: MAXCLOCK+1		      */
+	 .type = MAX_CLOCKS + 1,
+	 .exp_err = EINVAL,
+	 },
+	/* Linux specific */
+	{				/* case 08: CLOCK_MONOTONIC_COARSE    */
+	 .type = CLOCK_MONOTONIC_COARSE,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 09: CLOCK_MONOTONIC_RAW       */
+	 .type = CLOCK_MONOTONIC_RAW,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 10: CLOCK_BOOTTIME	      */
+	 .type = CLOCK_BOOTTIME,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 11: CLOCK_PROCESS_CPUTIME_ID  */
+	 .type = CLOCK_PROCESS_CPUTIME_ID,
+	 .exp_err = EINVAL,
+	 },
+	{				/* case 12: CLOCK_THREAD_CPUTIME_ID   */
+	 .type = CLOCK_THREAD_CPUTIME_ID,
+	 .exp_err = EINVAL,
+	 },
+};
+
+/*
+ * Some tests may cause libc to segfault when passing bad arguments.
+ */
+static int sys_clock_settime(clockid_t clk_id, struct timespec *tp)
+{
+	return tst_syscall(__NR_clock_settime, clk_id, tp);
+}
+
+static void verify_clock_settime(unsigned int i)
+{
+	struct timespec spec, *specptr;
+
+	specptr = &spec;
+
+	if (tc[i].replace == 0) {
+
+		SAFE_CLOCK_GETTIME(CLOCK_REALTIME, specptr);
+
+		/* add 1 sec to wall clock */
+		specptr->tv_sec += 1;
+
+	} else {
+
+		/* use given time spec */
+		*specptr = tc[i].newtime;
+	}
+
+	/* bad pointer case */
+	if (tc[i].exp_err == EFAULT)
+		specptr = tst_get_bad_addr(NULL);
+
+	TEST(sys_clock_settime(tc[i].type, specptr));
+
+	if (TST_RET == -1) {
+
+		if (tc[i].exp_err == TST_ERR) {
+
+			tst_res(TPASS, "clock_settime(2): failed as expected");
+
+		} else {
+
+			tst_res(TFAIL | TTERRNO, "clock_settime(2): clock %s "
+					"expected %s, failed with %s instead.",
+					clock_name(tc[i].type),
+					tst_strerrno(tc[i].exp_err));
+		}
+
+		return;
+	}
+
+	tst_res(TFAIL | TTERRNO, "clock_settime(2): clock %s passed "
+				 "unexpectedly, expected %s",
+				 clock_name(tc[i].type),
+				 tst_strerrno(tc[i].exp_err));
+}
+
+static struct tst_test test = {
+	.test = verify_clock_settime,
+	.tcnt = ARRAY_SIZE(tc),
+	.needs_root = 1,
+	.restore_wallclock = 1,
+};
-- 
2.20.1


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

* [LTP] [PATCH v4 8/8] timers/clock_settime: remove clock_settime tests
  2019-01-29 17:36             ` [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h Rafael David Tinoco
                                 ` (5 preceding siblings ...)
  2019-01-29 17:36               ` [LTP] [PATCH v4 7/8] syscalls/clock_settime: create syscall clock_settime tests Rafael David Tinoco
@ 2019-01-29 17:36               ` Rafael David Tinoco
  2019-01-30 13:50               ` [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h Cyril Hrubis
  7 siblings, 0 replies; 37+ messages in thread
From: Rafael David Tinoco @ 2019-01-29 17:36 UTC (permalink / raw)
  To: ltp

clock_settime{01,02} syscall tests were created, using the new API,
based on existing and older kernel/timers/clock_settime{02,03} tests.

This commit deletes older timers/clock_settime/* tests.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 runtest/timers                                |   2 -
 .../kernel/timers/clock_settime/.gitignore    |   2 -
 .../kernel/timers/clock_settime/Makefile      |  27 ---
 .../timers/clock_settime/clock_settime02.c    | 128 -------------
 .../timers/clock_settime/clock_settime03.c    | 173 ------------------
 5 files changed, 332 deletions(-)
 delete mode 100644 testcases/kernel/timers/clock_settime/.gitignore
 delete mode 100644 testcases/kernel/timers/clock_settime/Makefile
 delete mode 100644 testcases/kernel/timers/clock_settime/clock_settime02.c
 delete mode 100644 testcases/kernel/timers/clock_settime/clock_settime03.c

diff --git a/runtest/timers b/runtest/timers
index a58ac57fc..618d2cb0c 100644
--- a/runtest/timers
+++ b/runtest/timers
@@ -1,8 +1,6 @@
 #DESCRIPTION:Posix Timer Tests
 clock_gettime02 clock_gettime02
 clock_gettime03 clock_gettime03
-clock_settime02 clock_settime02
-clock_settime03 clock_settime03
 timer_create02 timer_create02
 timer_create03 timer_create03
 timer_create04 timer_create04
diff --git a/testcases/kernel/timers/clock_settime/.gitignore b/testcases/kernel/timers/clock_settime/.gitignore
deleted file mode 100644
index 957c5ac26..000000000
--- a/testcases/kernel/timers/clock_settime/.gitignore
+++ /dev/null
@@ -1,2 +0,0 @@
-/clock_settime02
-/clock_settime03
diff --git a/testcases/kernel/timers/clock_settime/Makefile b/testcases/kernel/timers/clock_settime/Makefile
deleted file mode 100644
index 8de247075..000000000
--- a/testcases/kernel/timers/clock_settime/Makefile
+++ /dev/null
@@ -1,27 +0,0 @@
-#
-#  Copyright (c) International Business Machines  Corp., 2001
-#
-#  This program is free software;  you can redistribute it and/or modify
-#  it under the terms of the GNU General Public License as published by
-#  the Free Software Foundation; either version 2 of the License, or
-#  (at your option) any later version.
-#
-#  This program is distributed in the hope that it will be useful,
-#  but WITHOUT ANY WARRANTY;  without even the implied warranty of
-#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
-#  the GNU General Public License for more details.
-#
-#  You should have received a copy of the GNU General Public License
-#  along with this program;  if not, write to the Free Software
-#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
-#
-
-top_srcdir		?= ../../../..
-
-include $(top_srcdir)/include/mk/testcases.mk
-
-CPPFLAGS		+= -D_GNU_SOURCE -I$(abs_srcdir)/../include
-
-LDLIBS			+= -lpthread -lrt
-
-include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/timers/clock_settime/clock_settime02.c b/testcases/kernel/timers/clock_settime/clock_settime02.c
deleted file mode 100644
index 65721c1ae..000000000
--- a/testcases/kernel/timers/clock_settime/clock_settime02.c
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
- * Copyright (c) Wipro Technologies Ltd, 2003.  All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- */
-/**************************************************************************
- *
- *    TEST IDENTIFIER	: clock_settime02
- *
- *    EXECUTED BY	: root / superuser
- *
- *    TEST TITLE	: Basic test for clock_settime(2)
- *
- *    TEST CASE TOTAL	: 1
- *
- *    AUTHOR		: Aniruddha Marathe <aniruddha.marathe@wipro.com>
- *
- *    SIGNALS
- * 	Uses SIGUSR1 to pause before test if option set.
- * 	(See the parse_opts(3) man page).
- *
- *    DESCRIPTION
- *     This is a Phase I test for the clock_settime(2) system call.
- *     It is intended to provide a limited exposure of the system call.
- *
- * 	Setup:
- *	  Setup signal handling.
- *	  Pause for SIGUSR1 if option specified.
- *
- * 	Test:
- *	 Loop if the proper options are given.
- *	 Set the parameters of timespec struct
- *	 Execute system call
- *	 Check return code, if system call failed (return=-1)
- *		Log the errno and Issue a FAIL message.
- *	 Otherwise, Issue a PASS message.
- *
- * 	Cleanup:
- * 	  Print errno log and/or timing stats if options given
- *
- * USAGE:  <for command-line>
- * clock_settime02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-p]
- * where:
- * 	-c n : Run n copies simultaneously.
- *	-e   : Turn on errno logging.
- *	-i n : Execute test n times.
- *	-I x : Execute test for x seconds.
- *	-p   : Pause for SIGUSR1 before starting
- *	-P x : Pause for x seconds between iterations.
- *	-t   : Turn on syscall timing.
- *
- *RESTRICTIONS:
- * None
- *****************************************************************************/
-
-#include <stdlib.h>
-#include <errno.h>
-#include <time.h>
-#include <signal.h>
-
-#include "test.h"
-#include "common_timers.h"
-
-static void setup(void);
-static void cleanup(void);
-
-char *TCID = "clock_settime02";
-int TST_TOTAL = 1;
-static struct timespec saved;
-
-int main(int ac, char **av)
-{
-	int lc;
-	struct timespec spec;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		spec.tv_sec = saved.tv_sec + 1;
-		spec.tv_nsec = 0;
-
-		TEST(ltp_syscall(__NR_clock_settime, CLOCK_REALTIME, &spec));
-		tst_resm((TEST_RETURN < 0 ? TFAIL | TTERRNO : TPASS),
-			 "clock_settime %s",
-			 (TEST_RETURN == 0 ? "passed" : "failed"));
-	}
-
-	cleanup();
-	tst_exit();
-}
-
-static void setup(void)
-{
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	tst_require_root();
-
-	/* Save the current time specifications */
-	if (ltp_syscall(__NR_clock_gettime, CLOCK_REALTIME, &saved) < 0)
-		tst_brkm(TBROK, NULL, "Could not save the current time");
-
-	TEST_PAUSE;
-}
-
-static void cleanup(void)
-{
-	/* Set the saved time */
-	if (clock_settime(CLOCK_REALTIME, &saved) < 0) {
-		tst_resm(TWARN, "FATAL COULD NOT RESET THE CLOCK");
-		tst_resm(TFAIL, "Error Setting Time, errno=%d", errno);
-	}
-}
diff --git a/testcases/kernel/timers/clock_settime/clock_settime03.c b/testcases/kernel/timers/clock_settime/clock_settime03.c
deleted file mode 100644
index 38b41d025..000000000
--- a/testcases/kernel/timers/clock_settime/clock_settime03.c
+++ /dev/null
@@ -1,173 +0,0 @@
-/*
- * Copyright (c) Wipro Technologies Ltd, 2003.  All Rights Reserved.
- * Copyright (c) 2011 Cyril Hrubis <chrubis@suse.cz>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- */
-
-#include <errno.h>
-#include <time.h>
-#include <pwd.h>
-#include <unistd.h>
-
-#include "test.h"
-#include "safe_macros.h"
-#include "common_timers.h"
-
-static void setup(void);
-static void cleanup(void);
-static int setup_test(int option);
-
-clockid_t clocks[] = {
-	CLOCK_REALTIME,
-	CLOCK_MONOTONIC,
-	MAX_CLOCKS,
-	MAX_CLOCKS + 1,
-	CLOCK_REALTIME,
-	CLOCK_REALTIME,
-	CLOCK_REALTIME,
-	CLOCK_PROCESS_CPUTIME_ID,
-	CLOCK_THREAD_CPUTIME_ID
-};
-
-int testcases[] = {
-	EFAULT,			/* tp bad               */
-	EINVAL,			/* CLOCK_MONOTONIC      */
-	EINVAL,			/* MAX_CLOCKS           */
-	EINVAL,			/* MAX_CLOCKS + 1       */
-	EINVAL,			/* Invalid timespec     */
-	EINVAL,			/* NSEC_PER_SEC + 1     */
-	EPERM,			/* non-root user        */
-	EINVAL,			/* PROCESS_CPUTIME_ID	*/
-	EINVAL,			/* THREAD_CPUTIME_ID	*/
-};
-
-char *TCID = "clock_settime03";
-int TST_TOTAL = ARRAY_SIZE(testcases);
-
-char nobody_uid[] = "nobody";
-struct passwd *ltpuser;
-static struct timespec spec, *temp, saved;
-
-int main(int ac, char **av)
-{
-	int lc, i;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++) {
-
-			if (setup_test(i) < 0)
-				continue;
-
-			TEST(ltp_syscall(__NR_clock_settime, clocks[i], temp));
-
-			/* Change the UID back to root */
-			if (i == TST_TOTAL - 1) {
-				SAFE_SETEUID(cleanup, 0);
-			}
-
-			/* check return code */
-			if (TEST_RETURN == -1 && TEST_ERRNO == testcases[i]) {
-				tst_resm(TPASS | TTERRNO,
-					 "clock_settime(2) got expected "
-					 "failure.");
-			} else {
-				tst_resm(TFAIL | TTERRNO,
-					 "clock_settime(2) failed to produce "
-					 "expected error (return code = %ld)",
-					 TEST_RETURN);
-				/* Restore the clock to its previous state. */
-				if (TEST_RETURN == 0) {
-					if (ltp_syscall(__NR_clock_settime,
-						    CLOCK_REALTIME,
-						    &saved) < 0) {
-						tst_resm(TWARN | TERRNO,
-							 "FATAL: could not set "
-							 "the clock!");
-					}
-				}
-			}
-
-		}
-
-	}
-
-	cleanup();
-	tst_exit();
-}
-
-static int setup_test(int option)
-{
-	/* valid timespec */
-	spec = saved;
-	temp = &spec;
-
-	/* error sceanrios */
-	switch (option) {
-	case 0:
-		/* Make tp argument bad pointer */
-		temp = (struct timespec *)-1;
-		break;
-	case 4:
-		/* Make the parameter of timespec invalid */
-		spec.tv_nsec = -1;
-		break;
-	case 5:
-		/* Make the parameter of timespec invalid */
-		spec.tv_nsec = NSEC_PER_SEC + 1;
-		break;
-	case 6:
-		/* change the User to non-root */
-		spec.tv_nsec = 0;
-		if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
-			tst_resm(TWARN, "user \"nobody\" not present; "
-				 "skipping test");
-			return -1;
-		}
-		if (seteuid(ltpuser->pw_uid) == -1) {
-			tst_resm(TWARN | TERRNO,
-				 "seteuid failed to set the effective "
-				 "uid to %d (nobody)", ltpuser->pw_uid);
-			return -1;
-		}
-		break;
-	}
-	return 0;
-}
-
-static void setup(void)
-{
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	tst_require_root();
-
-	if (ltp_syscall(__NR_clock_gettime, CLOCK_REALTIME, &saved) < 0)
-		tst_brkm(TBROK, NULL, "Clock gettime failed");
-
-	spec.tv_sec = 1;
-	spec.tv_nsec = 0;
-
-	TEST_PAUSE;
-}
-
-static void cleanup(void)
-{
-}
-- 
2.20.1


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

* [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h
  2019-01-29 17:36             ` [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h Rafael David Tinoco
                                 ` (6 preceding siblings ...)
  2019-01-29 17:36               ` [LTP] [PATCH v4 8/8] timers/clock_settime: remove " Rafael David Tinoco
@ 2019-01-30 13:50               ` Cyril Hrubis
  2019-01-30 14:50                 ` Rafael David Tinoco
  7 siblings, 1 reply; 37+ messages in thread
From: Cyril Hrubis @ 2019-01-30 13:50 UTC (permalink / raw)
  To: ltp

Hi!
Series pushed with some fine tunning (will respond to respective
patches), thanks!

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v4 4/8] lib: new restore_wallclock field to restore realtime clock
  2019-01-29 17:36               ` [LTP] [PATCH v4 4/8] lib: new restore_wallclock field to restore realtime clock Rafael David Tinoco
@ 2019-01-30 13:53                 ` Cyril Hrubis
  0 siblings, 0 replies; 37+ messages in thread
From: Cyril Hrubis @ 2019-01-30 13:53 UTC (permalink / raw)
  To: ltp

Hi!
I've changed the tst_wallclock.c so that it restores the time only if it
was saved previously, otherwise we will either set completely wrong time
if something has failed prior call to tst_wallclock_save() or generage
bogus error messages when the test exits because of unsufficient
priviledges.

full diff:

diff --git a/lib/tst_wallclock.c b/lib/tst_wallclock.c
index 1513882f4..f1c96c0cf 100644
--- a/lib/tst_wallclock.c
+++ b/lib/tst_wallclock.c
@@ -16,6 +16,8 @@
 
 static struct timespec real_begin, mono_begin;
 
+static int clock_saved;
+
 void tst_wallclock_save(void)
 {
 	/* save initial monotonic time to restore it when needed */
@@ -25,12 +27,19 @@ void tst_wallclock_save(void)
 
 	if (tst_clock_gettime(CLOCK_MONOTONIC_RAW, &mono_begin))
 		tst_brk(TBROK | TERRNO, "tst_clock_gettime() monotonic failed");
+
+	clock_saved = 1;
 }
 
 void tst_wallclock_restore(void)
 {
 	static struct timespec mono_end, elapsed, adjust;
 
+	if (!clock_saved)
+		return;
+
+	clock_saved = 0;
+
 	if (tst_clock_gettime(CLOCK_MONOTONIC_RAW, &mono_end))
 		tst_brk(TBROK | TERRNO, "tst_clock_gettime() monotonic failed");
 

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v4 6/8] tst_timer: Turn clock_name() function public
  2019-01-29 17:36               ` [LTP] [PATCH v4 6/8] tst_timer: Turn clock_name() function public Rafael David Tinoco
@ 2019-01-30 13:54                 ` Cyril Hrubis
  0 siblings, 0 replies; 37+ messages in thread
From: Cyril Hrubis @ 2019-01-30 13:54 UTC (permalink / raw)
  To: ltp

Hi!
I've renamed the function to tst_clock_name() just to keep it consistent
with the rest of the LTP API.

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v4 7/8] syscalls/clock_settime: create syscall clock_settime tests
  2019-01-29 17:36               ` [LTP] [PATCH v4 7/8] syscalls/clock_settime: create syscall clock_settime tests Rafael David Tinoco
@ 2019-01-30 13:56                 ` Cyril Hrubis
  0 siblings, 0 replies; 37+ messages in thread
From: Cyril Hrubis @ 2019-01-30 13:56 UTC (permalink / raw)
  To: ltp

Hi!
> +	if (elapsed > DELTA_US && elapsed < (DELTA_US + DELTA_EPS))
> +		tst_res(TPASS, "clock_settime(2): was able to advance time");
> +	else
> +		tst_res(TFAIL, "clock_settime(2): could not advance time");

I've changed this to elapsed >= DELTA_US just in case.

> +	/* test 02: move backward */
> +
> +	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &begin);
> +
> +	change = tst_timespec_sub_us(begin, DELTA_US);
> +
> +	if (clock_settime(CLOCK_REALTIME, &change) != 0)
> +		tst_brk(TBROK | TTERRNO, "could not set realtime change");
> +
> +	SAFE_CLOCK_GETTIME(CLOCK_REALTIME, &end);
> +
> +	elapsed = tst_timespec_diff_us(end, begin);
> +
> +	if (~(elapsed) < DELTA_US && ~(elapsed) > (DELTA_US - DELTA_EPS))
> +		tst_res(TPASS, "clock_settime(2): was able to recede time");
> +	else
> +		tst_res(TFAIL, "clock_settime(2): could not recede time");

And here as well.

> +}
> +
> +static struct tst_test test = {
> +	.test_all = verify_clock_settime,
> +	.needs_root = 1,
> +	.restore_wallclock = 1,
> +};
> diff --git a/testcases/kernel/syscalls/clock_settime/clock_settime02.c b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
> new file mode 100644
> index 000000000..09c9dc4a4
> --- /dev/null
> +++ b/testcases/kernel/syscalls/clock_settime/clock_settime02.c
> @@ -0,0 +1,148 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2019 Linaro Limited. All rights reserved.
> + * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
> + */
> +
> +/*
> + * Basic tests for errors of clock_settime(2) on different clock types.
> + */
> +
> +#include "config.h"
> +#include "tst_test.h"
> +#include "lapi/syscalls.h"
> +#include "tst_timer.h"
> +#include "tst_safe_clocks.h"
> +
> +#define DELTA_SEC 10
> +#define NSEC_PER_SEC (1000000000L)
> +#define MAX_CLOCKS 16
> +
> +struct test_case {
> +	clockid_t type;
> +	struct timespec newtime;
> +	int exp_err;
> +	int replace;
> +};
> +
> +struct test_case tc[] = {
> +	{				/* case 01: REALTIME: timespec NULL   */
> +	 .type = CLOCK_REALTIME,
> +	 .exp_err = EFAULT,
> +	 .replace = 1,
> +	 },
> +	{				/* case 02: REALTIME: tv_sec = -1     */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_sec = -1,
> +	 .exp_err = EINVAL,
> +	 .replace = 1,
> +	 },
> +	{				/* case 03: REALTIME: tv_nsec = -1    */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_nsec = -1,
> +	 .exp_err = EINVAL,
> +	 .replace = 1,
> +	 },
> +	{				/* case 04: REALTIME: tv_nsec = 1s+1  */
> +	 .type = CLOCK_REALTIME,
> +	 .newtime.tv_nsec = NSEC_PER_SEC + 1,
> +	 .exp_err = EINVAL,
> +	 .replace = 1,
> +	 },
> +	{				/* case 05: MONOTONIC		      */
> +	 .type = CLOCK_MONOTONIC,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 06: MAXCLOCK		      */
> +	 .type = MAX_CLOCKS,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 07: MAXCLOCK+1		      */
> +	 .type = MAX_CLOCKS + 1,
> +	 .exp_err = EINVAL,
> +	 },
> +	/* Linux specific */
> +	{				/* case 08: CLOCK_MONOTONIC_COARSE    */
> +	 .type = CLOCK_MONOTONIC_COARSE,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 09: CLOCK_MONOTONIC_RAW       */
> +	 .type = CLOCK_MONOTONIC_RAW,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 10: CLOCK_BOOTTIME	      */
> +	 .type = CLOCK_BOOTTIME,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 11: CLOCK_PROCESS_CPUTIME_ID  */
> +	 .type = CLOCK_PROCESS_CPUTIME_ID,
> +	 .exp_err = EINVAL,
> +	 },
> +	{				/* case 12: CLOCK_THREAD_CPUTIME_ID   */
> +	 .type = CLOCK_THREAD_CPUTIME_ID,
> +	 .exp_err = EINVAL,
> +	 },
> +};
> +
> +/*
> + * Some tests may cause libc to segfault when passing bad arguments.
> + */
> +static int sys_clock_settime(clockid_t clk_id, struct timespec *tp)
> +{
> +	return tst_syscall(__NR_clock_settime, clk_id, tp);
> +}
> +
> +static void verify_clock_settime(unsigned int i)
> +{
> +	struct timespec spec, *specptr;
> +
> +	specptr = &spec;
> +
> +	if (tc[i].replace == 0) {
> +
> +		SAFE_CLOCK_GETTIME(CLOCK_REALTIME, specptr);
> +
> +		/* add 1 sec to wall clock */
> +		specptr->tv_sec += 1;
> +
> +	} else {
> +
> +		/* use given time spec */
> +		*specptr = tc[i].newtime;
> +	}
> +
> +	/* bad pointer case */
> +	if (tc[i].exp_err == EFAULT)
> +		specptr = tst_get_bad_addr(NULL);
> +
> +	TEST(sys_clock_settime(tc[i].type, specptr));
> +
> +	if (TST_RET == -1) {
> +
> +		if (tc[i].exp_err == TST_ERR) {
> +
> +			tst_res(TPASS, "clock_settime(2): failed as expected");

I've made this message a bit more verbose, we print withc kind of errno
we got and name of the clock.

> +
> +		} else {
> +
> +			tst_res(TFAIL | TTERRNO, "clock_settime(2): clock %s "
> +					"expected %s, failed with %s instead.",
> +					clock_name(tc[i].type),
> +					tst_strerrno(tc[i].exp_err));

The format string didn't match the parameters here, fixed that.

> +		}
> +
> +		return;
> +	}
> +
> +	tst_res(TFAIL | TTERRNO, "clock_settime(2): clock %s passed "
> +				 "unexpectedly, expected %s",
> +				 clock_name(tc[i].type),
> +				 tst_strerrno(tc[i].exp_err));
> +}
> +
> +static struct tst_test test = {
> +	.test = verify_clock_settime,
> +	.tcnt = ARRAY_SIZE(tc),
> +	.needs_root = 1,
> +	.restore_wallclock = 1,
> +};
> -- 
> 2.20.1
> 

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h
  2019-01-30 13:50               ` [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h Cyril Hrubis
@ 2019-01-30 14:50                 ` Rafael David Tinoco
  0 siblings, 0 replies; 37+ messages in thread
From: Rafael David Tinoco @ 2019-01-30 14:50 UTC (permalink / raw)
  To: ltp

> On 30 Jan 2019, at 11:50, Cyril Hrubis <chrubis@suse.cz> wrote:
> 
> Hi!
> Series pushed with some fine tunning (will respond to respective
> patches), thanks!
> 
> -- 
> Cyril Hrubis
> chrubis@suse.cz

Thanks a lot!

Best,
Rafael

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

end of thread, other threads:[~2019-01-30 14:50 UTC | newest]

Thread overview: 37+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-05 19:30 [LTP] [PATCH 1/2] syscalls/clock_settime01.c: create syscall clock_settime test Rafael David Tinoco
2018-12-05 19:30 ` [LTP] [PATCH 2/2] timers/clock_settime: remove clock_settime tests Rafael David Tinoco
2018-12-06 13:03 ` [LTP] [PATCH 1/2] syscalls/clock_settime01.c: create syscall clock_settime test Cyril Hrubis
2018-12-06 14:49   ` Rafael David Tinoco
2018-12-06 19:07   ` [LTP] [PATCH v2 1/2] syscalls/clock_settime01.c: create syscall clock_settime Rafael David Tinoco
2018-12-06 19:07     ` [LTP] [PATCH v2 2/2] timers/clock_settime: remove clock_settime tests Rafael David Tinoco
2018-12-06 19:11     ` [LTP] [PATCH v2 1/2] syscalls/clock_settime01.c: create syscall clock_settime Rafael David Tinoco
2018-12-11 14:27     ` Cyril Hrubis
2018-12-11 16:05       ` Rafael David Tinoco
2018-12-12 20:37       ` [LTP] [PATCH v3 1/6] tst_timer.h: add tst_timespect_rem_us() function Rafael David Tinoco
2018-12-12 20:37         ` [LTP] [PATCH v3 2/6] lib: add tst_clock_settime() to tst_clocks.h Rafael David Tinoco
2018-12-12 20:37         ` [LTP] [PATCH v3 3/6] lib: include SAFE_CLOCK_SETTIME() macro Rafael David Tinoco
2018-12-12 20:37         ` [LTP] [PATCH v3 4/6] lib: new restore_wallclock field to restore realtime clock Rafael David Tinoco
2019-01-24 15:12           ` Cyril Hrubis
2018-12-12 20:37         ` [LTP] [PATCH v3 5/6] syscalls/clock_settime: create syscall clock_settime tests Rafael David Tinoco
2018-12-12 20:46           ` Rafael David Tinoco
2019-01-24 16:11           ` Cyril Hrubis
2019-01-29 17:36             ` [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h Rafael David Tinoco
2019-01-29 17:36               ` [LTP] [PATCH v4 2/8] lib: include SAFE_CLOCK_SETTIME() macro Rafael David Tinoco
2019-01-29 17:36               ` [LTP] [PATCH v4 3/8] tst_timer: Add tst_timespec_add() Rafael David Tinoco
2019-01-29 17:36               ` [LTP] [PATCH v4 4/8] lib: new restore_wallclock field to restore realtime clock Rafael David Tinoco
2019-01-30 13:53                 ` Cyril Hrubis
2019-01-29 17:36               ` [LTP] [PATCH v4 5/8] tst_timer: Add tst_timespec_sub_us() Rafael David Tinoco
2019-01-29 17:36               ` [LTP] [PATCH v4 6/8] tst_timer: Turn clock_name() function public Rafael David Tinoco
2019-01-30 13:54                 ` Cyril Hrubis
2019-01-29 17:36               ` [LTP] [PATCH v4 7/8] syscalls/clock_settime: create syscall clock_settime tests Rafael David Tinoco
2019-01-30 13:56                 ` Cyril Hrubis
2019-01-29 17:36               ` [LTP] [PATCH v4 8/8] timers/clock_settime: remove " Rafael David Tinoco
2019-01-30 13:50               ` [LTP] [PATCH v4 1/8] lib: add tst_clock_settime() to tst_clocks.h Cyril Hrubis
2019-01-30 14:50                 ` Rafael David Tinoco
2018-12-12 20:37         ` [LTP] [PATCH v3 6/6] timers/clock_settime: remove clock_settime tests Rafael David Tinoco
2019-01-08 12:04           ` Rafael David Tinoco
2019-01-08 12:42             ` Cyril Hrubis
2019-01-08 12:46               ` Rafael David Tinoco
2019-01-24 12:58             ` Rafael David Tinoco
2019-01-24 13:18               ` Cyril Hrubis
2019-01-24 15:06         ` [LTP] [PATCH v3 1/6] tst_timer.h: add tst_timespect_rem_us() function Cyril Hrubis

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.