1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2012 Linux Test Project. All Rights Reserved. 4f08c3bdfSopenharmony_ci * Ngie Cooper, April 2012 5f08c3bdfSopenharmony_ci * 6f08c3bdfSopenharmony_ci * DESCRIPTION 7f08c3bdfSopenharmony_ci * verify that IN_DELETE_SELF functions as expected 8f08c3bdfSopenharmony_ci * 9f08c3bdfSopenharmony_ci * ALGORITHM 10f08c3bdfSopenharmony_ci * This testcase creates a temporary directory, then add watches to a 11f08c3bdfSopenharmony_ci * predefined file and subdirectory, and delete the file and directory to 12f08c3bdfSopenharmony_ci * ensure that the IN_DELETE_SELF event is captured properly. 13f08c3bdfSopenharmony_ci * 14f08c3bdfSopenharmony_ci * Because of how the inotify(7) API is designed, we also need to catch the 15f08c3bdfSopenharmony_ci * IN_ATTRIB and IN_IGNORED events. 16f08c3bdfSopenharmony_ci */ 17f08c3bdfSopenharmony_ci 18f08c3bdfSopenharmony_ci#include "config.h" 19f08c3bdfSopenharmony_ci 20f08c3bdfSopenharmony_ci#if defined(HAVE_SYS_INOTIFY_H) 21f08c3bdfSopenharmony_ci# include <sys/inotify.h> 22f08c3bdfSopenharmony_ci#endif 23f08c3bdfSopenharmony_ci#include <errno.h> 24f08c3bdfSopenharmony_ci#include <string.h> 25f08c3bdfSopenharmony_ci#include "tst_test.h" 26f08c3bdfSopenharmony_ci#include "inotify.h" 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_ci#if defined(HAVE_SYS_INOTIFY_H) 29f08c3bdfSopenharmony_ci 30f08c3bdfSopenharmony_ci#define EVENT_MAX 1024 31f08c3bdfSopenharmony_ci/* size of the event structure, not counting name */ 32f08c3bdfSopenharmony_ci#define EVENT_SIZE (sizeof(struct inotify_event)) 33f08c3bdfSopenharmony_ci/* reasonable guess as to size of 1024 events */ 34f08c3bdfSopenharmony_ci#define EVENT_BUF_LEN (EVENT_MAX * (EVENT_SIZE + 16)) 35f08c3bdfSopenharmony_ci 36f08c3bdfSopenharmony_ci 37f08c3bdfSopenharmony_ci#define BUF_SIZE 256 38f08c3bdfSopenharmony_ci 39f08c3bdfSopenharmony_cistruct event_t { 40f08c3bdfSopenharmony_ci char name[BUF_SIZE]; 41f08c3bdfSopenharmony_ci unsigned int mask; 42f08c3bdfSopenharmony_ci}; 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_ci#define TEST_DIR "test_dir" 45f08c3bdfSopenharmony_ci#define TEST_FILE "test_file" 46f08c3bdfSopenharmony_ci 47f08c3bdfSopenharmony_cistruct event_t event_set[EVENT_MAX]; 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_cichar event_buf[EVENT_BUF_LEN]; 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_ciint fd_notify, reap_wd_file, reap_wd_dir, wd_dir, wd_file; 52f08c3bdfSopenharmony_ci 53f08c3bdfSopenharmony_cistatic void cleanup(void) 54f08c3bdfSopenharmony_ci{ 55f08c3bdfSopenharmony_ci if (reap_wd_dir && myinotify_rm_watch(fd_notify, wd_dir) == -1) 56f08c3bdfSopenharmony_ci tst_res(TWARN, 57f08c3bdfSopenharmony_ci "inotify_rm_watch(%d, %d) [1] failed", fd_notify, 58f08c3bdfSopenharmony_ci wd_dir); 59f08c3bdfSopenharmony_ci 60f08c3bdfSopenharmony_ci if (reap_wd_file && myinotify_rm_watch(fd_notify, wd_file) == -1) 61f08c3bdfSopenharmony_ci tst_res(TWARN, 62f08c3bdfSopenharmony_ci "inotify_rm_watch(%d, %d) [2] failed", fd_notify, 63f08c3bdfSopenharmony_ci wd_file); 64f08c3bdfSopenharmony_ci 65f08c3bdfSopenharmony_ci if (fd_notify > 0) 66f08c3bdfSopenharmony_ci SAFE_CLOSE(fd_notify); 67f08c3bdfSopenharmony_ci} 68f08c3bdfSopenharmony_ci 69f08c3bdfSopenharmony_cistatic void setup(void) 70f08c3bdfSopenharmony_ci{ 71f08c3bdfSopenharmony_ci fd_notify = SAFE_MYINOTIFY_INIT(); 72f08c3bdfSopenharmony_ci} 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_civoid verify_inotify(void) 75f08c3bdfSopenharmony_ci{ 76f08c3bdfSopenharmony_ci int i = 0, test_num = 0, len; 77f08c3bdfSopenharmony_ci int test_cnt = 0; 78f08c3bdfSopenharmony_ci 79f08c3bdfSopenharmony_ci SAFE_MKDIR(TEST_DIR, 00700); 80f08c3bdfSopenharmony_ci close(SAFE_CREAT(TEST_FILE, 00600)); 81f08c3bdfSopenharmony_ci 82f08c3bdfSopenharmony_ci wd_dir = SAFE_MYINOTIFY_ADD_WATCH(fd_notify, TEST_DIR, IN_ALL_EVENTS); 83f08c3bdfSopenharmony_ci reap_wd_dir = 1; 84f08c3bdfSopenharmony_ci 85f08c3bdfSopenharmony_ci wd_file = SAFE_MYINOTIFY_ADD_WATCH(fd_notify, TEST_FILE, IN_ALL_EVENTS); 86f08c3bdfSopenharmony_ci reap_wd_file = 1; 87f08c3bdfSopenharmony_ci 88f08c3bdfSopenharmony_ci SAFE_RMDIR(TEST_DIR); 89f08c3bdfSopenharmony_ci reap_wd_dir = 0; 90f08c3bdfSopenharmony_ci 91f08c3bdfSopenharmony_ci event_set[test_cnt].mask = IN_DELETE_SELF; 92f08c3bdfSopenharmony_ci strcpy(event_set[test_cnt].name, ""); 93f08c3bdfSopenharmony_ci test_cnt++; 94f08c3bdfSopenharmony_ci event_set[test_cnt].mask = IN_IGNORED; 95f08c3bdfSopenharmony_ci strcpy(event_set[test_cnt].name, ""); 96f08c3bdfSopenharmony_ci test_cnt++; 97f08c3bdfSopenharmony_ci 98f08c3bdfSopenharmony_ci SAFE_UNLINK(TEST_FILE); 99f08c3bdfSopenharmony_ci reap_wd_file = 0; 100f08c3bdfSopenharmony_ci 101f08c3bdfSopenharmony_ci /* 102f08c3bdfSopenharmony_ci * When a file is unlinked, the link count is reduced by 1, and when it 103f08c3bdfSopenharmony_ci * hits 0 the file is removed. 104f08c3bdfSopenharmony_ci * 105f08c3bdfSopenharmony_ci * This isn't well documented in inotify(7), but it's intuitive if you 106f08c3bdfSopenharmony_ci * understand how Unix works. 107f08c3bdfSopenharmony_ci */ 108f08c3bdfSopenharmony_ci event_set[test_cnt].mask = IN_ATTRIB; 109f08c3bdfSopenharmony_ci strcpy(event_set[test_cnt].name, ""); 110f08c3bdfSopenharmony_ci test_cnt++; 111f08c3bdfSopenharmony_ci 112f08c3bdfSopenharmony_ci event_set[test_cnt].mask = IN_DELETE_SELF; 113f08c3bdfSopenharmony_ci strcpy(event_set[test_cnt].name, TEST_FILE); 114f08c3bdfSopenharmony_ci test_cnt++; 115f08c3bdfSopenharmony_ci event_set[test_cnt].mask = IN_IGNORED; 116f08c3bdfSopenharmony_ci strcpy(event_set[test_cnt].name, ""); 117f08c3bdfSopenharmony_ci test_cnt++; 118f08c3bdfSopenharmony_ci 119f08c3bdfSopenharmony_ci len = read(fd_notify, event_buf, EVENT_BUF_LEN); 120f08c3bdfSopenharmony_ci if (len == -1) 121f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, "read failed"); 122f08c3bdfSopenharmony_ci 123f08c3bdfSopenharmony_ci while (i < len) { 124f08c3bdfSopenharmony_ci struct inotify_event *event; 125f08c3bdfSopenharmony_ci event = (struct inotify_event *)&event_buf[i]; 126f08c3bdfSopenharmony_ci if (test_num >= test_cnt) { 127f08c3bdfSopenharmony_ci tst_res(TFAIL, 128f08c3bdfSopenharmony_ci "got unnecessary event: " 129f08c3bdfSopenharmony_ci "wd=%d mask=%04x cookie=%u len=%u " 130f08c3bdfSopenharmony_ci "name=\"%.*s\"", event->wd, event->mask, 131f08c3bdfSopenharmony_ci event->cookie, event->len, event->len, event->name); 132f08c3bdfSopenharmony_ci 133f08c3bdfSopenharmony_ci } else if ((event_set[test_num].mask == event->mask) 134f08c3bdfSopenharmony_ci && 135f08c3bdfSopenharmony_ci (!strncmp 136f08c3bdfSopenharmony_ci (event_set[test_num].name, event->name, 137f08c3bdfSopenharmony_ci event->len))) { 138f08c3bdfSopenharmony_ci tst_res(TPASS, 139f08c3bdfSopenharmony_ci "got event: wd=%d mask=%04x " 140f08c3bdfSopenharmony_ci "cookie=%u len=%u name=\"%.*s\"", 141f08c3bdfSopenharmony_ci event->wd, event->mask, event->cookie, 142f08c3bdfSopenharmony_ci event->len, event->len, event->name); 143f08c3bdfSopenharmony_ci 144f08c3bdfSopenharmony_ci } else { 145f08c3bdfSopenharmony_ci tst_res(TFAIL, "got event: wd=%d mask=%04x " 146f08c3bdfSopenharmony_ci "(expected %x) cookie=%u len=%u " 147f08c3bdfSopenharmony_ci "name=\"%.*s\" (expected \"%s\") %d", 148f08c3bdfSopenharmony_ci event->wd, event->mask, 149f08c3bdfSopenharmony_ci event_set[test_num].mask, 150f08c3bdfSopenharmony_ci event->cookie, event->len, 151f08c3bdfSopenharmony_ci event->len, event->name, 152f08c3bdfSopenharmony_ci event_set[test_num].name, 153f08c3bdfSopenharmony_ci strncmp(event_set[test_num].name, event->name, event->len)); 154f08c3bdfSopenharmony_ci } 155f08c3bdfSopenharmony_ci test_num++; 156f08c3bdfSopenharmony_ci i += EVENT_SIZE + event->len; 157f08c3bdfSopenharmony_ci } 158f08c3bdfSopenharmony_ci 159f08c3bdfSopenharmony_ci for (; test_num < test_cnt; test_num++) { 160f08c3bdfSopenharmony_ci tst_res(TFAIL, "didn't get event: mask=%04x ", 161f08c3bdfSopenharmony_ci event_set[test_num].mask); 162f08c3bdfSopenharmony_ci } 163f08c3bdfSopenharmony_ci 164f08c3bdfSopenharmony_ci} 165f08c3bdfSopenharmony_ci 166f08c3bdfSopenharmony_cistatic struct tst_test test = { 167f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 168f08c3bdfSopenharmony_ci .setup = setup, 169f08c3bdfSopenharmony_ci .cleanup = cleanup, 170f08c3bdfSopenharmony_ci .test_all = verify_inotify, 171f08c3bdfSopenharmony_ci}; 172f08c3bdfSopenharmony_ci 173f08c3bdfSopenharmony_ci#else 174f08c3bdfSopenharmony_ci TST_TEST_TCONF("system doesn't have required inotify support"); 175f08c3bdfSopenharmony_ci#endif /* defined(HAVE_SYS_INOTIFY_H) */ 176