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