[size=0.875]1 [size=0.875]2 [size=0.875]3 [size=0.875]4 [size=0.875]5 [size=0.875]6 [size=0.875]7 [size=0.875]8 | [size=0.875][size=0.875]struct inotify_event [size=0.875]{ [size=0.875] int wd; /* Watch descriptor. */ [size=0.875] uint32_t mask; /* Watch mask. */ [size=0.875] uint32_t cookie; /* Cookie to synchronize two events. */ [size=0.875] uint32_t len; /* Length (including NULs) of name. */ [size=0.875] char name __flexarr; /* Name. */ [size=0.875] }; |
[size=0.875]1 [size=0.875]2 [size=0.875]3 [size=0.875]4 [size=0.875]5 [size=0.875]6 [size=0.875]7 [size=0.875]8 [size=0.875]9 [size=0.875]10 [size=0.875]11 [size=0.875]12 [size=0.875]13 [size=0.875]14 [size=0.875]15 [size=0.875]16 [size=0.875]17 [size=0.875]18 [size=0.875]19 [size=0.875]20 [size=0.875]21 [size=0.875]22 [size=0.875]23 [size=0.875]24 [size=0.875]25 [size=0.875]26 [size=0.875]27 [size=0.875]28 [size=0.875]29 [size=0.875]30 [size=0.875]31 [size=0.875]32 [size=0.875]33 [size=0.875]34 [size=0.875]35 [size=0.875]36 [size=0.875]37 [size=0.875]38 [size=0.875]39 [size=0.875]40 [size=0.875]41 [size=0.875]42 [size=0.875]43 [size=0.875]44 [size=0.875]45 [size=0.875]46 [size=0.875]47 [size=0.875]48 [size=0.875]49 [size=0.875]50 [size=0.875]51 [size=0.875]52 [size=0.875]53 [size=0.875]54 [size=0.875]55 [size=0.875]56 [size=0.875]57 [size=0.875]58 [size=0.875]59 [size=0.875]60 [size=0.875]61 [size=0.875]62 [size=0.875]63 [size=0.875]64 [size=0.875]65 [size=0.875]66 [size=0.875]67 [size=0.875]68 [size=0.875]69 [size=0.875]70 [size=0.875]71 [size=0.875]72 [size=0.875]73 | [size=0.875][size=0.875]/* Signal handler that simply resets a flag to cause termination */ [size=0.875]void signal_handler (int signum) [size=0.875]{ [size=0.875] keep_running = 0; [size=0.875]} [size=0.875]int main (int argc, char **argv) [size=0.875]{ [size=0.875] /* This is the file descriptor for the inotify watch */ [size=0.875] int inotify_fd; [size=0.875] keep_running = 1; [size=0.875] /* Set a ctrl-c signal handler */ [size=0.875] if (signal (SIGINT, signal_handler) == SIG_IGN) [size=0.875] { [size=0.875] /* Reset to SIG_IGN (ignore) if that was the prior state */ [size=0.875] signal (SIGINT, SIG_IGN); [size=0.875] } [size=0.875] /* First we open the inotify dev entry */ [size=0.875] inotify_fd = open_inotify_fd (); [size=0.875] if (inotify_fd > 0) [size=0.875] { [size=0.875] /* We will need a place to enqueue inotify events, [size=0.875] this is needed because if you do not read events [size=0.875] fast enough, you will miss them. This queue is [size=0.875] probably too small if you are monitoring something [size=0.875] like a directory with a lot of files and the directory [size=0.875] is deleted. [size=0.875] */ [size=0.875] queue_t q; [size=0.875] q = queue_create (128); [size=0.875] /* This is the watch descriptor returned for each item we are [size=0.875] watching. A real application might keep these for some use [size=0.875] in the application. This sample only makes sure that none of [size=0.875] the watch descriptors is less than 0. [size=0.875] */ [size=0.875] int wd; [size=0.875] /* Watch all events (IN_ALL_EVENTS) for the directories and [size=0.875] files passed in as arguments. [size=0.875] Read the article for why you might want to alter this for [size=0.875] more efficient inotify use in your app. [size=0.875] */ [size=0.875] int index; [size=0.875] wd = 0; [size=0.875] printf("\n"); [size=0.875] for (index = 1; (index < argc) && (wd >= 0); index++) [size=0.875] { [size=0.875] wd = watch_dir (inotify_fd, argv[index], IN_ALL_EVENTS); [size=0.875] } [size=0.875] if (wd > 0) [size=0.875] { [size=0.875] /* Wait for events and process them until a [size=0.875] termination condition is detected [size=0.875] */ [size=0.875] process_inotify_events (q, inotify_fd); [size=0.875] } [size=0.875] printf ("\nTerminating\n"); [size=0.875] /* Finish up by closing the fd, destroying the queue, [size=0.875] and returning a proper code [size=0.875] */ [size=0.875] close_inotify_fd (inotify_fd); [size=0.875] queue_destroy (q); [size=0.875] } [size=0.875] return 0; [size=0.875]} |
[size=0.875]1 [size=0.875]2 [size=0.875]3 [size=0.875]4 [size=0.875]5 [size=0.875]6 [size=0.875]7 [size=0.875]8 [size=0.875]9 [size=0.875]10 [size=0.875]11 [size=0.875]12 [size=0.875]13 [size=0.875]14 [size=0.875]15 | [size=0.875][size=0.875]/* Create an inotify instance and open a file descriptor [size=0.875] to access it */ [size=0.875]int open_inotify_fd () [size=0.875]{ [size=0.875] int fd; [size=0.875] watched_items = 0; [size=0.875] fd = inotify_init (); [size=0.875] if (fd < 0) [size=0.875] { [size=0.875] perror ("inotify_init () = "); [size=0.875] } [size=0.875] return fd; [size=0.875] } |
[size=0.875]1 [size=0.875]2 [size=0.875]3 [size=0.875]4 [size=0.875]5 [size=0.875]6 [size=0.875]7 [size=0.875]8 [size=0.875]9 [size=0.875]10 [size=0.875]11 [size=0.875]12 [size=0.875]13 [size=0.875]14 [size=0.875]15 [size=0.875]16 [size=0.875]17 [size=0.875]18 [size=0.875]19 | [size=0.875][size=0.875]int watch_dir (int fd, const char *dirname, unsigned long mask) [size=0.875]{ [size=0.875] int wd; [size=0.875] wd = inotify_add_watch (fd, dirname, mask); [size=0.875] if (wd < 0) [size=0.875] { [size=0.875] printf ("Cannot add watch for \"%s\" with event mask %lX", dirname, [size=0.875] mask); [size=0.875] fflush (stdout); [size=0.875] perror (" "); [size=0.875] } [size=0.875] else [size=0.875] { [size=0.875] watched_items++; [size=0.875] printf ("Watching %s WD=%d\n", dirname, wd); [size=0.875] printf ("Watching = %d items\n", watched_items); [size=0.875] } [size=0.875] return wd; [size=0.875]} |
[size=0.875]1 [size=0.875]2 [size=0.875]3 [size=0.875]4 [size=0.875]5 [size=0.875]6 [size=0.875]7 [size=0.875]8 [size=0.875]9 [size=0.875]10 [size=0.875]11 [size=0.875]12 [size=0.875]13 [size=0.875]14 [size=0.875]15 [size=0.875]16 [size=0.875]17 [size=0.875]18 [size=0.875]19 [size=0.875]20 | [size=0.875][size=0.875]int process_inotify_events (queue_t q, int fd) [size=0.875]{ [size=0.875] while (keep_running && (watched_items > 0)) [size=0.875] { [size=0.875] if (event_check (fd) > 0) [size=0.875] { [size=0.875] int r; [size=0.875] r = read_events (q, fd); [size=0.875] if (r < 0) [size=0.875] { [size=0.875] break; [size=0.875] } [size=0.875] else [size=0.875] { [size=0.875] handle_events (q); [size=0.875] } [size=0.875] } [size=0.875] } [size=0.875] return 0; [size=0.875] } |
歡迎光臨 比思論壇 (http://108.170.5.98/) | Powered by Discuz! X2.5 |