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>
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 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)
83 perror ("inotify_init");
85 fprintf(stderr
, "Inotify not supported! You need a "
86 "2.6.13 kernel or later with CONFIG_INOTIFY "
90 read_int (PROCFS_MAX_USER_DEVICES
, &max_user_instances
);
91 read_int (PROCFS_MAX_USER_WATCHES
, &max_user_watches
);
92 read_int (PROCFS_MAX_QUEUED_EVENTS
, &max_queued_events
);
99 inotify_glue_watch (int fd
, const char *filename
, __u32 mask
)
103 wd
= inotify_add_watch (fd
, filename
, mask
);
106 perror ("inotify_add_watch");
108 fprintf(stderr
, "Maximum watch limit hit. "
109 "Try adjusting " PROCFS_MAX_USER_WATCHES
".\n");
117 inotify_glue_ignore (int fd
, __u32 wd
)
121 ret
= inotify_rm_watch (fd
, wd
);
123 perror ("inotify_rm_watch");
129 #define MAX_PENDING_COUNT 5
130 #define PENDING_PAUSE_NANOSECONDS 2000000
131 #define PENDING_THRESHOLD(qsize) ((unsigned int) (qsize) >> 1)
132 #define PENDING_MARGINAL_COST(p) ((unsigned int) (1 << (p)))
135 inotify_snarf_events (int fd
, int timeout_ms
, int *nr
, void **buffer_out
)
137 struct pollfd pollfd
= { fd
, POLLIN
| POLLPRI
, 0 };
138 unsigned int prev_pending
= 0, pending_count
= 0;
139 static struct inotify_event
*buffer
= NULL
;
140 static size_t buffer_size
;
143 /* Allocate our buffer the first time we try to read events. */
144 if (buffer
== NULL
) {
145 /* guess the avg len */
146 buffer_size
= sizeof (struct inotify_event
) + 16;
147 buffer_size
*= max_queued_events
;
148 buffer
= malloc (buffer_size
);
156 /* Set nr to 0, so it will be sure to contain something
157 valid if the poll times out. */
160 /* Wait for the file descriptor to be ready to read. */
161 ret
= poll (&pollfd
, 1, timeout_ms
);
168 /* Reading events in groups significantly helps performance.
169 * If there are some events (but not too many!) ready, wait a
170 * bit more to see if more events come in. */
172 while (pending_count
< MAX_PENDING_COUNT
) {
173 struct timespec ts
= {0, PENDING_PAUSE_NANOSECONDS
};
174 unsigned int pending
;
176 if (ioctl (fd
, FIONREAD
, &pending
) == -1)
179 /* Don't wait if the number of pending events is too close
180 * to the maximum queue size. */
181 pending
/= sizeof (struct inotify_event
) + 16;
182 if (pending
> PENDING_THRESHOLD (max_queued_events
))
185 /* With each successive iteration, the minimum rate for
186 * further sleep doubles. */
187 if (pending
-prev_pending
< PENDING_MARGINAL_COST(pending_count
))
190 prev_pending
= pending
;
193 nanosleep (&ts
, NULL
);
196 *nr
= read (fd
, buffer
, buffer_size
);
198 *buffer_out
= buffer
;