Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / glue / inotify-glue.c
blobca81eebf85b2e78772e644dedbfbb79032405c93
1 /*
2 * inotify-glue.c
4 * Copyright (C) 2004 Novell, Inc.
6 */
8 /*
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.
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <sys/ioctl.h>
35 #include <sys/select.h>
36 #include <sys/types.h>
38 #include "inotify.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. */
53 static void
54 read_int (const char *filename, int *var)
56 int fd, n;
57 char buffer[32];
58 char *buffer_endptr = NULL;
60 fd = open (filename, O_RDONLY);
61 if (fd == -1)
62 return;
63 if (read (fd, buffer, 31) > 0) {
64 n = (int) strtol (buffer, &buffer_endptr, 10);
65 if (*buffer != '\0' && *buffer_endptr == '\0')
66 *var = n;
68 close (fd);
72 int
73 inotify_glue_init (void)
75 static int fd = 0;
76 if (fd)
77 return fd;
78 fd = inotify_init ();
79 if (fd < 0) {
80 int _errno = errno;
81 perror ("inotify_init");
82 switch (_errno) {
83 case ENOSYS:
84 fprintf(stderr, "Inotify not supported! You need a "
85 "2.6.13 kernel or later with CONFIG_INOTIFY "
86 "enabled.");
87 break;
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);
95 return fd;
99 int
100 inotify_glue_watch (int fd, const char *filename, __u32 mask)
102 int wd;
104 wd = inotify_add_watch (fd, filename, mask);
105 if (wd < 0) {
106 int _errno = errno;
107 perror ("inotify_add_watch");
108 switch (_errno) {
109 case ENOSPC:
110 fprintf(stderr, "Maximum watch limit hit. "
111 "Try adjusting " PROCFS_MAX_USER_WATCHES ".\n");
112 break;
116 return wd;
121 inotify_glue_ignore (int fd, __s32 wd)
123 int ret;
125 ret = inotify_rm_watch (fd, wd);
126 if (ret < 0)
127 perror ("inotify_rm_watch");
129 return ret;
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)))
138 void
139 inotify_snarf_events (int fd, int timeout_secs, int *nr, void **buffer_out)
141 struct timeval timeout;
142 fd_set read_fds;
143 int select_retval;
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);
154 if (!buffer) {
155 perror ("malloc");
156 *buffer_out = NULL;
157 return;
161 /* Set nr to 0, so it will be sure to contain something
162 valid if the select times out. */
163 *nr = 0;
165 /* Wait for the file descriptor to be ready to read. */
167 timeout.tv_sec = timeout_secs;
168 timeout.tv_usec = 0;
170 FD_ZERO (&read_fds);
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)
177 return;
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)
187 break;
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))
193 break;
195 /* With each successive iteration, the minimum rate for
196 * further sleep doubles. */
197 if (pending-prev_pending < PENDING_MARGINAL_COST(pending_count))
198 break;
200 prev_pending = pending;
201 ++pending_count;
203 timeout.tv_sec = 0;
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;