1 /* $NetBSD: h_inotify_directory.c,v 1.1 2023/08/19 22:56:44 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2023 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Theodore Preduta. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 #include <sys/cdefs.h> 32 __RCSID("$NetBSD: h_inotify_directory.c,v 1.1 2023/08/19 22:56:44 christos Exp $"); 33 34 #include "h_linux.h" 35 36 #include <sys/null.h> 37 38 #include <compat/linux/linux_syscall.h> 39 #include <compat/linux/common/linux_inotify.h> 40 41 #define INOTIFY_ALL_DIRECTORY (LINUX_IN_ATTRIB|LINUX_IN_CREATE \ 42 |LINUX_IN_MOVE_SELF|LINUX_IN_MOVED_FROM \ 43 |LINUX_IN_MOVED_TO|LINUX_IN_DELETE \ 44 |LINUX_IN_DELETE_SELF) 45 46 char buf[8192]; 47 48 struct { 49 uint32_t mask; 50 bool cookie; 51 char name[16]; 52 } target_events[] = { 53 { .mask = LINUX_IN_CREATE, .cookie = 0, .name = "test", }, 54 { .mask = LINUX_IN_MOVED_FROM, .cookie = 1, .name = "test", }, 55 { .mask = LINUX_IN_MOVED_TO, .cookie = 1, .name = "test2", }, 56 { .mask = LINUX_IN_DELETE, .cookie = 0, .name = "test2", }, 57 { .mask = LINUX_IN_MOVE_SELF, .cookie = 0, .name = "", }, 58 { .mask = LINUX_IN_DELETE_SELF, .cookie = 0, .name = "", }, 59 { .mask = LINUX_IN_IGNORED, .cookie = 0, .name = "", }, 60 }; 61 62 void 63 _start(void) 64 { 65 int fd, wd, targetfd; 66 char *cur_buf; 67 struct linux_inotify_event *cur_ie; 68 69 RS(mkdir("test", 0644)); 70 71 RS(fd = syscall(LINUX_SYS_inotify_init)); 72 RS(wd = syscall(LINUX_SYS_inotify_add_watch, fd, (register_t)"test", 73 INOTIFY_ALL_DIRECTORY)); 74 75 /* Create some events. */ 76 RS(targetfd = open("test/test", LINUX_O_RDWR|LINUX_O_CREAT, 0644)); 77 RS(write(targetfd, &targetfd, sizeof(targetfd))); 78 RS(close(targetfd)); 79 RS(rename("test/test", "test/test2")); 80 RS(unlink("test/test2")); 81 RS(rename("test", "test2")); 82 RS(rmdir("test2")); 83 84 /* Check the events. */ 85 RS(read(fd, buf, sizeof(buf))); 86 cur_buf = buf; 87 for (size_t i = 0; i < __arraycount(target_events); i++) { 88 cur_ie = (struct linux_inotify_event *)cur_buf; 89 90 REQUIRE(cur_ie->wd == wd); 91 REQUIRE(cur_ie->mask == target_events[i].mask); 92 93 if (target_events[i].cookie) 94 REQUIRE(cur_ie->cookie != 0); 95 else 96 REQUIRE(cur_ie->cookie == 0); 97 98 if (target_events[i].name[0] != '\0') { 99 REQUIRE(cur_ie->len > strlen(target_events[i].name)); 100 REQUIRE(strcmp(cur_ie->name, target_events[i].name) == 0); 101 } else 102 REQUIRE(cur_ie->len == 0); 103 104 cur_buf += sizeof(struct linux_inotify_event) + cur_ie->len; 105 } 106 107 exit(0); 108 } 109