]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
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 | ||
708961c7 | 177 | static bool path_spec_check_good(PathSpec *s, bool initial, bool from_trigger_notify) { |
d7cf8c24 | 178 | bool b, good = false; |
4b562198 MS |
179 | |
180 | switch (s->type) { | |
181 | ||
182 | case PATH_EXISTS: | |
708961c7 | 183 | good = access(s->path, F_OK) >= 0; |
4b562198 MS |
184 | break; |
185 | ||
186 | case PATH_EXISTS_GLOB: | |
187 | good = glob_exists(s->path) > 0; | |
188 | break; | |
189 | ||
190 | case PATH_DIRECTORY_NOT_EMPTY: { | |
191 | int k; | |
192 | ||
193 | k = dir_is_empty(s->path); | |
194 | good = !(k == -ENOENT || k > 0); | |
195 | break; | |
196 | } | |
197 | ||
714d943f | 198 | case PATH_CHANGED: |
d7cf8c24 | 199 | case PATH_MODIFIED: |
4b562198 | 200 | b = access(s->path, F_OK) >= 0; |
708961c7 | 201 | good = !initial && !from_trigger_notify && b != s->previous_exists; |
4b562198 MS |
202 | s->previous_exists = b; |
203 | break; | |
4b562198 MS |
204 | |
205 | default: | |
206 | ; | |
207 | } | |
208 | ||
209 | return good; | |
210 | } | |
211 | ||
57020a3a | 212 | static void path_spec_mkdir(PathSpec *s, mode_t mode) { |
4b562198 MS |
213 | int r; |
214 | ||
3742095b | 215 | if (IN_SET(s->type, PATH_EXISTS, PATH_EXISTS_GLOB)) |
4b562198 MS |
216 | return; |
217 | ||
15f55e80 ZJS |
218 | r = mkdir_p_label(s->path, mode); |
219 | if (r < 0) | |
da927ba9 | 220 | log_warning_errno(r, "mkdir(%s) failed: %m", s->path); |
4b562198 MS |
221 | } |
222 | ||
57020a3a | 223 | static void path_spec_dump(PathSpec *s, FILE *f, const char *prefix) { |
23450c89 ZJS |
224 | const char *type; |
225 | ||
226 | assert_se(type = path_type_to_string(s->type)); | |
227 | fprintf(f, "%s%s: %s\n", prefix, type, s->path); | |
4b562198 MS |
228 | } |
229 | ||
57020a3a LP |
230 | void path_spec_done(PathSpec *s) { |
231 | assert(s); | |
4b562198 | 232 | assert(s->inotify_fd == -1); |
57020a3a | 233 | |
4b562198 MS |
234 | free(s->path); |
235 | } | |
236 | ||
237 | static void path_init(Unit *u) { | |
238 | Path *p = PATH(u); | |
239 | ||
240 | assert(u); | |
ac155bb8 | 241 | assert(u->load_state == UNIT_STUB); |
4b562198 MS |
242 | |
243 | p->directory_mode = 0755; | |
244 | } | |
245 | ||
74051b9b | 246 | void path_free_specs(Path *p) { |
01f78473 LP |
247 | PathSpec *s; |
248 | ||
249 | assert(p); | |
250 | ||
251 | while ((s = p->specs)) { | |
718db961 | 252 | path_spec_unwatch(s); |
71fda00f | 253 | LIST_REMOVE(spec, p->specs, s); |
57020a3a | 254 | path_spec_done(s); |
01f78473 LP |
255 | free(s); |
256 | } | |
257 | } | |
258 | ||
74051b9b LP |
259 | static void path_done(Unit *u) { |
260 | Path *p = PATH(u); | |
261 | ||
262 | assert(p); | |
263 | ||
74051b9b LP |
264 | path_free_specs(p); |
265 | } | |
266 | ||
eef85c4a | 267 | static int path_add_mount_dependencies(Path *p) { |
01f78473 LP |
268 | PathSpec *s; |
269 | int r; | |
270 | ||
271 | assert(p); | |
01f78473 LP |
272 | |
273 | LIST_FOREACH(spec, s, p->specs) { | |
eef85c4a | 274 | r = unit_require_mounts_for(UNIT(p), s->path, UNIT_DEPENDENCY_FILE); |
e0207c8d | 275 | if (r < 0) |
01f78473 | 276 | return r; |
e0207c8d | 277 | } |
01f78473 LP |
278 | |
279 | return 0; | |
280 | } | |
281 | ||
282 | static int path_verify(Path *p) { | |
283 | assert(p); | |
75193d41 | 284 | assert(UNIT(p)->load_state == UNIT_LOADED); |
01f78473 | 285 | |
d85ff944 YW |
286 | if (!p->specs) |
287 | return log_unit_error_errno(UNIT(p), SYNTHETIC_ERRNO(ENOEXEC), "Path unit lacks path setting. Refusing."); | |
01f78473 LP |
288 | |
289 | return 0; | |
290 | } | |
291 | ||
6c155fe3 LP |
292 | static int path_add_default_dependencies(Path *p) { |
293 | int r; | |
294 | ||
295 | assert(p); | |
296 | ||
4c9ea260 LP |
297 | if (!UNIT(p)->default_dependencies) |
298 | return 0; | |
299 | ||
35d8c19a | 300 | r = unit_add_dependency_by_name(UNIT(p), UNIT_BEFORE, SPECIAL_PATHS_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
e3d84721 LP |
301 | if (r < 0) |
302 | return r; | |
2a77d31d | 303 | |
463d0d15 | 304 | if (MANAGER_IS_SYSTEM(UNIT(p)->manager)) { |
5a724170 | 305 | r = unit_add_two_dependencies_by_name(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
e0207c8d | 306 | if (r < 0) |
6c155fe3 | 307 | return r; |
2a77d31d | 308 | } |
6c155fe3 | 309 | |
5a724170 | 310 | return unit_add_two_dependencies_by_name(UNIT(p), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
eef85c4a LP |
311 | } |
312 | ||
313 | static int path_add_trigger_dependencies(Path *p) { | |
314 | Unit *x; | |
315 | int r; | |
316 | ||
317 | assert(p); | |
318 | ||
319 | if (!hashmap_isempty(UNIT(p)->dependencies[UNIT_TRIGGERS])) | |
320 | return 0; | |
321 | ||
322 | r = unit_load_related_unit(UNIT(p), ".service", &x); | |
323 | if (r < 0) | |
324 | return r; | |
325 | ||
326 | return unit_add_two_dependencies(UNIT(p), UNIT_BEFORE, UNIT_TRIGGERS, x, true, UNIT_DEPENDENCY_IMPLICIT); | |
6c155fe3 LP |
327 | } |
328 | ||
75193d41 ZJS |
329 | static int path_add_extras(Path *p) { |
330 | int r; | |
331 | ||
332 | r = path_add_trigger_dependencies(p); | |
333 | if (r < 0) | |
334 | return r; | |
335 | ||
336 | r = path_add_mount_dependencies(p); | |
337 | if (r < 0) | |
338 | return r; | |
339 | ||
340 | return path_add_default_dependencies(p); | |
341 | } | |
342 | ||
01f78473 LP |
343 | static int path_load(Unit *u) { |
344 | Path *p = PATH(u); | |
345 | int r; | |
346 | ||
347 | assert(u); | |
ac155bb8 | 348 | assert(u->load_state == UNIT_STUB); |
01f78473 | 349 | |
c3620770 | 350 | r = unit_load_fragment_and_dropin(u, true); |
e0207c8d | 351 | if (r < 0) |
01f78473 LP |
352 | return r; |
353 | ||
75193d41 ZJS |
354 | if (u->load_state != UNIT_LOADED) |
355 | return 0; | |
a40eb732 | 356 | |
75193d41 ZJS |
357 | r = path_add_extras(p); |
358 | if (r < 0) | |
359 | return r; | |
01f78473 LP |
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 | ||
6fcbec6f LP |
418 | if (p->state != state) |
419 | bus_unit_send_pending_change_signal(UNIT(p), false); | |
420 | ||
01f78473 LP |
421 | old_state = p->state; |
422 | p->state = state; | |
423 | ||
708961c7 | 424 | if (!IN_SET(state, PATH_WAITING, PATH_RUNNING)) |
01f78473 LP |
425 | path_unwatch(p); |
426 | ||
427 | if (state != old_state) | |
3541bf1f | 428 | log_unit_debug(UNIT(p), "Changed %s -> %s", path_state_to_string(old_state), path_state_to_string(state)); |
01f78473 | 429 | |
2ad2e41a | 430 | unit_notify(UNIT(p), state_translation_table[old_state], state_translation_table[state], 0); |
01f78473 LP |
431 | } |
432 | ||
708961c7 | 433 | static void path_enter_waiting(Path *p, bool initial, bool from_trigger_notify); |
01f78473 | 434 | |
be847e82 | 435 | static int path_coldplug(Unit *u) { |
01f78473 LP |
436 | Path *p = PATH(u); |
437 | ||
438 | assert(p); | |
439 | assert(p->state == PATH_DEAD); | |
440 | ||
441 | if (p->deserialized_state != p->state) { | |
442 | ||
3742095b | 443 | if (IN_SET(p->deserialized_state, PATH_WAITING, PATH_RUNNING)) |
708961c7 | 444 | path_enter_waiting(p, true, false); |
be847e82 | 445 | else |
01f78473 LP |
446 | path_set_state(p, p->deserialized_state); |
447 | } | |
448 | ||
449 | return 0; | |
450 | } | |
451 | ||
cd43ca73 | 452 | static void path_enter_dead(Path *p, PathResult f) { |
01f78473 LP |
453 | assert(p); |
454 | ||
a0fef983 | 455 | if (p->result == PATH_SUCCESS) |
cd43ca73 | 456 | p->result = f; |
01f78473 | 457 | |
aac99f30 | 458 | unit_log_result(UNIT(p), p->result == PATH_SUCCESS, path_result_to_string(p->result)); |
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 | ||
50cbaba4 | 480 | r = manager_add_job(UNIT(p)->manager, JOB_START, trigger, JOB_REPLACE, NULL, &error, NULL); |
e0207c8d | 481 | if (r < 0) |
01f78473 LP |
482 | goto fail; |
483 | ||
484 | path_set_state(p, PATH_RUNNING); | |
8fca6944 MS |
485 | path_unwatch(p); |
486 | ||
01f78473 LP |
487 | return; |
488 | ||
489 | fail: | |
f2341e0a | 490 | log_unit_warning(UNIT(p), "Failed to queue unit startup job: %s", bus_error_message(&error, r)); |
cd43ca73 | 491 | path_enter_dead(p, PATH_FAILURE_RESOURCES); |
01f78473 LP |
492 | } |
493 | ||
708961c7 | 494 | static bool path_check_good(Path *p, bool initial, bool from_trigger_notify) { |
01f78473 | 495 | PathSpec *s; |
01f78473 | 496 | |
ac3f50ca | 497 | assert(p); |
672028dc | 498 | |
93e63b2a | 499 | LIST_FOREACH(spec, s, p->specs) |
708961c7 | 500 | if (path_spec_check_good(s, initial, from_trigger_notify)) |
93e63b2a | 501 | return true; |
01f78473 | 502 | |
93e63b2a | 503 | return false; |
ac3f50ca | 504 | } |
01f78473 | 505 | |
708961c7 MC |
506 | static void path_enter_waiting(Path *p, bool initial, bool from_trigger_notify) { |
507 | Unit *trigger; | |
ac3f50ca | 508 | int r; |
0595f9a1 | 509 | |
708961c7 MC |
510 | /* If the triggered unit is already running, so are we */ |
511 | trigger = UNIT_TRIGGER(UNIT(p)); | |
512 | if (trigger && !UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(trigger))) { | |
513 | path_set_state(p, PATH_RUNNING); | |
514 | path_unwatch(p); | |
515 | return; | |
516 | } | |
517 | ||
518 | if (path_check_good(p, initial, from_trigger_notify)) { | |
519 | log_unit_debug(UNIT(p), "Got triggered."); | |
520 | path_enter_running(p); | |
521 | return; | |
522 | } | |
ac3f50ca | 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 | ||
708961c7 MC |
532 | if (path_check_good(p, false, from_trigger_notify)) { |
533 | log_unit_debug(UNIT(p), "Got triggered."); | |
534 | path_enter_running(p); | |
535 | return; | |
536 | } | |
01f78473 LP |
537 | |
538 | path_set_state(p, PATH_WAITING); | |
539 | return; | |
540 | ||
541 | fail: | |
f2341e0a | 542 | log_unit_warning_errno(UNIT(p), r, "Failed to enter waiting state: %m"); |
cd43ca73 | 543 | path_enter_dead(p, PATH_FAILURE_RESOURCES); |
01f78473 LP |
544 | } |
545 | ||
0e456f97 LP |
546 | static void path_mkdir(Path *p) { |
547 | PathSpec *s; | |
548 | ||
549 | assert(p); | |
550 | ||
551 | if (!p->make_directory) | |
552 | return; | |
553 | ||
4b562198 | 554 | LIST_FOREACH(spec, s, p->specs) |
57020a3a | 555 | path_spec_mkdir(s, p->directory_mode); |
0e456f97 LP |
556 | } |
557 | ||
01f78473 LP |
558 | static int path_start(Unit *u) { |
559 | Path *p = PATH(u); | |
07299350 | 560 | int r; |
01f78473 LP |
561 | |
562 | assert(p); | |
3742095b | 563 | assert(IN_SET(p->state, PATH_DEAD, PATH_FAILED)); |
01f78473 | 564 | |
a4191c9f LP |
565 | r = unit_test_trigger_loaded(u); |
566 | if (r < 0) | |
567 | return r; | |
01f78473 | 568 | |
97a3f4ee | 569 | r = unit_test_start_limit(u); |
07299350 LP |
570 | if (r < 0) { |
571 | path_enter_dead(p, PATH_FAILURE_START_LIMIT_HIT); | |
572 | return r; | |
573 | } | |
574 | ||
4b58153d LP |
575 | r = unit_acquire_invocation_id(u); |
576 | if (r < 0) | |
577 | return r; | |
578 | ||
0e456f97 LP |
579 | path_mkdir(p); |
580 | ||
cd43ca73 | 581 | p->result = PATH_SUCCESS; |
708961c7 | 582 | path_enter_waiting(p, true, false); |
00dc5d76 | 583 | |
82a2b6bb | 584 | return 1; |
01f78473 LP |
585 | } |
586 | ||
587 | static int path_stop(Unit *u) { | |
588 | Path *p = PATH(u); | |
589 | ||
590 | assert(p); | |
3742095b | 591 | assert(IN_SET(p->state, PATH_WAITING, PATH_RUNNING)); |
01f78473 | 592 | |
cd43ca73 | 593 | path_enter_dead(p, PATH_SUCCESS); |
82a2b6bb | 594 | return 1; |
01f78473 LP |
595 | } |
596 | ||
597 | static int path_serialize(Unit *u, FILE *f, FDSet *fds) { | |
598 | Path *p = PATH(u); | |
7a16cd4b | 599 | PathSpec *s; |
01f78473 LP |
600 | |
601 | assert(u); | |
602 | assert(f); | |
603 | assert(fds); | |
604 | ||
d68c645b LP |
605 | (void) serialize_item(f, "state", path_state_to_string(p->state)); |
606 | (void) serialize_item(f, "result", path_result_to_string(p->result)); | |
01f78473 | 607 | |
7a16cd4b | 608 | LIST_FOREACH(spec, s, p->specs) { |
23450c89 | 609 | const char *type; |
7a16cd4b ZJS |
610 | _cleanup_free_ char *escaped = NULL; |
611 | ||
612 | escaped = cescape(s->path); | |
613 | if (!escaped) | |
614 | return log_oom(); | |
615 | ||
23450c89 | 616 | assert_se(type = path_type_to_string(s->type)); |
7a16cd4b | 617 | (void) serialize_item_format(f, "path-spec", "%s %i %s", |
23450c89 | 618 | type, |
7a16cd4b | 619 | s->previous_exists, |
f285f077 | 620 | escaped); |
7a16cd4b ZJS |
621 | } |
622 | ||
01f78473 LP |
623 | return 0; |
624 | } | |
625 | ||
626 | static int path_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
627 | Path *p = PATH(u); | |
628 | ||
629 | assert(u); | |
630 | assert(key); | |
631 | assert(value); | |
632 | assert(fds); | |
633 | ||
634 | if (streq(key, "state")) { | |
635 | PathState state; | |
636 | ||
e0207c8d ZJS |
637 | state = path_state_from_string(value); |
638 | if (state < 0) | |
f2341e0a | 639 | log_unit_debug(u, "Failed to parse state value: %s", value); |
01f78473 LP |
640 | else |
641 | p->deserialized_state = state; | |
cd43ca73 LP |
642 | |
643 | } else if (streq(key, "result")) { | |
644 | PathResult f; | |
645 | ||
646 | f = path_result_from_string(value); | |
647 | if (f < 0) | |
f2341e0a | 648 | log_unit_debug(u, "Failed to parse result value: %s", value); |
cd43ca73 LP |
649 | else if (f != PATH_SUCCESS) |
650 | p->result = f; | |
651 | ||
7a16cd4b ZJS |
652 | } else if (streq(key, "path-spec")) { |
653 | int previous_exists, skip = 0, r; | |
654 | _cleanup_free_ char *type_str = NULL; | |
655 | ||
656 | if (sscanf(value, "%ms %i %n", &type_str, &previous_exists, &skip) < 2) | |
657 | log_unit_debug(u, "Failed to parse path-spec value: %s", value); | |
658 | else { | |
659 | _cleanup_free_ char *unescaped = NULL; | |
660 | PathType type; | |
661 | PathSpec *s; | |
662 | ||
663 | type = path_type_from_string(type_str); | |
664 | if (type < 0) { | |
665 | log_unit_warning(u, "Unknown path type \"%s\", ignoring.", type_str); | |
666 | return 0; | |
667 | } | |
668 | ||
669 | r = cunescape(value+skip, 0, &unescaped); | |
670 | if (r < 0) { | |
671 | log_unit_warning_errno(u, r, "Failed to unescape serialize path: %m"); | |
672 | return 0; | |
673 | } | |
674 | ||
675 | LIST_FOREACH(spec, s, p->specs) | |
676 | if (s->type == type && | |
677 | path_equal(s->path, unescaped)) { | |
678 | ||
679 | s->previous_exists = previous_exists; | |
680 | break; | |
681 | } | |
682 | } | |
683 | ||
01f78473 | 684 | } else |
f2341e0a | 685 | log_unit_debug(u, "Unknown serialization key: %s", key); |
01f78473 LP |
686 | |
687 | return 0; | |
688 | } | |
689 | ||
44a6b1b6 | 690 | _pure_ static UnitActiveState path_active_state(Unit *u) { |
01f78473 LP |
691 | assert(u); |
692 | ||
693 | return state_translation_table[PATH(u)->state]; | |
694 | } | |
695 | ||
44a6b1b6 | 696 | _pure_ static const char *path_sub_state_to_string(Unit *u) { |
01f78473 LP |
697 | assert(u); |
698 | ||
699 | return path_state_to_string(PATH(u)->state); | |
700 | } | |
701 | ||
718db961 LP |
702 | static int path_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
703 | PathSpec *s = userdata; | |
704 | Path *p; | |
4b562198 | 705 | int changed; |
01f78473 | 706 | |
718db961 LP |
707 | assert(s); |
708 | assert(s->unit); | |
01f78473 LP |
709 | assert(fd >= 0); |
710 | ||
718db961 LP |
711 | p = PATH(s->unit); |
712 | ||
ec2ce0c5 | 713 | if (!IN_SET(p->state, PATH_WAITING, PATH_RUNNING)) |
718db961 | 714 | return 0; |
01f78473 | 715 | |
7a16cd4b | 716 | /* log_debug("inotify wakeup on %s.", UNIT(p)->id); */ |
01f78473 | 717 | |
01f78473 | 718 | LIST_FOREACH(spec, s, p->specs) |
57020a3a | 719 | if (path_spec_owns_inotify_fd(s, fd)) |
01f78473 LP |
720 | break; |
721 | ||
722 | if (!s) { | |
723 | log_error("Got event on unknown fd."); | |
724 | goto fail; | |
725 | } | |
726 | ||
718db961 | 727 | changed = path_spec_fd_event(s, revents); |
4b562198 | 728 | if (changed < 0) |
01f78473 | 729 | goto fail; |
01f78473 | 730 | |
672028dc LP |
731 | if (changed) |
732 | path_enter_running(p); | |
733 | else | |
708961c7 | 734 | path_enter_waiting(p, false, false); |
672028dc | 735 | |
718db961 | 736 | return 0; |
01f78473 LP |
737 | |
738 | fail: | |
cd43ca73 | 739 | path_enter_dead(p, PATH_FAILURE_RESOURCES); |
718db961 | 740 | return 0; |
01f78473 LP |
741 | } |
742 | ||
3ecaa09b LP |
743 | static void path_trigger_notify(Unit *u, Unit *other) { |
744 | Path *p = PATH(u); | |
01f78473 | 745 | |
3ecaa09b LP |
746 | assert(u); |
747 | assert(other); | |
01f78473 | 748 | |
0377cd29 | 749 | /* Invoked whenever the unit we trigger changes state or gains or loses a job */ |
01f78473 | 750 | |
0377cd29 LP |
751 | /* Filter out invocations with bogus state */ |
752 | assert(UNIT_IS_LOAD_COMPLETE(other->load_state)); | |
01f78473 | 753 | |
47ab8f73 LP |
754 | /* Don't propagate state changes from the triggered unit if we are already down */ |
755 | if (!IN_SET(p->state, PATH_WAITING, PATH_RUNNING)) | |
756 | return; | |
757 | ||
758 | /* Propagate start limit hit state */ | |
759 | if (other->start_limit_hit) { | |
760 | path_enter_dead(p, PATH_FAILURE_UNIT_START_LIMIT_HIT); | |
761 | return; | |
762 | } | |
763 | ||
764 | /* Don't propagate anything if there's still a job queued */ | |
765 | if (other->job) | |
766 | return; | |
767 | ||
3ecaa09b LP |
768 | if (p->state == PATH_RUNNING && |
769 | UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) { | |
f2341e0a | 770 | log_unit_debug(UNIT(p), "Got notified about unit deactivation."); |
708961c7 MC |
771 | path_enter_waiting(p, false, true); |
772 | } else if (p->state == PATH_WAITING && | |
773 | !UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) { | |
774 | log_unit_debug(UNIT(p), "Got notified about unit activation."); | |
775 | path_enter_waiting(p, false, true); | |
01f78473 | 776 | } |
01f78473 LP |
777 | } |
778 | ||
fdf20a31 | 779 | static void path_reset_failed(Unit *u) { |
5632e374 LP |
780 | Path *p = PATH(u); |
781 | ||
782 | assert(p); | |
783 | ||
fdf20a31 | 784 | if (p->state == PATH_FAILED) |
5632e374 LP |
785 | path_set_state(p, PATH_DEAD); |
786 | ||
cd43ca73 | 787 | p->result = PATH_SUCCESS; |
5632e374 LP |
788 | } |
789 | ||
01f78473 LP |
790 | static const char* const path_type_table[_PATH_TYPE_MAX] = { |
791 | [PATH_EXISTS] = "PathExists", | |
8092a428 | 792 | [PATH_EXISTS_GLOB] = "PathExistsGlob", |
2c5859af | 793 | [PATH_DIRECTORY_NOT_EMPTY] = "DirectoryNotEmpty", |
01f78473 | 794 | [PATH_CHANGED] = "PathChanged", |
e9223856 | 795 | [PATH_MODIFIED] = "PathModified", |
01f78473 LP |
796 | }; |
797 | ||
798 | DEFINE_STRING_TABLE_LOOKUP(path_type, PathType); | |
799 | ||
cd43ca73 LP |
800 | static const char* const path_result_table[_PATH_RESULT_MAX] = { |
801 | [PATH_SUCCESS] = "success", | |
2c5859af | 802 | [PATH_FAILURE_RESOURCES] = "resources", |
07299350 | 803 | [PATH_FAILURE_START_LIMIT_HIT] = "start-limit-hit", |
47ab8f73 | 804 | [PATH_FAILURE_UNIT_START_LIMIT_HIT] = "unit-start-limit-hit", |
cd43ca73 LP |
805 | }; |
806 | ||
807 | DEFINE_STRING_TABLE_LOOKUP(path_result, PathResult); | |
808 | ||
01f78473 | 809 | const UnitVTable path_vtable = { |
7d17cfbc | 810 | .object_size = sizeof(Path), |
718db961 | 811 | |
f975e971 LP |
812 | .sections = |
813 | "Unit\0" | |
814 | "Path\0" | |
815 | "Install\0", | |
5b9fbf89 YW |
816 | .private_section = "Path", |
817 | ||
818 | .can_transient = true, | |
c80a9a33 LP |
819 | .can_fail = true, |
820 | .can_trigger = true, | |
01f78473 | 821 | |
0e456f97 | 822 | .init = path_init, |
01f78473 LP |
823 | .done = path_done, |
824 | .load = path_load, | |
825 | ||
826 | .coldplug = path_coldplug, | |
827 | ||
828 | .dump = path_dump, | |
829 | ||
830 | .start = path_start, | |
831 | .stop = path_stop, | |
832 | ||
833 | .serialize = path_serialize, | |
834 | .deserialize_item = path_deserialize_item, | |
835 | ||
836 | .active_state = path_active_state, | |
837 | .sub_state_to_string = path_sub_state_to_string, | |
838 | ||
3ecaa09b LP |
839 | .trigger_notify = path_trigger_notify, |
840 | ||
fdf20a31 | 841 | .reset_failed = path_reset_failed, |
5632e374 | 842 | |
5b9fbf89 | 843 | .bus_set_property = bus_path_set_property, |
01f78473 | 844 | }; |