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