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, library or
113 module instance.
114
115 -EDOM
116 The passed event source is not an inotify process event source.
117
118 -EBADF
119 The passed file descriptor is not valid.
120
121 -ENOSYS
122 sd_event_add_inotify_fd() was called without /proc/ mounted.
123
125 Example 1. A simple program that uses inotify to monitor one or two
126 directories
127
128 /* SPDX-License-Identifier: MIT-0 */
129
130 #include <stdio.h>
131 #include <string.h>
132 #include <sys/inotify.h>
133
134 #include <systemd/sd-event.h>
135
136 #define _cleanup_(f) __attribute__((cleanup(f)))
137
138 static int inotify_handler(sd_event_source *source,
139 const struct inotify_event *event,
140 void *userdata) {
141
142 const char *desc = NULL;
143
144 sd_event_source_get_description(source, &desc);
145
146 if (event->mask & IN_Q_OVERFLOW)
147 printf("inotify-handler <%s>: overflow\n", desc);
148 else if (event->mask & IN_CREATE)
149 printf("inotify-handler <%s>: create on %s\n", desc, event->name);
150 else if (event->mask & IN_DELETE)
151 printf("inotify-handler <%s>: delete on %s\n", desc, event->name);
152 else if (event->mask & IN_MOVED_TO)
153 printf("inotify-handler <%s>: moved-to on %s\n", desc, event->name);
154
155 /* Terminate the program if an "exit" file appears */
156 if ((event->mask & (IN_CREATE|IN_MOVED_TO)) &&
157 strcmp(event->name, "exit") == 0)
158 sd_event_exit(sd_event_source_get_event(source), 0);
159
160 return 1;
161 }
162
163 int main(int argc, char **argv) {
164 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
165 _cleanup_(sd_event_source_unrefp) sd_event_source *source1 = NULL, *source2 = NULL;
166
167 const char *path1 = argc > 1 ? argv[1] : "/tmp";
168 const char *path2 = argc > 2 ? argv[2] : NULL;
169
170 /* Note: failure handling is omitted for brevity */
171
172 sd_event_default(&event);
173
174 sd_event_add_inotify(event, &source1, path1,
175 IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_TO,
176 inotify_handler, NULL);
177 if (path2)
178 sd_event_add_inotify(event, &source2, path2,
179 IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_TO,
180 inotify_handler, NULL);
181
182 sd_event_loop(event);
183
184 return 0;
185 }
186
188 Functions described here are available as a shared library, which can
189 be compiled against and linked to with the libsystemd pkg-config(1)
190 file.
191
192 The code described here uses getenv(3), which is declared to be not
193 multi-thread-safe. This means that the code calling the functions
194 described here must not call setenv(3) from a parallel thread. It is
195 recommended to only do calls to setenv() from an early phase of the
196 program when no other threads have been started.
197
199 systemd(1), sd-event(3), sd_event_new(3), sd_event_now(3),
200 sd_event_add_io(3), sd_event_add_time(3), sd_event_add_signal(3),
201 sd_event_add_defer(3), sd_event_add_child(3),
202 sd_event_source_set_enabled(3), sd_event_source_set_priority(3),
203 sd_event_source_set_userdata(3), sd_event_source_set_description(3),
204 sd_event_source_set_floating(3), waitid(2)
205
206
207
208systemd 254 SD_EVENT_ADD_INOTIFY(3)