Debian Codegraph @

src/basic/inotify-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "fd-util.h"
4 #include "inotify-util.h"
5 #include "log.h"
6 #include "memory-util.h"
7
8 bool inotify_event_next(
9 union inotify_event_buffer *buffer,
10 size_t size,
11 struct inotify_event **iterator,
12 int log_level) {
13
14 struct inotify_event *e;
15 size_t offset = 0;
16
17 assert(buffer);
18 assert(iterator);
19
20 if (*iterator) {
21 assert((uint8_t*) *iterator >= buffer->raw);
22 offset = (uint8_t*) *iterator - buffer->raw;
23 offset += offsetof(struct inotify_event, name) + (*iterator)->len;
24 }
25
26 if (size == offset)
27 return false; /* reached end of list */
28
29 if (size < offset ||
30 size - offset < offsetof(struct inotify_event, name)) {
31 log_full(log_level, "Received invalid inotify event, ignoring.");
32 return false;
33 }
34
35 e = CAST_ALIGN_PTR(struct inotify_event, buffer->raw + offset);
36 if (size - offset - offsetof(struct inotify_event, name) < e->len) {
37 log_full(log_level, "Received invalid inotify event, ignoring.");
38 return false;
39 }
40
41 *iterator = e;
42 return true;
43 }
44
45 int inotify_add_watch_fd(int fd, int what, uint32_t mask) {
46 int wd;
47
48 assert(fd >= 0);
49 assert(what >= 0);
50
51 /* This is like inotify_add_watch(), except that the file to watch is not referenced by a path, but by an fd */
52 wd = inotify_add_watch(fd, FORMAT_PROC_FD_PATH(what), mask);
53 if (wd < 0) {
54 if (errno != ENOENT)
55 return -errno;
56
57 return proc_fd_enoent_errno();
58 }
59
60 return wd;
61 }
62
63 int inotify_add_watch_and_warn(int fd, const char *pathname, uint32_t mask) {
64 int wd;
65
66 wd = inotify_add_watch(fd, pathname, mask);
67 if (wd < 0) {
68 if (errno == ENOSPC)
69 return log_error_errno(errno, "Failed to add a watch for %s: inotify watch limit reached", pathname);
70
71 return log_error_errno(errno, "Failed to add a watch for %s: %m", pathname);
72 }
73
74 return wd;
75 }