From patchwork Fri Sep 28 06:24:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoming Ni X-Patchwork-Id: 976049 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=none (p=none dis=none) header.from=huawei.com Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42M2tr4RqQz9s1x for ; Fri, 28 Sep 2018 17:12:36 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 18EB23E6611 for ; Fri, 28 Sep 2018 09:12:31 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-5.smtp.seeweb.it (in-5.smtp.seeweb.it [IPv6:2001:4b78:1:20::5]) by picard.linux.it (Postfix) with ESMTP id 08C4F3E601F for ; Fri, 28 Sep 2018 09:12:28 +0200 (CEST) Received: from huawei.com (szxga06-in.huawei.com [45.249.212.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by in-5.smtp.seeweb.it (Postfix) with ESMTPS id 369C86010DC for ; Fri, 28 Sep 2018 09:12:27 +0200 (CEST) Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 0127B82EF1BC6 for ; Fri, 28 Sep 2018 15:12:24 +0800 (CST) Received: from linux-work.huawei.com (10.67.189.174) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.399.0; Fri, 28 Sep 2018 15:12:18 +0800 From: nixiaoming To: , Date: Fri, 28 Sep 2018 14:24:11 +0800 Message-ID: <20180928062411.93875-1-nixiaoming@huawei.com> X-Mailer: git-send-email 2.10.1 MIME-Version: 1.0 X-Originating-IP: [10.67.189.174] X-CFilter-Loop: Reflected X-Virus-Scanned: clamav-milter 0.99.2 at in-5.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=-0.0 required=7.0 tests=SPF_HELO_PASS,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-5.smtp.seeweb.it Cc: nixiaoming@huawei.com Subject: [LTP] [PATCH v5] syscalls/fanotify11: new test for report thread id 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" syscalls/fanotify11 test fanotify_init with FAN_EVENT_INFO_TID data->pid is ID(pid) of the thread that caused the event test fanotify_init without FAN_EVENT_INFO_TID data->pid is ID(tgid) of the process that caused the event Signed-off-by: nixiaoming --- this is not testing an upstream feature, only RFC. runtest/syscalls | 1 + testcases/kernel/syscalls/fanotify/.gitignore | 1 + testcases/kernel/syscalls/fanotify/Makefile | 2 +- testcases/kernel/syscalls/fanotify/fanotify11.c | 131 ++++++++++++++++++++++++ 4 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 testcases/kernel/syscalls/fanotify/fanotify11.c diff --git a/runtest/syscalls b/runtest/syscalls index 0d0be77..84b0d6e 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -497,6 +497,7 @@ fanotify06 fanotify06 fanotify07 fanotify07 fanotify08 fanotify08 fanotify09 fanotify09 +fanotify11 fanotify11 ioperm01 ioperm01 ioperm02 ioperm02 diff --git a/testcases/kernel/syscalls/fanotify/.gitignore b/testcases/kernel/syscalls/fanotify/.gitignore index ec75539..62180ab 100644 --- a/testcases/kernel/syscalls/fanotify/.gitignore +++ b/testcases/kernel/syscalls/fanotify/.gitignore @@ -7,3 +7,4 @@ /fanotify07 /fanotify08 /fanotify09 +/fanotify11 diff --git a/testcases/kernel/syscalls/fanotify/Makefile b/testcases/kernel/syscalls/fanotify/Makefile index bb58878..5d01b48 100644 --- a/testcases/kernel/syscalls/fanotify/Makefile +++ b/testcases/kernel/syscalls/fanotify/Makefile @@ -17,7 +17,7 @@ # top_srcdir ?= ../../../.. - +fanotify11: CFLAGS+=-pthread include $(top_srcdir)/include/mk/testcases.mk include $(top_srcdir)/include/mk/generic_leaf_target.mk diff --git a/testcases/kernel/syscalls/fanotify/fanotify11.c b/testcases/kernel/syscalls/fanotify/fanotify11.c new file mode 100644 index 0000000..3de4531 --- /dev/null +++ b/testcases/kernel/syscalls/fanotify/fanotify11.c @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2018 Huawei. All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 or any later of the GNU General Public License + * as published by the Free Software Foundation. + * + * 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. + * + * Further, this software is distributed without any warranty that it is + * free of the rightful claim of any third person regarding infringement + * or the like. Any license provided herein, whether implied or + * otherwise, applies only to this software file. Patent licenses, if + * any, provided herein do not apply to combinations of this program with + * other software, or any other product whatsoever. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Started by nixiaoming + * + * DESCRIPTION + * After fanotify_init adds flags FAN_EVENT_INFO_TID, + * check whether the program can accurately identify which thread id + * in the multithreaded program triggered the event.. + * + */ +#define _GNU_SOURCE +#include "config.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "tst_test.h" +#include "tst_safe_pthread.h" +#include "fanotify.h" + +#if defined(HAVE_SYS_FANOTIFY_H) +#include +#ifdef FAN_EVENT_INFO_TID + +#define gettid() syscall(SYS_gettid) +static int tid; + +void *thread_create_file(void *arg LTP_ATTRIBUTE_UNUSED) +{ + char tid_file[64] = {0}; + + tid = gettid(); + snprintf(tid_file, sizeof(tid_file), "test_tid_%d", tid); + SAFE_FILE_PRINTF(tid_file, "1"); + + pthread_exit(0); +} + +static unsigned int tcases[] = { + FAN_CLASS_NOTIF, + FAN_CLASS_NOTIF | FAN_EVENT_INFO_TID +}; + +void test01(unsigned int i) +{ + int ret; + pthread_t p_id; + struct fanotify_event_metadata data; + int fd_notify; + int tgid = getpid(); + + fd_notify = fanotify_init(tcases[i], 0); + if (fd_notify < 0) { + if (errno == EINVAL && (tcases[i] & FAN_EVENT_INFO_TID)) + return; + tst_brk(TBROK | TERRNO, "fanotify_init(0x%x, 0) failed", + tcases[i]); + } + + ret = fanotify_mark(fd_notify, FAN_MARK_ADD, + FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD, AT_FDCWD, "."); + if (ret != 0) + tst_brk(TBROK, "fanotify_mark FAN_MARK_ADD fail ret=%d\n", ret); + + SAFE_PTHREAD_CREATE(&p_id, NULL, thread_create_file, NULL); + + SAFE_READ(0, fd_notify, &data, sizeof(struct fanotify_event_metadata)); + tst_res(TINFO, "%s FAN_EVENT_INFO_TID: tgid=%d, tid=%d, data.pid=%d\n", + (tcases[i] & FAN_EVENT_INFO_TID) ? "with" : "without", + tgid, tid, data.pid); + if ((tcases[i] & FAN_EVENT_INFO_TID) && (data.pid == tid)) + tst_res(TPASS, "data.pid == tid\n"); + else if (!(tcases[i] & FAN_EVENT_INFO_TID) && (data.pid == tgid)) + tst_res(TPASS, "data.pid == tgid\n"); + else + tst_res(TFAIL, "fail.."); + + if (data.fd != FAN_NOFD) + SAFE_CLOSE(data.fd); + SAFE_CLOSE(fd_notify); + SAFE_PTHREAD_JOIN(p_id, NULL); +} + + +static struct tst_test test = { + .test = test01, + .tcnt = ARRAY_SIZE(tcases), + .needs_tmpdir = 1, + .needs_root = 1, +}; + + +#else +TST_TEST_TCONF("system doesn't support flags: FAN_EVENT_INFO_TID"); + +#endif /* ifdef FAN_EVENT_INFO_TID */ + +#else +TST_TEST_TCONF("system doesn't have required fanotify support"); +#endif