1SD_EVENT_ADD_INOTIFY(3)      sd_event_add_inotify      SD_EVENT_ADD_INOTIFY(3)
2
3
4

NAME

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

SYNOPSIS

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

DESCRIPTION

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 handler must reference a function to call when the
38       inode changes. The handler function will be passed the userdata
39       pointer, which may be chosen freely by the caller. The handler also
40       receives a pointer to a struct inotify_event structure containing
41       information about the inode event. The mask parameter specifies which
42       types of inode events to watch specifically. It must contain an OR-ed
43       combination of IN_ACCESS, IN_ATTRIB, IN_CLOSE_WRITE, ... flags. See
44       inotify(7) for further information.
45
46       sd_event_add_inotify_fd() is identical to sd_event_add_inotify(),
47       except that it takes a file descriptor to an inode (possibly an O_PATH
48       one, but any other will do too) instead of a path in the file system.
49
50       If multiple event sources are installed for the same inode the backing
51       inotify watch descriptor is automatically shared. The mask parameter
52       may contain any flag defined by the inotify API, with the exception of
53       IN_MASK_ADD.
54
55       The handler is enabled continuously (SD_EVENT_ON), but this may be
56       changed with sd_event_source_set_enabled(3). Alternatively, the
57       IN_ONESHOT mask flag may be used to request SD_EVENT_ONESHOT mode. If
58       the handler function returns a negative error code, it will be disabled
59       after the invocation, even if the SD_EVENT_ON mode was requested
60       before.
61
62       As a special limitation the priority of inotify event sources may only
63       be altered (see sd_event_source_set_priority(3)) in the time between
64       creation of the event source object with sd_event_add_inotify() and the
65       beginning of the next event loop iteration. Attempts of changing the
66       priority any later will be refused. Consider freeing and allocating a
67       new inotify event source to change the priority at that point.
68
69       To destroy an event source object use sd_event_source_unref(3), but
70       note that the event source is only removed from the event loop when all
71       references to the event source are dropped. To make sure an event
72       source does not fire anymore, even when there's still a reference to it
73       kept, consider disabling it with sd_event_source_set_enabled(3).
74
75       If the second parameter of sd_event_add_inotify() is passed as NULL no
76       reference to the event source object is returned. In this case the
77       event source is considered "floating", and will be destroyed implicitly
78       when the event loop itself is destroyed.
79
80       If the handler parameter to sd_event_add_inotify() is NULL, and the
81       event source fires, this will be considered a request to exit the event
82       loop. In this case, the userdata parameter, cast to an integer, is
83       passed as the exit code parameter to sd_event_exit(3).
84
85       sd_event_source_get_inotify_mask() retrieves the configured inotify
86       watch mask of an event source created previously with
87       sd_event_add_inotify(). It takes the event source object as the source
88       parameter and a pointer to a uint32_t variable to return the mask in.
89

RETURN VALUE

91       On success, these functions return 0 or a positive integer. On failure,
92       they return a negative errno-style error code.
93
94   Errors
95       Returned errors may indicate the following problems:
96
97       -ENOMEM
98           Not enough memory to allocate an object.
99
100       -EINVAL
101           An invalid argument has been passed. This includes specifying a
102           mask with IN_MASK_ADD set.
103
104       -ESTALE
105           The event loop is already terminated.
106
107       -ECHILD
108           The event loop has been created in a different process.
109
110       -EDOM
111           The passed event source is not an inotify process event source.
112
113       -EBADF
114           The passed file descriptor is not valid.
115
116       -ENOSYS
117           sd_event_add_inotify_fd() was called without /proc/ mounted.
118

EXAMPLES

120       Example 1. A simple program that uses inotify to monitor one or two
121       directories
122
123           /* SPDX-License-Identifier: CC0-1.0 */
124
125           #include <stdio.h>
126           #include <string.h>
127           #include <sys/inotify.h>
128
129           #include <systemd/sd-event.h>
130
131           #define _cleanup_(f) __attribute__((cleanup(f)))
132
133           static int inotify_handler(sd_event_source *source,
134                                      const struct inotify_event *event,
135                                      void *userdata) {
136
137             const char *desc = NULL;
138
139             sd_event_source_get_description(source, &desc);
140
141             if (event->mask & IN_Q_OVERFLOW)
142               printf("inotify-handler <%s>: overflow\n", desc);
143             else if (event->mask & IN_CREATE)
144               printf("inotify-handler <%s>: create on %s\n", desc, event->name);
145             else if (event->mask & IN_DELETE)
146               printf("inotify-handler <%s>: delete on %s\n", desc, event->name);
147             else if (event->mask & IN_MOVED_TO)
148               printf("inotify-handler <%s>: moved-to on %s\n", desc, event->name);
149
150             /* Terminate the program if an "exit" file appears */
151             if ((event->mask & (IN_CREATE|IN_MOVED_TO)) &&
152                 strcmp(event->name, "exit") == 0)
153               sd_event_exit(sd_event_source_get_event(source), 0);
154
155             return 1;
156           }
157
158           int main(int argc, char **argv) {
159             _cleanup_(sd_event_unrefp) sd_event *event = NULL;
160             _cleanup_(sd_event_source_unrefp) sd_event_source *source1 = NULL, *source2 = NULL;
161
162             const char *path1 = argc > 1 ? argv[1] : "/tmp";
163             const char *path2 = argc > 2 ? argv[2] : NULL;
164
165             /* Note: failure handling is omitted for brevity */
166
167             sd_event_default(&event);
168
169             sd_event_add_inotify(event, &source1, path1,
170                                  IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_TO,
171                                  inotify_handler, NULL);
172             if (path2)
173               sd_event_add_inotify(event, &source2, path2,
174                                    IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_TO,
175                                    inotify_handler, NULL);
176
177             sd_event_loop(event);
178
179             return 0;
180           }
181

NOTES

183       These APIs are implemented as a shared library, which can be compiled
184       and linked to with the libsystemd pkg-config(1) file.
185

SEE ALSO

187       systemd(1), sd-event(3), sd_event_new(3), sd_event_now(3),
188       sd_event_add_io(3), sd_event_add_time(3), sd_event_add_signal(3),
189       sd_event_add_defer(3), sd_event_add_child(3),
190       sd_event_source_set_enabled(3), sd_event_source_set_priority(3),
191       sd_event_source_set_userdata(3), sd_event_source_set_description(3),
192       sd_event_source_set_floating(3), waitid(2)
193
194
195
196systemd 250                                            SD_EVENT_ADD_INOTIFY(3)
Impressum