1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2020 CTERA Networks. All Rights Reserved. 4f08c3bdfSopenharmony_ci * 5f08c3bdfSopenharmony_ci * Started by Amir Goldstein <amir73il@gmail.com> 6f08c3bdfSopenharmony_ci * 7f08c3bdfSopenharmony_ci * DESCRIPTION 8f08c3bdfSopenharmony_ci * Check that dnotify event is reported to both parent and subdir 9f08c3bdfSopenharmony_ci */ 10f08c3bdfSopenharmony_ci 11f08c3bdfSopenharmony_ci#include <signal.h> 12f08c3bdfSopenharmony_ci#include <stdio.h> 13f08c3bdfSopenharmony_ci#include <unistd.h> 14f08c3bdfSopenharmony_ci#include "tst_test.h" 15f08c3bdfSopenharmony_ci#include "lapi/fcntl.h" 16f08c3bdfSopenharmony_ci 17f08c3bdfSopenharmony_ci#define TEST_DIR "test_dir" 18f08c3bdfSopenharmony_ci 19f08c3bdfSopenharmony_ci#define TEST_SIG SIGRTMIN+1 20f08c3bdfSopenharmony_ci 21f08c3bdfSopenharmony_cistatic int parent_fd, subdir_fd; 22f08c3bdfSopenharmony_cistatic int got_parent_event, got_subdir_event; 23f08c3bdfSopenharmony_ci 24f08c3bdfSopenharmony_cistatic void dnotify_handler(int sig, siginfo_t *si, void *data __attribute__((unused))) 25f08c3bdfSopenharmony_ci{ 26f08c3bdfSopenharmony_ci if (si->si_fd == parent_fd) 27f08c3bdfSopenharmony_ci got_parent_event = 1; 28f08c3bdfSopenharmony_ci else if (si->si_fd == subdir_fd) 29f08c3bdfSopenharmony_ci got_subdir_event = 1; 30f08c3bdfSopenharmony_ci else 31f08c3bdfSopenharmony_ci tst_brk(TBROK, "Got unexpected signal %d with si_fd %d", sig, si->si_fd); 32f08c3bdfSopenharmony_ci} 33f08c3bdfSopenharmony_ci 34f08c3bdfSopenharmony_cistatic void setup_dnotify(int fd) 35f08c3bdfSopenharmony_ci{ 36f08c3bdfSopenharmony_ci struct sigaction act; 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_ci act.sa_sigaction = dnotify_handler; 39f08c3bdfSopenharmony_ci sigemptyset(&act.sa_mask); 40f08c3bdfSopenharmony_ci act.sa_flags = SA_SIGINFO; 41f08c3bdfSopenharmony_ci sigaction(TEST_SIG, &act, NULL); 42f08c3bdfSopenharmony_ci 43f08c3bdfSopenharmony_ci TEST(fcntl(fd, F_SETSIG, TEST_SIG)); 44f08c3bdfSopenharmony_ci if (TST_RET != 0) { 45f08c3bdfSopenharmony_ci tst_brk(TBROK, "F_SETSIG failed errno = %d : %s", 46f08c3bdfSopenharmony_ci TST_ERR, strerror(TST_ERR)); 47f08c3bdfSopenharmony_ci } 48f08c3bdfSopenharmony_ci TEST(fcntl(fd, F_NOTIFY, DN_ATTRIB|DN_MULTISHOT)); 49f08c3bdfSopenharmony_ci if (TST_RET != 0) { 50f08c3bdfSopenharmony_ci tst_brk(TBROK, "F_NOTIFY failed errno = %d : %s", 51f08c3bdfSopenharmony_ci TST_ERR, strerror(TST_ERR)); 52f08c3bdfSopenharmony_ci } 53f08c3bdfSopenharmony_ci} 54f08c3bdfSopenharmony_ci 55f08c3bdfSopenharmony_cistatic void verify_dnotify(void) 56f08c3bdfSopenharmony_ci{ 57f08c3bdfSopenharmony_ci parent_fd = SAFE_OPEN(".", O_RDONLY); 58f08c3bdfSopenharmony_ci subdir_fd = SAFE_OPEN(TEST_DIR, O_RDONLY); 59f08c3bdfSopenharmony_ci /* Watch "." and its children for changes */ 60f08c3bdfSopenharmony_ci setup_dnotify(parent_fd); 61f08c3bdfSopenharmony_ci /* Also watch subdir itself for changes */ 62f08c3bdfSopenharmony_ci setup_dnotify(subdir_fd); 63f08c3bdfSopenharmony_ci /* Generate DN_ATTRIB event on subdir that should send a signal on both fds */ 64f08c3bdfSopenharmony_ci SAFE_CHMOD(TEST_DIR, 0755); 65f08c3bdfSopenharmony_ci if (got_parent_event) 66f08c3bdfSopenharmony_ci tst_res(TPASS, "Got event on parent as expected"); 67f08c3bdfSopenharmony_ci else 68f08c3bdfSopenharmony_ci tst_res(TFAIL, "Missing event on parent"); 69f08c3bdfSopenharmony_ci if (got_subdir_event) 70f08c3bdfSopenharmony_ci tst_res(TPASS, "Got event on subdir as expected"); 71f08c3bdfSopenharmony_ci else 72f08c3bdfSopenharmony_ci tst_res(TFAIL, "Missing event on subdir"); 73f08c3bdfSopenharmony_ci SAFE_CLOSE(parent_fd); 74f08c3bdfSopenharmony_ci SAFE_CLOSE(subdir_fd); 75f08c3bdfSopenharmony_ci} 76f08c3bdfSopenharmony_ci 77f08c3bdfSopenharmony_cistatic void setup(void) 78f08c3bdfSopenharmony_ci{ 79f08c3bdfSopenharmony_ci SAFE_MKDIR(TEST_DIR, 00700); 80f08c3bdfSopenharmony_ci} 81f08c3bdfSopenharmony_ci 82f08c3bdfSopenharmony_cistatic void cleanup(void) 83f08c3bdfSopenharmony_ci{ 84f08c3bdfSopenharmony_ci if (parent_fd > 0) 85f08c3bdfSopenharmony_ci SAFE_CLOSE(parent_fd); 86f08c3bdfSopenharmony_ci if (subdir_fd > 0) 87f08c3bdfSopenharmony_ci SAFE_CLOSE(subdir_fd); 88f08c3bdfSopenharmony_ci} 89f08c3bdfSopenharmony_ci 90f08c3bdfSopenharmony_cistatic struct tst_test test = { 91f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 92f08c3bdfSopenharmony_ci .setup = setup, 93f08c3bdfSopenharmony_ci .cleanup = cleanup, 94f08c3bdfSopenharmony_ci .test_all = verify_dnotify, 95f08c3bdfSopenharmony_ci .needs_kconfigs = (const char *[]) { "CONFIG_DNOTIFY=y", NULL }, 96f08c3bdfSopenharmony_ci}; 97