From patchwork Wed Mar 18 15:37:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cyril Hrubis X-Patchwork-Id: 1257602 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.cz Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48jDcZ0Gnkz9sP7 for ; Thu, 19 Mar 2020 02:34:53 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 5BE4C3C5520 for ; Wed, 18 Mar 2020 16:34:51 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-2.smtp.seeweb.it (in-2.smtp.seeweb.it [IPv6:2001:4b78:1:20::2]) by picard.linux.it (Postfix) with ESMTP id DE2DF3C0489 for ; Wed, 18 Mar 2020 16:34:18 +0100 (CET) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-2.smtp.seeweb.it (Postfix) with ESMTPS id 9528B6011F6 for ; Wed, 18 Mar 2020 16:34:18 +0100 (CET) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 470C9AE2B for ; Wed, 18 Mar 2020 15:34:18 +0000 (UTC) From: Cyril Hrubis To: ltp@lists.linux.it Date: Wed, 18 Mar 2020 16:37:57 +0100 Message-Id: <20200318153801.3529-6-chrubis@suse.cz> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200318153801.3529-1-chrubis@suse.cz> References: <20200318153801.3529-1-chrubis@suse.cz> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-2.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.2 required=7.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-2.smtp.seeweb.it Subject: [LTP] [PATCH v2 5/9] syscalls/sysinfo03: Add time namespace test X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" 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 --- runtest/containers | 3 + runtest/syscalls | 1 + testcases/kernel/syscalls/sysinfo/.gitignore | 1 + testcases/kernel/syscalls/sysinfo/sysinfo03.c | 81 +++++++++++++++++++ 4 files changed, 86 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 6f2dcd82a..fb0b9e539 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -1465,6 +1465,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..af1024915 --- /dev/null +++ b/testcases/kernel/syscalls/sysinfo/sysinfo03.c @@ -0,0 +1,81 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + Copyright (c) 2020 Cyril Hrubis + */ +/* + + 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 +#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" + } + .tags = (const struct tst_tag[]) { + {"linux-git", "ecc421e05bab"}, + } +};