1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
6 * Copyright (C) 2004 Novell, Inc.
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
36 #include <sys/ioctl.h>
38 #include <sys/types.h>
41 #include "inotify-syscalls.h"
43 #define PROCFS_PREFIX "/proc/sys/fs/inotify"
45 #define PROCFS_MAX_USER_DEVICES PROCFS_PREFIX "/max_user_instances"
46 #define PROCFS_MAX_USER_WATCHES PROCFS_PREFIX "/max_user_watches"
47 #define PROCFS_MAX_QUEUED_EVENTS PROCFS_PREFIX "/max_queued_events"
49 /* Inotify sysfs knobs, initialized to their pre-sysfs defaults */
50 static int max_user_instances
= 8;
51 static int max_user_watches
= 8192;
52 static int max_queued_events
= 256;
54 static int snarf_cancellation_pipe
[2];
56 /* Paranoid code to read an integer from a sysfs (well, any) file. */
58 read_int (const char *filename
, int *var
)
62 char *buffer_endptr
= NULL
;
64 fd
= open (filename
, O_RDONLY
);
67 if (read (fd
, buffer
, 31) > 0) {
68 n
= (int) strtol (buffer
, &buffer_endptr
, 10);
69 if (*buffer
!= '\0' && *buffer_endptr
== '\0')
77 inotify_glue_init (void)
87 perror ("inotify_init");
89 fprintf(stderr
, "Inotify not supported! You need a "
90 "2.6.13 kernel or later with CONFIG_INOTIFY "
94 if (pipe (snarf_cancellation_pipe
) == -1)
95 perror ("Can't create snarf_cancellation_pipe");
97 read_int (PROCFS_MAX_USER_DEVICES
, &max_user_instances
);
98 read_int (PROCFS_MAX_USER_WATCHES
, &max_user_watches
);
99 read_int (PROCFS_MAX_QUEUED_EVENTS
, &max_queued_events
);
106 inotify_glue_watch (int fd
, const char *filename
, __u32 mask
)
110 wd
= inotify_add_watch (fd
, filename
, mask
);
112 static int watch_limit_hit
= 0;
116 if (! watch_limit_hit
|| err
!= ENOSPC
)
117 perror ("inotify_add_watch");
119 if (! watch_limit_hit
&& err
== ENOSPC
) {
120 fprintf(stderr
, "Maximum watch limit hit. "
121 "Try adjusting " PROCFS_MAX_USER_WATCHES
".\n");
131 inotify_glue_ignore (int fd
, __u32 wd
)
135 ret
= inotify_rm_watch (fd
, wd
);
137 perror ("inotify_rm_watch");
143 inotify_snarf_cancel ()
145 write (snarf_cancellation_pipe
[1],
146 &snarf_cancellation_pipe
, 1); // write a convenient byte
150 #define MAX_PENDING_COUNT 5
151 #define PENDING_PAUSE_NANOSECONDS 2000000
152 #define PENDING_THRESHOLD(qsize) ((unsigned int) (qsize) >> 1)
153 #define PENDING_MARGINAL_COST(p) ((unsigned int) (1 << (p)))
156 inotify_snarf_events (int fd
, int *nr
, void **buffer_out
)
158 struct pollfd pollfd
[2] = { { fd
, POLLIN
| POLLPRI
, 0 }, { snarf_cancellation_pipe
[0], POLLIN
, 0} };
159 unsigned int prev_pending
= 0, pending_count
= 0;
160 static struct inotify_event
*buffer
= NULL
;
161 static size_t buffer_size
;
164 /* Allocate our buffer the first time we try to read events. */
165 if (buffer
== NULL
) {
166 /* guess the avg len */
167 buffer_size
= sizeof (struct inotify_event
) + 16;
168 buffer_size
*= max_queued_events
;
169 buffer
= malloc (buffer_size
);
177 /* Set nr to 0, so it will be sure to contain something
178 valid if the poll times out. */
181 /* Wait for the file descriptor to be ready to read. */
182 ret
= poll (pollfd
, 2, -1);
190 /* Return immediately if something happened on the
191 snarf cancellation pipe. */
192 if (pollfd
[1].revents
!= 0)
195 /* Reading events in groups significantly helps performance.
196 * If there are some events (but not too many!) ready, wait a
197 * bit more to see if more events come in. */
199 while (pending_count
< MAX_PENDING_COUNT
) {
200 struct timespec ts
= {0, PENDING_PAUSE_NANOSECONDS
};
201 unsigned int pending
;
203 if (ioctl (fd
, FIONREAD
, &pending
) == -1)
206 /* Don't wait if the number of pending events is too close
207 * to the maximum queue size. */
208 pending
/= sizeof (struct inotify_event
) + 16;
209 if (pending
> PENDING_THRESHOLD (max_queued_events
))
212 /* With each successive iteration, the minimum rate for
213 * further sleep doubles. */
214 if (pending
-prev_pending
< PENDING_MARGINAL_COST(pending_count
))
217 prev_pending
= pending
;
220 nanosleep (&ts
, NULL
);
223 *nr
= read (fd
, buffer
, buffer_size
);
225 *buffer_out
= buffer
;