Path: blob/master/tools/testing/selftests/cgroup/wait_inotify.c
26285 views
// SPDX-License-Identifier: GPL-2.01/*2* Wait until an inotify event on the given cgroup file.3*/4#include <linux/limits.h>5#include <sys/inotify.h>6#include <sys/mman.h>7#include <sys/ptrace.h>8#include <sys/stat.h>9#include <sys/types.h>10#include <errno.h>11#include <fcntl.h>12#include <poll.h>13#include <stdio.h>14#include <stdlib.h>15#include <string.h>16#include <unistd.h>1718static const char usage[] = "Usage: %s [-v] <cgroup_file>\n";19static char *file;20static int verbose;2122static inline void fail_message(char *msg)23{24fprintf(stderr, msg, file);25exit(1);26}2728int main(int argc, char *argv[])29{30char *cmd = argv[0];31int c, fd;32struct pollfd fds = { .events = POLLIN, };3334while ((c = getopt(argc, argv, "v")) != -1) {35switch (c) {36case 'v':37verbose++;38break;39}40argv++, argc--;41}4243if (argc != 2) {44fprintf(stderr, usage, cmd);45return -1;46}47file = argv[1];48fd = open(file, O_RDONLY);49if (fd < 0)50fail_message("Cgroup file %s not found!\n");51close(fd);5253fd = inotify_init();54if (fd < 0)55fail_message("inotify_init() fails on %s!\n");56if (inotify_add_watch(fd, file, IN_MODIFY) < 0)57fail_message("inotify_add_watch() fails on %s!\n");58fds.fd = fd;5960/*61* poll waiting loop62*/63for (;;) {64int ret = poll(&fds, 1, 10000);6566if (ret < 0) {67if (errno == EINTR)68continue;69perror("poll");70exit(1);71}72if ((ret > 0) && (fds.revents & POLLIN))73break;74}75if (verbose) {76struct inotify_event events[10];77long len;7879usleep(1000);80len = read(fd, events, sizeof(events));81printf("Number of events read = %ld\n",82len/sizeof(struct inotify_event));83}84close(fd);85return 0;86}878889