File: inotify-glue.c

package info (click to toggle)
muine 0.8.7-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,436 kB
  • ctags: 3,081
  • sloc: cs: 12,764; sh: 8,895; ansic: 5,290; xml: 1,408; makefile: 493
file content (196 lines) | stat: -rw-r--r-- 5,101 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * inotify-glue.c
 *
 * Copyright (C) 2004 Novell, Inc.
 *
 */

/*
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/types.h>

#include "inotify.h"
#include "inotify-syscalls.h"

#define PROCFS_PREFIX           "/proc/sys/fs/inotify"

#define PROCFS_MAX_USER_DEVICES  PROCFS_PREFIX "/max_user_instances"
#define PROCFS_MAX_USER_WATCHES  PROCFS_PREFIX "/max_user_watches"
#define PROCFS_MAX_QUEUED_EVENTS PROCFS_PREFIX "/max_queued_events"

/* Inotify sysfs knobs, initialized to their pre-sysfs defaults */
static int max_user_instances = 8;
static int max_user_watches = 8192;
static int max_queued_events = 256;

/* Paranoid code to read an integer from a sysfs (well, any) file. */
static void
read_int (const char *filename, int *var)
{
	int fd, n;
	char buffer[32];
	char *buffer_endptr = NULL;

	fd = open (filename, O_RDONLY);
	if (fd == -1)
		return;
	if (read (fd, buffer, 31) > 0) {
		n = (int) strtol (buffer, &buffer_endptr, 10);
		if (*buffer != '\0' && *buffer_endptr == '\0')
			*var = n;
	}
	close (fd);
}


int
inotify_glue_init (void)
{
	static int fd = 0;

	if (fd)
		return fd;

	fd = inotify_init ();
	if (fd < 0) {
		int err = errno;
		perror ("inotify_init");
		if (err == ENOSYS)
			fprintf(stderr, "Inotify not supported!  You need a "
				"2.6.13 kernel or later with CONFIG_INOTIFY "
				"enabled.");
	}

	read_int (PROCFS_MAX_USER_DEVICES, &max_user_instances);
	read_int (PROCFS_MAX_USER_WATCHES, &max_user_watches);
	read_int (PROCFS_MAX_QUEUED_EVENTS, &max_queued_events);

	return fd;
}


int
inotify_glue_watch (int fd, const char *filename, __u32 mask)
{
	int wd;

	wd = inotify_add_watch (fd, filename, mask);
	if (wd < 0) {
		int err = errno;
		perror ("inotify_add_watch");
		if (err == ENOSPC)
			fprintf(stderr, "Maximum watch limit hit. "
				"Try adjusting " PROCFS_MAX_USER_WATCHES ".\n");
	}

	return wd;
}


int
inotify_glue_ignore (int fd, __u32 wd)
{
	int ret;

	ret = inotify_rm_watch (fd, wd);
	if (ret < 0)
		perror ("inotify_rm_watch");

	return ret;
}


#define MAX_PENDING_COUNT		5
#define PENDING_PAUSE_NANOSECONDS	2000000
#define PENDING_THRESHOLD(qsize)	((unsigned int) (qsize) >> 1)
#define PENDING_MARGINAL_COST(p)	((unsigned int) (1 << (p)))

void
inotify_snarf_events (int fd, int timeout_ms, int *nr, void **buffer_out)
{
	struct pollfd pollfd = { fd, POLLIN | POLLPRI, 0 };
	unsigned int prev_pending = 0, pending_count = 0;
	static struct inotify_event *buffer = NULL;
	static size_t buffer_size;
	int ret;

	/* Allocate our buffer the first time we try to read events. */
	if (buffer == NULL) {
		/* guess the avg len */
		buffer_size = sizeof (struct inotify_event) + 16;
		buffer_size *= max_queued_events;
		buffer = malloc (buffer_size);
		if (!buffer) {
			perror ("malloc");
			*buffer_out = NULL;
			return;
		}
	}

	/* Set nr to 0, so it will be sure to contain something
	   valid if the poll times out. */
	*nr = 0;

	/* Wait for the file descriptor to be ready to read. */
	ret = poll (&pollfd, 1, timeout_ms);
	if (ret == -1 || ret == 0)
		return;

	/* Reading events in groups significantly helps performance.
	 * If there are some events (but not too many!) ready, wait a
	 * bit more to see if more events come in. */

	while (pending_count < MAX_PENDING_COUNT) {
		struct timespec ts = {0, PENDING_PAUSE_NANOSECONDS};
		unsigned int pending;

		if (ioctl (fd, FIONREAD, &pending) == -1)
			break;

		/* Don't wait if the number of pending events is too close
		 * to the maximum queue size. */
		pending /= sizeof (struct inotify_event) + 16;		
		if (pending > PENDING_THRESHOLD (max_queued_events))
			break;

		/* With each successive iteration, the minimum rate for
		 * further sleep doubles. */
		if (pending-prev_pending < PENDING_MARGINAL_COST(pending_count))
			break;

		prev_pending = pending;
		++pending_count;

		nanosleep (&ts, NULL);
	}

	*nr = read (fd, buffer, buffer_size);

	*buffer_out = buffer;
}