From patchwork Thu Aug 4 08:48:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Cervesato X-Patchwork-Id: 1663592 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=suse.com header.i=@suse.com header.a=rsa-sha256 header.s=susede1 header.b=O1pDYnVn; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Lz2T5447Tz9sGR for ; Thu, 4 Aug 2022 18:48:56 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id AE5543C93E4 for ; Thu, 4 Aug 2022 10:48:52 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-3.smtp.seeweb.it (in-3.smtp.seeweb.it [IPv6:2001:4b78:1:20::3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by picard.linux.it (Postfix) with ESMTPS id 27BCA3C5AA6 for ; Thu, 4 Aug 2022 10:48:50 +0200 (CEST) Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by in-3.smtp.seeweb.it (Postfix) with ESMTPS id 45B291A011DF for ; Thu, 4 Aug 2022 10:48:50 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id A89F43760E; Thu, 4 Aug 2022 08:48:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1659602929; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Y1uXtKMRAJs4bIaU3a9HlmfzJu+IJ+1zdhYhpcXpu2M=; b=O1pDYnVndLqOPHz4RB07wU2MxfrPQaT2fwtziywHYHXc0YTqnr8D+0hDZPcJ/X0zDF9mwE MIYvHIfIxqHcEmuW3uzzWaX5PAKytMDPMyQwE1qipq5s1qJQGrfUr2VvTCa07017usPlmj uI5DwaxeX8pfKwNIIOJ3gUjMe5OTuzY= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 3E6641348A; Thu, 4 Aug 2022 08:48:49 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 0AgyCfGH62IWPAAAMHmgww (envelope-from ); Thu, 04 Aug 2022 08:48:49 +0000 To: ltp@lists.linux.it Date: Thu, 4 Aug 2022 10:48:12 +0200 Message-Id: <20220804084812.22737-2-andrea.cervesato@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220804084812.22737-1-andrea.cervesato@suse.com> References: <20220804084812.22737-1-andrea.cervesato@suse.com> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.102.4 at in-3.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,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-3.smtp.seeweb.it Subject: [LTP] [PATCH v1 2/2] Refactor pidns02 test using new LTP API 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: , X-Patchwork-Original-From: Andrea Cervesato via ltp From: Andrea Cervesato Reply-To: Andrea Cervesato Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Signed-off-by: Andrea Cervesato --- testcases/kernel/containers/pidns/pidns02.c | 130 +++++--------------- 1 file changed, 30 insertions(+), 100 deletions(-) diff --git a/testcases/kernel/containers/pidns/pidns02.c b/testcases/kernel/containers/pidns/pidns02.c index 2bc9035d8..a106ed8b7 100644 --- a/testcases/kernel/containers/pidns/pidns02.c +++ b/testcases/kernel/containers/pidns/pidns02.c @@ -1,114 +1,44 @@ +// SPDX-License-Identifier: GPL-2.0 /* -* Copyright (c) International Business Machines Corp., 2007 -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See -* the GNU General Public License for more details. -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -* -*************************************************************************** - -* File: pidns02.c -* -* Description: -* The pidns02.c testcase builds into the ltp framework to verify -* the basic functionality of PID Namespace. -* -* Verify that: -* 1. When parent clone a process with flag CLONE_NEWPID, the session ID of -* child should be always one. -* -* 2. When parent clone a process with flag CLONE_NEWPID, the parent process group ID -* should be always one. -* -* Total Tests -* -* Test Name: pidns02 -* -* Test Assertion & Strategy: -* -* From main() clone a new child process with passing the clone_flag as CLONE_NEWPID, -* Call the setid() inside container. -* Inside the cloned pid check for the getsid(0) and getpgid(0) -* Verify with global macro defined value for parent pid & child pid. -* -* Usage: -* pidns02 -*/ - -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include "pidns_helper.h" -#include "test.h" + * Copyright (c) International Business Machines Corp., 2007 + * Copyright (C) 2022 SUSE LLC Andrea Cervesato + */ -char *TCID = "pidns02"; -int TST_TOTAL = 1; +/*\ + * [Description] + * + * Clone a process with CLONE_NEWPID flag and check: + * + * - child session ID must be 1 + * - parent process group ID must be 1 + */ -#define PGID 1 -#define SID 1 +#include "tst_test.h" +#include "lapi/namespaces_constants.h" -/* - * child_fn1() - Inside container - */ -int child_fn1(void *vtest) +static int child_func(LTP_ATTRIBUTE_UNUSED void *arg) { - pid_t pgid, sid; + pid_t sid, pgid; - setsid(); - - pgid = getpgid(0); sid = getsid(0); + pgid = getpgid(0); - printf("Checking session id & group id inside container\n"); - if (pgid == PGID && sid == SID) { - printf("Success: Got Group ID = %d & Session ID = %d\n", - pgid, sid); - exit(0); - } else { - printf("Got unexpected result of Group ID = %d & Session ID = " - "%d\n", pgid, sid); - exit(1); - } -} + TST_EXP_PASS(sid == 1); + TST_EXP_PASS(pgid == 1); -static void setup(void) -{ - tst_require_root(); - check_newpid(); + return 0; } -int main(void) +static void run(void) { - int status; - - setup(); - - TEST(do_clone_unshare_test(T_CLONE, CLONE_NEWPID, child_fn1, NULL)); - if (TEST_RETURN == -1) { - tst_brkm(TFAIL | TTERRNO, NULL, "clone failed"); - } else if ((wait(&status)) == -1) { - tst_brkm(TFAIL | TERRNO, NULL, "wait failed"); - } - - if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { - tst_resm(TFAIL | TERRNO, "child exited abnormally"); - } else if (WIFSIGNALED(status)) { - tst_resm(TFAIL | TERRNO, "child exited with signal %d", - WTERMSIG(status)); - } - - tst_exit(); + int ret; + ret = ltp_clone_quick(CLONE_NEWNS | SIGCHLD, child_func, NULL); + if (ret < 0) + tst_brk(TBROK | TERRNO, "clone failed"); } + +static struct tst_test test = { + .test_all = run, + .needs_root = 1, +};