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>
36 #include <sys/types.h>
39 #include "inotify-syscalls.h"
41 #define PROCFS_PREFIX "/proc/sys/fs/inotify"
43 #define PROCFS_MAX_USER_DEVICES PROCFS_PREFIX "/max_user_instances"
44 #define PROCFS_MAX_USER_WATCHES PROCFS_PREFIX "/max_user_watches"
45 #define PROCFS_MAX_QUEUED_EVENTS PROCFS_PREFIX "/max_queued_events"
47 /* Inotify sysfs knobs, initialized to their pre-sysfs defaults */
48 static int max_user_instances
= 8;
49 static int max_user_watches
= 8192;
50 static unsigned int max_queued_events
= 256;
52 /* Paranoid code to read an integer from a sysfs (well, any) file. */
54 read_int (const char *filename
, int *var
)
58 char *buffer_endptr
= NULL
;
60 fd
= open (filename
, O_RDONLY
);
63 if (read (fd
, buffer
, 31) > 0) {
64 n
= (int) strtol (buffer
, &buffer_endptr
, 10);
65 if (*buffer
!= '\0' && *buffer_endptr
== '\0')
73 inotify_glue_init (void)
81 perror ("inotify_init");
84 fprintf(stderr
, "Inotify not supported! You need a "
85 "2.6.13 kernel or later with CONFIG_INOTIFY "
91 read_int (PROCFS_MAX_USER_DEVICES
, &max_user_instances
);
92 read_int (PROCFS_MAX_USER_WATCHES
, &max_user_watches
);
93 read_int (PROCFS_MAX_QUEUED_EVENTS
, &max_queued_events
);
100 inotify_glue_watch (int fd
, const char *filename
, __u32 mask
)
104 wd
= inotify_add_watch (fd
, filename
, mask
);
107 perror ("inotify_add_watch");
110 fprintf(stderr
, "Maximum watch limit hit. "
111 "Try adjusting " PROCFS_MAX_USER_WATCHES
".\n");
121 inotify_glue_ignore (int fd
, __s32 wd
)
125 ret
= inotify_rm_watch (fd
, wd
);
127 perror ("inotify_rm_watch");
133 #define MAX_PENDING_COUNT 5
134 #define PENDING_PAUSE_MICROSECONDS 2000
135 #define PENDING_THRESHOLD(qsize) ((unsigned int) (qsize) >> 1)
136 #define PENDING_MARGINAL_COST(p) ((unsigned int) (1 << (p)))
139 inotify_snarf_events (int fd
, int timeout_secs
, int *nr
, void **buffer_out
)
141 struct timeval timeout
;
144 unsigned int prev_pending
= 0, pending_count
= 0;
145 static struct inotify_event
*buffer
= NULL
;
146 static size_t buffer_size
;
148 /* Allocate our buffer the first time we try to read events. */
149 if (buffer
== NULL
) {
150 /* guess the avg len */
151 buffer_size
= sizeof (struct inotify_event
) + 16;
152 buffer_size
*= max_queued_events
;
153 buffer
= malloc (buffer_size
);
161 /* Set nr to 0, so it will be sure to contain something
162 valid if the select times out. */
165 /* Wait for the file descriptor to be ready to read. */
167 timeout
.tv_sec
= timeout_secs
;
171 FD_SET (fd
, &read_fds
);
173 select_retval
= select (fd
+ 1, &read_fds
, NULL
, NULL
, &timeout
);
175 /* If we time out or get an error, just return */
176 if (select_retval
<= 0)
179 /* Reading events in groups significantly helps performance.
180 * If there are some events (but not too many!) ready, wait a
181 * bit more to see if more events come in. */
183 while (pending_count
< MAX_PENDING_COUNT
) {
184 unsigned int pending
;
186 if (ioctl (fd
, FIONREAD
, &pending
) == -1)
188 pending
/= sizeof (struct inotify_event
) + 16; /* guess len */
190 /* Don't wait if the number of pending events is too close
191 * to the maximum queue size. */
192 if (pending
> PENDING_THRESHOLD (max_queued_events
))
195 /* With each successive iteration, the minimum rate for
196 * further sleep doubles. */
197 if (pending
-prev_pending
< PENDING_MARGINAL_COST(pending_count
))
200 prev_pending
= pending
;
204 timeout
.tv_usec
= PENDING_PAUSE_MICROSECONDS
;
205 select (0, NULL
, NULL
, NULL
, &timeout
);
208 *nr
= read (fd
, buffer
, buffer_size
);
210 *buffer_out
= buffer
;