]>
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" |
3ffd4af2 | 12 | #include "fd-util.h" |
77601719 | 13 | #include "fs-util.h" |
7d50b32a | 14 | #include "glob-util.h" |
f8c16f42 | 15 | #include "macro.h" |
07630cea | 16 | #include "mkdir.h" |
3ffd4af2 | 17 | #include "path.h" |
d68c645b | 18 | #include "serialize.h" |
07630cea | 19 | #include "special.h" |
8fcde012 | 20 | #include "stat-util.h" |
8b43440b | 21 | #include "string-table.h" |
07630cea LP |
22 | #include "string-util.h" |
23 | #include "unit-name.h" | |
24 | #include "unit.h" | |
01f78473 LP |
25 | |
26 | static const UnitActiveState state_translation_table[_PATH_STATE_MAX] = { | |
27 | [PATH_DEAD] = UNIT_INACTIVE, | |
28 | [PATH_WAITING] = UNIT_ACTIVE, | |
29 | [PATH_RUNNING] = UNIT_ACTIVE, | |
fdf20a31 | 30 | [PATH_FAILED] = UNIT_FAILED |
01f78473 LP |
31 | }; |
32 | ||
718db961 LP |
33 | static int path_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
34 | ||
35 | int path_spec_watch(PathSpec *s, sd_event_io_handler_t handler) { | |
57020a3a | 36 | |
4b562198 MS |
37 | static const int flags_table[_PATH_TYPE_MAX] = { |
38 | [PATH_EXISTS] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB, | |
39 | [PATH_EXISTS_GLOB] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB, | |
40 | [PATH_CHANGED] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO, | |
e9223856 | 41 | [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, |
4b562198 MS |
42 | [PATH_DIRECTORY_NOT_EMPTY] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CREATE|IN_MOVED_TO |
43 | }; | |
44 | ||
45 | bool exists = false; | |
bc41f93e | 46 | char *slash, *oldslash = NULL; |
4b562198 | 47 | int r; |
0e456f97 | 48 | |
4b562198 | 49 | assert(s); |
718db961 LP |
50 | assert(s->unit); |
51 | assert(handler); | |
0e456f97 | 52 | |
718db961 | 53 | path_spec_unwatch(s); |
4b562198 | 54 | |
f8c16f42 ZJS |
55 | s->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); |
56 | if (s->inotify_fd < 0) { | |
4b562198 MS |
57 | r = -errno; |
58 | goto fail; | |
59 | } | |
60 | ||
151b9b96 | 61 | r = sd_event_add_io(s->unit->manager->event, &s->event_source, s->inotify_fd, EPOLLIN, handler, s); |
f8c16f42 | 62 | if (r < 0) |
4b562198 | 63 | goto fail; |
4b562198 | 64 | |
7dfbe2e3 TG |
65 | (void) sd_event_source_set_description(s->event_source, "path"); |
66 | ||
858d36c1 | 67 | /* This function assumes the path was passed through path_simplify()! */ |
80127127 | 68 | assert(!strstr(s->path, "//")); |
4b562198 | 69 | |
180d6802 MS |
70 | for (slash = strchr(s->path, '/'); ; slash = strchr(slash+1, '/')) { |
71 | char *cut = NULL; | |
4b562198 | 72 | int flags; |
bc41f93e ZJS |
73 | char tmp; |
74 | ||
75 | if (slash) { | |
180d6802 MS |
76 | cut = slash + (slash == s->path); |
77 | tmp = *cut; | |
78 | *cut = '\0'; | |
79 | ||
bc41f93e | 80 | flags = IN_MOVE_SELF | IN_DELETE_SELF | IN_ATTRIB | IN_CREATE | IN_MOVED_TO; |
180d6802 | 81 | } else |
bc41f93e | 82 | flags = flags_table[s->type]; |
4b562198 | 83 | |
180d6802 | 84 | r = inotify_add_watch(s->inotify_fd, s->path, flags); |
bc41f93e | 85 | if (r < 0) { |
3742095b | 86 | if (IN_SET(errno, EACCES, ENOENT)) { |
180d6802 MS |
87 | if (cut) |
88 | *cut = tmp; | |
bc41f93e | 89 | break; |
180d6802 | 90 | } |
4b562198 | 91 | |
27c3112d FB |
92 | /* This second call to inotify_add_watch() should fail like the previous |
93 | * one and is done for logging the error in a comprehensive way. */ | |
94 | r = inotify_add_watch_and_warn(s->inotify_fd, s->path, flags); | |
95 | if (r < 0) { | |
96 | if (cut) | |
97 | *cut = tmp; | |
98 | goto fail; | |
bc41f93e | 99 | } |
27c3112d FB |
100 | |
101 | /* Hmm, we succeeded in adding the watch this time... let's continue. */ | |
102 | } | |
103 | exists = true; | |
104 | ||
105 | /* Path exists, we don't need to watch parent too closely. */ | |
106 | if (oldslash) { | |
107 | char *cut2 = oldslash + (oldslash == s->path); | |
108 | char tmp2 = *cut2; | |
109 | *cut2 = '\0'; | |
110 | ||
111 | (void) inotify_add_watch(s->inotify_fd, s->path, IN_MOVE_SELF); | |
112 | /* Error is ignored, the worst can happen is we get spurious events. */ | |
113 | ||
114 | *cut2 = tmp2; | |
28a79bd2 | 115 | } |
bc41f93e | 116 | |
180d6802 MS |
117 | if (cut) |
118 | *cut = tmp; | |
119 | ||
120 | if (slash) | |
bc41f93e | 121 | oldslash = slash; |
180d6802 | 122 | else { |
bc41f93e ZJS |
123 | /* whole path has been iterated over */ |
124 | s->primary_wd = r; | |
125 | break; | |
126 | } | |
127 | } | |
128 | ||
28a79bd2 | 129 | if (!exists) { |
f2341e0a LP |
130 | r = log_error_errno(errno, "Failed to add watch on any of the components of %s: %m", s->path); |
131 | /* either EACCESS or ENOENT */ | |
28a79bd2 ZJS |
132 | goto fail; |
133 | } | |
134 | ||
4b562198 MS |
135 | return 0; |
136 | ||
137 | fail: | |
718db961 | 138 | path_spec_unwatch(s); |
4b562198 | 139 | return r; |
0e456f97 LP |
140 | } |
141 | ||
718db961 LP |
142 | void path_spec_unwatch(PathSpec *s) { |
143 | assert(s); | |
62347bc2 | 144 | |
718db961 | 145 | s->event_source = sd_event_source_unref(s->event_source); |
03e334a1 | 146 | s->inotify_fd = safe_close(s->inotify_fd); |
62347bc2 LP |
147 | } |
148 | ||
718db961 | 149 | int path_spec_fd_event(PathSpec *s, uint32_t revents) { |
0254e944 | 150 | union inotify_event_buffer buffer; |
4b562198 | 151 | struct inotify_event *e; |
f7c1ad4f | 152 | ssize_t l; |
4b562198 MS |
153 | int r = 0; |
154 | ||
baaa35ad ZJS |
155 | if (revents != EPOLLIN) |
156 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
157 | "Got invalid poll event on inotify."); | |
4b562198 | 158 | |
0254e944 | 159 | l = read(s->inotify_fd, &buffer, sizeof(buffer)); |
f7c1ad4f | 160 | if (l < 0) { |
3742095b | 161 | if (IN_SET(errno, EAGAIN, EINTR)) |
f7c1ad4f | 162 | return 0; |
4b562198 | 163 | |
4a62c710 | 164 | return log_error_errno(errno, "Failed to read inotify event: %m"); |
f7c1ad4f | 165 | } |
4b562198 | 166 | |
f7c1ad4f | 167 | FOREACH_INOTIFY_EVENT(e, buffer, l) { |
3742095b | 168 | if (IN_SET(s->type, PATH_CHANGED, PATH_MODIFIED) && |
714d943f | 169 | s->primary_wd == e->wd) |
4b562198 | 170 | r = 1; |
4b562198 | 171 | } |
a163db44 | 172 | |
4b562198 MS |
173 | return r; |
174 | } | |
175 | ||
57020a3a | 176 | static bool path_spec_check_good(PathSpec *s, bool initial) { |
4b562198 MS |
177 | bool good = false; |
178 | ||
179 | switch (s->type) { | |
180 | ||
181 | case PATH_EXISTS: | |
182 | good = access(s->path, F_OK) >= 0; | |
183 | break; | |
184 | ||
185 | case PATH_EXISTS_GLOB: | |
186 | good = glob_exists(s->path) > 0; | |
187 | break; | |
188 | ||
189 | case PATH_DIRECTORY_NOT_EMPTY: { | |
190 | int k; | |
191 | ||
192 | k = dir_is_empty(s->path); | |
193 | good = !(k == -ENOENT || k > 0); | |
194 | break; | |
195 | } | |
196 | ||
714d943f MS |
197 | case PATH_CHANGED: |
198 | case PATH_MODIFIED: { | |
4b562198 MS |
199 | bool b; |
200 | ||
201 | b = access(s->path, F_OK) >= 0; | |
202 | good = !initial && b != s->previous_exists; | |
203 | s->previous_exists = b; | |
204 | break; | |
205 | } | |
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 LP |
504 | bool good = false; |
505 | ||
ac3f50ca | 506 | assert(p); |
672028dc | 507 | |
01f78473 | 508 | LIST_FOREACH(spec, s, p->specs) { |
57020a3a | 509 | good = path_spec_check_good(s, initial); |
01f78473 LP |
510 | |
511 | if (good) | |
512 | break; | |
513 | } | |
514 | ||
ac3f50ca LP |
515 | return good; |
516 | } | |
01f78473 | 517 | |
ac3f50ca LP |
518 | static void path_enter_waiting(Path *p, bool initial, bool recheck) { |
519 | int r; | |
0595f9a1 | 520 | |
ac3f50ca LP |
521 | if (recheck) |
522 | if (path_check_good(p, initial)) { | |
f2341e0a | 523 | log_unit_debug(UNIT(p), "Got triggered."); |
ac3f50ca LP |
524 | path_enter_running(p); |
525 | return; | |
526 | } | |
527 | ||
e0207c8d ZJS |
528 | r = path_watch(p); |
529 | if (r < 0) | |
ac3f50ca LP |
530 | goto fail; |
531 | ||
532 | /* Hmm, so now we have created inotify watches, but the file | |
533 | * might have appeared/been removed by now, so we must | |
534 | * recheck */ | |
535 | ||
536 | if (recheck) | |
537 | if (path_check_good(p, false)) { | |
f2341e0a | 538 | log_unit_debug(UNIT(p), "Got triggered."); |
ac3f50ca LP |
539 | path_enter_running(p); |
540 | return; | |
541 | } | |
01f78473 LP |
542 | |
543 | path_set_state(p, PATH_WAITING); | |
544 | return; | |
545 | ||
546 | fail: | |
f2341e0a | 547 | log_unit_warning_errno(UNIT(p), r, "Failed to enter waiting state: %m"); |
cd43ca73 | 548 | path_enter_dead(p, PATH_FAILURE_RESOURCES); |
01f78473 LP |
549 | } |
550 | ||
0e456f97 LP |
551 | static void path_mkdir(Path *p) { |
552 | PathSpec *s; | |
553 | ||
554 | assert(p); | |
555 | ||
556 | if (!p->make_directory) | |
557 | return; | |
558 | ||
4b562198 | 559 | LIST_FOREACH(spec, s, p->specs) |
57020a3a | 560 | path_spec_mkdir(s, p->directory_mode); |
0e456f97 LP |
561 | } |
562 | ||
01f78473 LP |
563 | static int path_start(Unit *u) { |
564 | Path *p = PATH(u); | |
07299350 | 565 | int r; |
01f78473 LP |
566 | |
567 | assert(p); | |
3742095b | 568 | assert(IN_SET(p->state, PATH_DEAD, PATH_FAILED)); |
01f78473 | 569 | |
a4191c9f LP |
570 | r = unit_test_trigger_loaded(u); |
571 | if (r < 0) | |
572 | return r; | |
01f78473 | 573 | |
97a3f4ee | 574 | r = unit_test_start_limit(u); |
07299350 LP |
575 | if (r < 0) { |
576 | path_enter_dead(p, PATH_FAILURE_START_LIMIT_HIT); | |
577 | return r; | |
578 | } | |
579 | ||
4b58153d LP |
580 | r = unit_acquire_invocation_id(u); |
581 | if (r < 0) | |
582 | return r; | |
583 | ||
0e456f97 LP |
584 | path_mkdir(p); |
585 | ||
cd43ca73 | 586 | p->result = PATH_SUCCESS; |
ac3f50ca | 587 | path_enter_waiting(p, true, true); |
00dc5d76 | 588 | |
82a2b6bb | 589 | return 1; |
01f78473 LP |
590 | } |
591 | ||
592 | static int path_stop(Unit *u) { | |
593 | Path *p = PATH(u); | |
594 | ||
595 | assert(p); | |
3742095b | 596 | assert(IN_SET(p->state, PATH_WAITING, PATH_RUNNING)); |
01f78473 | 597 | |
cd43ca73 | 598 | path_enter_dead(p, PATH_SUCCESS); |
82a2b6bb | 599 | return 1; |
01f78473 LP |
600 | } |
601 | ||
602 | static int path_serialize(Unit *u, FILE *f, FDSet *fds) { | |
603 | Path *p = PATH(u); | |
604 | ||
605 | assert(u); | |
606 | assert(f); | |
607 | assert(fds); | |
608 | ||
d68c645b LP |
609 | (void) serialize_item(f, "state", path_state_to_string(p->state)); |
610 | (void) serialize_item(f, "result", path_result_to_string(p->result)); | |
01f78473 LP |
611 | |
612 | return 0; | |
613 | } | |
614 | ||
615 | static int path_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
616 | Path *p = PATH(u); | |
617 | ||
618 | assert(u); | |
619 | assert(key); | |
620 | assert(value); | |
621 | assert(fds); | |
622 | ||
623 | if (streq(key, "state")) { | |
624 | PathState state; | |
625 | ||
e0207c8d ZJS |
626 | state = path_state_from_string(value); |
627 | if (state < 0) | |
f2341e0a | 628 | log_unit_debug(u, "Failed to parse state value: %s", value); |
01f78473 LP |
629 | else |
630 | p->deserialized_state = state; | |
cd43ca73 LP |
631 | |
632 | } else if (streq(key, "result")) { | |
633 | PathResult f; | |
634 | ||
635 | f = path_result_from_string(value); | |
636 | if (f < 0) | |
f2341e0a | 637 | log_unit_debug(u, "Failed to parse result value: %s", value); |
cd43ca73 LP |
638 | else if (f != PATH_SUCCESS) |
639 | p->result = f; | |
640 | ||
01f78473 | 641 | } else |
f2341e0a | 642 | log_unit_debug(u, "Unknown serialization key: %s", key); |
01f78473 LP |
643 | |
644 | return 0; | |
645 | } | |
646 | ||
44a6b1b6 | 647 | _pure_ static UnitActiveState path_active_state(Unit *u) { |
01f78473 LP |
648 | assert(u); |
649 | ||
650 | return state_translation_table[PATH(u)->state]; | |
651 | } | |
652 | ||
44a6b1b6 | 653 | _pure_ static const char *path_sub_state_to_string(Unit *u) { |
01f78473 LP |
654 | assert(u); |
655 | ||
656 | return path_state_to_string(PATH(u)->state); | |
657 | } | |
658 | ||
718db961 LP |
659 | static int path_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
660 | PathSpec *s = userdata; | |
661 | Path *p; | |
4b562198 | 662 | int changed; |
01f78473 | 663 | |
718db961 LP |
664 | assert(s); |
665 | assert(s->unit); | |
01f78473 LP |
666 | assert(fd >= 0); |
667 | ||
718db961 LP |
668 | p = PATH(s->unit); |
669 | ||
ec2ce0c5 | 670 | if (!IN_SET(p->state, PATH_WAITING, PATH_RUNNING)) |
718db961 | 671 | return 0; |
01f78473 | 672 | |
ac155bb8 | 673 | /* log_debug("inotify wakeup on %s.", u->id); */ |
01f78473 | 674 | |
01f78473 | 675 | LIST_FOREACH(spec, s, p->specs) |
57020a3a | 676 | if (path_spec_owns_inotify_fd(s, fd)) |
01f78473 LP |
677 | break; |
678 | ||
679 | if (!s) { | |
680 | log_error("Got event on unknown fd."); | |
681 | goto fail; | |
682 | } | |
683 | ||
718db961 | 684 | changed = path_spec_fd_event(s, revents); |
4b562198 | 685 | if (changed < 0) |
01f78473 | 686 | goto fail; |
01f78473 | 687 | |
672028dc LP |
688 | /* If we are already running, then remember that one event was |
689 | * dispatched so that we restart the service only if something | |
690 | * actually changed on disk */ | |
691 | p->inotify_triggered = true; | |
692 | ||
672028dc LP |
693 | if (changed) |
694 | path_enter_running(p); | |
695 | else | |
ac3f50ca | 696 | path_enter_waiting(p, false, true); |
672028dc | 697 | |
718db961 | 698 | return 0; |
01f78473 LP |
699 | |
700 | fail: | |
cd43ca73 | 701 | path_enter_dead(p, PATH_FAILURE_RESOURCES); |
718db961 | 702 | return 0; |
01f78473 LP |
703 | } |
704 | ||
3ecaa09b LP |
705 | static void path_trigger_notify(Unit *u, Unit *other) { |
706 | Path *p = PATH(u); | |
01f78473 | 707 | |
3ecaa09b LP |
708 | assert(u); |
709 | assert(other); | |
01f78473 | 710 | |
3ecaa09b LP |
711 | /* Invoked whenever the unit we trigger changes state or gains |
712 | * or loses a job */ | |
01f78473 | 713 | |
3ecaa09b LP |
714 | if (other->load_state != UNIT_LOADED) |
715 | return; | |
01f78473 | 716 | |
3ecaa09b LP |
717 | if (p->state == PATH_RUNNING && |
718 | UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) { | |
f2341e0a | 719 | log_unit_debug(UNIT(p), "Got notified about unit deactivation."); |
672028dc | 720 | |
3ecaa09b LP |
721 | /* Hmm, so inotify was triggered since the |
722 | * last activation, so I guess we need to | |
723 | * recheck what is going on. */ | |
724 | path_enter_waiting(p, false, p->inotify_triggered); | |
01f78473 | 725 | } |
01f78473 LP |
726 | } |
727 | ||
fdf20a31 | 728 | static void path_reset_failed(Unit *u) { |
5632e374 LP |
729 | Path *p = PATH(u); |
730 | ||
731 | assert(p); | |
732 | ||
fdf20a31 | 733 | if (p->state == PATH_FAILED) |
5632e374 LP |
734 | path_set_state(p, PATH_DEAD); |
735 | ||
cd43ca73 | 736 | p->result = PATH_SUCCESS; |
5632e374 LP |
737 | } |
738 | ||
01f78473 LP |
739 | static const char* const path_type_table[_PATH_TYPE_MAX] = { |
740 | [PATH_EXISTS] = "PathExists", | |
8092a428 | 741 | [PATH_EXISTS_GLOB] = "PathExistsGlob", |
2c5859af | 742 | [PATH_DIRECTORY_NOT_EMPTY] = "DirectoryNotEmpty", |
01f78473 | 743 | [PATH_CHANGED] = "PathChanged", |
e9223856 | 744 | [PATH_MODIFIED] = "PathModified", |
01f78473 LP |
745 | }; |
746 | ||
747 | DEFINE_STRING_TABLE_LOOKUP(path_type, PathType); | |
748 | ||
cd43ca73 LP |
749 | static const char* const path_result_table[_PATH_RESULT_MAX] = { |
750 | [PATH_SUCCESS] = "success", | |
2c5859af | 751 | [PATH_FAILURE_RESOURCES] = "resources", |
07299350 | 752 | [PATH_FAILURE_START_LIMIT_HIT] = "start-limit-hit", |
cd43ca73 LP |
753 | }; |
754 | ||
755 | DEFINE_STRING_TABLE_LOOKUP(path_result, PathResult); | |
756 | ||
01f78473 | 757 | const UnitVTable path_vtable = { |
7d17cfbc | 758 | .object_size = sizeof(Path), |
718db961 | 759 | |
f975e971 LP |
760 | .sections = |
761 | "Unit\0" | |
762 | "Path\0" | |
763 | "Install\0", | |
5b9fbf89 YW |
764 | .private_section = "Path", |
765 | ||
766 | .can_transient = true, | |
01f78473 | 767 | |
0e456f97 | 768 | .init = path_init, |
01f78473 LP |
769 | .done = path_done, |
770 | .load = path_load, | |
771 | ||
772 | .coldplug = path_coldplug, | |
773 | ||
774 | .dump = path_dump, | |
775 | ||
776 | .start = path_start, | |
777 | .stop = path_stop, | |
778 | ||
779 | .serialize = path_serialize, | |
780 | .deserialize_item = path_deserialize_item, | |
781 | ||
782 | .active_state = path_active_state, | |
783 | .sub_state_to_string = path_sub_state_to_string, | |
784 | ||
3ecaa09b LP |
785 | .trigger_notify = path_trigger_notify, |
786 | ||
fdf20a31 | 787 | .reset_failed = path_reset_failed, |
5632e374 | 788 | |
5b9fbf89 YW |
789 | .bus_vtable = bus_path_vtable, |
790 | .bus_set_property = bus_path_set_property, | |
01f78473 | 791 | }; |