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 specifie 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 sd_event_source_get_inotify_mask() retrieves the configured inotify
72 watch mask of an event source created previously with
73 sd_event_add_inotify(). It takes the event source object as the source
74 parameter and a pointer to a uint32_t variable to return the mask in.
75
77 On success, these functions return 0 or a positive integer. On failure,
78 they return a negative errno-style error code.
79
80 Errors
81 Returned errors may indicate the following problems:
82
83 -ENOMEM
84 Not enough memory to allocate an object.
85
86 -EINVAL
87 An invalid argument has been passed. This includes specifying a
88 mask with IN_MASK_ADD set.
89
90 -ESTALE
91 The event loop is already terminated.
92
93 -ECHILD
94 The event loop has been created in a different process.
95
96 -EDOM
97 The passed event source is not an inotify process event source.
98
100 Example 1. A simple program that uses inotify to monitor one or two
101 directories
102
103 #include <stdio.h>
104 #include <string.h>
105 #include <sys/inotify.h>
106
107 #include <systemd/sd-event.h>
108
109 #define _cleanup_(f) __attribute__((cleanup(f)))
110
111 static int inotify_handler(sd_event_source *source,
112 const struct inotify_event *event,
113 void *userdata) {
114
115 const char *desc = NULL;
116
117 sd_event_source_get_description(source, &desc);
118
119 if (event->mask & IN_Q_OVERFLOW)
120 printf("inotify-handler <%s>: overflow\n", desc);
121 else if (event->mask & IN_CREATE)
122 printf("inotify-handler <%s>: create on %s\n", desc, event->name);
123 else if (event->mask & IN_DELETE)
124 printf("inotify-handler <%s>: delete on %s\n", desc, event->name);
125 else if (event->mask & IN_MOVED_TO)
126 printf("inotify-handler <%s>: moved-to on %s\n", desc, event->name);
127
128 /* Terminate the program if an "exit" file appears */
129 if ((event->mask & (IN_CREATE|IN_MOVED_TO)) &&
130 strcmp(event->name, "exit") == 0)
131 sd_event_exit(sd_event_source_get_event(source), 0);
132
133 return 1;
134 }
135
136 int main(int argc, char **argv) {
137 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
138 _cleanup_(sd_event_source_unrefp) sd_event_source *source1 = NULL, *source2 = NULL;
139
140 const char *path1 = argc > 1 ? argv[1] : "/tmp";
141 const char *path2 = argc > 2 ? argv[2] : NULL;
142
143 /* Note: failure handling is omitted for brevity */
144
145 sd_event_default(&event);
146
147 sd_event_add_inotify(event, &source1, path1,
148 IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_TO,
149 inotify_handler, NULL);
150 if (path2)
151 sd_event_add_inotify(event, &source2, path2,
152 IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_TO,
153 inotify_handler, NULL);
154
155 sd_event_loop(event);
156
157 return 0;
158 }
159
161 These APIs are implemented as a shared library, which can be compiled
162 and linked to with the libsystemd pkg-config(1) file.
163
165 systemd(1), sd-event(3), sd_event_new(3), sd_event_now(3),
166 sd_event_add_io(3), sd_event_add_time(3), sd_event_add_signal(3),
167 sd_event_add_defer(3), sd_event_add_child(3),
168 sd_event_source_set_enabled(3), sd_event_source_set_priority(3),
169 sd_event_source_set_userdata(3), sd_event_source_set_description(3),
170 sd_event_source_set_floating(3), waitid(2)
171
172
173
174systemd 245 SD_EVENT_ADD_INOTIFY(3)