Cleanup child indexables in beagle-extract-content. Print timestamp with timezone...
[beagle.git] / glue / inotify-glue.c
blobc0569746baa6e3f7f2725361cabac809c0671828
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 static int watch_limit_hit = 0;
114 int err = errno;
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");
122 watch_limit_hit = 1;
126 return wd;
131 inotify_glue_ignore (int fd, __u32 wd)
133 int ret;
135 ret = inotify_rm_watch (fd, wd);
136 if (ret < 0)
137 perror ("inotify_rm_watch");
139 return ret;
142 void
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)))
155 void
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;
162 int ret;
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);
170 if (!buffer) {
171 perror ("malloc");
172 *buffer_out = NULL;
173 return;
177 /* Set nr to 0, so it will be sure to contain something
178 valid if the poll times out. */
179 *nr = 0;
181 /* Wait for the file descriptor to be ready to read. */
182 ret = poll (pollfd, 2, -1);
183 if (ret == -1) {
184 if (errno != EINTR)
185 perror ("poll");
186 return;
187 } else if (ret == 0)
188 return;
190 /* Return immediately if something happened on the
191 snarf cancellation pipe. */
192 if (pollfd [1].revents != 0)
193 return;
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)
204 break;
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))
210 break;
212 /* With each successive iteration, the minimum rate for
213 * further sleep doubles. */
214 if (pending-prev_pending < PENDING_MARGINAL_COST(pending_count))
215 break;
217 prev_pending = pending;
218 ++pending_count;
220 nanosleep (&ts, NULL);
223 *nr = read (fd, buffer, buffer_size);
225 *buffer_out = buffer;