]>
Commit | Line | Data |
---|---|---|
01f78473 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <sys/inotify.h> | |
23 | #include <sys/epoll.h> | |
24 | #include <sys/ioctl.h> | |
25 | #include <errno.h> | |
26 | #include <unistd.h> | |
27 | ||
28 | #include "unit.h" | |
29 | #include "unit-name.h" | |
30 | #include "path.h" | |
31 | #include "dbus-path.h" | |
a40eb732 | 32 | #include "special.h" |
398ef8ba | 33 | #include "bus-errors.h" |
01f78473 LP |
34 | |
35 | static const UnitActiveState state_translation_table[_PATH_STATE_MAX] = { | |
36 | [PATH_DEAD] = UNIT_INACTIVE, | |
37 | [PATH_WAITING] = UNIT_ACTIVE, | |
38 | [PATH_RUNNING] = UNIT_ACTIVE, | |
032ff4af | 39 | [PATH_MAINTENANCE] = UNIT_MAINTENANCE |
01f78473 LP |
40 | }; |
41 | ||
42 | static void path_done(Unit *u) { | |
43 | Path *p = PATH(u); | |
44 | PathSpec *s; | |
45 | ||
46 | assert(p); | |
47 | ||
48 | while ((s = p->specs)) { | |
49 | LIST_REMOVE(PathSpec, spec, p->specs, s); | |
50 | free(s); | |
51 | } | |
52 | } | |
53 | ||
54 | int path_add_one_mount_link(Path *p, Mount *m) { | |
55 | PathSpec *s; | |
56 | int r; | |
57 | ||
58 | assert(p); | |
59 | assert(m); | |
60 | ||
61 | if (p->meta.load_state != UNIT_LOADED || | |
62 | m->meta.load_state != UNIT_LOADED) | |
63 | return 0; | |
64 | ||
65 | LIST_FOREACH(spec, s, p->specs) { | |
66 | ||
67 | if (!path_startswith(s->path, m->where)) | |
68 | continue; | |
69 | ||
2c966c03 | 70 | if ((r = unit_add_two_dependencies(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0) |
01f78473 LP |
71 | return r; |
72 | } | |
73 | ||
74 | return 0; | |
75 | } | |
76 | ||
77 | static int path_add_mount_links(Path *p) { | |
78 | Meta *other; | |
79 | int r; | |
80 | ||
81 | assert(p); | |
82 | ||
83 | LIST_FOREACH(units_per_type, other, p->meta.manager->units_per_type[UNIT_MOUNT]) | |
84 | if ((r = path_add_one_mount_link(p, (Mount*) other)) < 0) | |
85 | return r; | |
86 | ||
87 | return 0; | |
88 | } | |
89 | ||
90 | static int path_verify(Path *p) { | |
91 | assert(p); | |
92 | ||
4cd1fbcc | 93 | if (p->meta.load_state != UNIT_LOADED) |
01f78473 LP |
94 | return 0; |
95 | ||
96 | if (!p->specs) { | |
97 | log_error("%s lacks path setting. Refusing.", p->meta.id); | |
98 | return -EINVAL; | |
99 | } | |
100 | ||
101 | return 0; | |
102 | } | |
103 | ||
6c155fe3 LP |
104 | static int path_add_default_dependencies(Path *p) { |
105 | int r; | |
106 | ||
107 | assert(p); | |
108 | ||
109 | if (p->meta.manager->running_as == MANAGER_SYSTEM) | |
110 | if ((r = unit_add_two_dependencies_by_name(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0) | |
111 | return r; | |
112 | ||
113 | return unit_add_two_dependencies_by_name(UNIT(p), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true); | |
114 | } | |
115 | ||
01f78473 LP |
116 | static int path_load(Unit *u) { |
117 | Path *p = PATH(u); | |
118 | int r; | |
119 | ||
120 | assert(u); | |
121 | assert(u->meta.load_state == UNIT_STUB); | |
122 | ||
123 | if ((r = unit_load_fragment_and_dropin(u)) < 0) | |
124 | return r; | |
125 | ||
126 | if (u->meta.load_state == UNIT_LOADED) { | |
127 | ||
128 | if (!p->unit) | |
129 | if ((r = unit_load_related_unit(u, ".service", &p->unit))) | |
130 | return r; | |
131 | ||
132 | if ((r = unit_add_dependency(u, UNIT_BEFORE, p->unit, true)) < 0) | |
133 | return r; | |
134 | ||
135 | if ((r = path_add_mount_links(p)) < 0) | |
136 | return r; | |
a40eb732 | 137 | |
a40eb732 | 138 | if (p->meta.default_dependencies) |
6c155fe3 | 139 | if ((r = path_add_default_dependencies(p)) < 0) |
a40eb732 | 140 | return r; |
01f78473 LP |
141 | } |
142 | ||
143 | return path_verify(p); | |
144 | } | |
145 | ||
146 | static void path_dump(Unit *u, FILE *f, const char *prefix) { | |
147 | Path *p = PATH(u); | |
148 | const char *prefix2; | |
149 | char *p2; | |
150 | PathSpec *s; | |
151 | ||
152 | p2 = strappend(prefix, "\t"); | |
153 | prefix2 = p2 ? p2 : prefix; | |
154 | ||
155 | fprintf(f, | |
156 | "%sPath State: %s\n" | |
157 | "%sUnit: %s\n", | |
158 | prefix, path_state_to_string(p->state), | |
159 | prefix, p->unit->meta.id); | |
160 | ||
161 | LIST_FOREACH(spec, s, p->specs) | |
162 | fprintf(f, | |
163 | "%s%s: %s\n", | |
164 | prefix, | |
165 | path_type_to_string(s->type), | |
166 | s->path); | |
167 | ||
168 | free(p2); | |
169 | } | |
170 | ||
171 | static void path_unwatch_one(Path *p, PathSpec *s) { | |
172 | ||
173 | if (s->inotify_fd < 0) | |
174 | return; | |
175 | ||
176 | unit_unwatch_fd(UNIT(p), &s->watch); | |
177 | ||
178 | close_nointr_nofail(s->inotify_fd); | |
179 | s->inotify_fd = -1; | |
180 | } | |
181 | ||
182 | static int path_watch_one(Path *p, PathSpec *s) { | |
183 | static const int flags_table[_PATH_TYPE_MAX] = { | |
184 | [PATH_EXISTS] = IN_DELETE_SELF|IN_MOVE_SELF, | |
185 | [PATH_CHANGED] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO, | |
186 | [PATH_DIRECTORY_NOT_EMPTY] = IN_DELETE_SELF|IN_MOVE_SELF|IN_CREATE|IN_MOVED_TO | |
187 | }; | |
188 | ||
189 | bool exists = false; | |
190 | char *k; | |
191 | int r; | |
192 | ||
193 | assert(p); | |
194 | assert(s); | |
195 | ||
196 | path_unwatch_one(p, s); | |
197 | ||
198 | if (!(k = strdup(s->path))) | |
199 | return -ENOMEM; | |
200 | ||
201 | if ((s->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC)) < 0) { | |
202 | r = -errno; | |
203 | goto fail; | |
204 | } | |
205 | ||
206 | if (unit_watch_fd(UNIT(p), s->inotify_fd, EPOLLIN, &s->watch) < 0) { | |
207 | r = -errno; | |
208 | goto fail; | |
209 | } | |
210 | ||
211 | if ((s->primary_wd = inotify_add_watch(s->inotify_fd, k, flags_table[s->type])) >= 0) | |
212 | exists = true; | |
213 | ||
214 | for (;;) { | |
215 | int flags; | |
216 | char *slash; | |
217 | ||
218 | /* This assumes the path was passed through path_kill_slashes()! */ | |
219 | if (!(slash = strrchr(k, '/'))) | |
220 | break; | |
221 | ||
222 | *slash = 0; | |
223 | ||
224 | flags = IN_DELETE_SELF|IN_MOVE_SELF; | |
225 | if (!exists) | |
226 | flags |= IN_CREATE | IN_MOVED_TO | IN_ATTRIB; | |
227 | ||
228 | if (inotify_add_watch(s->inotify_fd, k, flags) >= 0) | |
229 | exists = true; | |
230 | } | |
231 | ||
232 | return 0; | |
233 | ||
234 | fail: | |
235 | free(k); | |
236 | ||
237 | path_unwatch_one(p, s); | |
238 | return r; | |
239 | } | |
240 | ||
241 | static void path_unwatch(Path *p) { | |
242 | PathSpec *s; | |
243 | ||
244 | assert(p); | |
245 | ||
246 | LIST_FOREACH(spec, s, p->specs) | |
247 | path_unwatch_one(p, s); | |
248 | } | |
249 | ||
250 | static int path_watch(Path *p) { | |
251 | int r; | |
252 | PathSpec *s; | |
253 | ||
254 | assert(p); | |
255 | ||
256 | LIST_FOREACH(spec, s, p->specs) | |
257 | if ((r = path_watch_one(p, s)) < 0) | |
258 | return r; | |
259 | ||
260 | return 0; | |
261 | } | |
262 | ||
263 | static void path_set_state(Path *p, PathState state) { | |
264 | PathState old_state; | |
265 | assert(p); | |
266 | ||
267 | old_state = p->state; | |
268 | p->state = state; | |
269 | ||
270 | if (state != PATH_WAITING) | |
271 | path_unwatch(p); | |
272 | ||
273 | if (state != old_state) | |
274 | log_debug("%s changed %s -> %s", | |
275 | p->meta.id, | |
276 | path_state_to_string(old_state), | |
277 | path_state_to_string(state)); | |
278 | ||
279 | unit_notify(UNIT(p), state_translation_table[old_state], state_translation_table[state]); | |
280 | } | |
281 | ||
282 | static void path_enter_waiting(Path *p, bool initial); | |
283 | ||
284 | static int path_coldplug(Unit *u) { | |
285 | Path *p = PATH(u); | |
286 | ||
287 | assert(p); | |
288 | assert(p->state == PATH_DEAD); | |
289 | ||
290 | if (p->deserialized_state != p->state) { | |
291 | ||
292 | if (p->deserialized_state == PATH_WAITING || | |
293 | p->deserialized_state == PATH_RUNNING) | |
294 | path_enter_waiting(p, true); | |
295 | else | |
296 | path_set_state(p, p->deserialized_state); | |
297 | } | |
298 | ||
299 | return 0; | |
300 | } | |
301 | ||
302 | static void path_enter_dead(Path *p, bool success) { | |
303 | assert(p); | |
304 | ||
305 | if (!success) | |
306 | p->failure = true; | |
307 | ||
18c78fb1 | 308 | path_set_state(p, p->failure ? PATH_MAINTENANCE : PATH_DEAD); |
01f78473 LP |
309 | } |
310 | ||
311 | static void path_enter_running(Path *p) { | |
312 | int r; | |
398ef8ba LP |
313 | DBusError error; |
314 | ||
01f78473 | 315 | assert(p); |
398ef8ba | 316 | dbus_error_init(&error); |
01f78473 | 317 | |
ba3e67a7 LP |
318 | /* Don't start job if we are supposed to go down */ |
319 | if (p->meta.job && p->meta.job->type == JOB_STOP) | |
320 | return; | |
321 | ||
398ef8ba | 322 | if ((r = manager_add_job(p->meta.manager, JOB_START, p->unit, JOB_REPLACE, true, &error, NULL)) < 0) |
01f78473 LP |
323 | goto fail; |
324 | ||
325 | path_set_state(p, PATH_RUNNING); | |
326 | return; | |
327 | ||
328 | fail: | |
398ef8ba | 329 | log_warning("%s failed to queue unit startup job: %s", p->meta.id, bus_error(&error, r)); |
01f78473 | 330 | path_enter_dead(p, false); |
398ef8ba LP |
331 | |
332 | dbus_error_free(&error); | |
01f78473 LP |
333 | } |
334 | ||
335 | ||
336 | static void path_enter_waiting(Path *p, bool initial) { | |
337 | PathSpec *s; | |
338 | int r; | |
339 | bool good = false; | |
340 | ||
341 | LIST_FOREACH(spec, s, p->specs) { | |
342 | ||
343 | switch (s->type) { | |
344 | ||
345 | case PATH_EXISTS: | |
346 | good = access(s->path, F_OK) >= 0; | |
347 | break; | |
348 | ||
349 | case PATH_DIRECTORY_NOT_EMPTY: | |
350 | good = dir_is_empty(s->path) == 0; | |
351 | break; | |
352 | ||
353 | case PATH_CHANGED: { | |
354 | bool b; | |
355 | ||
356 | b = access(s->path, F_OK) >= 0; | |
357 | good = !initial && b != s->previous_exists; | |
358 | s->previous_exists = b; | |
359 | break; | |
360 | } | |
361 | ||
362 | default: | |
363 | ; | |
364 | } | |
365 | ||
366 | if (good) | |
367 | break; | |
368 | } | |
369 | ||
370 | if (good) { | |
371 | path_enter_running(p); | |
372 | return; | |
373 | } | |
374 | ||
375 | if ((r = path_watch(p)) < 0) | |
376 | goto fail; | |
377 | ||
378 | path_set_state(p, PATH_WAITING); | |
379 | return; | |
380 | ||
381 | fail: | |
382 | log_warning("%s failed to enter waiting state: %s", p->meta.id, strerror(-r)); | |
383 | path_enter_dead(p, false); | |
384 | } | |
385 | ||
386 | static int path_start(Unit *u) { | |
387 | Path *p = PATH(u); | |
388 | ||
389 | assert(p); | |
18c78fb1 | 390 | assert(p->state == PATH_DEAD || p->state == PATH_MAINTENANCE); |
01f78473 LP |
391 | |
392 | if (p->unit->meta.load_state != UNIT_LOADED) | |
393 | return -ENOENT; | |
394 | ||
395 | p->failure = false; | |
396 | path_enter_waiting(p, true); | |
397 | return 0; | |
398 | } | |
399 | ||
400 | static int path_stop(Unit *u) { | |
401 | Path *p = PATH(u); | |
402 | ||
403 | assert(p); | |
404 | assert(p->state == PATH_WAITING || p->state == PATH_RUNNING); | |
405 | ||
406 | path_enter_dead(p, true); | |
407 | return 0; | |
408 | } | |
409 | ||
410 | static int path_serialize(Unit *u, FILE *f, FDSet *fds) { | |
411 | Path *p = PATH(u); | |
412 | ||
413 | assert(u); | |
414 | assert(f); | |
415 | assert(fds); | |
416 | ||
417 | unit_serialize_item(u, f, "state", path_state_to_string(p->state)); | |
418 | ||
419 | return 0; | |
420 | } | |
421 | ||
422 | static int path_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
423 | Path *p = PATH(u); | |
424 | ||
425 | assert(u); | |
426 | assert(key); | |
427 | assert(value); | |
428 | assert(fds); | |
429 | ||
430 | if (streq(key, "state")) { | |
431 | PathState state; | |
432 | ||
433 | if ((state = path_state_from_string(value)) < 0) | |
434 | log_debug("Failed to parse state value %s", value); | |
435 | else | |
436 | p->deserialized_state = state; | |
437 | } else | |
438 | log_debug("Unknown serialization key '%s'", key); | |
439 | ||
440 | return 0; | |
441 | } | |
442 | ||
443 | static UnitActiveState path_active_state(Unit *u) { | |
444 | assert(u); | |
445 | ||
446 | return state_translation_table[PATH(u)->state]; | |
447 | } | |
448 | ||
449 | static const char *path_sub_state_to_string(Unit *u) { | |
450 | assert(u); | |
451 | ||
452 | return path_state_to_string(PATH(u)->state); | |
453 | } | |
454 | ||
455 | static void path_fd_event(Unit *u, int fd, uint32_t events, Watch *w) { | |
456 | Path *p = PATH(u); | |
457 | int l; | |
458 | ssize_t k; | |
459 | struct inotify_event *buf = NULL; | |
460 | PathSpec *s; | |
461 | ||
462 | assert(p); | |
463 | assert(fd >= 0); | |
464 | ||
465 | if (p->state != PATH_WAITING) | |
466 | return; | |
467 | ||
468 | log_debug("inotify wakeup on %s.", u->meta.id); | |
469 | ||
470 | if (events != EPOLLIN) { | |
471 | log_error("Got Invalid poll event on inotify."); | |
472 | goto fail; | |
473 | } | |
474 | ||
475 | LIST_FOREACH(spec, s, p->specs) | |
476 | if (s->inotify_fd == fd) | |
477 | break; | |
478 | ||
479 | if (!s) { | |
480 | log_error("Got event on unknown fd."); | |
481 | goto fail; | |
482 | } | |
483 | ||
484 | if (ioctl(fd, FIONREAD, &l) < 0) { | |
485 | log_error("FIONREAD failed: %s", strerror(errno)); | |
486 | goto fail; | |
487 | } | |
488 | ||
489 | if (!(buf = malloc(l))) { | |
490 | log_error("Failed to allocate buffer: %s", strerror(-ENOMEM)); | |
491 | goto fail; | |
492 | } | |
493 | ||
494 | if ((k = read(fd, buf, l)) < 0) { | |
495 | log_error("Failed to read inotify event: %s", strerror(-errno)); | |
496 | goto fail; | |
497 | } | |
498 | ||
499 | if ((size_t) k < sizeof(struct inotify_event) || | |
500 | (size_t) k < sizeof(struct inotify_event) + buf->len) { | |
501 | log_error("inotify event too small."); | |
502 | goto fail; | |
503 | } | |
504 | ||
505 | if (s->type == PATH_CHANGED && s->primary_wd == buf->wd) | |
506 | path_enter_running(p); | |
507 | else | |
508 | path_enter_waiting(p, false); | |
509 | ||
510 | free(buf); | |
511 | ||
512 | return; | |
513 | ||
514 | fail: | |
515 | free(buf); | |
516 | path_enter_dead(p, false); | |
517 | } | |
518 | ||
519 | void path_unit_notify(Unit *u, UnitActiveState new_state) { | |
520 | char *n; | |
521 | int r; | |
522 | Iterator i; | |
523 | ||
524 | if (u->meta.type == UNIT_PATH) | |
525 | return; | |
526 | ||
527 | SET_FOREACH(n, u->meta.names, i) { | |
528 | char *k; | |
529 | Unit *t; | |
530 | Path *p; | |
531 | ||
532 | if (!(k = unit_name_change_suffix(n, ".path"))) { | |
533 | r = -ENOMEM; | |
534 | goto fail; | |
535 | } | |
536 | ||
537 | t = manager_get_unit(u->meta.manager, k); | |
538 | free(k); | |
539 | ||
540 | if (!t) | |
541 | continue; | |
542 | ||
543 | if (t->meta.load_state != UNIT_LOADED) | |
544 | continue; | |
545 | ||
546 | p = PATH(t); | |
547 | ||
548 | if (p->unit != u) | |
549 | continue; | |
550 | ||
551 | if (p->state == PATH_RUNNING && new_state == UNIT_INACTIVE) { | |
552 | log_debug("%s got notified about unit deactivation.", p->meta.id); | |
553 | path_enter_waiting(p, false); | |
554 | } | |
555 | } | |
556 | ||
557 | return; | |
558 | ||
559 | fail: | |
560 | log_error("Failed find path unit: %s", strerror(-r)); | |
561 | } | |
562 | ||
5632e374 LP |
563 | static void path_reset_maintenance(Unit *u) { |
564 | Path *p = PATH(u); | |
565 | ||
566 | assert(p); | |
567 | ||
568 | if (p->state == PATH_MAINTENANCE) | |
569 | path_set_state(p, PATH_DEAD); | |
570 | ||
571 | p->failure = false; | |
572 | } | |
573 | ||
01f78473 LP |
574 | static const char* const path_state_table[_PATH_STATE_MAX] = { |
575 | [PATH_DEAD] = "dead", | |
576 | [PATH_WAITING] = "waiting", | |
577 | [PATH_RUNNING] = "running", | |
18c78fb1 | 578 | [PATH_MAINTENANCE] = "maintenance" |
01f78473 LP |
579 | }; |
580 | ||
581 | DEFINE_STRING_TABLE_LOOKUP(path_state, PathState); | |
582 | ||
583 | static const char* const path_type_table[_PATH_TYPE_MAX] = { | |
584 | [PATH_EXISTS] = "PathExists", | |
585 | [PATH_CHANGED] = "PathChanged", | |
586 | [PATH_DIRECTORY_NOT_EMPTY] = "DirectoryNotEmpty" | |
587 | }; | |
588 | ||
589 | DEFINE_STRING_TABLE_LOOKUP(path_type, PathType); | |
590 | ||
591 | const UnitVTable path_vtable = { | |
592 | .suffix = ".path", | |
593 | ||
594 | .done = path_done, | |
595 | .load = path_load, | |
596 | ||
597 | .coldplug = path_coldplug, | |
598 | ||
599 | .dump = path_dump, | |
600 | ||
601 | .start = path_start, | |
602 | .stop = path_stop, | |
603 | ||
604 | .serialize = path_serialize, | |
605 | .deserialize_item = path_deserialize_item, | |
606 | ||
607 | .active_state = path_active_state, | |
608 | .sub_state_to_string = path_sub_state_to_string, | |
609 | ||
610 | .fd_event = path_fd_event, | |
611 | ||
5632e374 LP |
612 | .reset_maintenance = path_reset_maintenance, |
613 | ||
01f78473 LP |
614 | .bus_message_handler = bus_path_message_handler |
615 | }; |