From patchwork Tue Mar 27 13:38:36 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Murphy Zhou X-Patchwork-Id: 891564 X-Patchwork-Delegate: chrubis@suse.cz Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) 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=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 409XD66HG5z9s0b for ; Wed, 28 Mar 2018 00:39:02 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id AA2363E62D2 for ; Tue, 27 Mar 2018 15:38:59 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-6.smtp.seeweb.it (in-6.smtp.seeweb.it [217.194.8.6]) by picard.linux.it (Postfix) with ESMTP id 4A3F53E62C0 for ; Tue, 27 Mar 2018 15:38:58 +0200 (CEST) Received: from mx1.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-6.smtp.seeweb.it (Postfix) with ESMTPS id 74CC5140127B for ; Tue, 27 Mar 2018 15:38:55 +0200 (CEST) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0E2208185928 for ; Tue, 27 Mar 2018 13:38:54 +0000 (UTC) Received: from localhost (dhcp-12-130.nay.redhat.com [10.66.12.130]) by smtp.corp.redhat.com (Postfix) with ESMTP id 76CEC10B0097; Tue, 27 Mar 2018 13:38:53 +0000 (UTC) From: Xiong Zhou To: ltp@lists.linux.it Date: Tue, 27 Mar 2018 21:38:36 +0800 Message-Id: <1522157916-20620-1-git-send-email-xzhou@redhat.com> In-Reply-To: <1522070895-2289-1-git-send-email-xzhou@redhat.com> References: <1522070895-2289-1-git-send-email-xzhou@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 27 Mar 2018 13:38:54 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 27 Mar 2018 13:38:54 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'xzhou@redhat.com' RCPT:'' X-Virus-Scanned: clamav-milter 0.99.2 at in-6.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=-0.0 required=7.0 tests=SPF_PASS, T_RP_MATCHES_RCVD autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-6.smtp.seeweb.it Cc: berrange@redhat.com Subject: [LTP] [PATCH v2] fcntl37: test posix lock across execve X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.18 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" Signed-off-by: Xiong Zhou --- v2: s/LDLIBS/CFLAGS/ in Makefile. No fork() and no another function when checking the lock. TST_NO_DEFAULT_MAIN to pass fd across execve. Update author info. It's from Daniel originally. I copied from fcntl36.c for v1 patch and forgot to update it. Sorry for missing this. testcases/kernel/syscalls/fcntl/Makefile | 11 ++-- testcases/kernel/syscalls/fcntl/fcntl37.c | 101 ++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 testcases/kernel/syscalls/fcntl/fcntl37.c diff --git a/testcases/kernel/syscalls/fcntl/Makefile b/testcases/kernel/syscalls/fcntl/Makefile index ae37214..7a215c8 100644 --- a/testcases/kernel/syscalls/fcntl/Makefile +++ b/testcases/kernel/syscalls/fcntl/Makefile @@ -21,11 +21,14 @@ top_srcdir ?= ../../../.. fcntl33: LDLIBS+=-lrt fcntl33_64: LDLIBS+=-lrt -fcntl34: LDLIBS += -lpthread -fcntl34_64: LDLIBS += -lpthread +fcntl34: CFLAGS += -pthread +fcntl34_64: CFLAGS += -pthread -fcntl36: LDLIBS += -lpthread -fcntl36_64: LDLIBS += -lpthread +fcntl36: CFLAGS += -pthread +fcntl36_64: CFLAGS += -pthread + +fcntl37: CFLAGS += -pthread +fcntl37_64: CFLAGS += -pthread include $(top_srcdir)/include/mk/testcases.mk include $(abs_srcdir)/../utils/newer_64.mk diff --git a/testcases/kernel/syscalls/fcntl/fcntl37.c b/testcases/kernel/syscalls/fcntl/fcntl37.c new file mode 100644 index 0000000..d847ae3 --- /dev/null +++ b/testcases/kernel/syscalls/fcntl/fcntl37.c @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2018 Red Hat Inc. All Rights Reserved. + * + * 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 would 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, see . + * + * Author: "Daniel P. Berrangé" + * + * This is testing for + * + * "Record locks are not inherited by a child created via fork(2), + * but are preserved across an execve(2)." + * + * from fcntl(2) man page. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define TST_NO_DEFAULT_MAIN + +#include "lapi/fcntl.h" +#include "tst_safe_pthread.h" +#include "tst_test.h" + +static const char fname[] = "tst_lock_execve"; + +static void *thread_fn(void *arg) +{ + int fd = *(intptr_t *)arg; + tst_res(TINFO, "Thread running. fd %d", fd); + while(1) sleep(1); + return NULL; +} + +int main(int argc, char *argv[]) +{ + int fd, ret = 0; + + if (argc == 2) { + fd = atoi(argv[1]); + struct flock flck = { + .l_type = F_WRLCK, + .l_whence = SEEK_SET, + .l_start = 0, + .l_len = 1, + }; + SAFE_FCNTL(fd, F_GETLK, &flck); + if (flck.l_type == F_UNLCK) + tst_res(TFAIL, "Record lock gets lost after execve"); + else + tst_res(TPASS, "Record lock survives execve"); + SAFE_CLOSE(fd); + SAFE_UNLINK(fname); + exit(0); + } + + fd = SAFE_OPEN(fname, O_RDWR|O_CREAT, 0755); + struct flock64 flck = { + .l_type = F_WRLCK, + .l_whence = SEEK_SET, + .l_start = 0, + .l_len = 1, + }; + SAFE_FCNTL(fd, F_SETLK, &flck); + + pthread_t th; + SAFE_PTHREAD_CREATE(&th, NULL, thread_fn, (void *)&fd); + sleep(1); + + int flags=SAFE_FCNTL(fd, F_GETFD); + flags &= ~FD_CLOEXEC; + SAFE_FCNTL(fd, F_SETFD, flags); + + char fdstr[10]; + snprintf(fdstr, 10, "%d", fd); + char * const newargv[] = { argv[0], fdstr, NULL }; + tst_res(TINFO, "execve %s with %s locked", argv[0], fname); + ret = execve(argv[0], newargv, NULL); + return ret; +}