From patchwork Wed Sep 19 20:50:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Safonov X-Patchwork-Id: 10606543 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D090C1508 for ; Wed, 19 Sep 2018 20:51:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C08792CBF7 for ; Wed, 19 Sep 2018 20:51:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BE4B92CC43; Wed, 19 Sep 2018 20:51:09 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 360862CC0A for ; Wed, 19 Sep 2018 20:51:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733101AbeITCaq (ORCPT ); Wed, 19 Sep 2018 22:30:46 -0400 Received: from mail-ed1-f68.google.com ([209.85.208.68]:38070 "EHLO mail-ed1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733070AbeITCap (ORCPT ); Wed, 19 Sep 2018 22:30:45 -0400 Received: by mail-ed1-f68.google.com with SMTP id h33-v6so6046643edb.5 for ; Wed, 19 Sep 2018 13:51:05 -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; bh=IWl+BsB9BfevdUY2WNa0fYCOuIWf1cvA+H9ptzwfKtc=; b=T+GbgY2rJRZcmW0Pnz/ocICcE6cmIe5Dqnz2XO4s+Jw0UfHOg0JDk/nl1QgrYgVgXe oxHjNWCWXlxJmFhRS+/nIKnJ9DSaYYMVuZgXXQo10koRWHP1cP8WKMjeAVn4VkXbh0Ak LDe/53p7rZaQB2LqWupbnfUrcm6lsQGMM0/W7kk/uIZGMMYHgqRfqc4qkFk/Fc80lnWP rjS+x40PDGYLawc1RFpCEw51WPeJaPAMcRPwVS9x4uUzn0zThrorWhmTFYGaHJeksEU/ uqF+yOzNLsm9Vj7XuYcvAeX+UgY9l9j7/fxmdai6mfeuwBj3wcTYwF612L82VVJegur9 +g7A== 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; bh=IWl+BsB9BfevdUY2WNa0fYCOuIWf1cvA+H9ptzwfKtc=; b=Vcht2qTpxtfkd+5TlrEl6mr3xYS0F/u6SkYI6K77IAiXzSaplsNMjLhFFif2ztT3es wCu8jso1e/4krsIJGb0BOQxEUokDR7mQJFqMDihD5YKCt6tPpuvGXzYg+0SQpRHjrGAB GqGybDUfl8vbnzvSgdEQRzkEmKLweGUQL0SaxbycBHURRudgfrlepuZrK8gkMKkT1wec F7cGN6/oyuFff5JcynZE+VYeDDSBRmCdF4w+QOTOrcEkTfT4BMEI3vX+CTRMo2G+KPP6 ACexQ19vDQQvp2SJqVQUke0Am79lymC5WsZ9DgP0Nzxvnf3gDNM2S4gyJAcgOSEymqCe /Tyg== X-Gm-Message-State: APzg51AEZ1VY8EUFglCGTsMxhbJ59pHIKB5FbAvBA+MFkX26x+L3gIWM ERezD/DOvNuwwuGVgsowHa7CMw== X-Google-Smtp-Source: ANB0Vdb/vmWbrce2pDgWj184qzrAel450u17meixyH+nIh0zfHrdIOxYQHnGbq34iZH+WQZ1Q+hBjA== X-Received: by 2002:a50:82c5:: with SMTP id 63-v6mr61268513edg.200.1537390264720; Wed, 19 Sep 2018 13:51:04 -0700 (PDT) Received: from dhcp.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id t17-v6sm1747729edb.27.2018.09.19.13.51.03 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 19 Sep 2018 13:51:04 -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 , Christian Brauner , Cyrill Gorcunov , "Eric W. Biederman" , "H. Peter Anvin" , Ingo Molnar , Jeff Dike , Oleg Nesterov , Pavel Emelyanov , Shuah Khan , Thomas Gleixner , containers@lists.linux-foundation.org, criu@openvz.org, linux-api@vger.kernel.org, x86@kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC 17/20] selftest/timens: Add test for timerfd Date: Wed, 19 Sep 2018 21:50:34 +0100 Message-Id: <20180919205037.9574-18-dima@arista.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20180919205037.9574-1-dima@arista.com> References: <20180919205037.9574-1-dima@arista.com> Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Andrei Vagin Check that timerfd_create takes into account clock offsets. Cc: linux-kselftest@vger.kernel.org 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/timerfd.c | 96 +++++++++++++++++++++++++++++++ 3 files changed, 98 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..914a4cd9a0df --- /dev/null +++ b/tools/testing/selftests/timens/timerfd.c @@ -0,0 +1,96 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "log.h" + +#ifndef CLONE_NEWTIME +# define CLONE_NEWTIME 0x00001000 +#endif + +int run_test(int clockid) +{ + struct itimerspec new_value; + struct timespec now; + long long elapsed; + int fd, i; + + if (clock_gettime(clockid, &now)) + return pr_perror("clock_gettime"); + + for (i = 0; i < 2; i++) { + int flags = 0; + + pr_msg("timerfd_settime: %d", "INFO", clockid); + 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"); + + if (i == 1) + flags |= TFD_TIMER_ABSTIME; + + if (timerfd_settime(fd, flags, &new_value, NULL)) + return pr_perror("timerfd_settime"); + + if (timerfd_gettime(fd, &new_value)) + return pr_perror("timerfd_gettime"); + + elapsed = new_value.it_value.tv_sec; + if (abs(elapsed - 3600) > 60) { + printf("FAIL\n"); + return 1; + } + + close(fd); + } + + printf("PASS\n"); + + return 0; +} + +int main(int argc, char *argv[]) +{ + struct timespec tp; + int ret; + + if (unshare(CLONE_NEWTIME)) + return pr_perror("unshare"); + + if (clock_gettime(CLOCK_MONOTONIC, &tp)) + return pr_perror("clock_gettime"); + tp.tv_sec = 7 * 24 * 3600; + if (clock_settime(CLOCK_MONOTONIC, &tp)) + return pr_perror("clock_settime"); + + if (clock_gettime(CLOCK_BOOTTIME, &tp)) + return pr_perror("clock_gettime"); + tp.tv_sec += 9 * 24 * 3600; + tp.tv_nsec = 0; + if (clock_settime(CLOCK_BOOTTIME, &tp)) + return pr_perror("clock_settime"); + + ret = 0; + ret |= run_test(CLOCK_BOOTTIME); + ret |= run_test(CLOCK_MONOTONIC); + return ret; +} +