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