chiark / gitweb /
notify: add missing include
[elogind.git] / src / path.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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"
32 #include "special.h"
33 #include "bus-errors.h"
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,
39         [PATH_FAILED] = UNIT_FAILED
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
70                 if ((r = unit_add_two_dependencies(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
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
93         if (p->meta.load_state != UNIT_LOADED)
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
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_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true);
114 }
115
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;
137
138                 if (p->meta.default_dependencies)
139                         if ((r = path_add_default_dependencies(p)) < 0)
140                                 return r;
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         PathSpec *s;
149
150         assert(p);
151         assert(f);
152
153         fprintf(f,
154                 "%sPath State: %s\n"
155                 "%sUnit: %s\n",
156                 prefix, path_state_to_string(p->state),
157                 prefix, p->unit->meta.id);
158
159         LIST_FOREACH(spec, s, p->specs)
160                 fprintf(f,
161                         "%s%s: %s\n",
162                         prefix,
163                         path_type_to_string(s->type),
164                         s->path);
165 }
166
167 static void path_unwatch_one(Path *p, PathSpec *s) {
168
169         if (s->inotify_fd < 0)
170                 return;
171
172         unit_unwatch_fd(UNIT(p), &s->watch);
173
174         close_nointr_nofail(s->inotify_fd);
175         s->inotify_fd = -1;
176 }
177
178 static int path_watch_one(Path *p, PathSpec *s) {
179         static const int flags_table[_PATH_TYPE_MAX] = {
180                 [PATH_EXISTS] = IN_DELETE_SELF|IN_MOVE_SELF,
181                 [PATH_CHANGED] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO,
182                 [PATH_DIRECTORY_NOT_EMPTY] = IN_DELETE_SELF|IN_MOVE_SELF|IN_CREATE|IN_MOVED_TO
183         };
184
185         bool exists = false;
186         char *k;
187         int r;
188
189         assert(p);
190         assert(s);
191
192         path_unwatch_one(p, s);
193
194         if (!(k = strdup(s->path)))
195                 return -ENOMEM;
196
197         if ((s->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC)) < 0) {
198                 r = -errno;
199                 goto fail;
200         }
201
202         if (unit_watch_fd(UNIT(p), s->inotify_fd, EPOLLIN, &s->watch) < 0) {
203                 r = -errno;
204                 goto fail;
205         }
206
207         if ((s->primary_wd = inotify_add_watch(s->inotify_fd, k, flags_table[s->type])) >= 0)
208                 exists = true;
209
210         for (;;) {
211                 int flags;
212                 char *slash;
213
214                 /* This assumes the path was passed through path_kill_slashes()! */
215                 if (!(slash = strrchr(k, '/')))
216                         break;
217
218                 *slash = 0;
219
220                 flags = IN_DELETE_SELF|IN_MOVE_SELF;
221                 if (!exists)
222                         flags |= IN_CREATE | IN_MOVED_TO | IN_ATTRIB;
223
224                 if (inotify_add_watch(s->inotify_fd, k, flags) >= 0)
225                         exists = true;
226         }
227
228         return 0;
229
230 fail:
231         free(k);
232
233         path_unwatch_one(p, s);
234         return r;
235 }
236
237 static void path_unwatch(Path *p) {
238         PathSpec *s;
239
240         assert(p);
241
242         LIST_FOREACH(spec, s, p->specs)
243                 path_unwatch_one(p, s);
244 }
245
246 static int path_watch(Path *p) {
247         int r;
248         PathSpec *s;
249
250         assert(p);
251
252         LIST_FOREACH(spec, s, p->specs)
253                 if ((r = path_watch_one(p, s)) < 0)
254                         return r;
255
256         return 0;
257 }
258
259 static void path_set_state(Path *p, PathState state) {
260         PathState old_state;
261         assert(p);
262
263         old_state = p->state;
264         p->state = state;
265
266         if (state != PATH_WAITING)
267                 path_unwatch(p);
268
269         if (state != old_state)
270                 log_debug("%s changed %s -> %s",
271                           p->meta.id,
272                           path_state_to_string(old_state),
273                           path_state_to_string(state));
274
275         unit_notify(UNIT(p), state_translation_table[old_state], state_translation_table[state]);
276 }
277
278 static void path_enter_waiting(Path *p, bool initial);
279
280 static int path_coldplug(Unit *u) {
281         Path *p = PATH(u);
282
283         assert(p);
284         assert(p->state == PATH_DEAD);
285
286         if (p->deserialized_state != p->state) {
287
288                 if (p->deserialized_state == PATH_WAITING ||
289                     p->deserialized_state == PATH_RUNNING)
290                         path_enter_waiting(p, true);
291                 else
292                         path_set_state(p, p->deserialized_state);
293         }
294
295         return 0;
296 }
297
298 static void path_enter_dead(Path *p, bool success) {
299         assert(p);
300
301         if (!success)
302                 p->failure = true;
303
304         path_set_state(p, p->failure ? PATH_FAILED : PATH_DEAD);
305 }
306
307 static void path_enter_running(Path *p) {
308         int r;
309         DBusError error;
310
311         assert(p);
312         dbus_error_init(&error);
313
314         /* Don't start job if we are supposed to go down */
315         if (p->meta.job && p->meta.job->type == JOB_STOP)
316                 return;
317
318         if ((r = manager_add_job(p->meta.manager, JOB_START, p->unit, JOB_REPLACE, true, &error, NULL)) < 0)
319                 goto fail;
320
321         path_set_state(p, PATH_RUNNING);
322         return;
323
324 fail:
325         log_warning("%s failed to queue unit startup job: %s", p->meta.id, bus_error(&error, r));
326         path_enter_dead(p, false);
327
328         dbus_error_free(&error);
329 }
330
331
332 static void path_enter_waiting(Path *p, bool initial) {
333         PathSpec *s;
334         int r;
335         bool good = false;
336
337         LIST_FOREACH(spec, s, p->specs) {
338
339                 switch (s->type) {
340
341                 case PATH_EXISTS:
342                         good = access(s->path, F_OK) >= 0;
343                         break;
344
345                 case PATH_DIRECTORY_NOT_EMPTY:
346                         good = dir_is_empty(s->path) == 0;
347                         break;
348
349                 case PATH_CHANGED: {
350                         bool b;
351
352                         b = access(s->path, F_OK) >= 0;
353                         good = !initial && b != s->previous_exists;
354                         s->previous_exists = b;
355                         break;
356                 }
357
358                 default:
359                         ;
360                 }
361
362                 if (good)
363                         break;
364         }
365
366         if (good) {
367                 path_enter_running(p);
368                 return;
369         }
370
371         if ((r = path_watch(p)) < 0)
372                 goto fail;
373
374         path_set_state(p, PATH_WAITING);
375         return;
376
377 fail:
378         log_warning("%s failed to enter waiting state: %s", p->meta.id, strerror(-r));
379         path_enter_dead(p, false);
380 }
381
382 static int path_start(Unit *u) {
383         Path *p = PATH(u);
384
385         assert(p);
386         assert(p->state == PATH_DEAD || p->state == PATH_FAILED);
387
388         if (p->unit->meta.load_state != UNIT_LOADED)
389                 return -ENOENT;
390
391         p->failure = false;
392 path_enter_waiting(p, true);
393         return 0;
394 }
395
396 static int path_stop(Unit *u) {
397         Path *p = PATH(u);
398
399         assert(p);
400         assert(p->state == PATH_WAITING || p->state == PATH_RUNNING);
401
402         path_enter_dead(p, true);
403         return 0;
404 }
405
406 static int path_serialize(Unit *u, FILE *f, FDSet *fds) {
407         Path *p = PATH(u);
408
409         assert(u);
410         assert(f);
411         assert(fds);
412
413         unit_serialize_item(u, f, "state", path_state_to_string(p->state));
414
415         return 0;
416 }
417
418 static int path_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
419         Path *p = PATH(u);
420
421         assert(u);
422         assert(key);
423         assert(value);
424         assert(fds);
425
426         if (streq(key, "state")) {
427                 PathState state;
428
429                 if ((state = path_state_from_string(value)) < 0)
430                         log_debug("Failed to parse state value %s", value);
431                 else
432                         p->deserialized_state = state;
433         } else
434                 log_debug("Unknown serialization key '%s'", key);
435
436         return 0;
437 }
438
439 static UnitActiveState path_active_state(Unit *u) {
440         assert(u);
441
442         return state_translation_table[PATH(u)->state];
443 }
444
445 static const char *path_sub_state_to_string(Unit *u) {
446         assert(u);
447
448         return path_state_to_string(PATH(u)->state);
449 }
450
451 static void path_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
452         Path *p = PATH(u);
453         int l;
454         ssize_t k;
455         struct inotify_event *buf = NULL;
456         PathSpec *s;
457
458         assert(p);
459         assert(fd >= 0);
460
461         if (p->state != PATH_WAITING)
462                 return;
463
464         log_debug("inotify wakeup on %s.", u->meta.id);
465
466         if (events != EPOLLIN) {
467                 log_error("Got Invalid poll event on inotify.");
468                 goto fail;
469         }
470
471         LIST_FOREACH(spec, s, p->specs)
472                 if (s->inotify_fd == fd)
473                         break;
474
475         if (!s) {
476                 log_error("Got event on unknown fd.");
477                 goto fail;
478         }
479
480         if (ioctl(fd, FIONREAD, &l) < 0) {
481                 log_error("FIONREAD failed: %s", strerror(errno));
482                 goto fail;
483         }
484
485         if (!(buf = malloc(l))) {
486                 log_error("Failed to allocate buffer: %s", strerror(-ENOMEM));
487                 goto fail;
488         }
489
490         if ((k = read(fd, buf, l)) < 0) {
491                 log_error("Failed to read inotify event: %s", strerror(-errno));
492                 goto fail;
493         }
494
495         if ((size_t) k < sizeof(struct inotify_event) ||
496             (size_t) k < sizeof(struct inotify_event) + buf->len) {
497                 log_error("inotify event too small.");
498                 goto fail;
499         }
500
501         if (s->type == PATH_CHANGED && s->primary_wd == buf->wd)
502                 path_enter_running(p);
503         else
504                 path_enter_waiting(p, false);
505
506         free(buf);
507
508         return;
509
510 fail:
511         free(buf);
512         path_enter_dead(p, false);
513 }
514
515 void path_unit_notify(Unit *u, UnitActiveState new_state) {
516         char *n;
517         int r;
518         Iterator i;
519
520         if (u->meta.type == UNIT_PATH)
521                 return;
522
523         SET_FOREACH(n, u->meta.names, i) {
524                 char *k;
525                 Unit *t;
526                 Path *p;
527
528                 if (!(k = unit_name_change_suffix(n, ".path"))) {
529                         r = -ENOMEM;
530                         goto fail;
531                 }
532
533                 t = manager_get_unit(u->meta.manager, k);
534                 free(k);
535
536                 if (!t)
537                         continue;
538
539                 if (t->meta.load_state != UNIT_LOADED)
540                         continue;
541
542                 p = PATH(t);
543
544                 if (p->unit != u)
545                         continue;
546
547                 if (p->state == PATH_RUNNING && new_state == UNIT_INACTIVE) {
548                         log_debug("%s got notified about unit deactivation.", p->meta.id);
549                         path_enter_waiting(p, false);
550                 }
551         }
552
553         return;
554
555 fail:
556         log_error("Failed find path unit: %s", strerror(-r));
557 }
558
559 static void path_reset_failed(Unit *u) {
560         Path *p = PATH(u);
561
562         assert(p);
563
564         if (p->state == PATH_FAILED)
565                 path_set_state(p, PATH_DEAD);
566
567         p->failure = false;
568 }
569
570 static const char* const path_state_table[_PATH_STATE_MAX] = {
571         [PATH_DEAD] = "dead",
572         [PATH_WAITING] = "waiting",
573         [PATH_RUNNING] = "running",
574         [PATH_FAILED] = "failed"
575 };
576
577 DEFINE_STRING_TABLE_LOOKUP(path_state, PathState);
578
579 static const char* const path_type_table[_PATH_TYPE_MAX] = {
580         [PATH_EXISTS] = "PathExists",
581         [PATH_CHANGED] = "PathChanged",
582         [PATH_DIRECTORY_NOT_EMPTY] = "DirectoryNotEmpty"
583 };
584
585 DEFINE_STRING_TABLE_LOOKUP(path_type, PathType);
586
587 const UnitVTable path_vtable = {
588         .suffix = ".path",
589
590         .done = path_done,
591         .load = path_load,
592
593         .coldplug = path_coldplug,
594
595         .dump = path_dump,
596
597         .start = path_start,
598         .stop = path_stop,
599
600         .serialize = path_serialize,
601         .deserialize_item = path_deserialize_item,
602
603         .active_state = path_active_state,
604         .sub_state_to_string = path_sub_state_to_string,
605
606         .fd_event = path_fd_event,
607
608         .reset_failed = path_reset_failed,
609
610         .bus_interface = "org.freedesktop.systemd1.Path",
611         .bus_message_handler = bus_path_message_handler
612 };