1SD_EVENT_ADD_INOTIFY(3) sd_event_add_inotify SD_EVENT_ADD_INOTIFY(3)
2
3
4
6 sd_event_add_inotify, sd_event_add_inotify_fd,
7 sd_event_source_get_inotify_mask, sd_event_inotify_handler_t - Add an
8 "inotify" file system inode event source to an event loop
9
11 #include <systemd/sd-event.h>
12
13 typedef struct sd_event_source sd_event_source;
14
15 typedef int (*sd_event_inotify_handler_t)(sd_event_source *s,
16 const struct inotify_event *event,
17 void *userdata);
18
19 int sd_event_add_inotify(sd_event *event, sd_event_source **source,
20 const char *path, uint32_t mask,
21 sd_event_inotify_handler_t handler,
22 void *userdata);
23
24 int sd_event_add_inotify_fd(sd_event *event, sd_event_source **source,
25 int fd, uint32_t mask,
26 sd_event_inotify_handler_t handler,
27 void *userdata);
28
29 int sd_event_source_get_inotify_mask(sd_event_source *source,
30 uint32_t *mask);
31
33 sd_event_add_inotify() adds a new inotify(7) file system inode event
34 source to an event loop. The event loop object is specified in the
35 event parameter, the event source object is returned in the source
36 parameter. The path parameter specifies the path of the file system
37 inode to watch. The mask parameter specifies which types of inode
38 events to watch specifically. It must contain an OR-ed combination of
39 IN_ACCESS, IN_ATTRIB, IN_CLOSE_WRITE, ... flags. See inotify(7) for
40 further information.
41
42 The handler must reference a function to call when the inode changes or
43 NULL. The handler function will be passed the userdata pointer, which
44 may be chosen freely by the caller. The handler also receives a pointer
45 to a struct inotify_event structure containing information about the
46 inode event. The handler may return negative to signal an error (see
47 below), other return values are ignored. If handler is NULL, a default
48 handler that calls sd_event_exit(3) will be used.
49
50 sd_event_add_inotify_fd() is identical to sd_event_add_inotify(),
51 except that it takes a file descriptor to an inode (possibly an O_PATH
52 one, but any other will do too) instead of a path in the file system.
53
54 If multiple event sources are installed for the same inode the backing
55 inotify watch descriptor is automatically shared. The mask parameter
56 may contain any flag defined by the inotify API, with the exception of
57 IN_MASK_ADD.
58
59 The handler is enabled continuously (SD_EVENT_ON), but this may be
60 changed with sd_event_source_set_enabled(3). Alternatively, the
61 IN_ONESHOT mask flag may be used to request SD_EVENT_ONESHOT mode. If
62 the handler function returns a negative error code, it will be disabled
63 after the invocation, even if the SD_EVENT_ON mode was requested
64 before.
65
66 As a special limitation the priority of inotify event sources may only
67 be altered (see sd_event_source_set_priority(3)) in the time between
68 creation of the event source object with sd_event_add_inotify() and the
69 beginning of the next event loop iteration. Attempts of changing the
70 priority any later will be refused. Consider freeing and allocating a
71 new inotify event source to change the priority at that point.
72
73 To destroy an event source object use sd_event_source_unref(3), but
74 note that the event source is only removed from the event loop when all
75 references to the event source are dropped. To make sure an event
76 source does not fire anymore, even when there's still a reference to it
77 kept, consider disabling it with sd_event_source_set_enabled(3).
78
79 If the second parameter of sd_event_add_inotify() is passed as NULL no
80 reference to the event source object is returned. In this case the
81 event source is considered "floating", and will be destroyed implicitly
82 when the event loop itself is destroyed.
83
84 If the handler parameter to sd_event_add_inotify() is NULL, and the
85 event source fires, this will be considered a request to exit the event
86 loop. In this case, the userdata parameter, cast to an integer, is
87 passed as the exit code parameter to sd_event_exit(3).
88
89 sd_event_source_get_inotify_mask() retrieves the configured inotify
90 watch mask of an event source created previously with
91 sd_event_add_inotify(). It takes the event source object as the source
92 parameter and a pointer to a uint32_t variable to return the mask in.
93
95 On success, these functions return 0 or a positive integer. On failure,
96 they return a negative errno-style error code.
97
98 Errors
99 Returned errors may indicate the following problems:
100
101 -ENOMEM
102 Not enough memory to allocate an object.
103
104 -EINVAL
105 An invalid argument has been passed. This includes specifying a
106 mask with IN_MASK_ADD set.
107
108 -ESTALE
109 The event loop is already terminated.
110
111 -ECHILD
112 The event loop has been created in a different process.
113
114 -EDOM
115 The passed event source is not an inotify process event source.
116
117 -EBADF
118 The passed file descriptor is not valid.
119
120 -ENOSYS
121 sd_event_add_inotify_fd() was called without /proc/ mounted.
122
124 Example 1. A simple program that uses inotify to monitor one or two
125 directories
126
127 /* SPDX-License-Identifier: MIT-0 */
128
129 #include <stdio.h>
130 #include <string.h>
131 #include <sys/inotify.h>
132
133 #include <systemd/sd-event.h>
134
135 #define _cleanup_(f) __attribute__((cleanup(f)))
136
137 static int inotify_handler(sd_event_source *source,
138 const struct inotify_event *event,
139 void *userdata) {
140
141 const char *desc = NULL;
142
143 sd_event_source_get_description(source, &desc);
144
145 if (event->mask & IN_Q_OVERFLOW)
146 printf("inotify-handler <%s>: overflow\n", desc);
147 else if (event->mask & IN_CREATE)
148 printf("inotify-handler <%s>: create on %s\n", desc, event->name);
149 else if (event->mask & IN_DELETE)
150 printf("inotify-handler <%s>: delete on %s\n", desc, event->name);
151 else if (event->mask & IN_MOVED_TO)
152 printf("inotify-handler <%s>: moved-to on %s\n", desc, event->name);
153
154 /* Terminate the program if an "exit" file appears */
155 if ((event->mask & (IN_CREATE|IN_MOVED_TO)) &&
156 strcmp(event->name, "exit") == 0)
157 sd_event_exit(sd_event_source_get_event(source), 0);
158
159 return 1;
160 }
161
162 int main(int argc, char **argv) {
163 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
164 _cleanup_(sd_event_source_unrefp) sd_event_source *source1 = NULL, *source2 = NULL;
165
166 const char *path1 = argc > 1 ? argv[1] : "/tmp";
167 const char *path2 = argc > 2 ? argv[2] : NULL;
168
169 /* Note: failure handling is omitted for brevity */
170
171 sd_event_default(&event);
172
173 sd_event_add_inotify(event, &source1, path1,
174 IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_TO,
175 inotify_handler, NULL);
176 if (path2)
177 sd_event_add_inotify(event, &source2, path2,
178 IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_TO,
179 inotify_handler, NULL);
180
181 sd_event_loop(event);
182
183 return 0;
184 }
185
187 These APIs are implemented as a shared library, which can be compiled
188 and linked to with the libsystemd pkg-config(1) file.
189
191 systemd(1), sd-event(3), sd_event_new(3), sd_event_now(3),
192 sd_event_add_io(3), sd_event_add_time(3), sd_event_add_signal(3),
193 sd_event_add_defer(3), sd_event_add_child(3),
194 sd_event_source_set_enabled(3), sd_event_source_set_priority(3),
195 sd_event_source_set_userdata(3), sd_event_source_set_description(3),
196 sd_event_source_set_floating(3), waitid(2)
197
198
199
200systemd 253 SD_EVENT_ADD_INOTIFY(3)