-
Notifications
You must be signed in to change notification settings - Fork 7
/
watch.c
313 lines (273 loc) · 8.03 KB
/
watch.c
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*******************************************************************************
Copyright (c) 2011-2014 Dmitry Matveev <me@dmitrymatveev.co.uk>
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 "compat.h"
#include <errno.h> /* errno */
#include <fcntl.h> /* open */
#include <unistd.h> /* close */
#include <string.h> /* strdup */
#include <stdlib.h> /* free */
#include <assert.h>
#include <sys/types.h>
#include <sys/event.h> /* kevent */
#include <sys/stat.h> /* stat */
#include <stdio.h> /* snprintf */
#include "utils.h"
#include "watch.h"
#include "sys/inotify.h"
/**
* Convert the inotify watch mask to the kqueue event filter flags.
*
* @param[in] flags An inotify watch mask.
* @param[in] wf A kqueue watch internal flags.
* @return Converted kqueue event filter flags.
**/
uint32_t
inotify_to_kqueue (uint32_t flags, watch_flags_t wf)
{
uint32_t result = 0;
if (!(S_ISREG (wf) || S_ISDIR (wf) || S_ISLNK (wf))) {
return result;
}
#ifdef NOTE_OPEN
if (flags & IN_OPEN)
result |= NOTE_OPEN;
#endif
#ifdef NOTE_CLOSE
if (flags & IN_CLOSE_NOWRITE)
result |= NOTE_CLOSE;
if (flags & IN_CLOSE_WRITE && S_ISREG (wf))
result |= (NOTE_CLOSE | NOTE_WRITE);
#endif
#ifdef NOTE_READ
if (flags & IN_ACCESS && S_ISREG (wf))
result |= NOTE_READ;
#endif
if (flags & IN_ATTRIB)
result |= NOTE_ATTRIB;
if (flags & IN_MODIFY && S_ISREG (wf))
result |= NOTE_WRITE;
if (!(wf & WF_ISSUBWATCH)) {
if (S_ISDIR (wf)) {
result |= NOTE_WRITE;
#ifdef HAVE_NOTE_EXTEND_ON_SUBFILE_RENAME
result |= NOTE_EXTEND;
#endif
}
if (flags & IN_ATTRIB && S_ISREG (wf))
result |= NOTE_LINK;
if (flags & IN_MOVE_SELF)
result |= NOTE_RENAME;
result |= NOTE_DELETE | NOTE_REVOKE;
}
return result;
}
/**
* Convert the kqueue event filter flags to the inotify watch mask.
*
* @param[in] flags A kqueue filter flags.
* @param[in] wf A kqueue watch internal flags.
* @return Converted inotify watch mask.
**/
uint32_t
kqueue_to_inotify (uint32_t flags, watch_flags_t wf)
{
uint32_t result = 0;
#ifdef NOTE_OPEN
if (flags & NOTE_OPEN)
result |= IN_OPEN;
#endif
#ifdef NOTE_CLOSE
if (flags & NOTE_CLOSE) {
if (wf & WF_MODIFIED && S_ISREG (wf))
result |= IN_CLOSE_WRITE;
else
result |= IN_CLOSE_NOWRITE;
}
#endif
#ifdef NOTE_READ
if (flags & NOTE_READ && S_ISREG (wf))
result |= IN_ACCESS;
#endif
if (flags & NOTE_ATTRIB)
result |= IN_ATTRIB;
if (flags & NOTE_LINK && S_ISREG (wf) && !(wf & WF_ISSUBWATCH))
result |= IN_ATTRIB;
if (flags & NOTE_WRITE && S_ISREG (wf))
result |= IN_MODIFY;
if (flags & NOTE_DELETE && !(wf & WF_ISSUBWATCH)) {
/* Treat deletes as link number changes if links still exist */
if (wf & WF_DELETED || !S_ISREG (wf))
result |= IN_DELETE_SELF;
else
result |= IN_ATTRIB;
}
if (flags & NOTE_RENAME && !(wf & WF_ISSUBWATCH))
result |= IN_MOVE_SELF;
if (flags & NOTE_REVOKE && !(wf & WF_ISSUBWATCH))
result |= IN_UNMOUNT;
/* IN_ISDIR flag for subwatches is set in the enqueue_event routine */
if ((result & (IN_ATTRIB | IN_OPEN | IN_ACCESS | IN_CLOSE))
&& S_ISDIR (wf) && !(wf & WF_ISSUBWATCH)) {
result |= IN_ISDIR;
}
return result;
}
/* struct kevent is declared slightly differently on the different BSDs.
* This macros will help to avoid cast warnings on the supported platforms. */
#if defined (__NetBSD__)
#define PTR_TO_UDATA(X) ((intptr_t)X)
#else
#define PTR_TO_UDATA(X) (X)
#endif
/**
* Register vnode kqueue watch in kernel kqueue(2) subsystem
*
* @param[in] w A pointer to a watch
* @param[in] fflags A filter flags in kqueue format
* @return 1 on success, -1 on error and 0 if no events have been registered
**/
int
watch_register_event (watch *w, uint32_t fflags)
{
assert (w != NULL);
int kq = w->iw->wrk->kq;
assert (kq != -1);
struct kevent ev;
EV_SET (&ev,
w->fd,
EVFILT_VNODE,
EV_ADD | EV_ENABLE | EV_CLEAR,
fflags,
0,
PTR_TO_UDATA (w));
return kevent (kq, &ev, 1, NULL, 0, NULL);
}
/**
* Opens a file descriptor of kqueue watch
*
* @param[in] dirfd A filedes of parent directory or AT_FDCWD.
* @param[in] path A pointer to filename
* @param[in] flags A watch flags in inotify format
* @return A file descriptor of opened kqueue watch
**/
int
watch_open (int dirfd, const char *path, uint32_t flags)
{
assert (path != NULL);
int openflags = O_NONBLOCK;
#ifdef O_EVTONLY
openflags |= O_EVTONLY;
#else
openflags |= O_RDONLY;
#endif
#ifdef O_CLOEXEC
openflags |= O_CLOEXEC;
#endif
if (flags & IN_DONT_FOLLOW) {
#ifdef O_SYMLINK
openflags |= O_SYMLINK;
#else
openflags |= O_NOFOLLOW;
#endif
}
#ifdef O_DIRECTORY
if (flags & IN_ONLYDIR) {
openflags |= O_DIRECTORY;
}
#endif
int fd = openat (dirfd, path, openflags);
if (fd == -1) {
return -1;
}
#ifndef O_DIRECTORY
if (flags & IN_ONLYDIR) {
struct stat st;
if (fstat (fd, &st) == -1) {
perror_msg ("Failed to fstat on watch open %s", path);
close (fd);
return -1;
}
if (!S_ISDIR (st.st_mode)) {
errno = ENOTDIR;
close (fd);
return -1;
}
}
#endif
#ifndef O_CLOEXEC
if (set_cloexec_flag (fd, 1) == -1) {
close (fd);
return -1;
}
#endif
return fd;
}
/**
* Initialize a watch.
*
* @param[in] iw; A backreference to parent #i_watch.
* @param[in] watch_type The type of the watch.
* @param[in] fd A file descriptor of a watched entry.
* @param[in] st A stat structure of watch.
* @return A pointer to a watch on success, NULL on failure.
**/
watch *
watch_init (i_watch *iw, watch_type_t watch_type, int fd, struct stat *st)
{
assert (iw != NULL);
assert (fd != -1);
watch_flags_t wf = watch_type != WATCH_USER ? WF_ISSUBWATCH : 0;
wf |= st->st_mode & S_IFMT;
uint32_t fflags = inotify_to_kqueue (iw->flags, wf);
/* Skip watches with empty kqueue filter flags */
if (fflags == 0) {
return NULL;
}
watch *w = calloc (1, sizeof (struct watch));
if (w == NULL) {
perror_msg ("Failed to allocate watch");
return NULL;
}
w->iw = iw;
w->fd = fd;
w->flags = wf;
w->refcount = 0;
/* Inode number obtained via fstat call cannot be used here as it
* differs from readdir`s one at mount points. */
w->inode = st->st_ino;
if (watch_register_event (w, fflags) == -1) {
free (w);
return NULL;
}
return w;
}
/**
* Free a watch and all the associated memory.
*
* @param[in] w A pointer to a watch.
**/
void
watch_free (watch *w)
{
assert (w != NULL);
if (w->fd != -1) {
close (w->fd);
}
free (w);
}