All of lore.kernel.org
 help / color / mirror / Atom feed
From: Richard Palethorpe <rpalethorpe@suse.de>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH 4/8] syscalls/sysinfo03: Add time namespace test
Date: Mon, 09 Mar 2020 13:32:52 +0100	[thread overview]
Message-ID: <87k13tg13f.fsf@our.domain.is.not.set> (raw)
In-Reply-To: <20200305134834.16736-5-chrubis@suse.cz>

Hello,

Cyril Hrubis <chrubis@suse.cz> writes:

> This tests that the uptime in sysinfo() is adjusted correctly by the
> namespace offset.
>
> Also check that /proc/uptime is consistent with the uptime from the
> sysinfo() syscall.
>
> Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
> ---
>  runtest/containers                            |  3 +
>  runtest/syscalls                              |  1 +
>  testcases/kernel/syscalls/sysinfo/.gitignore  |  1 +
>  testcases/kernel/syscalls/sysinfo/sysinfo03.c | 78 +++++++++++++++++++
>  4 files changed, 83 insertions(+)
>  create mode 100644 testcases/kernel/syscalls/sysinfo/sysinfo03.c
>
> diff --git a/runtest/containers b/runtest/containers
> index 871cd2a42..4dc05af93 100644
> --- a/runtest/containers
> +++ b/runtest/containers
> @@ -85,3 +85,6 @@ userns04 userns04
>  userns05 userns05
>  userns06 userns06
>  userns07 userns07
> +
> +# time namespaces
> +sysinfo03 sysinfo03
> diff --git a/runtest/syscalls b/runtest/syscalls
> index 06d96470c..34bd5c3da 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -1441,6 +1441,7 @@ sysfs06 sysfs06
>  
>  sysinfo01 sysinfo01
>  sysinfo02 sysinfo02
> +sysinfo03 sysinfo03
>  
>  syslog01 syslog01
>  syslog02 syslog02
> diff --git a/testcases/kernel/syscalls/sysinfo/.gitignore b/testcases/kernel/syscalls/sysinfo/.gitignore
> index aa7c26946..8ad2279a4 100644
> --- a/testcases/kernel/syscalls/sysinfo/.gitignore
> +++ b/testcases/kernel/syscalls/sysinfo/.gitignore
> @@ -1,2 +1,3 @@
>  /sysinfo01
>  /sysinfo02
> +/sysinfo03
> diff --git a/testcases/kernel/syscalls/sysinfo/sysinfo03.c b/testcases/kernel/syscalls/sysinfo/sysinfo03.c
> new file mode 100644
> index 000000000..979e6e0a7
> --- /dev/null
> +++ b/testcases/kernel/syscalls/sysinfo/sysinfo03.c
> @@ -0,0 +1,78 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> +  Copyright (c) 2020 Cyril Hrubis <chrubis@suse.cz>
> + */
> +/*
> +
> +  Test if CLOCK_BOOTTIME namespace offset is applied to sysinfo uptime and that
> +  it's consistent with /proc/uptime as well.
> +
> +  After a call to unshare(CLONE_NEWTIME) a new timer namespace is created, the
> +  process that has called the unshare() can adjust offsets for CLOCK_MONOTONIC
> +  and CLOCK_BOOTTIME for its children by writing to the '/proc/self/timens_offsets'.
> +
> + */
> +
> +#include <sys/sysinfo.h>
> +#include "lapi/namespaces_constants.h"
> +#include "tst_test.h"
> +
> +static int offsets[] = {
> +	10,
> +	-10,
> +	3600,
> +};
> +
> +static long read_proc_uptime(void)
> +{
> +	long sec, sec_rem;
> +
> +	SAFE_FILE_SCANF("/proc/uptime", "%li.%li", &sec, &sec_rem);
> +
> +	return sec + (sec_rem ? 1 : 0);
> +}
> +
> +static void verify_sysinfo(unsigned int n)
> +{
> +	struct sysinfo si;
> +	long uptime;
> +	int off = offsets[n];
> +
> +	SAFE_UNSHARE(CLONE_NEWTIME);
> +
> +        SAFE_FILE_PRINTF("/proc/self/timens_offsets", "%d %d 0",
> +	                 CLOCK_BOOTTIME, off);
> +
> +	sysinfo(&si);
> +
> +	uptime = si.uptime;
> +
> +	if (!SAFE_FORK()) {
> +		sysinfo(&si);
> +		long proc_uptime = read_proc_uptime();
> +
> +		long diff = si.uptime - uptime;
> +
> +		if (diff < off || diff > off + 1)
> +			tst_res(TFAIL, "Wrong sysinfo uptime offset %li", diff);
> +		else
> +			tst_res(TPASS, "Correct sysinfo uptime offset %i", off);
> +
> +		if (si.uptime < proc_uptime || si.uptime > proc_uptime + 1) {
> +			tst_res(TFAIL, "/proc/uptime %li differs from sysinfo %li",
> +			        proc_uptime, si.uptime);
> +		} else {
> +			tst_res(TPASS, "/proc/uptime is consistent with sysinfo");
> +		}
> +	}
> +}
> +
> +static struct tst_test test = {
> +	.tcnt = ARRAY_SIZE(offsets),
> +	.test = verify_sysinfo,
> +	.needs_root = 1,
> +	.forks_child = 1,
> +	.needs_kconfigs = (const char *[]) {
> +		"CONFIG_TIME_NS=y"
> +	}
> +};

Will you add a git ref when the fix is in mainline/for-next?

Should be safe to add it as soon as it is in the for-next tree.

> -- 
> 2.23.0


-- 
Thank you,
Richard.

  reply	other threads:[~2020-03-09 12:32 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-05 13:48 [LTP] [PATCH 0/8] Add basic time namespace testcases Cyril Hrubis
2020-03-05 13:48 ` [LTP] [PATCH 1/8] lapi/namespace_constants.h: Add CLONE_NEWTIME Cyril Hrubis
2020-03-05 13:48 ` [LTP] [PATCH 2/8] lapi: Add a configure check and fallback for setns Cyril Hrubis
2020-03-09 10:15   ` Petr Vorel
2020-03-09 10:34     ` Cyril Hrubis
2020-03-05 13:48 ` [LTP] [PATCH 3/8] include/tst_timer: Fix normalization Cyril Hrubis
2020-03-05 13:48 ` [LTP] [PATCH 4/8] syscalls/sysinfo03: Add time namespace test Cyril Hrubis
2020-03-09 12:32   ` Richard Palethorpe [this message]
2020-03-09 12:48     ` Cyril Hrubis
2020-03-09 13:26       ` Richard Palethorpe
2020-03-09 13:33         ` Cyril Hrubis
2020-03-05 13:48 ` [LTP] [PATCH 5/8] syscalls/clock_nanosleep03: Add test for time namespace Cyril Hrubis
2020-03-05 13:48 ` [LTP] [PATCH 6/8] syscalls/clock_gettime03: Add basic time namespace test Cyril Hrubis
2020-03-09 13:23   ` Richard Palethorpe
2020-03-09 13:49     ` Cyril Hrubis
2020-03-05 13:48 ` [LTP] [PATCH 7/8] containers/timens: Add basic error test Cyril Hrubis
2020-03-10 12:06   ` Richard Palethorpe
2020-03-05 13:48 ` [LTP] [PATCH 8/8] syscalls/timerfd04: Add time namespace test Cyril Hrubis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87k13tg13f.fsf@our.domain.is.not.set \
    --to=rpalethorpe@suse.de \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.