|
| 1 | +#include <sys/types.h> |
| 2 | +#include <sys/time.h> |
| 3 | +#include <stdio.h> |
| 4 | +#include <unistd.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <fcntl.h> |
| 7 | +#include <err.h> |
| 8 | +#include <getopt.h> |
| 9 | +#include <poll.h> |
| 10 | +#include <sys/inotify.h> |
| 11 | +#include <limits.h> |
| 12 | + |
| 13 | +#include <sys/wait.h> |
| 14 | +#include <errno.h> |
| 15 | +#include <string.h> |
| 16 | +#include <sysexits.h> |
| 17 | + |
| 18 | +#define FALSE 0 |
| 19 | +#define TRUE 1 |
| 20 | + |
| 21 | +#define BUF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1)) |
| 22 | + |
| 23 | + |
| 24 | +/* List of all nodesql */ |
| 25 | +enum { |
| 26 | + C_FILE, |
| 27 | + C_TIMEOUT, |
| 28 | +}; |
| 29 | + |
| 30 | +int |
| 31 | +cbsd_fwatch_usage(void) |
| 32 | +{ |
| 33 | + printf("Wait for file modification to terminate\n"); |
| 34 | + printf("require: --file, --timeout\n"); |
| 35 | + printf( |
| 36 | + "usage: cbsd_fwatch --file=path_to_file --timeout=0 (in seconds, 0 is infinity)\n"); |
| 37 | + return (EX_USAGE); |
| 38 | +} |
| 39 | + |
| 40 | +/* Display information from inotify_event structure */ |
| 41 | +static void displayInotifyEvent(struct inotify_event *i) |
| 42 | +{ |
| 43 | +// printf(" wd =%2d; ", i->wd); |
| 44 | +// if (i->cookie > 0) |
| 45 | +// printf("cookie =%4d; ", i->cookie); |
| 46 | + |
| 47 | +// printf("mask =\n"); |
| 48 | + if (i->mask & IN_ACCESS) printf("IN_ACCESS\n"); |
| 49 | + if (i->mask & IN_ATTRIB) printf("IN_ATTRIB\n"); |
| 50 | + if (i->mask & IN_CLOSE_NOWRITE) printf("IN_CLOSE_NOWRITE\n"); |
| 51 | + if (i->mask & IN_CLOSE_WRITE) printf("IN_CLOSE_WRITE\n"); |
| 52 | + if (i->mask & IN_CREATE) printf("IN_CREATE\n"); |
| 53 | + if (i->mask & IN_DELETE) printf("deleted\n"); |
| 54 | + if (i->mask & IN_DELETE_SELF) printf("deleted\n"); |
| 55 | + if (i->mask & IN_IGNORED) printf("IN_IGNORED\n"); |
| 56 | + if (i->mask & IN_ISDIR) printf("IN_ISDIR\n"); |
| 57 | + if (i->mask & IN_MODIFY) printf("written\n"); |
| 58 | + if (i->mask & IN_MOVE_SELF) printf("renamed\n"); |
| 59 | + if (i->mask & IN_MOVED_FROM) printf("renamed\n"); |
| 60 | + if (i->mask & IN_MOVED_TO) printf("renamed\n"); |
| 61 | + if (i->mask & IN_OPEN) printf("IN_OPEN\n"); |
| 62 | + if (i->mask & IN_Q_OVERFLOW) printf("IN_Q_OVERFLOW\n"); |
| 63 | + if (i->mask & IN_UNMOUNT) printf("IN_UNMOUNT\n"); |
| 64 | + |
| 65 | +// if (i->len > 0) |
| 66 | +// printf(" name = %s\n", i->name); |
| 67 | +} |
| 68 | + |
| 69 | +int |
| 70 | +main(int argc, char *argv[]) |
| 71 | +{ |
| 72 | + int fd; |
| 73 | + int ret; |
| 74 | + fd_set rfds; |
| 75 | + int kq; |
| 76 | + int nev; |
| 77 | + int inotifyFd, j; |
| 78 | + // static const struct timespec tout = { 1, 0 }; |
| 79 | + struct inotify_event *event; |
| 80 | + ssize_t numRead; |
| 81 | + char buf[BUF_LEN] __attribute__ ((aligned(8))); |
| 82 | + char *p; |
| 83 | +// struct pollfd fds[2]; |
| 84 | + struct pollfd fds[1]; |
| 85 | + nfds_t nfds; |
| 86 | + int poll_num; |
| 87 | + int *wd; |
| 88 | + |
| 89 | + int optcode = 0; |
| 90 | + int option_index = 0; |
| 91 | + char *watchfile = NULL; |
| 92 | + int timeout = 10; |
| 93 | + char cmd[10]; |
| 94 | + |
| 95 | + struct option long_options[] = { { "file", required_argument, 0, |
| 96 | + C_FILE }, |
| 97 | + { "timeout", required_argument, 0, C_TIMEOUT }, |
| 98 | + /* End of options marker */ |
| 99 | + { 0, 0, 0, 0 } }; |
| 100 | + |
| 101 | + if (argc < 2) { |
| 102 | + cbsd_fwatch_usage(); |
| 103 | + return 0; |
| 104 | + } |
| 105 | + |
| 106 | + while (TRUE) { |
| 107 | + optcode = getopt_long_only(argc, argv, "", long_options, |
| 108 | + &option_index); |
| 109 | + if (optcode == -1) { |
| 110 | + break; |
| 111 | + } |
| 112 | + switch (optcode) { |
| 113 | + case C_FILE: |
| 114 | + watchfile = malloc(strlen(optarg) + 1); |
| 115 | + memset(watchfile, 0, strlen(optarg) + 1); |
| 116 | + strcpy(watchfile, optarg); |
| 117 | + break; |
| 118 | + case C_TIMEOUT: |
| 119 | + timeout = atoi(optarg); |
| 120 | + break; |
| 121 | + } |
| 122 | + } // while |
| 123 | + |
| 124 | + // zero for getopt *variables for next execute |
| 125 | + optarg = NULL; |
| 126 | + optind = 0; |
| 127 | + optopt = 0; |
| 128 | + opterr = 0; |
| 129 | + |
| 130 | + if (!watchfile) { |
| 131 | + cbsd_fwatch_usage(); |
| 132 | + return 1; |
| 133 | + } |
| 134 | + |
| 135 | + inotifyFd = inotify_init1(IN_NONBLOCK); |
| 136 | + |
| 137 | + if (inotifyFd == -1) { |
| 138 | + printf("inotify_init1"); |
| 139 | + exit(1); |
| 140 | + } |
| 141 | + |
| 142 | + wd = calloc(argc, sizeof(int)); |
| 143 | + if (wd == NULL) { |
| 144 | + perror("calloc"); |
| 145 | + exit(EXIT_FAILURE); |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | +// wd[0] = inotify_add_watch(inotifyFd, watchfile, IN_ALL_EVENTS); |
| 150 | + wd[0] = inotify_add_watch(inotifyFd, watchfile, IN_OPEN | IN_CLOSE); |
| 151 | + if (wd[0] == -1) { |
| 152 | + printf("inotify_add_watch"); |
| 153 | + exit(1); |
| 154 | + } |
| 155 | + |
| 156 | + /* Prepare for polling. */ |
| 157 | +// nfds = 2; |
| 158 | +// fds[0].fd = STDIN_FILENO; /* Console input */ |
| 159 | +// fds[0].events = POLLIN; |
| 160 | +// fds[1].fd = inotifyFd; /* Inotify input */ |
| 161 | +// fds[1].events = POLLIN; |
| 162 | + |
| 163 | + nfds = 1; |
| 164 | + fds[0].fd = inotifyFd; /* Inotify input */ |
| 165 | + fds[0].events = POLLIN; |
| 166 | + |
| 167 | + while (TRUE) { |
| 168 | + poll_num = poll(fds, nfds, 1000 * timeout); |
| 169 | + if ( poll_num == 0 ) { |
| 170 | + // timeout |
| 171 | + exit(0); |
| 172 | + } |
| 173 | + |
| 174 | + if ( poll_num == -1 ) { |
| 175 | + if (errno == EINTR) |
| 176 | + continue; |
| 177 | + printf("poll error"); |
| 178 | + exit(EXIT_FAILURE); |
| 179 | + } |
| 180 | + break; |
| 181 | + } |
| 182 | + |
| 183 | + numRead = read(inotifyFd, buf, BUF_LEN); |
| 184 | + if (numRead == 0) { |
| 185 | + printf("read() from inotify fd returned 0!"); |
| 186 | + exit(1); |
| 187 | + } |
| 188 | + if (numRead == -1) { |
| 189 | + printf("read"); |
| 190 | + exit(1); |
| 191 | + } |
| 192 | + |
| 193 | + printf("Read %ld bytes from inotify fd\n", (long) numRead); |
| 194 | + |
| 195 | + /* Process all of the events in buffer returned by read() */ |
| 196 | + |
| 197 | + for (p = buf; p < buf + numRead; ) { |
| 198 | + event = (struct inotify_event *) p; |
| 199 | + displayInotifyEvent(event); |
| 200 | + p += sizeof(struct inotify_event) + event->len; |
| 201 | + } |
| 202 | + |
| 203 | + |
| 204 | + close(inotifyFd); |
| 205 | + free(wd); |
| 206 | + return(0); |
| 207 | +} |
0 commit comments