Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp4138327ybi; Mon, 29 Jul 2019 20:06:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqzwEhDh1wsyECPPARbM97BPh7700mKPc323MFI64X0cpMlQm17nujwIrvtAGsXdRoknN5vi X-Received: by 2002:a17:90a:2562:: with SMTP id j89mr115147844pje.123.1564455960496; Mon, 29 Jul 2019 20:06:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564455960; cv=none; d=google.com; s=arc-20160816; b=ARt3Z8frDFBo8CX78AOzm3tBKSl6B0F6Fsrz3x79VPRBWy/P8ui+7d+2VD8qIxIki5 fLbHl3ZUwUwxEAPMqBzO9KQ1lbDBdDYu8xlRqmYaOjkmPVpcxvmTOz5rNkQUoEVvlLL2 YBcLYeWpFFLM3DWLPhwLPNpJG54644R250K9LS1i2/rRE2aWmwTnnqTkcCbPea+2khRv AwbAxbK9zPKM7jGWht5nW1esdIIIu4ZQIeEuygQx1k+b+6DsvYSTInsSMcmPNKjxkV7Y /Km07fczIrnHl/zoNL26jktRxp5IouErTJ+R2L63w+oO98MoyzSkafWWN+A1gCrcFeQV OxdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=PNJxm2QMtZcp14624QbakT+mhmccQwYHPQgZhw4FCC0=; b=Vo+f0e6FXy1+buMffxqvM8Au4zWuqhrArzqqKlL1Na78kV+/SqUFUaX/LoWZemjiUW Syzvyytg8JNSx4yznX4Rt9f3JLQZgmmCbsQ5y2KI5QAQs+4R2sC7SXpS856xK8/+Gvsm Al8enO5xKKhxNztTnxRtAmhgv1H5EVCKZjY6pI9PtkLwlhRD4w1C6/RNLklsJK9G5nlq GkR6H/B4uSzv+l+vkfeEcDjDE+qS70mUyRU6F8hzoAHEAvIyQ681SolHw6nmK+2svRjq wwnPG1hItJXDZfU1XwR4Seafuq2AEZTI1Wbwm0NS0L+5XIMgBGd8FX/bQSQW/iZac6BA x2/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@arista.com header.s=googlenew header.b=Zhx0iZ7m; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=REJECT dis=NONE) header.from=arista.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ch18si26792238plb.76.2019.07.29.20.05.45; Mon, 29 Jul 2019 20:06:00 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@arista.com header.s=googlenew header.b=Zhx0iZ7m; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=REJECT dis=NONE) header.from=arista.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389397AbfG2V6y (ORCPT + 99 others); Mon, 29 Jul 2019 17:58:54 -0400 Received: from mail-wm1-f67.google.com ([209.85.128.67]:40511 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389278AbfG2V6u (ORCPT ); Mon, 29 Jul 2019 17:58:50 -0400 Received: by mail-wm1-f67.google.com with SMTP id v19so54830131wmj.5 for ; Mon, 29 Jul 2019 14:58:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=googlenew; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PNJxm2QMtZcp14624QbakT+mhmccQwYHPQgZhw4FCC0=; b=Zhx0iZ7mRqW5plMP0JwzBNNA2fyOBmx9Mi6BehgBdC6t3A7RO4x0BV+iXqhYgP0arJ 87pZcLLmM/5XoSeA+hAZeVgUZjrOa2/NQCAEbo6WcMh3b5g6ecOfU/qcUERBJx/5xG7n tKKuqaYxG9jtbxjCKayrilNZcQIhn5//TfcYXDCLWEdGmB7uj4te9NLyxz1tDPl98TWX ErPoePEenNDdLrF/RWhQ44yGhASRapz2HBS8V6rcL/KfzNGDoK1vDGoPOM0v2/8b5T96 /X1RVqoKfGnAIdzEKTAggevQTtav7YaqxydXcf0vahGnXlAB1ncaqzYRzeojOgnCgDy0 gWlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PNJxm2QMtZcp14624QbakT+mhmccQwYHPQgZhw4FCC0=; b=NnxmGsUY4+JYeBhXHAQRvSfOkbmyYSyNFbSQS9FLTIrMaI+VdaJAVKLIOiuBVbLhBS n9yt/Np8fOtTpR6cdYPCwckxTKvJ6GqJb7CkvpNxWh8q10K4fjoVorPUpmqEzafwLBhn 1Z4ouL39uN3mAseHyET6uiDkjFO1sfbrbE7017yyBXv//l0+L7LiqJRyTP1CUaQ70OaL nblSaNWyOYAJnGJ/gucQ7WlvprALoxMr0HCGd+lVcEB0JdMcUTlcGGuB4BeE3kKmnVuf Cw8XA2YGAKdOeBrrrC+KkC0bCkF0/ragUJ19qWL2SAjoN/KbDfgijJbx1cITyoob2y4K aFxQ== X-Gm-Message-State: APjAAAWu+2ireNqOH4O9t4g6fZtcgvVOU+en2edxDQpyB8kYOtW4LF/h 2ol7DK2WGLyevEGPe83DJoWejkIBjDd135VATs+83JKXk0nF3mcGuw/E8GVn/+J25xgMmaITe3h qBzkhHeNVfxbpgFSKuUfi7fHaUy+sJE5uPOr3ArBK3dPQtFv9BepKA9mnrgTuBUsORskBntDiu6 xpUeMhStsvmjjnmkIw28Nrwpiu3cAUFCor0hkZJBo= X-Received: by 2002:a1c:d108:: with SMTP id i8mr105848941wmg.28.1564437527184; Mon, 29 Jul 2019 14:58:47 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id x20sm49230728wmc.1.2019.07.29.14.58.45 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Mon, 29 Jul 2019 14:58:46 -0700 (PDT) From: Dmitry Safonov To: linux-kernel@vger.kernel.org Cc: Dmitry Safonov <0x7f454c46@gmail.com>, Dmitry Safonov , Adrian Reber , Andrei Vagin , Andy Lutomirski , Arnd Bergmann , Christian Brauner , Cyrill Gorcunov , "Eric W. Biederman" , "H. Peter Anvin" , Ingo Molnar , Jann Horn , Jeff Dike , Oleg Nesterov , Pavel Emelyanov , Shuah Khan , Thomas Gleixner , Vincenzo Frascino , containers@lists.linux-foundation.org, criu@openvz.org, linux-api@vger.kernel.org, x86@kernel.org, Andrei Vagin Subject: [PATCHv5 34/37] selftest/timens: Add procfs selftest Date: Mon, 29 Jul 2019 22:57:16 +0100 Message-Id: <20190729215758.28405-35-dima@arista.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190729215758.28405-1-dima@arista.com> References: <20190729215758.28405-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CLOUD-SEC-AV-Info: arista,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Check that /proc/uptime is correct inside a new time namespace. Co-developed-by: Andrei Vagin Signed-off-by: Andrei Vagin Signed-off-by: Dmitry Safonov --- tools/testing/selftests/timens/.gitignore | 1 + tools/testing/selftests/timens/Makefile | 2 +- tools/testing/selftests/timens/procfs.c | 144 ++++++++++++++++++++++ 3 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/timens/procfs.c diff --git a/tools/testing/selftests/timens/.gitignore b/tools/testing/selftests/timens/.gitignore index 9b6c8ddac2c8..94ffdd9cead7 100644 --- a/tools/testing/selftests/timens/.gitignore +++ b/tools/testing/selftests/timens/.gitignore @@ -1,3 +1,4 @@ clock_nanosleep +procfs timens timerfd diff --git a/tools/testing/selftests/timens/Makefile b/tools/testing/selftests/timens/Makefile index 76a1dc891184..f96f50d1fef8 100644 --- a/tools/testing/selftests/timens/Makefile +++ b/tools/testing/selftests/timens/Makefile @@ -1,4 +1,4 @@ -TEST_GEN_PROGS := timens timerfd clock_nanosleep +TEST_GEN_PROGS := timens timerfd clock_nanosleep procfs CFLAGS := -Wall -Werror diff --git a/tools/testing/selftests/timens/procfs.c b/tools/testing/selftests/timens/procfs.c new file mode 100644 index 000000000000..3c3c6e634d50 --- /dev/null +++ b/tools/testing/selftests/timens/procfs.c @@ -0,0 +1,144 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "log.h" +#include "timens.h" + +/* + * Test shouldn't be run for a day, so add 10 days to child + * time and check parent's time to be in the same day. + */ +#define MAX_TEST_TIME_SEC (60*5) +#define DAY_IN_SEC (60*60*24) +#define TEN_DAYS_IN_SEC (10*DAY_IN_SEC) + +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) + +static int child_ns, parent_ns; + +static int switch_ns(int fd) +{ + if (setns(fd, CLONE_NEWTIME)) + return pr_perror("setns()"); + + return 0; +} + +static int init_namespaces(void) +{ + char path[] = "/proc/self/ns/time_for_children"; + struct stat st1, st2; + + parent_ns = open(path, O_RDONLY); + if (parent_ns <= 0) + return pr_perror("Unable to open %s", path); + + if (fstat(parent_ns, &st1)) + return pr_perror("Unable to stat the parent timens"); + + if (unshare(CLONE_NEWTIME)) + return pr_perror("Can't unshare() timens"); + + child_ns = open(path, O_RDONLY); + if (child_ns <= 0) + return pr_perror("Unable to open %s", path); + + if (fstat(child_ns, &st2)) + return pr_perror("Unable to stat the timens"); + + if (st1.st_ino == st2.st_ino) + return pr_err("The same child_ns after CLONE_NEWTIME"); + + if (_settime(CLOCK_BOOTTIME, TEN_DAYS_IN_SEC)) + return -1; + + return 0; +} + +static int read_proc_uptime(struct timespec *uptime) +{ + unsigned long up_sec, up_nsec; + FILE *proc; + + proc = fopen("/proc/uptime", "r"); + if (proc == NULL) { + pr_perror("Unable to open /proc/uptime"); + return -1; + } + + if (fscanf(proc, "%lu.%02lu", &up_sec, &up_nsec) != 2) { + if (errno) { + pr_perror("fscanf"); + return -errno; + } + pr_err("failed to parse /proc/uptime"); + return -1; + } + fclose(proc); + + uptime->tv_sec = up_sec; + uptime->tv_nsec = up_nsec; + return 0; +} + +static int check_uptime(void) +{ + struct timespec uptime_new, uptime_old; + time_t uptime_expected; + double prec = MAX_TEST_TIME_SEC; + + if (switch_ns(parent_ns)) + return pr_err("switch_ns(%d)", parent_ns); + + if (read_proc_uptime(&uptime_old)) + return 1; + + if (switch_ns(child_ns)) + return pr_err("switch_ns(%d)", child_ns); + + if (read_proc_uptime(&uptime_new)) + return 1; + + uptime_expected = uptime_old.tv_sec + TEN_DAYS_IN_SEC; + if (fabs(difftime(uptime_new.tv_sec, uptime_expected)) > prec) { + pr_fail("uptime in /proc/uptime: old %ld, new %ld [%ld]", + uptime_old.tv_sec, uptime_new.tv_sec, + uptime_old.tv_sec + TEN_DAYS_IN_SEC); + return 1; + } + + ksft_test_result_pass("Passed for /proc/uptime\n"); + return 0; +} + +int main(int argc, char *argv[]) +{ + int ret = 0; + + nscheck(); + + ksft_set_plan(1); + + if (init_namespaces()) + return 1; + + ret |= check_uptime(); + + if (ret) + ksft_exit_fail(); + ksft_exit_pass(); + return ret; +} -- 2.22.0