4 * Copyright (C) 2004 Novell, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
34 #include <sys/ioctl.h>
35 #include <sys/select.h>
39 #define SYSFS_PREFIX "/sys/class/misc/inotify"
41 #define SYSFS_MAX_USER_DEVICES SYSFS_PREFIX "/max_user_devices"
42 #define SYSFS_MAX_USER_WATCHES SYSFS_PREFIX "/max_user_watches"
43 #define SYSFS_MAX_QUEUED_EVENTS SYSFS_PREFIX "/max_queued_events"
45 /* Inotify sysfs knobs, initialized to their pre-sysfs defaults */
46 static int max_user_devices
= 8;
47 static int max_user_watches
= 8192;
48 static unsigned int max_queued_events
= 256;
50 /* Paranoid code to read an integer from a sysfs (well, any) file. */
52 read_int (const char *filename
, int *var
)
56 char *buffer_endptr
= NULL
;
58 fd
= open (filename
, O_RDONLY
);
61 if (read (fd
, buffer
, 31) > 0) {
62 n
= (int) strtol (buffer
, &buffer_endptr
, 10);
63 if (*buffer
!= '\0' && *buffer_endptr
== '\0')
71 inotify_glue_init (void)
73 static int initialized
= 0;
78 read_int (SYSFS_MAX_USER_DEVICES
, &max_user_devices
);
79 read_int (SYSFS_MAX_USER_WATCHES
, &max_user_watches
);
80 read_int (SYSFS_MAX_QUEUED_EVENTS
, &max_queued_events
);
85 inotify_glue_watch (int fd
, const char *filename
, __u32 mask
)
87 struct inotify_watch_request iwr
;
90 file_fd
= open (filename
, O_RDONLY
);
98 wd
= ioctl (fd
, INOTIFY_WATCH
, &iwr
);
104 fprintf(stderr
, "Maximum watch limit hit. Try adjusting /sys/class/misc/inotify/max_user_watches\n");
107 fprintf(stderr
, "This usually indicates an inotify version incompatibility.\n");
122 inotify_glue_ignore (int fd
, __s32 wd
)
126 ret
= ioctl (fd
, INOTIFY_IGNORE
, &wd
);
134 #define MAX_PENDING_COUNT 5
135 #define PENDING_PAUSE_MICROSECONDS 2000
136 #define PENDING_THRESHOLD(qsize) ((qsize) >> 1)
137 #define PENDING_MARGINAL_COST(p) ((unsigned int)(1 << (p)))
140 inotify_snarf_events (int fd
, int timeout_secs
, int *nr
, void **buffer_out
)
142 struct timeval timeout
;
145 unsigned int prev_pending
= 0, pending_count
= 0;
146 static struct inotify_event
*buffer
= NULL
;
147 static size_t buffer_size
;
149 /* Allocate our buffer the first time we try to read events. */
150 if (buffer
== NULL
) {
151 /* guess the avg len */
152 buffer_size
= sizeof (struct inotify_event
) + 16;
153 buffer_size
*= max_queued_events
;
154 buffer
= malloc (buffer_size
);
162 /* Set nr to 0, so it will be sure to contain something
163 valid if the select times out. */
166 /* Wait for the file descriptor to be ready to read. */
168 timeout
.tv_sec
= timeout_secs
;
172 FD_SET (fd
, &read_fds
);
174 select_retval
= select (fd
+ 1, &read_fds
, NULL
, NULL
, &timeout
);
176 /* If we time out or get an error, just return */
177 if (select_retval
<= 0)
180 /* Reading events in groups significantly helps performance.
181 * If there are some events (but not too many!) ready, wait a
182 * bit more to see if more events come in. */
184 while (pending_count
< MAX_PENDING_COUNT
) {
185 unsigned int pending
;
187 if (ioctl (fd
, FIONREAD
, &pending
) == -1)
189 pending
/= sizeof (struct inotify_event
) + 16; /* guess len */
191 /* Don't wait if the number of pending events is too close
192 * to the maximum queue size. */
193 if (pending
> PENDING_THRESHOLD (max_queued_events
))
196 /* With each successive iteration, the minimum rate for
197 * further sleep doubles. */
198 if (pending
-prev_pending
< PENDING_MARGINAL_COST(pending_count
))
201 prev_pending
= pending
;
205 timeout
.tv_usec
= PENDING_PAUSE_MICROSECONDS
;
206 select (0, NULL
, NULL
, NULL
, &timeout
);
209 *nr
= read (fd
, buffer
, buffer_size
);
211 *buffer_out
= buffer
;