1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2018 Huawei. All Rights Reserved. 4f08c3bdfSopenharmony_ci * 5f08c3bdfSopenharmony_ci * Started by nixiaoming <nixiaoming@huawei.com> 6f08c3bdfSopenharmony_ci */ 7f08c3bdfSopenharmony_ci 8f08c3bdfSopenharmony_ci/*\ 9f08c3bdfSopenharmony_ci * [Description] 10f08c3bdfSopenharmony_ci * After fanotify_init adds flags FAN_REPORT_TID, 11f08c3bdfSopenharmony_ci * check whether the program can accurately identify which thread id 12f08c3bdfSopenharmony_ci * in the multithreaded program triggered the event. 13f08c3bdfSopenharmony_ci */ 14f08c3bdfSopenharmony_ci 15f08c3bdfSopenharmony_ci#define _GNU_SOURCE 16f08c3bdfSopenharmony_ci#include "config.h" 17f08c3bdfSopenharmony_ci 18f08c3bdfSopenharmony_ci#include <stdio.h> 19f08c3bdfSopenharmony_ci#include <stdlib.h> 20f08c3bdfSopenharmony_ci 21f08c3bdfSopenharmony_ci#include <sys/stat.h> 22f08c3bdfSopenharmony_ci#include <sys/types.h> 23f08c3bdfSopenharmony_ci#include <errno.h> 24f08c3bdfSopenharmony_ci#include <string.h> 25f08c3bdfSopenharmony_ci#include <unistd.h> 26f08c3bdfSopenharmony_ci#include <sys/syscall.h> 27f08c3bdfSopenharmony_ci#include <pthread.h> 28f08c3bdfSopenharmony_ci#include <sys/stat.h> 29f08c3bdfSopenharmony_ci#include <linux/limits.h> 30f08c3bdfSopenharmony_ci#include "tst_test.h" 31f08c3bdfSopenharmony_ci#include "tst_safe_pthread.h" 32f08c3bdfSopenharmony_ci 33f08c3bdfSopenharmony_ci#ifdef HAVE_SYS_FANOTIFY_H 34f08c3bdfSopenharmony_ci#include "fanotify.h" 35f08c3bdfSopenharmony_ci 36f08c3bdfSopenharmony_ci#define gettid() syscall(SYS_gettid) 37f08c3bdfSopenharmony_cistatic int tid; 38f08c3bdfSopenharmony_ci 39f08c3bdfSopenharmony_cistatic int fan_report_tid_unsupported; 40f08c3bdfSopenharmony_ci 41f08c3bdfSopenharmony_cistatic void *thread_create_file(void *arg LTP_ATTRIBUTE_UNUSED) 42f08c3bdfSopenharmony_ci{ 43f08c3bdfSopenharmony_ci char tid_file[64] = {0}; 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_ci tid = gettid(); 46f08c3bdfSopenharmony_ci snprintf(tid_file, sizeof(tid_file), "test_tid_%d", tid); 47f08c3bdfSopenharmony_ci SAFE_FILE_PRINTF(tid_file, "1"); 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_ci pthread_exit(0); 50f08c3bdfSopenharmony_ci} 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_cistatic unsigned int tcases[] = { 53f08c3bdfSopenharmony_ci FAN_CLASS_NOTIF, 54f08c3bdfSopenharmony_ci FAN_CLASS_NOTIF | FAN_REPORT_TID 55f08c3bdfSopenharmony_ci}; 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_cistatic void test01(unsigned int i) 58f08c3bdfSopenharmony_ci{ 59f08c3bdfSopenharmony_ci pthread_t p_id; 60f08c3bdfSopenharmony_ci struct fanotify_event_metadata event; 61f08c3bdfSopenharmony_ci int fd_notify; 62f08c3bdfSopenharmony_ci int tgid = getpid(); 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_ci tst_res(TINFO, "Test #%u: %s FAN_REPORT_TID: tgid=%d, tid=%d, event.pid=%d", 65f08c3bdfSopenharmony_ci i, (tcases[i] & FAN_REPORT_TID) ? "with" : "without", 66f08c3bdfSopenharmony_ci tgid, tid, event.pid); 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_ci if (fan_report_tid_unsupported && (tcases[i] & FAN_REPORT_TID)) { 69f08c3bdfSopenharmony_ci FANOTIFY_INIT_FLAGS_ERR_MSG(FAN_REPORT_TID, fan_report_tid_unsupported); 70f08c3bdfSopenharmony_ci return; 71f08c3bdfSopenharmony_ci } 72f08c3bdfSopenharmony_ci 73f08c3bdfSopenharmony_ci fd_notify = SAFE_FANOTIFY_INIT(tcases[i], 0); 74f08c3bdfSopenharmony_ci 75f08c3bdfSopenharmony_ci SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD, 76f08c3bdfSopenharmony_ci FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD, AT_FDCWD, "."); 77f08c3bdfSopenharmony_ci 78f08c3bdfSopenharmony_ci SAFE_PTHREAD_CREATE(&p_id, NULL, thread_create_file, NULL); 79f08c3bdfSopenharmony_ci 80f08c3bdfSopenharmony_ci SAFE_READ(0, fd_notify, &event, sizeof(struct fanotify_event_metadata)); 81f08c3bdfSopenharmony_ci 82f08c3bdfSopenharmony_ci if ((tcases[i] & FAN_REPORT_TID) && event.pid == tid) 83f08c3bdfSopenharmony_ci tst_res(TPASS, "event.pid == tid"); 84f08c3bdfSopenharmony_ci else if (!(tcases[i] & FAN_REPORT_TID) && event.pid == tgid) 85f08c3bdfSopenharmony_ci tst_res(TPASS, "event.pid == tgid"); 86f08c3bdfSopenharmony_ci else 87f08c3bdfSopenharmony_ci tst_res(TFAIL, "unexpected event.pid value"); 88f08c3bdfSopenharmony_ci 89f08c3bdfSopenharmony_ci if (event.fd != FAN_NOFD) 90f08c3bdfSopenharmony_ci SAFE_CLOSE(event.fd); 91f08c3bdfSopenharmony_ci SAFE_CLOSE(fd_notify); 92f08c3bdfSopenharmony_ci SAFE_PTHREAD_JOIN(p_id, NULL); 93f08c3bdfSopenharmony_ci} 94f08c3bdfSopenharmony_ci 95f08c3bdfSopenharmony_cistatic void setup(void) 96f08c3bdfSopenharmony_ci{ 97f08c3bdfSopenharmony_ci fan_report_tid_unsupported = fanotify_init_flags_supported_by_kernel(FAN_REPORT_TID); 98f08c3bdfSopenharmony_ci} 99f08c3bdfSopenharmony_ci 100f08c3bdfSopenharmony_cistatic struct tst_test test = { 101f08c3bdfSopenharmony_ci .setup = setup, 102f08c3bdfSopenharmony_ci .test = test01, 103f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tcases), 104f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 105f08c3bdfSopenharmony_ci .needs_root = 1, 106f08c3bdfSopenharmony_ci}; 107f08c3bdfSopenharmony_ci 108f08c3bdfSopenharmony_ci#else 109f08c3bdfSopenharmony_ci TST_TEST_TCONF("system doesn't have required fanotify support"); 110f08c3bdfSopenharmony_ci#endif 111