1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2022 CTERA Networks. All Rights Reserved. 4f08c3bdfSopenharmony_ci * 5f08c3bdfSopenharmony_ci * Author: Amir Goldstein <amir73il@gmail.com> 6f08c3bdfSopenharmony_ci */ 7f08c3bdfSopenharmony_ci 8f08c3bdfSopenharmony_ci/*\ 9f08c3bdfSopenharmony_ci * [Description] 10f08c3bdfSopenharmony_ci * Test special inotify mask flags. 11f08c3bdfSopenharmony_ci * 12f08c3bdfSopenharmony_ci * Regression test for kernel commit: 13f08c3bdfSopenharmony_ci * a32e697cda27 ("inotify: show inotify mask flags in proc fdinfo") 14f08c3bdfSopenharmony_ci */ 15f08c3bdfSopenharmony_ci 16f08c3bdfSopenharmony_ci#include "config.h" 17f08c3bdfSopenharmony_ci 18f08c3bdfSopenharmony_ci#include <stdio.h> 19f08c3bdfSopenharmony_ci#include <unistd.h> 20f08c3bdfSopenharmony_ci#include <fcntl.h> 21f08c3bdfSopenharmony_ci#include <signal.h> 22f08c3bdfSopenharmony_ci#include <sys/wait.h> 23f08c3bdfSopenharmony_ci 24f08c3bdfSopenharmony_ci#include "tst_test.h" 25f08c3bdfSopenharmony_ci#include "tst_safe_macros.h" 26f08c3bdfSopenharmony_ci#include "inotify.h" 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_ci#if defined(HAVE_SYS_INOTIFY_H) 29f08c3bdfSopenharmony_ci#include <sys/inotify.h> 30f08c3bdfSopenharmony_ci 31f08c3bdfSopenharmony_ci#define EVENT_MAX 32 32f08c3bdfSopenharmony_ci/* Size of the event structure, not including the name */ 33f08c3bdfSopenharmony_ci#define EVENT_SIZE (sizeof(struct inotify_event)) 34f08c3bdfSopenharmony_ci#define EVENT_BUF_LEN (EVENT_MAX * (EVENT_SIZE + 16)) 35f08c3bdfSopenharmony_ci 36f08c3bdfSopenharmony_ci#define TEST_FILE "test_file" 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_cistatic char event_buf[EVENT_BUF_LEN]; 39f08c3bdfSopenharmony_ci 40f08c3bdfSopenharmony_cistatic struct tcase { 41f08c3bdfSopenharmony_ci const char *tname; 42f08c3bdfSopenharmony_ci unsigned int mask; 43f08c3bdfSopenharmony_ci int expect_events; 44f08c3bdfSopenharmony_ci} tcases[] = { 45f08c3bdfSopenharmony_ci { 46f08c3bdfSopenharmony_ci "Watch for multi events", 47f08c3bdfSopenharmony_ci IN_MODIFY, 48f08c3bdfSopenharmony_ci 2, 49f08c3bdfSopenharmony_ci }, 50f08c3bdfSopenharmony_ci { 51f08c3bdfSopenharmony_ci "Watch for single event", 52f08c3bdfSopenharmony_ci IN_MODIFY | IN_ONESHOT, 53f08c3bdfSopenharmony_ci 1, 54f08c3bdfSopenharmony_ci }, 55f08c3bdfSopenharmony_ci { 56f08c3bdfSopenharmony_ci "Watch for events on linked file", 57f08c3bdfSopenharmony_ci IN_MODIFY | IN_EXCL_UNLINK, 58f08c3bdfSopenharmony_ci 1, 59f08c3bdfSopenharmony_ci }, 60f08c3bdfSopenharmony_ci}; 61f08c3bdfSopenharmony_ci 62f08c3bdfSopenharmony_cistatic int fd_notify; 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_cistatic void verify_inotify(unsigned int n) 65f08c3bdfSopenharmony_ci{ 66f08c3bdfSopenharmony_ci struct tcase *tc = &tcases[n]; 67f08c3bdfSopenharmony_ci int fd, len; 68f08c3bdfSopenharmony_ci unsigned int tmpmask; 69f08c3bdfSopenharmony_ci char procfdinfo[100]; 70f08c3bdfSopenharmony_ci struct inotify_event *event = (struct inotify_event *)event_buf; 71f08c3bdfSopenharmony_ci 72f08c3bdfSopenharmony_ci tst_res(TINFO, "Test #%d: %s", n, tc->tname); 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_ci fd_notify = SAFE_MYINOTIFY_INIT1(O_NONBLOCK); 75f08c3bdfSopenharmony_ci 76f08c3bdfSopenharmony_ci SAFE_FILE_PRINTF(TEST_FILE, "1"); 77f08c3bdfSopenharmony_ci 78f08c3bdfSopenharmony_ci SAFE_MYINOTIFY_ADD_WATCH(fd_notify, ".", tc->mask); 79f08c3bdfSopenharmony_ci 80f08c3bdfSopenharmony_ci sprintf(procfdinfo, "/proc/%d/fdinfo/%d", (int)getpid(), fd_notify); 81f08c3bdfSopenharmony_ci if (FILE_LINES_SCANF(procfdinfo, "inotify wd:%*d ino:%*x sdev:%*x mask:%x", 82f08c3bdfSopenharmony_ci &tmpmask)) { 83f08c3bdfSopenharmony_ci tst_res(TFAIL, "Could not parse inotify fdinfo"); 84f08c3bdfSopenharmony_ci } else if (tmpmask != tc->mask) { 85f08c3bdfSopenharmony_ci tst_res(TFAIL, "Incorrect mask %x in inotify fdinfo (expected %x)", 86f08c3bdfSopenharmony_ci tmpmask, tc->mask); 87f08c3bdfSopenharmony_ci } else { 88f08c3bdfSopenharmony_ci tst_res(TPASS, "Correct mask in inotify fdinfo"); 89f08c3bdfSopenharmony_ci } 90f08c3bdfSopenharmony_ci 91f08c3bdfSopenharmony_ci fd = SAFE_OPEN(TEST_FILE, O_RDWR); 92f08c3bdfSopenharmony_ci SAFE_WRITE(SAFE_WRITE_ALL, fd, "2", 1); 93f08c3bdfSopenharmony_ci 94f08c3bdfSopenharmony_ci /* 95f08c3bdfSopenharmony_ci * Read the 1st IN_MODIFY event 96f08c3bdfSopenharmony_ci */ 97f08c3bdfSopenharmony_ci len = SAFE_READ(0, fd_notify, event_buf, EVENT_BUF_LEN); 98f08c3bdfSopenharmony_ci 99f08c3bdfSopenharmony_ci if (len < (int)sizeof(*event)) { 100f08c3bdfSopenharmony_ci tst_res(TFAIL, "Got no events"); 101f08c3bdfSopenharmony_ci } else if (event->mask == IN_MODIFY) { 102f08c3bdfSopenharmony_ci tst_res(TPASS, "Got 1st event as expected"); 103f08c3bdfSopenharmony_ci } else { 104f08c3bdfSopenharmony_ci tst_res(TFAIL, "Got event 0x%x (expected 0x%x)", 105f08c3bdfSopenharmony_ci event->mask, IN_MODIFY); 106f08c3bdfSopenharmony_ci } 107f08c3bdfSopenharmony_ci 108f08c3bdfSopenharmony_ci /* 109f08c3bdfSopenharmony_ci * Unlink file so IN_EXCL_UNLINK won't get IN_ACCESS event. 110f08c3bdfSopenharmony_ci * IN_ONESHOT won't get IN_ACCESS event because IN_MODIFY 111f08c3bdfSopenharmony_ci * was already generated. 112f08c3bdfSopenharmony_ci */ 113f08c3bdfSopenharmony_ci SAFE_UNLINK(TEST_FILE); 114f08c3bdfSopenharmony_ci SAFE_WRITE(SAFE_WRITE_ALL, fd, "3", 1); 115f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 116f08c3bdfSopenharmony_ci 117f08c3bdfSopenharmony_ci /* 118f08c3bdfSopenharmony_ci * Possibly read the 2nd IN_MODIFY event 119f08c3bdfSopenharmony_ci */ 120f08c3bdfSopenharmony_ci errno = 0; 121f08c3bdfSopenharmony_ci len = read(fd_notify, event_buf, EVENT_BUF_LEN); 122f08c3bdfSopenharmony_ci SAFE_CLOSE(fd_notify); 123f08c3bdfSopenharmony_ci if (len < 0 && errno == EAGAIN) { 124f08c3bdfSopenharmony_ci /* Treat no event same as we treat IN_IGNORED */ 125f08c3bdfSopenharmony_ci event->mask = IN_IGNORED; 126f08c3bdfSopenharmony_ci } else if (len < (int)sizeof(*event)) { 127f08c3bdfSopenharmony_ci tst_res(TFAIL | TERRNO, "Failed to read events"); 128f08c3bdfSopenharmony_ci return; 129f08c3bdfSopenharmony_ci } 130f08c3bdfSopenharmony_ci 131f08c3bdfSopenharmony_ci if (event->mask == IN_MODIFY) { 132f08c3bdfSopenharmony_ci if (tc->expect_events > 1) 133f08c3bdfSopenharmony_ci tst_res(TPASS, "Got 2nd event as expected"); 134f08c3bdfSopenharmony_ci else 135f08c3bdfSopenharmony_ci tst_res(TFAIL, "Got unexpected 2nd event"); 136f08c3bdfSopenharmony_ci } else if (event->mask == IN_IGNORED) { 137f08c3bdfSopenharmony_ci if (tc->expect_events == 1) 138f08c3bdfSopenharmony_ci tst_res(TPASS, "Got no more events as expected"); 139f08c3bdfSopenharmony_ci else 140f08c3bdfSopenharmony_ci tst_res(TFAIL, "Got only one event (expected %d)", 141f08c3bdfSopenharmony_ci tc->expect_events); 142f08c3bdfSopenharmony_ci } else { 143f08c3bdfSopenharmony_ci tst_res(TFAIL, "Got unexpected event 0x%x", 144f08c3bdfSopenharmony_ci event->mask); 145f08c3bdfSopenharmony_ci } 146f08c3bdfSopenharmony_ci} 147f08c3bdfSopenharmony_ci 148f08c3bdfSopenharmony_cistatic void cleanup(void) 149f08c3bdfSopenharmony_ci{ 150f08c3bdfSopenharmony_ci if (fd_notify > 0) 151f08c3bdfSopenharmony_ci SAFE_CLOSE(fd_notify); 152f08c3bdfSopenharmony_ci} 153f08c3bdfSopenharmony_ci 154f08c3bdfSopenharmony_cistatic struct tst_test test = { 155f08c3bdfSopenharmony_ci .max_runtime = 10, 156f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 157f08c3bdfSopenharmony_ci .cleanup = cleanup, 158f08c3bdfSopenharmony_ci .test = verify_inotify, 159f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tcases), 160f08c3bdfSopenharmony_ci .tags = (const struct tst_tag[]) { 161f08c3bdfSopenharmony_ci {"linux-git", "a32e697cda27"}, 162f08c3bdfSopenharmony_ci {} 163f08c3bdfSopenharmony_ci }, 164f08c3bdfSopenharmony_ci}; 165f08c3bdfSopenharmony_ci 166f08c3bdfSopenharmony_ci#else 167f08c3bdfSopenharmony_ci TST_TEST_TCONF("system doesn't have required inotify support"); 168f08c3bdfSopenharmony_ci#endif 169