1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2007 SWSoft. All Rights Reserved. 4f08c3bdfSopenharmony_ci * Author: Andrew Vagin <avagin@sw.ru> 5f08c3bdfSopenharmony_ci * 6f08c3bdfSopenharmony_ci * DESCRIPTION 7f08c3bdfSopenharmony_ci * Check that inotify work for a directory 8f08c3bdfSopenharmony_ci * 9f08c3bdfSopenharmony_ci * ALGORITHM 10f08c3bdfSopenharmony_ci * Execute sequence file's operation and check return events 11f08c3bdfSopenharmony_ci */ 12f08c3bdfSopenharmony_ci 13f08c3bdfSopenharmony_ci#include "config.h" 14f08c3bdfSopenharmony_ci 15f08c3bdfSopenharmony_ci#include <stdio.h> 16f08c3bdfSopenharmony_ci#include <sys/stat.h> 17f08c3bdfSopenharmony_ci#include <sys/types.h> 18f08c3bdfSopenharmony_ci#include <fcntl.h> 19f08c3bdfSopenharmony_ci#include <errno.h> 20f08c3bdfSopenharmony_ci#include <string.h> 21f08c3bdfSopenharmony_ci#include <sys/syscall.h> 22f08c3bdfSopenharmony_ci#include <limits.h> 23f08c3bdfSopenharmony_ci#include "tst_test.h" 24f08c3bdfSopenharmony_ci#include "inotify.h" 25f08c3bdfSopenharmony_ci 26f08c3bdfSopenharmony_ci#if defined(HAVE_SYS_INOTIFY_H) 27f08c3bdfSopenharmony_ci#include <sys/inotify.h> 28f08c3bdfSopenharmony_ci 29f08c3bdfSopenharmony_ci#ifndef IN_MOVE_SELF 30f08c3bdfSopenharmony_ci#define IN_MOVE_SELF 0x00000800 31f08c3bdfSopenharmony_ci#endif 32f08c3bdfSopenharmony_ci 33f08c3bdfSopenharmony_ci#define EVENT_MAX 1024 34f08c3bdfSopenharmony_ci/* size of the event structure, not counting name */ 35f08c3bdfSopenharmony_ci#define EVENT_SIZE (sizeof (struct inotify_event)) 36f08c3bdfSopenharmony_ci/* reasonable guess as to size of 1024 events */ 37f08c3bdfSopenharmony_ci#define EVENT_BUF_LEN (EVENT_MAX * (EVENT_SIZE + 16)) 38f08c3bdfSopenharmony_ci 39f08c3bdfSopenharmony_ci#define BUF_SIZE 256 40f08c3bdfSopenharmony_cistatic char fname1[BUF_SIZE], fname2[BUF_SIZE], fname3[BUF_SIZE]; 41f08c3bdfSopenharmony_cistatic int fd, fd_notify, reap_wd; 42f08c3bdfSopenharmony_cistatic int wd; 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_cistruct event_t { 45f08c3bdfSopenharmony_ci char name[BUF_SIZE]; 46f08c3bdfSopenharmony_ci unsigned int mask; 47f08c3bdfSopenharmony_ci}; 48f08c3bdfSopenharmony_ci#define FILE_NAME1 "test_file1" 49f08c3bdfSopenharmony_ci#define FILE_NAME2 "test_file2" 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_cistatic struct event_t event_set[EVENT_MAX]; 52f08c3bdfSopenharmony_ci 53f08c3bdfSopenharmony_cistatic char event_buf[EVENT_BUF_LEN]; 54f08c3bdfSopenharmony_ci 55f08c3bdfSopenharmony_civoid verify_inotify(void) 56f08c3bdfSopenharmony_ci{ 57f08c3bdfSopenharmony_ci unsigned int stored_cookie = UINT_MAX; 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_ci int test_cnt = 0; 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_ci /* 62f08c3bdfSopenharmony_ci * generate sequence of events 63f08c3bdfSopenharmony_ci */ 64f08c3bdfSopenharmony_ci SAFE_CHMOD(".", 0755); 65f08c3bdfSopenharmony_ci event_set[test_cnt].mask = IN_ISDIR | IN_ATTRIB; 66f08c3bdfSopenharmony_ci strcpy(event_set[test_cnt].name, ""); 67f08c3bdfSopenharmony_ci test_cnt++; 68f08c3bdfSopenharmony_ci 69f08c3bdfSopenharmony_ci if ((fd = creat(FILE_NAME1, 0755)) == -1) { 70f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, 71f08c3bdfSopenharmony_ci "creat(\"%s\", 755) failed", FILE_NAME1); 72f08c3bdfSopenharmony_ci } 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_ci event_set[test_cnt].mask = IN_CREATE; 75f08c3bdfSopenharmony_ci strcpy(event_set[test_cnt].name, FILE_NAME1); 76f08c3bdfSopenharmony_ci test_cnt++; 77f08c3bdfSopenharmony_ci event_set[test_cnt].mask = IN_OPEN; 78f08c3bdfSopenharmony_ci strcpy(event_set[test_cnt].name, FILE_NAME1); 79f08c3bdfSopenharmony_ci test_cnt++; 80f08c3bdfSopenharmony_ci 81f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 82f08c3bdfSopenharmony_ci event_set[test_cnt].mask = IN_CLOSE_WRITE; 83f08c3bdfSopenharmony_ci strcpy(event_set[test_cnt].name, FILE_NAME1); 84f08c3bdfSopenharmony_ci test_cnt++; 85f08c3bdfSopenharmony_ci 86f08c3bdfSopenharmony_ci SAFE_RENAME(FILE_NAME1, FILE_NAME2); 87f08c3bdfSopenharmony_ci event_set[test_cnt].mask = IN_MOVED_FROM; 88f08c3bdfSopenharmony_ci strcpy(event_set[test_cnt].name, FILE_NAME1); 89f08c3bdfSopenharmony_ci test_cnt++; 90f08c3bdfSopenharmony_ci event_set[test_cnt].mask = IN_MOVED_TO; 91f08c3bdfSopenharmony_ci strcpy(event_set[test_cnt].name, FILE_NAME2); 92f08c3bdfSopenharmony_ci test_cnt++; 93f08c3bdfSopenharmony_ci 94f08c3bdfSopenharmony_ci if (getcwd(fname1, BUF_SIZE) == NULL) { 95f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, 96f08c3bdfSopenharmony_ci "getcwd(%p, %d) failed", fname1, BUF_SIZE); 97f08c3bdfSopenharmony_ci } 98f08c3bdfSopenharmony_ci 99f08c3bdfSopenharmony_ci snprintf(fname2, BUF_SIZE, "%s.rename1", fname1); 100f08c3bdfSopenharmony_ci SAFE_RENAME(fname1, fname2); 101f08c3bdfSopenharmony_ci event_set[test_cnt].mask = IN_MOVE_SELF; 102f08c3bdfSopenharmony_ci strcpy(event_set[test_cnt].name, ""); 103f08c3bdfSopenharmony_ci test_cnt++; 104f08c3bdfSopenharmony_ci 105f08c3bdfSopenharmony_ci SAFE_UNLINK(FILE_NAME2); 106f08c3bdfSopenharmony_ci event_set[test_cnt].mask = IN_DELETE; 107f08c3bdfSopenharmony_ci strcpy(event_set[test_cnt].name, FILE_NAME2); 108f08c3bdfSopenharmony_ci test_cnt++; 109f08c3bdfSopenharmony_ci 110f08c3bdfSopenharmony_ci /* 111f08c3bdfSopenharmony_ci * test that duplicate events will be coalesced into 112f08c3bdfSopenharmony_ci * a single event. This test case should be last, that 113f08c3bdfSopenharmony_ci * we can correct determine kernel bug which exist before 114f08c3bdfSopenharmony_ci * 2.6.25. See comment below. 115f08c3bdfSopenharmony_ci */ 116f08c3bdfSopenharmony_ci snprintf(fname3, BUF_SIZE, "%s.rename2", fname1); 117f08c3bdfSopenharmony_ci SAFE_RENAME(fname2, fname3); 118f08c3bdfSopenharmony_ci 119f08c3bdfSopenharmony_ci SAFE_RENAME(fname3, fname1); 120f08c3bdfSopenharmony_ci event_set[test_cnt].mask = IN_MOVE_SELF; 121f08c3bdfSopenharmony_ci strcpy(event_set[test_cnt].name, ""); 122f08c3bdfSopenharmony_ci test_cnt++; 123f08c3bdfSopenharmony_ci 124f08c3bdfSopenharmony_ci int len, i = 0, test_num = 0; 125f08c3bdfSopenharmony_ci if ((len = read(fd_notify, event_buf, EVENT_BUF_LEN)) == -1) { 126f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, 127f08c3bdfSopenharmony_ci "read(%d, buf, %zu) failed", 128f08c3bdfSopenharmony_ci fd_notify, EVENT_BUF_LEN); 129f08c3bdfSopenharmony_ci 130f08c3bdfSopenharmony_ci } 131f08c3bdfSopenharmony_ci 132f08c3bdfSopenharmony_ci while (i < len) { 133f08c3bdfSopenharmony_ci struct inotify_event *event; 134f08c3bdfSopenharmony_ci event = (struct inotify_event *)&event_buf[i]; 135f08c3bdfSopenharmony_ci if (test_num >= test_cnt) { 136f08c3bdfSopenharmony_ci tst_res(TFAIL, 137f08c3bdfSopenharmony_ci "get unnecessary event: " 138f08c3bdfSopenharmony_ci "wd=%d mask=%08x cookie=%-5u len=%-2u " 139f08c3bdfSopenharmony_ci "name=\"%.*s\"", event->wd, event->mask, 140f08c3bdfSopenharmony_ci event->cookie, event->len, event->len, 141f08c3bdfSopenharmony_ci event->name); 142f08c3bdfSopenharmony_ci 143f08c3bdfSopenharmony_ci } else if ((event_set[test_num].mask == event->mask) 144f08c3bdfSopenharmony_ci && 145f08c3bdfSopenharmony_ci (!strncmp 146f08c3bdfSopenharmony_ci (event_set[test_num].name, event->name, 147f08c3bdfSopenharmony_ci event->len))) { 148f08c3bdfSopenharmony_ci int fail = 0; 149f08c3bdfSopenharmony_ci 150f08c3bdfSopenharmony_ci if (event->mask == IN_MOVED_FROM) { 151f08c3bdfSopenharmony_ci if (event->cookie == 0) 152f08c3bdfSopenharmony_ci fail = 1; 153f08c3bdfSopenharmony_ci else 154f08c3bdfSopenharmony_ci stored_cookie = event->cookie; 155f08c3bdfSopenharmony_ci } else if (event->mask == IN_MOVED_TO) { 156f08c3bdfSopenharmony_ci if (event->cookie != stored_cookie) 157f08c3bdfSopenharmony_ci fail = 1; 158f08c3bdfSopenharmony_ci else 159f08c3bdfSopenharmony_ci stored_cookie = UINT_MAX; 160f08c3bdfSopenharmony_ci } else { 161f08c3bdfSopenharmony_ci if (event->cookie != 0) 162f08c3bdfSopenharmony_ci fail = 1; 163f08c3bdfSopenharmony_ci } 164f08c3bdfSopenharmony_ci if (!fail) { 165f08c3bdfSopenharmony_ci tst_res(TPASS, 166f08c3bdfSopenharmony_ci "get event: wd=%d mask=%08x " 167f08c3bdfSopenharmony_ci "cookie=%-5u len=%-2u name=\"%.*s\"", 168f08c3bdfSopenharmony_ci event->wd, event->mask, 169f08c3bdfSopenharmony_ci event->cookie, event->len, 170f08c3bdfSopenharmony_ci event->len, event->name); 171f08c3bdfSopenharmony_ci } else { 172f08c3bdfSopenharmony_ci tst_res(TFAIL, 173f08c3bdfSopenharmony_ci "get event: wd=%d mask=%08x " 174f08c3bdfSopenharmony_ci "cookie=%-5u (wrong) len=%-2u " 175f08c3bdfSopenharmony_ci "name=\"%s\"", 176f08c3bdfSopenharmony_ci event->wd, event->mask, 177f08c3bdfSopenharmony_ci event->cookie, event->len, 178f08c3bdfSopenharmony_ci event->name); 179f08c3bdfSopenharmony_ci } 180f08c3bdfSopenharmony_ci } else { 181f08c3bdfSopenharmony_ci tst_res(TFAIL, "get event: wd=%d mask=%08x " 182f08c3bdfSopenharmony_ci "(expected %x) cookie=%-5u len=%-2u " 183f08c3bdfSopenharmony_ci "name=\"%s\" (expected \"%s\") %d", 184f08c3bdfSopenharmony_ci event->wd, event->mask, 185f08c3bdfSopenharmony_ci event_set[test_num].mask, 186f08c3bdfSopenharmony_ci event->cookie, event->len, event->name, 187f08c3bdfSopenharmony_ci event_set[test_num].name, 188f08c3bdfSopenharmony_ci strcmp(event_set[test_num].name, 189f08c3bdfSopenharmony_ci event->name)); 190f08c3bdfSopenharmony_ci } 191f08c3bdfSopenharmony_ci test_num++; 192f08c3bdfSopenharmony_ci i += EVENT_SIZE + event->len; 193f08c3bdfSopenharmony_ci } 194f08c3bdfSopenharmony_ci 195f08c3bdfSopenharmony_ci for (; test_num < test_cnt; test_num++) { 196f08c3bdfSopenharmony_ci tst_res(TFAIL, "didn't get event: mask=%08x ", 197f08c3bdfSopenharmony_ci event_set[test_num].mask); 198f08c3bdfSopenharmony_ci } 199f08c3bdfSopenharmony_ci} 200f08c3bdfSopenharmony_ci 201f08c3bdfSopenharmony_cistatic void setup(void) 202f08c3bdfSopenharmony_ci{ 203f08c3bdfSopenharmony_ci fd_notify = SAFE_MYINOTIFY_INIT(); 204f08c3bdfSopenharmony_ci 205f08c3bdfSopenharmony_ci wd = SAFE_MYINOTIFY_ADD_WATCH(fd_notify, ".", IN_ALL_EVENTS); 206f08c3bdfSopenharmony_ci reap_wd = 1; 207f08c3bdfSopenharmony_ci} 208f08c3bdfSopenharmony_ci 209f08c3bdfSopenharmony_cistatic void cleanup(void) 210f08c3bdfSopenharmony_ci{ 211f08c3bdfSopenharmony_ci if (reap_wd && myinotify_rm_watch(fd_notify, wd) < 0) { 212f08c3bdfSopenharmony_ci tst_res(TWARN, 213f08c3bdfSopenharmony_ci "inotify_rm_watch (%d, %d) failed,", fd_notify, wd); 214f08c3bdfSopenharmony_ci } 215f08c3bdfSopenharmony_ci 216f08c3bdfSopenharmony_ci if (fd_notify > 0) 217f08c3bdfSopenharmony_ci SAFE_CLOSE(fd_notify); 218f08c3bdfSopenharmony_ci} 219f08c3bdfSopenharmony_ci 220f08c3bdfSopenharmony_cistatic struct tst_test test = { 221f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 222f08c3bdfSopenharmony_ci .setup = setup, 223f08c3bdfSopenharmony_ci .cleanup = cleanup, 224f08c3bdfSopenharmony_ci .test_all = verify_inotify, 225f08c3bdfSopenharmony_ci}; 226f08c3bdfSopenharmony_ci 227f08c3bdfSopenharmony_ci#else 228f08c3bdfSopenharmony_ci TST_TEST_TCONF("system doesn't have required inotify support"); 229f08c3bdfSopenharmony_ci#endif 230