2006-04-25 Hendrik Brandt <heb@gnome-de.org>
[beagle.git] / glue / inotify-glue.c
blobd20e62a7668e1efa3f6c8d3f88ebe742678465bf
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 /*
4 * inotify-glue.c
6 * Copyright (C) 2004 Novell, Inc.
8 */
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.
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <fcntl.h>
33 #include <time.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <sys/ioctl.h>
37 #include <sys/poll.h>
38 #include <sys/types.h>
40 #include "inotify.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. */
57 static void
58 read_int (const char *filename, int *var)
60 int fd, n;
61 char buffer[32];
62 char *buffer_endptr = NULL;
64 fd = open (filename, O_RDONLY);
65 if (fd == -1)
66 return;
67 if (read (fd, buffer, 31) > 0) {
68 n = (int) strtol (buffer, &buffer_endptr, 10);
69 if (*buffer != '\0' && *buffer_endptr == '\0')
70 *var = n;
72 close (fd);
76 int
77 inotify_glue_init (void)
79 static int fd = 0;
81 if (fd)
82 return fd;
84 fd = inotify_init ();
85 if (fd < 0) {
86 int err = errno;
87 perror ("inotify_init");
88 if (err == ENOSYS)
89 fprintf(stderr, "Inotify not supported! You need a "
90 "2.6.13 kernel or later with CONFIG_INOTIFY "
91 "enabled.");
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);
101 return fd;
106 inotify_glue_watch (int fd, const char *filename, __u32 mask)
108 int wd;
110 wd = inotify_add_watch (fd, filename, mask);
111 if (wd < 0) {
112 int err = errno;
113 perror ("inotify_add_watch");
114 if (err == ENOSPC)
115 fprintf(stderr, "Maximum watch limit hit. "
116 "Try adjusting " PROCFS_MAX_USER_WATCHES ".\n");
119 return wd;
124 inotify_glue_ignore (int fd, __u32 wd)
126 int ret;
128 ret = inotify_rm_watch (fd, wd);
129 if (ret < 0)
130 perror ("inotify_rm_watch");
132 return ret;
135 void
136 inotify_snarf_cancel ()
138 write (snarf_cancellation_pipe [1],
139 &snarf_cancellation_pipe, 1); // write a convenient byte
143 #define MAX_PENDING_COUNT 5
144 #define PENDING_PAUSE_NANOSECONDS 2000000
145 #define PENDING_THRESHOLD(qsize) ((unsigned int) (qsize) >> 1)
146 #define PENDING_MARGINAL_COST(p) ((unsigned int) (1 << (p)))
148 void
149 inotify_snarf_events (int fd, int *nr, void **buffer_out)
151 struct pollfd pollfd [2] = { { fd, POLLIN | POLLPRI, 0 }, { snarf_cancellation_pipe [0], POLLIN, 0} };
152 unsigned int prev_pending = 0, pending_count = 0;
153 static struct inotify_event *buffer = NULL;
154 static size_t buffer_size;
155 int ret;
157 /* Allocate our buffer the first time we try to read events. */
158 if (buffer == NULL) {
159 /* guess the avg len */
160 buffer_size = sizeof (struct inotify_event) + 16;
161 buffer_size *= max_queued_events;
162 buffer = malloc (buffer_size);
163 if (!buffer) {
164 perror ("malloc");
165 *buffer_out = NULL;
166 return;
170 /* Set nr to 0, so it will be sure to contain something
171 valid if the poll times out. */
172 *nr = 0;
174 /* Wait for the file descriptor to be ready to read. */
175 ret = poll (pollfd, 2, -1);
176 if (ret == -1) {
177 if (errno != EINTR)
178 perror ("poll");
179 return;
180 } else if (ret == 0)
181 return;
183 /* Return immediately if something happened on the
184 snarf cancellation pipe. */
185 if (pollfd [1].revents != 0)
186 return;
188 /* Reading events in groups significantly helps performance.
189 * If there are some events (but not too many!) ready, wait a
190 * bit more to see if more events come in. */
192 while (pending_count < MAX_PENDING_COUNT) {
193 struct timespec ts = {0, PENDING_PAUSE_NANOSECONDS};
194 unsigned int pending;
196 if (ioctl (fd, FIONREAD, &pending) == -1)
197 break;
199 /* Don't wait if the number of pending events is too close
200 * to the maximum queue size. */
201 pending /= sizeof (struct inotify_event) + 16;
202 if (pending > PENDING_THRESHOLD (max_queued_events))
203 break;
205 /* With each successive iteration, the minimum rate for
206 * further sleep doubles. */
207 if (pending-prev_pending < PENDING_MARGINAL_COST(pending_count))
208 break;
210 prev_pending = pending;
211 ++pending_count;
213 nanosleep (&ts, NULL);
216 *nr = read (fd, buffer, buffer_size);
218 *buffer_out = buffer;