From patchwork Mon Mar 26 13:28:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Murphy Zhou X-Patchwork-Id: 890945 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 408w2T5J2lz9s2t for ; Tue, 27 Mar 2018 00:28:33 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id E492C3E642E for ; Mon, 26 Mar 2018 15:28:30 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-4.smtp.seeweb.it (in-4.smtp.seeweb.it [217.194.8.4]) by picard.linux.it (Postfix) with ESMTP id 14DB33E6417 for ; Mon, 26 Mar 2018 15:28:29 +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-4.smtp.seeweb.it (Postfix) with ESMTPS id 914761000520 for ; Mon, 26 Mar 2018 15:28:28 +0200 (CEST) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 51C248424D for ; Mon, 26 Mar 2018 13:28:26 +0000 (UTC) Received: from localhost (unknown [10.66.12.130]) by smtp.corp.redhat.com (Postfix) with ESMTP id AE636215CDAE; Mon, 26 Mar 2018 13:28:25 +0000 (UTC) From: Xiong Zhou To: ltp@lists.linux.it Date: Mon, 26 Mar 2018 21:28:15 +0800 Message-Id: <1522070895-2289-1-git-send-email-xzhou@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Mon, 26 Mar 2018 13:28:26 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Mon, 26 Mar 2018 13:28:26 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.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-4.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-4.smtp.seeweb.it Cc: berrange@redhat.com Subject: [LTP] [PATCH] 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: , MIME-Version: 1.0 Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Signed-off-by: Xiong Zhou --- testcases/kernel/syscalls/fcntl/Makefile | 3 + testcases/kernel/syscalls/fcntl/fcntl37.c | 146 ++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+) 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..7a06aa7 100644 --- a/testcases/kernel/syscalls/fcntl/Makefile +++ b/testcases/kernel/syscalls/fcntl/Makefile @@ -27,6 +27,9 @@ fcntl34_64: LDLIBS += -lpthread fcntl36: LDLIBS += -lpthread fcntl36_64: LDLIBS += -lpthread +fcntl37: LDLIBS += -lpthread +fcntl37_64: LDLIBS += -lpthread + 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..bac2168 --- /dev/null +++ b/testcases/kernel/syscalls/fcntl/fcntl37.c @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2018 RedHat 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: Xiong Zhou + * + * 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 + +#include "lapi/fcntl.h" +#include "tst_safe_pthread.h" +#include "tst_test.h" + +static const char fname[] = "tst_lock_execve"; +static const char flag_fname[] = "/tmp/execved"; +static void cleanup(void); + +static void *thread_fn(void *arg) +{ + int fd = *(int *)arg; + tst_res(TINFO, "Thread running. fd %d", fd); + /* Just need to be alive when execve. */ + sleep(5); + SAFE_CLOSE(fd); + return NULL; +} + +static void checklock(int fd) +{ + pid_t pid = SAFE_FORK(); + if (pid == 0) { + 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); + _exit(0); + } + waitpid(pid, NULL, 0); +} + +static void test01(void) +{ + int fd, ret; + struct stat stat_buf; + + /* + * If tmp/tst_lock_execve exists, execve to run checklock. + */ + ret = stat(flag_fname, &stat_buf); + if (ret == 0) { + checklock(fd); + cleanup(); + _exit(0); + } + + /* + * If tmp/tst_lock_execve does not exist, initialize it. + */ + SAFE_OPEN(flag_fname, O_RDWR|O_CREAT, 0755); + 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); + + /* + * Creat thread and keep it running after placing posix + * write lock. + */ + pthread_t th; + SAFE_PTHREAD_CREATE(&th, NULL, thread_fn, (void *)&fd); + sleep(1); + + /* + * Clear CLOEXEC + */ + int flags=SAFE_FCNTL(fd, F_GETFD); + flags &= ~FD_CLOEXEC; + SAFE_FCNTL(fd, F_SETFD, flags); + + /* + * Get full path name of running programm then execve. + */ + char prog_name[PATH_MAX]; + ret = readlink("/proc/self/exe", prog_name, PATH_MAX); + char * const newargv[] = { prog_name, NULL, NULL }; + tst_res(TINFO, "execve %s with %s locked", prog_name, fname); + execve(prog_name, newargv, NULL); + /* + * Failure info for debug + */ + perror("execve "); +} + +static void cleanup(void) +{ + SAFE_UNLINK(flag_fname); +} + +static struct tst_test test = { + .needs_tmpdir = 1, + .forks_child = 1, + .test_all = test01, + .cleanup = cleanup, +};