From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.9 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E85BDC10F14 for ; Fri, 11 Oct 2019 01:24:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B94E92196E for ; Fri, 11 Oct 2019 01:24:38 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=arista.com header.i=@arista.com header.b="j7AN3zNE" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728316AbfJKBYh (ORCPT ); Thu, 10 Oct 2019 21:24:37 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:45450 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728212AbfJKBYa (ORCPT ); Thu, 10 Oct 2019 21:24:30 -0400 Received: by mail-wr1-f67.google.com with SMTP id r5so9918958wrm.12 for ; Thu, 10 Oct 2019 18:24:29 -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=cNllUNu5Yq3J1PsESBueiKRvUlzqTmSZUMyxDhD1090=; b=j7AN3zNE4wh63E0xLuaWAifEANgfEwFTuCTnNZTXu1agw3VKzce/evAjxbhapnCgSH zcIsRyYThb9gEGU1gSZRuRn7F2tlbthQzp1JZ2WklzaSvtkzSSzdz5CC1PO0Ua21k+9y RcaUTZInwUbrJ4vJ3tkabm3rjgfLWS+wRMPhwAdZ4bfRqFFRYub2OajqLLmu/gTpVP83 dLF61R8b12KZzgrOCRCO7I3eAQq2XJDvDYcq3gazgoPSUg/4ze5K1/wGgTnOKXiKzjaU QytHtbRIHoyQTFCqbKvz8KoSYkXpc/pEJWMCoFCJiTz3JYZLJw2PBpPQzrCfJKGubIjX ScYA== 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=cNllUNu5Yq3J1PsESBueiKRvUlzqTmSZUMyxDhD1090=; b=hWGJVo8Np2IlwPKlDgD0OwHXNzSaE+VjCbDZsrP3Zr9diDwrmgLHRaRIfkA9AAHrDj 1TDDnXoBtowti1JYTauLn+U4MU1MaNmr5gKmFScZ7PjTFNV80iR/iLD5s/mklDsGJTFq Qny+nfHZH1IPWJJ7v0NWuYPUvjvdjSSrjVKtH34u0Po3l7FV7A+g7LjDbv4V9/pspfuU dAdNlH+kJ4NT4SZ67PSe/juwGy5FmkeJhYaHs3aajKL2N3D0ITVhJP+klqu6bel13gsc vvKu3iVsp81/mhk1X0rNRlqMgneFIxUGlkkLOiU0zjCC/pq5JTD8BraXYrn0FLneOdZQ HCtg== X-Gm-Message-State: APjAAAV/vOOn9AsdiJ4BpUb/4n6XaQBhwkdHGQ5f/RYXgnFU966+ELsI se+Q3yVv7PMO7/mo6cpRrClPeKOvSks= X-Google-Smtp-Source: APXvYqw6FaCjcHl1KFXyo5KfhjRg7ZierGHl6KsubmoWavM1Rq4aIWyRS95P4go9clVyYVGwJ+ppBQ== X-Received: by 2002:adf:9221:: with SMTP id 30mr11248037wrj.126.1570757068395; Thu, 10 Oct 2019 18:24:28 -0700 (PDT) Received: from localhost.localdomain ([2a02:8084:ea2:c100:228:f8ff:fe6f:83a8]) by smtp.gmail.com with ESMTPSA id l13sm7699795wmj.25.2019.10.10.18.24.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2019 18:24:27 -0700 (PDT) From: Dmitry Safonov To: linux-kernel@vger.kernel.org Cc: Dmitry Safonov <0x7f454c46@gmail.com>, Andrei Vagin , 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 Subject: [PATCHv7 28/33] selftests/timens: Add a test for timerfd Date: Fri, 11 Oct 2019 02:23:36 +0100 Message-Id: <20191011012341.846266-29-dima@arista.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191011012341.846266-1-dima@arista.com> References: <20191011012341.846266-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andrei Vagin Check that timerfd_create() takes into account clock offsets. Output on success: 1..3 ok 1 clockid=7 ok 2 clockid=1 ok 3 clockid=9 # Pass 3 Fail 0 Xfail 0 Xpass 0 Skip 0 Error 0 Output on failure: 1..3 not ok 1 clockid: 7 elapsed: 0 not ok 2 clockid: 1 elapsed: 0 not ok 3 clockid: 9 elapsed: 0 Bail out! Output with lack of permissions: 1..3 not ok 1 # SKIP need to run as root Output without support of time namespaces: 1..3 not ok 1 # SKIP Time namespaces are not supported Signed-off-by: Andrei Vagin Co-developed-by: Dmitry Safonov Signed-off-by: Dmitry Safonov --- tools/testing/selftests/timens/.gitignore | 1 + tools/testing/selftests/timens/Makefile | 2 +- tools/testing/selftests/timens/timerfd.c | 129 ++++++++++++++++++++++ 3 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/timens/timerfd.c diff --git a/tools/testing/selftests/timens/.gitignore b/tools/testing/selftests/timens/.gitignore index 27a693229ce1..b609f6ee9fb9 100644 --- a/tools/testing/selftests/timens/.gitignore +++ b/tools/testing/selftests/timens/.gitignore @@ -1 +1,2 @@ timens +timerfd diff --git a/tools/testing/selftests/timens/Makefile b/tools/testing/selftests/timens/Makefile index b877efb78974..66b90cd28e5c 100644 --- a/tools/testing/selftests/timens/Makefile +++ b/tools/testing/selftests/timens/Makefile @@ -1,4 +1,4 @@ -TEST_GEN_PROGS := timens +TEST_GEN_PROGS := timens timerfd CFLAGS := -Wall -Werror diff --git a/tools/testing/selftests/timens/timerfd.c b/tools/testing/selftests/timens/timerfd.c new file mode 100644 index 000000000000..619b096b7fe5 --- /dev/null +++ b/tools/testing/selftests/timens/timerfd.c @@ -0,0 +1,129 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "log.h" +#include "timens.h" + +static int tclock_gettime(clock_t clockid, struct timespec *now) +{ + if (clockid == CLOCK_BOOTTIME_ALARM) + clockid = CLOCK_BOOTTIME; + return clock_gettime(clockid, now); +} + +int run_test(int clockid, struct timespec now) +{ + struct itimerspec new_value; + long long elapsed; + int fd, i; + + if (tclock_gettime(clockid, &now)) + return pr_perror("clock_gettime(%d)", clockid); + + for (i = 0; i < 2; i++) { + int flags = 0; + + new_value.it_value.tv_sec = 3600; + new_value.it_value.tv_nsec = 0; + new_value.it_interval.tv_sec = 1; + new_value.it_interval.tv_nsec = 0; + + if (i == 1) { + new_value.it_value.tv_sec += now.tv_sec; + new_value.it_value.tv_nsec += now.tv_nsec; + } + + fd = timerfd_create(clockid, 0); + if (fd == -1) + return pr_perror("timerfd_create(%d)", clockid); + + if (i == 1) + flags |= TFD_TIMER_ABSTIME; + + if (timerfd_settime(fd, flags, &new_value, NULL)) + return pr_perror("timerfd_settime(%d)", clockid); + + if (timerfd_gettime(fd, &new_value)) + return pr_perror("timerfd_gettime(%d)", clockid); + + elapsed = new_value.it_value.tv_sec; + if (abs(elapsed - 3600) > 60) { + ksft_test_result_fail("clockid: %d elapsed: %lld\n", + clockid, elapsed); + return 1; + } + + close(fd); + } + + ksft_test_result_pass("clockid=%d\n", clockid); + + return 0; +} + +int main(int argc, char *argv[]) +{ + int ret, status, len, fd; + char buf[4096]; + pid_t pid; + struct timespec btime_now, mtime_now; + + nscheck(); + + ksft_set_plan(3); + + clock_gettime(CLOCK_MONOTONIC, &mtime_now); + clock_gettime(CLOCK_BOOTTIME, &btime_now); + + if (unshare_timens()) + return 1; + + len = snprintf(buf, sizeof(buf), "%d %d 0\n%d %d 0", + CLOCK_MONOTONIC, 70 * 24 * 3600, + CLOCK_BOOTTIME, 9 * 24 * 3600); + fd = open("/proc/self/timens_offsets", O_WRONLY); + if (fd < 0) + return pr_perror("/proc/self/timens_offsets"); + + if (write(fd, buf, len) != len) + return pr_perror("/proc/self/timens_offsets"); + + close(fd); + mtime_now.tv_sec += 70 * 24 * 3600; + btime_now.tv_sec += 9 * 24 * 3600; + + pid = fork(); + if (pid < 0) + return pr_perror("Unable to fork"); + if (pid == 0) { + ret = 0; + ret |= run_test(CLOCK_BOOTTIME, btime_now); + ret |= run_test(CLOCK_MONOTONIC, mtime_now); + ret |= run_test(CLOCK_BOOTTIME_ALARM, btime_now); + + if (ret) + ksft_exit_fail(); + ksft_exit_pass(); + return ret; + } + + if (waitpid(pid, &status, 0) != pid) + return pr_perror("Unable to wait the child process"); + + if (WIFEXITED(status)) + return WEXITSTATUS(status); + + return 1; +} + -- 2.23.0