From patchwork Wed Apr 8 21:17:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Francis Laniel X-Patchwork-Id: 1268338 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=privacyrequired.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=privacyrequired.com header.i=@privacyrequired.com header.a=rsa-sha256 header.s=stigmate header.b=CDJZgH2c; dkim-atps=neutral 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 48yHDy1h2hz9sR4 for ; Thu, 9 Apr 2020 07:18:10 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 866FE3C2D3C for ; Wed, 8 Apr 2020 23:18:07 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-7.smtp.seeweb.it (in-7.smtp.seeweb.it [IPv6:2001:4b78:1:20::7]) by picard.linux.it (Postfix) with ESMTP id C0BC13C2D42 for ; Wed, 8 Apr 2020 23:18:01 +0200 (CEST) Received: from confino.investici.org (confino.investici.org [IPv6:2a00:c38:11e:ffff::a020]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by in-7.smtp.seeweb.it (Postfix) with ESMTPS id BF5D1200C94 for ; Wed, 8 Apr 2020 23:18:00 +0200 (CEST) Received: from mx1.investici.org (unknown [127.0.0.1]) by confino.investici.org (Postfix) with ESMTP id 2EFB42171D; Wed, 8 Apr 2020 21:17:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=privacyrequired.com; s=stigmate; t=1586380679; bh=Ecu5wwbk7katxb8msk79U0Xe0/46vfR4pzxIHq12zV4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CDJZgH2cYFV/0a9Fm6ET7Ow6ap26l907vk0rhtHQE6Vq2VlpG39f//78ImVPQNAXg XbTUtyywFgTqm7ljF+nf2cheehXoZ8/ZhIi3326V8jU1FUHz9WCM/ideYRK4i43T9I TNDikXN2JanNnzt5EgUISDjgTdjeEt8nf/BQUVEU= Received: from [212.103.72.250] (mx1.investici.org [212.103.72.250]) (Authenticated sender: laniel_francis@privacyrequired.com) by localhost (Postfix) with ESMTPSA id 0E3E320EB7; Wed, 8 Apr 2020 21:17:59 +0000 (UTC) From: laniel_francis@privacyrequired.com To: ltp@lists.linux.it Date: Wed, 8 Apr 2020 23:17:00 +0200 Message-Id: <20200408211700.7958-2-laniel_francis@privacyrequired.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <208a947b-a64a-0962-b3de-0756f1cc1c92@cn.fujitsu.com> References: <208a947b-a64a-0962-b3de-0756f1cc1c92@cn.fujitsu.com> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-7.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU, SPF_HELO_PASS, SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-7.smtp.seeweb.it Subject: [LTP] [PATCH v2 1/2] Add SAFE_PIPE2. 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" From: Francis Laniel This macro is the equivalent of SAFE_PIPE for pipe2 system call. Reviewed-by: Yang Xu --- include/safe_macros_fn.h | 3 +++ include/tst_safe_macros.h | 3 +++ lib/safe_macros.c | 15 +++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/include/safe_macros_fn.h b/include/safe_macros_fn.h index 3df952811..ec2d34ae3 100644 --- a/include/safe_macros_fn.h +++ b/include/safe_macros_fn.h @@ -67,6 +67,9 @@ int safe_open(const char *file, const int lineno, int safe_pipe(const char *file, const int lineno, void (*cleanup_fn)(void), int fildes[2]); +int safe_pipe2(const char *file, const int lineno, void (*cleanup_fn) (void), + int fildes[2], int flags); + ssize_t safe_read(const char *file, const int lineno, void (*cleanup_fn)(void), char len_strict, int fildes, void *buf, size_t nbyte); diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h index d95d26219..1738d3cc6 100644 --- a/include/tst_safe_macros.h +++ b/include/tst_safe_macros.h @@ -87,6 +87,9 @@ static inline int safe_dup(const char *file, const int lineno, #define SAFE_PIPE(fildes) \ safe_pipe(__FILE__, __LINE__, NULL, (fildes)) +#define SAFE_PIPE2(fildes, flags) \ + safe_pipe2(__FILE__, __LINE__, NULL, (fildes), (flags)) + #define SAFE_READ(len_strict, fildes, buf, nbyte) \ safe_read(__FILE__, __LINE__, NULL, (len_strict), (fildes), (buf), (nbyte)) diff --git a/lib/safe_macros.c b/lib/safe_macros.c index dde9b7b5e..780245821 100644 --- a/lib/safe_macros.c +++ b/lib/safe_macros.c @@ -248,6 +248,21 @@ int safe_pipe(const char *file, const int lineno, void (*cleanup_fn) (void), return rval; } +int safe_pipe2(const char *file, const int lineno, void (*cleanup_fn) (void), + int fildes[2], int flags) +{ + int rval; + + rval = pipe2(fildes, flags); + if (rval == -1) { + tst_brkm(TBROK | TERRNO, cleanup_fn, + "%s:%d: pipe2({%d,%d}) failed", + file, lineno, fildes[0], fildes[1]); + } + + return rval; +} + ssize_t safe_read(const char *file, const int lineno, void (*cleanup_fn) (void), char len_strict, int fildes, void *buf, size_t nbyte) { From patchwork Wed Apr 8 21:17:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Francis Laniel X-Patchwork-Id: 1268339 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=privacyrequired.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=privacyrequired.com header.i=@privacyrequired.com header.a=rsa-sha256 header.s=stigmate header.b=HxRj4Jk6; dkim-atps=neutral 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 48yHF82Syrz9sP7 for ; Thu, 9 Apr 2020 07:18:20 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id A83CB3C2D31 for ; Wed, 8 Apr 2020 23:18:17 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-7.smtp.seeweb.it (in-7.smtp.seeweb.it [217.194.8.7]) by picard.linux.it (Postfix) with ESMTP id ED3663C2D3D for ; Wed, 8 Apr 2020 23:18:02 +0200 (CEST) Received: from confino.investici.org (confino.investici.org [IPv6:2a00:c38:11e:ffff::a020]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by in-7.smtp.seeweb.it (Postfix) with ESMTPS id 7D62B200CFE for ; Wed, 8 Apr 2020 23:18:02 +0200 (CEST) Received: from mx1.investici.org (unknown [127.0.0.1]) by confino.investici.org (Postfix) with ESMTP id 9ACD521722; Wed, 8 Apr 2020 21:18:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=privacyrequired.com; s=stigmate; t=1586380681; bh=E0zCn0YqzbQOktOrg3Xfa49F1op5nwRKWy/PuGqx4tQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HxRj4Jk6AXl4MBamucxd18gCi5eI61egOC6P9ZmTht5FKaaZTehDvZcZhsunsxhnE RG7ArPSKhqAvzk1dmNGCS9oNpubhBDsbSI9RMbORw3i8W4SgsDGlpTZw3rLbG2OoAM ApFAsT1ZiYYuSWnwb4WHqw1HMR7ceBkTlEDy2nsI= Received: from [212.103.72.250] (mx1.investici.org [212.103.72.250]) (Authenticated sender: laniel_francis@privacyrequired.com) by localhost (Postfix) with ESMTPSA id 7A19A20EB7; Wed, 8 Apr 2020 21:18:01 +0000 (UTC) From: laniel_francis@privacyrequired.com To: ltp@lists.linux.it Date: Wed, 8 Apr 2020 23:17:01 +0200 Message-Id: <20200408211700.7958-3-laniel_francis@privacyrequired.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <208a947b-a64a-0962-b3de-0756f1cc1c92@cn.fujitsu.com> References: <208a947b-a64a-0962-b3de-0756f1cc1c92@cn.fujitsu.com> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-7.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU, SPF_HELO_PASS, SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-7.smtp.seeweb.it Subject: [LTP] [PATCH v2 2/2] Add new test for pipe2 with/without O_NONBLOCK mode. 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" From: Francis Laniel The new test (pipe2_03.c) checks the following: 1. Create a pipe with O_NONBLOCK. 2. Check that this flag is set. 3. Check that pipe size is 16 * PAGE_SIZE. 4. Reduce pipe size to PAGE_SIZE. 5. Write buffer bigger than page size and see that second write fails. 6. Set pipe's flags to default. 7. Fork and do a write in the child, its state must be 'S' and is checked from the father. --- testcases/kernel/syscalls/pipe2/.gitignore | 1 + testcases/kernel/syscalls/pipe2/pipe2_03.c | 128 +++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 testcases/kernel/syscalls/pipe2/pipe2_03.c diff --git a/testcases/kernel/syscalls/pipe2/.gitignore b/testcases/kernel/syscalls/pipe2/.gitignore index cd38bb309..01d980dba 100644 --- a/testcases/kernel/syscalls/pipe2/.gitignore +++ b/testcases/kernel/syscalls/pipe2/.gitignore @@ -1,2 +1,3 @@ /pipe2_01 /pipe2_02 +/pipe2_03 diff --git a/testcases/kernel/syscalls/pipe2/pipe2_03.c b/testcases/kernel/syscalls/pipe2/pipe2_03.c new file mode 100644 index 000000000..c2b182e02 --- /dev/null +++ b/testcases/kernel/syscalls/pipe2/pipe2_03.c @@ -0,0 +1,128 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2020 Francis Laniel. All rights reserved. + * Author: Francis Laniel + * + * Test Description: + * This Program tests getting and setting the pipe size. + * It also tests what happen when you write to a full pipe depending on whether + * O_NONBLOCK is set or not. + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#include "lapi/fcntl.h" +#include "tst_test.h" + +#define PAGE_NR 16 +#define SECONDS 3 +#define MICROSECONDS 0 + +static int fds[2]; +static long page_size; + +static void setup(void) +{ + /* + * Create the pipe with O_NONBLOCK. + */ + SAFE_PIPE2(fds, O_NONBLOCK); + + /* + * Get the system page size. + */ + page_size = SAFE_SYSCONF(_SC_PAGESIZE); +} + +static void test_pipe2(void) +{ + long flags; + long pipe_size; + + char *buf; + + pid_t pid; + int status; + + flags = SAFE_FCNTL(fds[0], F_GETFL); + + if (!(flags & O_NONBLOCK)) + tst_res(TFAIL, "O_NONBLOCK flag must be set."); + + pipe_size = SAFE_FCNTL(fds[0], F_GETPIPE_SZ); + + if (pipe_size != page_size * PAGE_NR) + tst_res(TFAIL, "Default pipe page is 16 * 4096 = 65536B."); + + /* + * A pipe has two file descriptors. + * But in the kernel these two file descriptors point to the same pipe. + * So setting size from first file handle set size for the pipe. + */ + SAFE_FCNTL(fds[0], F_SETPIPE_SZ, 0); + + /* + * So getting size from the second file descriptor return the size of + * the pipe which was changed before with first file descriptor. + */ + pipe_size = SAFE_FCNTL(fds[1], F_GETPIPE_SZ); + + if (pipe_size != page_size) + tst_res(TFAIL, "Pipe size (%ld) must be page size (%ld)", + pipe_size, page_size); + + buf = alloca(page_size); + + SAFE_WRITE(1, fds[1], buf, page_size); + + /* + * This write should return -1 because pipe is already full. + */ + if (write(fds[1], buf, page_size) != -1) + tst_res(TFAIL | TERRNO, "write() succeeded and should not"); + + SAFE_FCNTL(fds[1], F_SETFL, flags & ~O_NONBLOCK); + + flags = SAFE_FCNTL(fds[1], F_GETFL); + + if (flags & O_NONBLOCK) + tst_res(TFAIL, "O_NONBLOCK flag must not be set."); + + pid = SAFE_FORK(); + + /* + * Since writes are now blocking the child must wait forever on this + * write. + */ + if (!pid) + SAFE_WRITE(1, fds[1], buf, page_size); + + if (TST_PROCESS_STATE_WAIT(pid, 'S', 1000)) + tst_res(TFAIL, "Child must be stopped."); + else + tst_res(TPASS, "Child is stopped."); + + SAFE_KILL(pid, SIGKILL); + + SAFE_WAIT(&status); +} + +static void cleanup(void) +{ + for (int i = 0; i < 2; i++) + if (fds[i] > 0) + SAFE_CLOSE(fds[i]); +} + +static struct tst_test test = { + .test_all = test_pipe2, + .setup = setup, + .cleanup = cleanup, + .forks_child = 1, +}; \ No newline at end of file