-
Notifications
You must be signed in to change notification settings - Fork 6
/
worker.c
612 lines (531 loc) · 15.1 KB
/
worker.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/*******************************************************************************
Copyright (c) 2011-2014 Dmitry Matveev <me@dmitrymatveev.co.uk>
Copyright (c) 2014-2018 Vladimir Kondratyev <vladimir@kondratyev.su>
SPDX-License-Identifier: MIT
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 <sys/types.h>
#include <sys/event.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <assert.h>
#include <dirent.h>
#include <fcntl.h> /* open() */
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> /* close() */
#include "sys/inotify.h"
#include "compat.h"
#include "event-queue.h"
#include "inotify-watch.h"
#include "utils.h"
#include "watch.h"
#include "worker-thread.h"
#include "worker.h"
static void
worker_cmd_reset (struct worker_cmd *cmd);
/**
* Prepare a command with the data of the inotify_add_watch() call.
*
* @param[in] cmd A pointer to #worker_cmd.
* @param[in] filename A file name of the watched entry.
* @param[in] mask A combination of the inotify watch flags.
**/
void
worker_cmd_add (struct worker_cmd *cmd, const char *filename, uint32_t mask)
{
assert (cmd != NULL);
worker_cmd_reset (cmd);
cmd->type = WCMD_ADD;
cmd->cmd.add.filename = filename;
cmd->cmd.add.mask = mask;
}
/**
* Prepare a command with the data of the inotify_rm_watch() call.
*
* @param[in] cmd A pointer to #worker_cmd
* @param[in] watch_id The identificator of a watch to remove.
**/
void
worker_cmd_remove (struct worker_cmd *cmd, int watch_id)
{
assert (cmd != NULL);
worker_cmd_reset (cmd);
cmd->type = WCMD_REMOVE;
cmd->cmd.rm_id = watch_id;
}
/**
* Prepare a command with the data of the libinotify_set_param() call.
*
* @param[in] cmd A pointer to #worker_cmd
* @param[in] param Worker-thread parameter name to set.
* @param[in] value Worker-thread parameter value to set.
**/
void
worker_cmd_param (struct worker_cmd *cmd, int param, intptr_t value)
{
assert (cmd != NULL);
worker_cmd_reset (cmd);
cmd->type = WCMD_PARAM;
cmd->cmd.param.param = param;
cmd->cmd.param.value = value;
}
/**
* Reset the worker command.
*
* @param[in] cmd A pointer to #worker_cmd.
**/
static void
worker_cmd_reset (struct worker_cmd *cmd)
{
assert (cmd != NULL);
memset (cmd, 0, sizeof (struct worker_cmd));
}
/**
* Signal user thread if worker command is done
*
* @param[in] cmd A pointer to #worker_cmd.
**/
void
worker_post (struct worker *wrk)
{
assert (wrk != NULL);
worker_lock (wrk);
++wrk->sema;
pthread_cond_broadcast (&wrk->cv);
worker_unlock (wrk);
}
/**
* Wait for worker command to complete
*
* @param[in] cmd A pointer to #worker_cmd.
**/
void
worker_wait (struct worker *wrk)
{
assert (wrk != NULL);
worker_lock (wrk);
while (wrk->sema == 0) {
pthread_cond_wait (&wrk->cv, &wrk->mutex);
}
--wrk->sema;
worker_unlock (wrk);
}
/**
* Signal worker thread that #worker_cmd should be executed.
*
* @param[in] wrk A pointer to #worker.
* @param[in] cmd A pointer to #worker_cmd passed to #worker.
* @return positive number or 0 on success, -1 on error
**/
int
worker_notify (struct worker *wrk, struct worker_cmd *cmd)
{
#ifdef EVFILT_USER
struct kevent ke;
EV_SET (&ke,
wrk->io[KQUEUE_FD],
EVFILT_USER,
0,
NOTE_TRIGGER,
#ifdef __DragonFly__
/* DragonflyBSD does not copy udata */
(intptr_t)cmd,
0
#else
0,
cmd
#endif
);
return kevent (wrk->kq, &ke, 1, NULL, 0, zero_tsp);
#else
return write (wrk->io[INOTIFY_FD], &cmd, sizeof (cmd));
#endif
}
/**
* Set communication pipe buffer size
* @param[in] wrk A pointer to #worker.
* @param[in] bufsize A buffer size allocated for communication pipe
* @return 0 on success, -1 otherwise
**/
int
worker_set_sockbufsize (struct worker *wrk, int bufsize)
{
assert (wrk != NULL);
if (bufsize <= 0) {
errno = EINVAL;
return -1;
}
if (set_sndbuf_size (wrk->io[KQUEUE_FD], bufsize)) {
perror_msg (("Failed to set send buffer size for socket"));
return -1;
}
#ifndef EVFILT_EMPTY
{
struct kevent ev;
EV_SET (&ev,
wrk->io[KQUEUE_FD],
EVFILT_WRITE,
EV_ADD | EV_ENABLE | EV_CLEAR,
NOTE_LOWAT,
bufsize,
0);
if (kevent (wrk->kq, &ev, 1, NULL, 0, zero_tsp) == -1) {
int save_errno = errno;
bufsize = wrk->sockbufsize;
set_sndbuf_size (wrk->io[KQUEUE_FD], bufsize);
errno = save_errno;
perror_msg (("Failed to register kqueue event on socket"));
return -1;
}
}
#endif
wrk->sockbufsize = bufsize;
return 0;
}
/**
* Create communication pipe
*
* @param[in] filedes A pair of descriptors used in referencing the new pipe
* @param[in] flags A Pipe flags in inotify(linux) or fcntl.h(BSD) format
* @return 0 on success, -1 otherwise
**/
static int
pipe_init (int fildes[2], int flags)
{
if (socketpair (AF_UNIX, SOCK_STREAM, 0, fildes) == -1) {
perror_msg (("Failed to create a socket pair"));
return -1;
}
#ifdef SO_NOSIGPIPE
{
int on = 1;
setsockopt (fildes[KQUEUE_FD],
SOL_SOCKET,
SO_NOSIGPIPE,
&on,
sizeof(on));
}
#endif
if (set_nonblock_flag (fildes[KQUEUE_FD], 1) == -1) {
perror_msg (("Failed to set socket into nonblocking mode"));
return -1;
}
if (set_cloexec_flag (fildes[KQUEUE_FD], 1) == -1) {
perror_msg (("Failed to set cloexec flag on socket"));
return -1;
}
/* Check flags for both linux and BSD CLOEXEC values */
if (set_cloexec_flag (fildes[INOTIFY_FD],
#ifdef O_CLOEXEC
flags & (IN_CLOEXEC|O_CLOEXEC)) == -1) {
#else
flags & IN_CLOEXEC) == -1) {
#endif
perror_msg (("Failed to set cloexec flag on socket"));
return -1;
}
/* Check flags for both linux and BSD NONBLOCK values */
if (set_nonblock_flag (fildes[INOTIFY_FD],
flags & (IN_NONBLOCK|O_NONBLOCK)) == -1) {
perror_msg (("Failed to set socket into nonblocking mode"));
return -1;
}
return 0;
}
/**
* Create a new worker and start its thread.
*
* @return A pointer to a new worker.
**/
struct worker*
worker_create (int flags)
{
pthread_attr_t attr;
struct kevent ev[3];
sigset_t set, oset;
int result, nevents = 1;
struct worker* wrk = calloc (1, sizeof (struct worker));
if (wrk == NULL) {
perror_msg (("Failed to create a new worker"));
goto failure;
}
wrk->io[INOTIFY_FD] = -1;
wrk->io[KQUEUE_FD] = -1;
#ifdef HAVE_KQUEUE1
wrk->kq = kqueue1 (O_CLOEXEC);
#else
wrk->kq = kqueue ();
if (wrk->kq != -1) {
(void)set_cloexec_flag (wrk->kq, 1);
}
#endif
if (wrk->kq == -1) {
perror_msg (("Failed to create a new kqueue"));
goto failure;
}
if (pipe_init (wrk->io, flags) == -1) {
perror_msg (("Failed to create a pipe"));
goto failure;
}
/* Set socket buffer size to IN_DEF_SOCKBUFSIZE bytes */
if (worker_set_sockbufsize(wrk, IN_DEF_SOCKBUFSIZE) == -1) {
goto failure;
}
SLIST_INIT (&wrk->head);
#ifdef EVFILT_USER
EV_SET (&ev[0], wrk->io[KQUEUE_FD], EVFILT_USER, EV_ADD | EV_CLEAR, 0, 0, 0);
#else
EV_SET (&ev[0],
wrk->io[KQUEUE_FD],
EVFILT_READ,
EV_ADD | EV_ENABLE | EV_CLEAR,
NOTE_LOWAT,
1,
0);
#endif
#ifdef EVFILT_EMPTY
/*
* Modern FreeBSDs always report full sendbuffer size in data field of
* EVFILT_WRITE kevent so we can not determine amount of data remaining in
* it reliably. As we want to know exact amount of bytes to avoid partial
* inotify event reads as much as possible, start using of EVFILT_EMPTY
* to check available send buffer space. Note that we still use
* EVFILT_WRITE with NOTE_LOWAT set too high to check EOF conditions.
*/
EV_SET (&ev[1],
wrk->io[KQUEUE_FD],
EVFILT_WRITE,
EV_ADD | EV_ENABLE | EV_CLEAR,
NOTE_LOWAT,
INT_MAX,
0);
EV_SET (&ev[2], wrk->io[KQUEUE_FD], EVFILT_EMPTY, EV_ADD | EV_CLEAR, 0, 0, 0);
nevents = 3;
#endif
if (kevent (wrk->kq, ev, nevents, NULL, 0, zero_tsp) == -1) {
perror_msg (("Failed to register kqueue event on pipe"));
goto failure;
}
wrk->wd_last = 0;
wrk->wd_overflow = false;
pthread_mutex_init (&wrk->cmd_mtx, NULL);
atomic_init (&wrk->mutex_rc, 0);
pthread_mutex_init (&wrk->mutex, NULL);
pthread_cond_init (&wrk->cv, NULL);
wrk->sema = 0;
event_queue_init (&wrk->eq);
watch_set_init (&wrk->watches);
/* create a run a worker thread */
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
sigfillset (&set);
pthread_sigmask (SIG_BLOCK, &set, &oset);
result = pthread_create (&wrk->thread, &attr, worker_thread, wrk);
pthread_attr_destroy (&attr);
pthread_sigmask (SIG_SETMASK, &oset, NULL);
if (result != 0) {
perror_msg (("Failed to start a new worker thread"));
goto failure;
}
return wrk;
failure:
if (wrk != NULL) {
if (wrk->io[INOTIFY_FD] != -1) {
close (wrk->io[INOTIFY_FD]);
}
worker_free (wrk);
}
return NULL;
}
/**
* Free a worker and all the associated memory.
*
* @param[in] wrk A pointer to #worker.
**/
void
worker_free (struct worker *wrk)
{
struct i_watch *iw;
assert (wrk != NULL);
if (wrk->io[KQUEUE_FD] != -1) {
close (wrk->io[KQUEUE_FD]);
wrk->io[KQUEUE_FD] = -1;
}
close (wrk->kq);
#ifdef WORKER_FAST_WATCHSET_DESTROY
watch_set_free (&wrk->watches);
#endif
while (!SLIST_EMPTY (&wrk->head)) {
iw = SLIST_FIRST (&wrk->head);
SLIST_REMOVE_HEAD (&wrk->head, next);
iwatch_free (iw);
}
/* Wait for user thread(s) to release worker`s mutex */
while (atomic_load (&wrk->mutex_rc) > 0) {
worker_cmd_lock (wrk);
worker_cmd_unlock (wrk);
}
pthread_mutex_destroy (&wrk->cmd_mtx);
/* And only after that destroy worker_cmd sync primitives */
pthread_cond_destroy (&wrk->cv);
pthread_mutex_destroy (&wrk->mutex);
event_queue_free (&wrk->eq);
free (wrk);
}
/**
* Allocate new inotify watch descriptor.
*
* @param[in] wrk A pointer to #worker.
* @return An unique (per watch) newly allocated descriptor
**/
int
worker_allocate_wd (struct worker *wrk)
{
bool allocated;
do {
if (wrk->wd_last == INT_MAX) {
wrk->wd_last = 0;
wrk->wd_overflow = true;
}
allocated = true;
++wrk->wd_last;
if (wrk->wd_overflow) {
struct i_watch *iw;
SLIST_FOREACH (iw, &wrk->head, next) {
if (iw->wd == wrk->wd_last) {
allocated = false;
break;
}
}
}
} while (!allocated);
return wrk->wd_last;
}
/**
* Add or modify a watch.
*
* @param[in] wrk A pointer to #worker.
* @param[in] path A file path to watch.
* @param[in] flags A combination of inotify watch flags.
* @return An id of an added watch on success, -1 on failure.
**/
int
worker_add_or_modify (struct worker *wrk,
const char *path,
uint32_t flags)
{
int fd;
struct stat st;
struct watch *w;
struct i_watch *iw;
assert (path != NULL);
assert (wrk != NULL);
/* Open inotify watch descriptor */
fd = iwatch_open (path, flags);
if (fd == -1) {
return -1;
}
if (fstat (fd, &st) == -1) {
perror_msg (("Failed to stat file %s", path));
close (fd);
return -1;
}
/* look up for an entry with these inode&device numbers */
w = watch_set_find (&wrk->watches, st.st_dev, st.st_ino);
if (w != NULL) {
struct watch_dep *wd;
close (fd);
fd = w->fd;
WD_FOREACH (wd, w) {
if (watch_dep_is_parent (wd)) {
iwatch_update_flags (wd->iw, flags);
return wd->iw->wd;
}
}
}
/* create a new entry if watch is not found */
iw = iwatch_init (wrk, fd, flags);
if (iw == NULL) {
return -1;
}
/* add inotify watch to worker`s watchlist */
SLIST_INSERT_HEAD (&wrk->head, iw, next);
return iw->wd;
}
/**
* Stop and remove a watch.
*
* @param[in] wrk A pointer to #worker.
* @param[in] id An ID of the watch to remove.
* @return 0 on success, -1 of failure.
**/
int
worker_remove (struct worker *wrk, int id)
{
struct i_watch *iw;
assert (wrk != NULL);
assert (id >= 0);
SLIST_FOREACH (iw, &wrk->head, next) {
if (iw->wd == id) {
worker_remove_iwatch (wrk, iw);
return 0;
}
}
errno = EINVAL;
return -1;
}
/**
* Stop and remove a watch.
*
* @param[in] wrk A pointer to #worker.
* @param[in] iw A pointer to #i_watch to remove.
**/
void
worker_remove_iwatch (struct worker *wrk, struct i_watch *iw)
{
assert (wrk != NULL);
assert (iw != NULL);
event_queue_enqueue (&wrk->eq, iw->wd, IN_IGNORED, 0, NULL);
SLIST_REMOVE (&wrk->head, iw, i_watch, next);
iwatch_free (iw);
}
/**
* Prepare a command with the data of the libinotify_set_param() call.
*
* @param[in] wrk A pointer to #worker.
* @param[in] param Worker-thread parameter name to set.
* @param[in] value Worker-thread parameter value to set.
* @return 0 on success, -1 on failure.
**/
int
worker_set_param (struct worker *wrk, int param, intptr_t value)
{
assert (wrk != NULL);
switch (param) {
case IN_SOCKBUFSIZE:
return worker_set_sockbufsize (wrk, value);
case IN_MAX_QUEUED_EVENTS:
return event_queue_set_max_events (&wrk->eq, value);
default:
errno = EINVAL;
}
return -1;
}