]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
01f78473 | 2 | |
01f78473 | 3 | #include <errno.h> |
07630cea LP |
4 | #include <sys/epoll.h> |
5 | #include <sys/inotify.h> | |
01f78473 LP |
6 | #include <unistd.h> |
7 | ||
07630cea LP |
8 | #include "bus-error.h" |
9 | #include "bus-util.h" | |
01f78473 | 10 | #include "dbus-path.h" |
6fcbec6f | 11 | #include "dbus-unit.h" |
7a16cd4b | 12 | #include "escape.h" |
3ffd4af2 | 13 | #include "fd-util.h" |
77601719 | 14 | #include "fs-util.h" |
7d50b32a | 15 | #include "glob-util.h" |
f8c16f42 | 16 | #include "macro.h" |
07630cea | 17 | #include "mkdir.h" |
3ffd4af2 | 18 | #include "path.h" |
7a16cd4b | 19 | #include "path-util.h" |
d68c645b | 20 | #include "serialize.h" |
07630cea | 21 | #include "special.h" |
8fcde012 | 22 | #include "stat-util.h" |
8b43440b | 23 | #include "string-table.h" |
07630cea LP |
24 | #include "string-util.h" |
25 | #include "unit-name.h" | |
26 | #include "unit.h" | |
01f78473 LP |
27 | |
28 | static const UnitActiveState state_translation_table[_PATH_STATE_MAX] = { | |
29 | [PATH_DEAD] = UNIT_INACTIVE, | |
30 | [PATH_WAITING] = UNIT_ACTIVE, | |
31 | [PATH_RUNNING] = UNIT_ACTIVE, | |
7a16cd4b | 32 | [PATH_FAILED] = UNIT_FAILED, |
01f78473 LP |
33 | }; |
34 | ||
718db961 LP |
35 | static int path_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
36 | ||
37 | int path_spec_watch(PathSpec *s, sd_event_io_handler_t handler) { | |
4b562198 MS |
38 | static const int flags_table[_PATH_TYPE_MAX] = { |
39 | [PATH_EXISTS] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB, | |
40 | [PATH_EXISTS_GLOB] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB, | |
41 | [PATH_CHANGED] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO, | |
e9223856 | 42 | [PATH_MODIFIED] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO|IN_MODIFY, |
7a16cd4b | 43 | [PATH_DIRECTORY_NOT_EMPTY] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CREATE|IN_MOVED_TO, |
4b562198 MS |
44 | }; |
45 | ||
46 | bool exists = false; | |
bc41f93e | 47 | char *slash, *oldslash = NULL; |
4b562198 | 48 | int r; |
0e456f97 | 49 | |
4b562198 | 50 | assert(s); |
718db961 LP |
51 | assert(s->unit); |
52 | assert(handler); | |
0e456f97 | 53 | |
718db961 | 54 | path_spec_unwatch(s); |
4b562198 | 55 | |
f8c16f42 ZJS |
56 | s->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); |
57 | if (s->inotify_fd < 0) { | |
4b562198 MS |
58 | r = -errno; |
59 | goto fail; | |
60 | } | |
61 | ||
151b9b96 | 62 | r = sd_event_add_io(s->unit->manager->event, &s->event_source, s->inotify_fd, EPOLLIN, handler, s); |
f8c16f42 | 63 | if (r < 0) |
4b562198 | 64 | goto fail; |
4b562198 | 65 | |
7dfbe2e3 TG |
66 | (void) sd_event_source_set_description(s->event_source, "path"); |
67 | ||
858d36c1 | 68 | /* This function assumes the path was passed through path_simplify()! */ |
80127127 | 69 | assert(!strstr(s->path, "//")); |
4b562198 | 70 | |
180d6802 MS |
71 | for (slash = strchr(s->path, '/'); ; slash = strchr(slash+1, '/')) { |
72 | char *cut = NULL; | |
4b562198 | 73 | int flags; |
bc41f93e ZJS |
74 | char tmp; |
75 | ||
76 | if (slash) { | |
180d6802 MS |
77 | cut = slash + (slash == s->path); |
78 | tmp = *cut; | |
79 | *cut = '\0'; | |
80 | ||
bc41f93e | 81 | flags = IN_MOVE_SELF | IN_DELETE_SELF | IN_ATTRIB | IN_CREATE | IN_MOVED_TO; |
180d6802 | 82 | } else |
bc41f93e | 83 | flags = flags_table[s->type]; |
4b562198 | 84 | |
180d6802 | 85 | r = inotify_add_watch(s->inotify_fd, s->path, flags); |
bc41f93e | 86 | if (r < 0) { |
3742095b | 87 | if (IN_SET(errno, EACCES, ENOENT)) { |
180d6802 MS |
88 | if (cut) |
89 | *cut = tmp; | |
bc41f93e | 90 | break; |
180d6802 | 91 | } |
4b562198 | 92 | |
27c3112d FB |
93 | /* This second call to inotify_add_watch() should fail like the previous |
94 | * one and is done for logging the error in a comprehensive way. */ | |
95 | r = inotify_add_watch_and_warn(s->inotify_fd, s->path, flags); | |
96 | if (r < 0) { | |
97 | if (cut) | |
98 | *cut = tmp; | |
99 | goto fail; | |
bc41f93e | 100 | } |
27c3112d FB |
101 | |
102 | /* Hmm, we succeeded in adding the watch this time... let's continue. */ | |
103 | } | |
104 | exists = true; | |
105 | ||
106 | /* Path exists, we don't need to watch parent too closely. */ | |
107 | if (oldslash) { | |
108 | char *cut2 = oldslash + (oldslash == s->path); | |
109 | char tmp2 = *cut2; | |
110 | *cut2 = '\0'; | |
111 | ||
112 | (void) inotify_add_watch(s->inotify_fd, s->path, IN_MOVE_SELF); | |
113 | /* Error is ignored, the worst can happen is we get spurious events. */ | |
114 | ||
115 | *cut2 = tmp2; | |
28a79bd2 | 116 | } |
bc41f93e | 117 | |
180d6802 MS |
118 | if (cut) |
119 | *cut = tmp; | |
120 | ||
121 | if (slash) | |
bc41f93e | 122 | oldslash = slash; |
180d6802 | 123 | else { |
bc41f93e ZJS |
124 | /* whole path has been iterated over */ |
125 | s->primary_wd = r; | |
126 | break; | |
127 | } | |
128 | } | |
129 | ||
28a79bd2 | 130 | if (!exists) { |
f2341e0a LP |
131 | r = log_error_errno(errno, "Failed to add watch on any of the components of %s: %m", s->path); |
132 | /* either EACCESS or ENOENT */ | |
28a79bd2 ZJS |
133 | goto fail; |
134 | } | |
135 | ||
4b562198 MS |
136 | return 0; |
137 | ||
138 | fail: | |
718db961 | 139 | path_spec_unwatch(s); |
4b562198 | 140 | return r; |
0e456f97 LP |
141 | } |
142 | ||
718db961 LP |
143 | void path_spec_unwatch(PathSpec *s) { |
144 | assert(s); | |
62347bc2 | 145 | |
718db961 | 146 | s->event_source = sd_event_source_unref(s->event_source); |
03e334a1 | 147 | s->inotify_fd = safe_close(s->inotify_fd); |
62347bc2 LP |
148 | } |
149 | ||
718db961 | 150 | int path_spec_fd_event(PathSpec *s, uint32_t revents) { |
0254e944 | 151 | union inotify_event_buffer buffer; |
4b562198 | 152 | struct inotify_event *e; |
f7c1ad4f | 153 | ssize_t l; |
4b562198 MS |
154 | int r = 0; |
155 | ||
baaa35ad ZJS |
156 | if (revents != EPOLLIN) |
157 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
158 | "Got invalid poll event on inotify."); | |
4b562198 | 159 | |
0254e944 | 160 | l = read(s->inotify_fd, &buffer, sizeof(buffer)); |
f7c1ad4f | 161 | if (l < 0) { |
3742095b | 162 | if (IN_SET(errno, EAGAIN, EINTR)) |
f7c1ad4f | 163 | return 0; |
4b562198 | 164 | |
4a62c710 | 165 | return log_error_errno(errno, "Failed to read inotify event: %m"); |
f7c1ad4f | 166 | } |
4b562198 | 167 | |
f7c1ad4f | 168 | FOREACH_INOTIFY_EVENT(e, buffer, l) { |
3742095b | 169 | if (IN_SET(s->type, PATH_CHANGED, PATH_MODIFIED) && |
714d943f | 170 | s->primary_wd == e->wd) |
4b562198 | 171 | r = 1; |
4b562198 | 172 | } |
a163db44 | 173 | |
4b562198 MS |
174 | return r; |
175 | } | |
176 | ||
57020a3a | 177 | static bool path_spec_check_good(PathSpec *s, bool initial) { |
d7cf8c24 | 178 | bool b, good = false; |
4b562198 MS |
179 | |
180 | switch (s->type) { | |
181 | ||
182 | case PATH_EXISTS: | |
d7cf8c24 ZJS |
183 | b = access(s->path, F_OK) >= 0; |
184 | good = b && !s->previous_exists; | |
185 | s->previous_exists = b; | |
4b562198 MS |
186 | break; |
187 | ||
188 | case PATH_EXISTS_GLOB: | |
189 | good = glob_exists(s->path) > 0; | |
190 | break; | |
191 | ||
192 | case PATH_DIRECTORY_NOT_EMPTY: { | |
193 | int k; | |
194 | ||
195 | k = dir_is_empty(s->path); | |
196 | good = !(k == -ENOENT || k > 0); | |
197 | break; | |
198 | } | |
199 | ||
714d943f | 200 | case PATH_CHANGED: |
d7cf8c24 | 201 | case PATH_MODIFIED: |
4b562198 MS |
202 | b = access(s->path, F_OK) >= 0; |
203 | good = !initial && b != s->previous_exists; | |
204 | s->previous_exists = b; | |
205 | break; | |
4b562198 MS |
206 | |
207 | default: | |
208 | ; | |
209 | } | |
210 | ||
211 | return good; | |
212 | } | |
213 | ||
57020a3a | 214 | static void path_spec_mkdir(PathSpec *s, mode_t mode) { |
4b562198 MS |
215 | int r; |
216 | ||
3742095b | 217 | if (IN_SET(s->type, PATH_EXISTS, PATH_EXISTS_GLOB)) |
4b562198 MS |
218 | return; |
219 | ||
15f55e80 ZJS |
220 | r = mkdir_p_label(s->path, mode); |
221 | if (r < 0) | |
da927ba9 | 222 | log_warning_errno(r, "mkdir(%s) failed: %m", s->path); |
4b562198 MS |
223 | } |
224 | ||
57020a3a | 225 | static void path_spec_dump(PathSpec *s, FILE *f, const char *prefix) { |
4b562198 MS |
226 | fprintf(f, |
227 | "%s%s: %s\n", | |
228 | prefix, | |
229 | path_type_to_string(s->type), | |
230 | s->path); | |
231 | } | |
232 | ||
57020a3a LP |
233 | void path_spec_done(PathSpec *s) { |
234 | assert(s); | |
4b562198 | 235 | assert(s->inotify_fd == -1); |
57020a3a | 236 | |
4b562198 MS |
237 | free(s->path); |
238 | } | |
239 | ||
240 | static void path_init(Unit *u) { | |
241 | Path *p = PATH(u); | |
242 | ||
243 | assert(u); | |
ac155bb8 | 244 | assert(u->load_state == UNIT_STUB); |
4b562198 MS |
245 | |
246 | p->directory_mode = 0755; | |
247 | } | |
248 | ||
74051b9b | 249 | void path_free_specs(Path *p) { |
01f78473 LP |
250 | PathSpec *s; |
251 | ||
252 | assert(p); | |
253 | ||
254 | while ((s = p->specs)) { | |
718db961 | 255 | path_spec_unwatch(s); |
71fda00f | 256 | LIST_REMOVE(spec, p->specs, s); |
57020a3a | 257 | path_spec_done(s); |
01f78473 LP |
258 | free(s); |
259 | } | |
260 | } | |
261 | ||
74051b9b LP |
262 | static void path_done(Unit *u) { |
263 | Path *p = PATH(u); | |
264 | ||
265 | assert(p); | |
266 | ||
74051b9b LP |
267 | path_free_specs(p); |
268 | } | |
269 | ||
eef85c4a | 270 | static int path_add_mount_dependencies(Path *p) { |
01f78473 LP |
271 | PathSpec *s; |
272 | int r; | |
273 | ||
274 | assert(p); | |
01f78473 LP |
275 | |
276 | LIST_FOREACH(spec, s, p->specs) { | |
eef85c4a | 277 | r = unit_require_mounts_for(UNIT(p), s->path, UNIT_DEPENDENCY_FILE); |
e0207c8d | 278 | if (r < 0) |
01f78473 | 279 | return r; |
e0207c8d | 280 | } |
01f78473 LP |
281 | |
282 | return 0; | |
283 | } | |
284 | ||
285 | static int path_verify(Path *p) { | |
286 | assert(p); | |
75193d41 | 287 | assert(UNIT(p)->load_state == UNIT_LOADED); |
01f78473 LP |
288 | |
289 | if (!p->specs) { | |
f2341e0a | 290 | log_unit_error(UNIT(p), "Path unit lacks path setting. Refusing."); |
6f40aa45 | 291 | return -ENOEXEC; |
01f78473 LP |
292 | } |
293 | ||
294 | return 0; | |
295 | } | |
296 | ||
6c155fe3 LP |
297 | static int path_add_default_dependencies(Path *p) { |
298 | int r; | |
299 | ||
300 | assert(p); | |
301 | ||
4c9ea260 LP |
302 | if (!UNIT(p)->default_dependencies) |
303 | return 0; | |
304 | ||
35d8c19a | 305 | r = unit_add_dependency_by_name(UNIT(p), UNIT_BEFORE, SPECIAL_PATHS_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
e3d84721 LP |
306 | if (r < 0) |
307 | return r; | |
2a77d31d | 308 | |
463d0d15 | 309 | if (MANAGER_IS_SYSTEM(UNIT(p)->manager)) { |
5a724170 | 310 | r = unit_add_two_dependencies_by_name(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
e0207c8d | 311 | if (r < 0) |
6c155fe3 | 312 | return r; |
2a77d31d | 313 | } |
6c155fe3 | 314 | |
5a724170 | 315 | return unit_add_two_dependencies_by_name(UNIT(p), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
eef85c4a LP |
316 | } |
317 | ||
318 | static int path_add_trigger_dependencies(Path *p) { | |
319 | Unit *x; | |
320 | int r; | |
321 | ||
322 | assert(p); | |
323 | ||
324 | if (!hashmap_isempty(UNIT(p)->dependencies[UNIT_TRIGGERS])) | |
325 | return 0; | |
326 | ||
327 | r = unit_load_related_unit(UNIT(p), ".service", &x); | |
328 | if (r < 0) | |
329 | return r; | |
330 | ||
331 | return unit_add_two_dependencies(UNIT(p), UNIT_BEFORE, UNIT_TRIGGERS, x, true, UNIT_DEPENDENCY_IMPLICIT); | |
6c155fe3 LP |
332 | } |
333 | ||
75193d41 ZJS |
334 | static int path_add_extras(Path *p) { |
335 | int r; | |
336 | ||
337 | r = path_add_trigger_dependencies(p); | |
338 | if (r < 0) | |
339 | return r; | |
340 | ||
341 | r = path_add_mount_dependencies(p); | |
342 | if (r < 0) | |
343 | return r; | |
344 | ||
345 | return path_add_default_dependencies(p); | |
346 | } | |
347 | ||
01f78473 LP |
348 | static int path_load(Unit *u) { |
349 | Path *p = PATH(u); | |
350 | int r; | |
351 | ||
352 | assert(u); | |
ac155bb8 | 353 | assert(u->load_state == UNIT_STUB); |
01f78473 | 354 | |
c3620770 | 355 | r = unit_load_fragment_and_dropin(u, true); |
e0207c8d | 356 | if (r < 0) |
01f78473 LP |
357 | return r; |
358 | ||
75193d41 ZJS |
359 | if (u->load_state != UNIT_LOADED) |
360 | return 0; | |
a40eb732 | 361 | |
75193d41 ZJS |
362 | r = path_add_extras(p); |
363 | if (r < 0) | |
364 | return r; | |
01f78473 LP |
365 | |
366 | return path_verify(p); | |
367 | } | |
368 | ||
369 | static void path_dump(Unit *u, FILE *f, const char *prefix) { | |
370 | Path *p = PATH(u); | |
3ecaa09b | 371 | Unit *trigger; |
01f78473 LP |
372 | PathSpec *s; |
373 | ||
e364ad06 LP |
374 | assert(p); |
375 | assert(f); | |
01f78473 | 376 | |
3ecaa09b LP |
377 | trigger = UNIT_TRIGGER(u); |
378 | ||
01f78473 LP |
379 | fprintf(f, |
380 | "%sPath State: %s\n" | |
cd43ca73 | 381 | "%sResult: %s\n" |
0e456f97 LP |
382 | "%sUnit: %s\n" |
383 | "%sMakeDirectory: %s\n" | |
384 | "%sDirectoryMode: %04o\n", | |
01f78473 | 385 | prefix, path_state_to_string(p->state), |
cd43ca73 | 386 | prefix, path_result_to_string(p->result), |
3ecaa09b | 387 | prefix, trigger ? trigger->id : "n/a", |
0e456f97 LP |
388 | prefix, yes_no(p->make_directory), |
389 | prefix, p->directory_mode); | |
01f78473 LP |
390 | |
391 | LIST_FOREACH(spec, s, p->specs) | |
57020a3a | 392 | path_spec_dump(s, f, prefix); |
01f78473 LP |
393 | } |
394 | ||
395 | static void path_unwatch(Path *p) { | |
396 | PathSpec *s; | |
397 | ||
398 | assert(p); | |
399 | ||
400 | LIST_FOREACH(spec, s, p->specs) | |
718db961 | 401 | path_spec_unwatch(s); |
01f78473 LP |
402 | } |
403 | ||
404 | static int path_watch(Path *p) { | |
405 | int r; | |
406 | PathSpec *s; | |
407 | ||
408 | assert(p); | |
409 | ||
e0207c8d | 410 | LIST_FOREACH(spec, s, p->specs) { |
718db961 | 411 | r = path_spec_watch(s, path_dispatch_io); |
e0207c8d | 412 | if (r < 0) |
01f78473 | 413 | return r; |
e0207c8d | 414 | } |
01f78473 LP |
415 | |
416 | return 0; | |
417 | } | |
418 | ||
419 | static void path_set_state(Path *p, PathState state) { | |
420 | PathState old_state; | |
421 | assert(p); | |
422 | ||
6fcbec6f LP |
423 | if (p->state != state) |
424 | bus_unit_send_pending_change_signal(UNIT(p), false); | |
425 | ||
01f78473 LP |
426 | old_state = p->state; |
427 | p->state = state; | |
428 | ||
672028dc LP |
429 | if (state != PATH_WAITING && |
430 | (state != PATH_RUNNING || p->inotify_triggered)) | |
01f78473 LP |
431 | path_unwatch(p); |
432 | ||
433 | if (state != old_state) | |
3541bf1f | 434 | log_unit_debug(UNIT(p), "Changed %s -> %s", path_state_to_string(old_state), path_state_to_string(state)); |
01f78473 | 435 | |
2ad2e41a | 436 | unit_notify(UNIT(p), state_translation_table[old_state], state_translation_table[state], 0); |
01f78473 LP |
437 | } |
438 | ||
ac3f50ca | 439 | static void path_enter_waiting(Path *p, bool initial, bool recheck); |
01f78473 | 440 | |
be847e82 | 441 | static int path_coldplug(Unit *u) { |
01f78473 LP |
442 | Path *p = PATH(u); |
443 | ||
444 | assert(p); | |
445 | assert(p->state == PATH_DEAD); | |
446 | ||
447 | if (p->deserialized_state != p->state) { | |
448 | ||
3742095b | 449 | if (IN_SET(p->deserialized_state, PATH_WAITING, PATH_RUNNING)) |
be847e82 LP |
450 | path_enter_waiting(p, true, true); |
451 | else | |
01f78473 LP |
452 | path_set_state(p, p->deserialized_state); |
453 | } | |
454 | ||
455 | return 0; | |
456 | } | |
457 | ||
cd43ca73 | 458 | static void path_enter_dead(Path *p, PathResult f) { |
01f78473 LP |
459 | assert(p); |
460 | ||
a0fef983 | 461 | if (p->result == PATH_SUCCESS) |
cd43ca73 | 462 | p->result = f; |
01f78473 | 463 | |
aac99f30 | 464 | unit_log_result(UNIT(p), p->result == PATH_SUCCESS, path_result_to_string(p->result)); |
cd43ca73 | 465 | path_set_state(p, p->result != PATH_SUCCESS ? PATH_FAILED : PATH_DEAD); |
01f78473 LP |
466 | } |
467 | ||
468 | static void path_enter_running(Path *p) { | |
4afd3348 | 469 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
e903182e | 470 | Unit *trigger; |
01f78473 | 471 | int r; |
398ef8ba | 472 | |
01f78473 | 473 | assert(p); |
3ecaa09b | 474 | |
ba3e67a7 | 475 | /* Don't start job if we are supposed to go down */ |
31afa0a4 | 476 | if (unit_stop_pending(UNIT(p))) |
ba3e67a7 LP |
477 | return; |
478 | ||
e903182e LP |
479 | trigger = UNIT_TRIGGER(UNIT(p)); |
480 | if (!trigger) { | |
481 | log_unit_error(UNIT(p), "Unit to trigger vanished."); | |
9e4942ed | 482 | path_enter_dead(p, PATH_FAILURE_RESOURCES); |
e903182e LP |
483 | return; |
484 | } | |
485 | ||
50cbaba4 | 486 | r = manager_add_job(UNIT(p)->manager, JOB_START, trigger, JOB_REPLACE, NULL, &error, NULL); |
e0207c8d | 487 | if (r < 0) |
01f78473 LP |
488 | goto fail; |
489 | ||
672028dc LP |
490 | p->inotify_triggered = false; |
491 | ||
01f78473 | 492 | path_set_state(p, PATH_RUNNING); |
8fca6944 MS |
493 | path_unwatch(p); |
494 | ||
01f78473 LP |
495 | return; |
496 | ||
497 | fail: | |
f2341e0a | 498 | log_unit_warning(UNIT(p), "Failed to queue unit startup job: %s", bus_error_message(&error, r)); |
cd43ca73 | 499 | path_enter_dead(p, PATH_FAILURE_RESOURCES); |
01f78473 LP |
500 | } |
501 | ||
ac3f50ca | 502 | static bool path_check_good(Path *p, bool initial) { |
01f78473 | 503 | PathSpec *s; |
01f78473 | 504 | |
ac3f50ca | 505 | assert(p); |
672028dc | 506 | |
93e63b2a ZJS |
507 | LIST_FOREACH(spec, s, p->specs) |
508 | if (path_spec_check_good(s, initial)) | |
509 | return true; | |
01f78473 | 510 | |
93e63b2a | 511 | return false; |
ac3f50ca | 512 | } |
01f78473 | 513 | |
ac3f50ca LP |
514 | static void path_enter_waiting(Path *p, bool initial, bool recheck) { |
515 | int r; | |
0595f9a1 | 516 | |
ac3f50ca LP |
517 | if (recheck) |
518 | if (path_check_good(p, initial)) { | |
f2341e0a | 519 | log_unit_debug(UNIT(p), "Got triggered."); |
ac3f50ca LP |
520 | path_enter_running(p); |
521 | return; | |
522 | } | |
523 | ||
e0207c8d ZJS |
524 | r = path_watch(p); |
525 | if (r < 0) | |
ac3f50ca LP |
526 | goto fail; |
527 | ||
528 | /* Hmm, so now we have created inotify watches, but the file | |
529 | * might have appeared/been removed by now, so we must | |
530 | * recheck */ | |
531 | ||
532 | if (recheck) | |
533 | if (path_check_good(p, false)) { | |
f2341e0a | 534 | log_unit_debug(UNIT(p), "Got triggered."); |
ac3f50ca LP |
535 | path_enter_running(p); |
536 | return; | |
537 | } | |
01f78473 LP |
538 | |
539 | path_set_state(p, PATH_WAITING); | |
540 | return; | |
541 | ||
542 | fail: | |
f2341e0a | 543 | log_unit_warning_errno(UNIT(p), r, "Failed to enter waiting state: %m"); |
cd43ca73 | 544 | path_enter_dead(p, PATH_FAILURE_RESOURCES); |
01f78473 LP |
545 | } |
546 | ||
0e456f97 LP |
547 | static void path_mkdir(Path *p) { |
548 | PathSpec *s; | |
549 | ||
550 | assert(p); | |
551 | ||
552 | if (!p->make_directory) | |
553 | return; | |
554 | ||
4b562198 | 555 | LIST_FOREACH(spec, s, p->specs) |
57020a3a | 556 | path_spec_mkdir(s, p->directory_mode); |
0e456f97 LP |
557 | } |
558 | ||
01f78473 LP |
559 | static int path_start(Unit *u) { |
560 | Path *p = PATH(u); | |
07299350 | 561 | int r; |
01f78473 LP |
562 | |
563 | assert(p); | |
3742095b | 564 | assert(IN_SET(p->state, PATH_DEAD, PATH_FAILED)); |
01f78473 | 565 | |
a4191c9f LP |
566 | r = unit_test_trigger_loaded(u); |
567 | if (r < 0) | |
568 | return r; | |
01f78473 | 569 | |
97a3f4ee | 570 | r = unit_test_start_limit(u); |
07299350 LP |
571 | if (r < 0) { |
572 | path_enter_dead(p, PATH_FAILURE_START_LIMIT_HIT); | |
573 | return r; | |
574 | } | |
575 | ||
4b58153d LP |
576 | r = unit_acquire_invocation_id(u); |
577 | if (r < 0) | |
578 | return r; | |
579 | ||
0e456f97 LP |
580 | path_mkdir(p); |
581 | ||
cd43ca73 | 582 | p->result = PATH_SUCCESS; |
ac3f50ca | 583 | path_enter_waiting(p, true, true); |
00dc5d76 | 584 | |
82a2b6bb | 585 | return 1; |
01f78473 LP |
586 | } |
587 | ||
588 | static int path_stop(Unit *u) { | |
589 | Path *p = PATH(u); | |
590 | ||
591 | assert(p); | |
3742095b | 592 | assert(IN_SET(p->state, PATH_WAITING, PATH_RUNNING)); |
01f78473 | 593 | |
cd43ca73 | 594 | path_enter_dead(p, PATH_SUCCESS); |
82a2b6bb | 595 | return 1; |
01f78473 LP |
596 | } |
597 | ||
598 | static int path_serialize(Unit *u, FILE *f, FDSet *fds) { | |
599 | Path *p = PATH(u); | |
7a16cd4b | 600 | PathSpec *s; |
01f78473 LP |
601 | |
602 | assert(u); | |
603 | assert(f); | |
604 | assert(fds); | |
605 | ||
d68c645b LP |
606 | (void) serialize_item(f, "state", path_state_to_string(p->state)); |
607 | (void) serialize_item(f, "result", path_result_to_string(p->result)); | |
01f78473 | 608 | |
7a16cd4b ZJS |
609 | LIST_FOREACH(spec, s, p->specs) { |
610 | _cleanup_free_ char *escaped = NULL; | |
611 | ||
612 | escaped = cescape(s->path); | |
613 | if (!escaped) | |
614 | return log_oom(); | |
615 | ||
616 | (void) serialize_item_format(f, "path-spec", "%s %i %s", | |
617 | path_type_to_string(s->type), | |
618 | s->previous_exists, | |
619 | s->path); | |
620 | } | |
621 | ||
01f78473 LP |
622 | return 0; |
623 | } | |
624 | ||
625 | static int path_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
626 | Path *p = PATH(u); | |
627 | ||
628 | assert(u); | |
629 | assert(key); | |
630 | assert(value); | |
631 | assert(fds); | |
632 | ||
633 | if (streq(key, "state")) { | |
634 | PathState state; | |
635 | ||
e0207c8d ZJS |
636 | state = path_state_from_string(value); |
637 | if (state < 0) | |
f2341e0a | 638 | log_unit_debug(u, "Failed to parse state value: %s", value); |
01f78473 LP |
639 | else |
640 | p->deserialized_state = state; | |
cd43ca73 LP |
641 | |
642 | } else if (streq(key, "result")) { | |
643 | PathResult f; | |
644 | ||
645 | f = path_result_from_string(value); | |
646 | if (f < 0) | |
f2341e0a | 647 | log_unit_debug(u, "Failed to parse result value: %s", value); |
cd43ca73 LP |
648 | else if (f != PATH_SUCCESS) |
649 | p->result = f; | |
650 | ||
7a16cd4b ZJS |
651 | } else if (streq(key, "path-spec")) { |
652 | int previous_exists, skip = 0, r; | |
653 | _cleanup_free_ char *type_str = NULL; | |
654 | ||
655 | if (sscanf(value, "%ms %i %n", &type_str, &previous_exists, &skip) < 2) | |
656 | log_unit_debug(u, "Failed to parse path-spec value: %s", value); | |
657 | else { | |
658 | _cleanup_free_ char *unescaped = NULL; | |
659 | PathType type; | |
660 | PathSpec *s; | |
661 | ||
662 | type = path_type_from_string(type_str); | |
663 | if (type < 0) { | |
664 | log_unit_warning(u, "Unknown path type \"%s\", ignoring.", type_str); | |
665 | return 0; | |
666 | } | |
667 | ||
668 | r = cunescape(value+skip, 0, &unescaped); | |
669 | if (r < 0) { | |
670 | log_unit_warning_errno(u, r, "Failed to unescape serialize path: %m"); | |
671 | return 0; | |
672 | } | |
673 | ||
674 | LIST_FOREACH(spec, s, p->specs) | |
675 | if (s->type == type && | |
676 | path_equal(s->path, unescaped)) { | |
677 | ||
678 | s->previous_exists = previous_exists; | |
679 | break; | |
680 | } | |
681 | } | |
682 | ||
01f78473 | 683 | } else |
f2341e0a | 684 | log_unit_debug(u, "Unknown serialization key: %s", key); |
01f78473 LP |
685 | |
686 | return 0; | |
687 | } | |
688 | ||
44a6b1b6 | 689 | _pure_ static UnitActiveState path_active_state(Unit *u) { |
01f78473 LP |
690 | assert(u); |
691 | ||
692 | return state_translation_table[PATH(u)->state]; | |
693 | } | |
694 | ||
44a6b1b6 | 695 | _pure_ static const char *path_sub_state_to_string(Unit *u) { |
01f78473 LP |
696 | assert(u); |
697 | ||
698 | return path_state_to_string(PATH(u)->state); | |
699 | } | |
700 | ||
718db961 LP |
701 | static int path_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
702 | PathSpec *s = userdata; | |
703 | Path *p; | |
4b562198 | 704 | int changed; |
01f78473 | 705 | |
718db961 LP |
706 | assert(s); |
707 | assert(s->unit); | |
01f78473 LP |
708 | assert(fd >= 0); |
709 | ||
718db961 LP |
710 | p = PATH(s->unit); |
711 | ||
ec2ce0c5 | 712 | if (!IN_SET(p->state, PATH_WAITING, PATH_RUNNING)) |
718db961 | 713 | return 0; |
01f78473 | 714 | |
7a16cd4b | 715 | /* log_debug("inotify wakeup on %s.", UNIT(p)->id); */ |
01f78473 | 716 | |
01f78473 | 717 | LIST_FOREACH(spec, s, p->specs) |
57020a3a | 718 | if (path_spec_owns_inotify_fd(s, fd)) |
01f78473 LP |
719 | break; |
720 | ||
721 | if (!s) { | |
722 | log_error("Got event on unknown fd."); | |
723 | goto fail; | |
724 | } | |
725 | ||
718db961 | 726 | changed = path_spec_fd_event(s, revents); |
4b562198 | 727 | if (changed < 0) |
01f78473 | 728 | goto fail; |
01f78473 | 729 | |
672028dc LP |
730 | /* If we are already running, then remember that one event was |
731 | * dispatched so that we restart the service only if something | |
732 | * actually changed on disk */ | |
733 | p->inotify_triggered = true; | |
734 | ||
672028dc LP |
735 | if (changed) |
736 | path_enter_running(p); | |
737 | else | |
ac3f50ca | 738 | path_enter_waiting(p, false, true); |
672028dc | 739 | |
718db961 | 740 | return 0; |
01f78473 LP |
741 | |
742 | fail: | |
cd43ca73 | 743 | path_enter_dead(p, PATH_FAILURE_RESOURCES); |
718db961 | 744 | return 0; |
01f78473 LP |
745 | } |
746 | ||
3ecaa09b LP |
747 | static void path_trigger_notify(Unit *u, Unit *other) { |
748 | Path *p = PATH(u); | |
01f78473 | 749 | |
3ecaa09b LP |
750 | assert(u); |
751 | assert(other); | |
01f78473 | 752 | |
3ecaa09b LP |
753 | /* Invoked whenever the unit we trigger changes state or gains |
754 | * or loses a job */ | |
01f78473 | 755 | |
3ecaa09b LP |
756 | if (other->load_state != UNIT_LOADED) |
757 | return; | |
01f78473 | 758 | |
3ecaa09b LP |
759 | if (p->state == PATH_RUNNING && |
760 | UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) { | |
f2341e0a | 761 | log_unit_debug(UNIT(p), "Got notified about unit deactivation."); |
672028dc | 762 | |
3ecaa09b LP |
763 | /* Hmm, so inotify was triggered since the |
764 | * last activation, so I guess we need to | |
765 | * recheck what is going on. */ | |
766 | path_enter_waiting(p, false, p->inotify_triggered); | |
01f78473 | 767 | } |
01f78473 LP |
768 | } |
769 | ||
fdf20a31 | 770 | static void path_reset_failed(Unit *u) { |
5632e374 LP |
771 | Path *p = PATH(u); |
772 | ||
773 | assert(p); | |
774 | ||
fdf20a31 | 775 | if (p->state == PATH_FAILED) |
5632e374 LP |
776 | path_set_state(p, PATH_DEAD); |
777 | ||
cd43ca73 | 778 | p->result = PATH_SUCCESS; |
5632e374 LP |
779 | } |
780 | ||
01f78473 LP |
781 | static const char* const path_type_table[_PATH_TYPE_MAX] = { |
782 | [PATH_EXISTS] = "PathExists", | |
8092a428 | 783 | [PATH_EXISTS_GLOB] = "PathExistsGlob", |
2c5859af | 784 | [PATH_DIRECTORY_NOT_EMPTY] = "DirectoryNotEmpty", |
01f78473 | 785 | [PATH_CHANGED] = "PathChanged", |
e9223856 | 786 | [PATH_MODIFIED] = "PathModified", |
01f78473 LP |
787 | }; |
788 | ||
789 | DEFINE_STRING_TABLE_LOOKUP(path_type, PathType); | |
790 | ||
cd43ca73 LP |
791 | static const char* const path_result_table[_PATH_RESULT_MAX] = { |
792 | [PATH_SUCCESS] = "success", | |
2c5859af | 793 | [PATH_FAILURE_RESOURCES] = "resources", |
07299350 | 794 | [PATH_FAILURE_START_LIMIT_HIT] = "start-limit-hit", |
cd43ca73 LP |
795 | }; |
796 | ||
797 | DEFINE_STRING_TABLE_LOOKUP(path_result, PathResult); | |
798 | ||
01f78473 | 799 | const UnitVTable path_vtable = { |
7d17cfbc | 800 | .object_size = sizeof(Path), |
718db961 | 801 | |
f975e971 LP |
802 | .sections = |
803 | "Unit\0" | |
804 | "Path\0" | |
805 | "Install\0", | |
5b9fbf89 YW |
806 | .private_section = "Path", |
807 | ||
808 | .can_transient = true, | |
01f78473 | 809 | |
0e456f97 | 810 | .init = path_init, |
01f78473 LP |
811 | .done = path_done, |
812 | .load = path_load, | |
813 | ||
814 | .coldplug = path_coldplug, | |
815 | ||
816 | .dump = path_dump, | |
817 | ||
818 | .start = path_start, | |
819 | .stop = path_stop, | |
820 | ||
821 | .serialize = path_serialize, | |
822 | .deserialize_item = path_deserialize_item, | |
823 | ||
824 | .active_state = path_active_state, | |
825 | .sub_state_to_string = path_sub_state_to_string, | |
826 | ||
3ecaa09b LP |
827 | .trigger_notify = path_trigger_notify, |
828 | ||
fdf20a31 | 829 | .reset_failed = path_reset_failed, |
5632e374 | 830 | |
5b9fbf89 YW |
831 | .bus_vtable = bus_path_vtable, |
832 | .bus_set_property = bus_path_set_property, | |
01f78473 | 833 | }; |