1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
22 #include <sys/inotify.h>
23 #include <sys/epoll.h>
24 #include <sys/ioctl.h>
29 #include "unit-name.h"
31 #include "dbus-path.h"
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
,
38 [PATH_MAINTENANCE
] = UNIT_MAINTENANCE
41 static void path_done(Unit
*u
) {
47 while ((s
= p
->specs
)) {
48 LIST_REMOVE(PathSpec
, spec
, p
->specs
, s
);
53 int path_add_one_mount_link(Path
*p
, Mount
*m
) {
60 if (p
->meta
.load_state
!= UNIT_LOADED
||
61 m
->meta
.load_state
!= UNIT_LOADED
)
64 LIST_FOREACH(spec
, s
, p
->specs
) {
66 if (!path_startswith(s
->path
, m
->where
))
69 if ((r
= unit_add_two_dependencies(UNIT(p
), UNIT_AFTER
, UNIT_REQUIRES
, UNIT(m
), true)) < 0)
76 static int path_add_mount_links(Path
*p
) {
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)
89 static int path_verify(Path
*p
) {
92 if (p
->meta
.load_state
!= UNIT_LOADED
)
96 log_error("%s lacks path setting. Refusing.", p
->meta
.id
);
103 static int path_load(Unit
*u
) {
108 assert(u
->meta
.load_state
== UNIT_STUB
);
110 if ((r
= unit_load_fragment_and_dropin(u
)) < 0)
113 if (u
->meta
.load_state
== UNIT_LOADED
) {
116 if ((r
= unit_load_related_unit(u
, ".service", &p
->unit
)))
119 if ((r
= unit_add_dependency(u
, UNIT_BEFORE
, p
->unit
, true)) < 0)
122 if ((r
= path_add_mount_links(p
)) < 0)
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)
131 return path_verify(p
);
134 static void path_dump(Unit
*u
, FILE *f
, const char *prefix
) {
140 p2
= strappend(prefix
, "\t");
141 prefix2
= p2
? p2
: prefix
;
146 prefix
, path_state_to_string(p
->state
),
147 prefix
, p
->unit
->meta
.id
);
149 LIST_FOREACH(spec
, s
, p
->specs
)
153 path_type_to_string(s
->type
),
159 static void path_unwatch_one(Path
*p
, PathSpec
*s
) {
161 if (s
->inotify_fd
< 0)
164 unit_unwatch_fd(UNIT(p
), &s
->watch
);
166 close_nointr_nofail(s
->inotify_fd
);
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
184 path_unwatch_one(p
, s
);
186 if (!(k
= strdup(s
->path
)))
189 if ((s
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
)) < 0) {
194 if (unit_watch_fd(UNIT(p
), s
->inotify_fd
, EPOLLIN
, &s
->watch
) < 0) {
199 if ((s
->primary_wd
= inotify_add_watch(s
->inotify_fd
, k
, flags_table
[s
->type
])) >= 0)
206 /* This assumes the path was passed through path_kill_slashes()! */
207 if (!(slash
= strrchr(k
, '/')))
212 flags
= IN_DELETE_SELF
|IN_MOVE_SELF
;
214 flags
|= IN_CREATE
| IN_MOVED_TO
| IN_ATTRIB
;
216 if (inotify_add_watch(s
->inotify_fd
, k
, flags
) >= 0)
225 path_unwatch_one(p
, s
);
229 static void path_unwatch(Path
*p
) {
234 LIST_FOREACH(spec
, s
, p
->specs
)
235 path_unwatch_one(p
, s
);
238 static int path_watch(Path
*p
) {
244 LIST_FOREACH(spec
, s
, p
->specs
)
245 if ((r
= path_watch_one(p
, s
)) < 0)
251 static void path_set_state(Path
*p
, PathState state
) {
255 old_state
= p
->state
;
258 if (state
!= PATH_WAITING
)
261 if (state
!= old_state
)
262 log_debug("%s changed %s -> %s",
264 path_state_to_string(old_state
),
265 path_state_to_string(state
));
267 unit_notify(UNIT(p
), state_translation_table
[old_state
], state_translation_table
[state
]);
270 static void path_enter_waiting(Path
*p
, bool initial
);
272 static int path_coldplug(Unit
*u
) {
276 assert(p
->state
== PATH_DEAD
);
278 if (p
->deserialized_state
!= p
->state
) {
280 if (p
->deserialized_state
== PATH_WAITING
||
281 p
->deserialized_state
== PATH_RUNNING
)
282 path_enter_waiting(p
, true);
284 path_set_state(p
, p
->deserialized_state
);
290 static void path_enter_dead(Path
*p
, bool success
) {
296 path_set_state(p
, p
->failure
? PATH_MAINTENANCE
: PATH_DEAD
);
299 static void path_enter_running(Path
*p
) {
303 if ((r
= manager_add_job(p
->meta
.manager
, JOB_START
, p
->unit
, JOB_REPLACE
, true, NULL
)) < 0)
306 path_set_state(p
, PATH_RUNNING
);
310 log_warning("%s failed to queue unit startup job: %s", p
->meta
.id
, strerror(-r
));
311 path_enter_dead(p
, false);
315 static void path_enter_waiting(Path
*p
, bool initial
) {
320 LIST_FOREACH(spec
, s
, p
->specs
) {
325 good
= access(s
->path
, F_OK
) >= 0;
328 case PATH_DIRECTORY_NOT_EMPTY
:
329 good
= dir_is_empty(s
->path
) == 0;
335 b
= access(s
->path
, F_OK
) >= 0;
336 good
= !initial
&& b
!= s
->previous_exists
;
337 s
->previous_exists
= b
;
350 path_enter_running(p
);
354 if ((r
= path_watch(p
)) < 0)
357 path_set_state(p
, PATH_WAITING
);
361 log_warning("%s failed to enter waiting state: %s", p
->meta
.id
, strerror(-r
));
362 path_enter_dead(p
, false);
365 static int path_start(Unit
*u
) {
369 assert(p
->state
== PATH_DEAD
|| p
->state
== PATH_MAINTENANCE
);
371 if (p
->unit
->meta
.load_state
!= UNIT_LOADED
)
375 path_enter_waiting(p
, true);
379 static int path_stop(Unit
*u
) {
383 assert(p
->state
== PATH_WAITING
|| p
->state
== PATH_RUNNING
);
385 path_enter_dead(p
, true);
389 static int path_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
396 unit_serialize_item(u
, f
, "state", path_state_to_string(p
->state
));
401 static int path_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
409 if (streq(key
, "state")) {
412 if ((state
= path_state_from_string(value
)) < 0)
413 log_debug("Failed to parse state value %s", value
);
415 p
->deserialized_state
= state
;
417 log_debug("Unknown serialization key '%s'", key
);
422 static UnitActiveState
path_active_state(Unit
*u
) {
425 return state_translation_table
[PATH(u
)->state
];
428 static const char *path_sub_state_to_string(Unit
*u
) {
431 return path_state_to_string(PATH(u
)->state
);
434 static void path_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
438 struct inotify_event
*buf
= NULL
;
444 if (p
->state
!= PATH_WAITING
)
447 log_debug("inotify wakeup on %s.", u
->meta
.id
);
449 if (events
!= EPOLLIN
) {
450 log_error("Got Invalid poll event on inotify.");
454 LIST_FOREACH(spec
, s
, p
->specs
)
455 if (s
->inotify_fd
== fd
)
459 log_error("Got event on unknown fd.");
463 if (ioctl(fd
, FIONREAD
, &l
) < 0) {
464 log_error("FIONREAD failed: %s", strerror(errno
));
468 if (!(buf
= malloc(l
))) {
469 log_error("Failed to allocate buffer: %s", strerror(-ENOMEM
));
473 if ((k
= read(fd
, buf
, l
)) < 0) {
474 log_error("Failed to read inotify event: %s", strerror(-errno
));
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.");
484 if (s
->type
== PATH_CHANGED
&& s
->primary_wd
== buf
->wd
)
485 path_enter_running(p
);
487 path_enter_waiting(p
, false);
495 path_enter_dead(p
, false);
498 void path_unit_notify(Unit
*u
, UnitActiveState new_state
) {
503 if (u
->meta
.type
== UNIT_PATH
)
506 SET_FOREACH(n
, u
->meta
.names
, i
) {
511 if (!(k
= unit_name_change_suffix(n
, ".path"))) {
516 t
= manager_get_unit(u
->meta
.manager
, k
);
522 if (t
->meta
.load_state
!= UNIT_LOADED
)
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);
539 log_error("Failed find path unit: %s", strerror(-r
));
542 static const char* const path_state_table
[_PATH_STATE_MAX
] = {
543 [PATH_DEAD
] = "dead",
544 [PATH_WAITING
] = "waiting",
545 [PATH_RUNNING
] = "running",
546 [PATH_MAINTENANCE
] = "maintenance"
549 DEFINE_STRING_TABLE_LOOKUP(path_state
, PathState
);
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"
557 DEFINE_STRING_TABLE_LOOKUP(path_type
, PathType
);
559 const UnitVTable path_vtable
= {
565 .coldplug
= path_coldplug
,
572 .serialize
= path_serialize
,
573 .deserialize_item
= path_deserialize_item
,
575 .active_state
= path_active_state
,
576 .sub_state_to_string
= path_sub_state_to_string
,
578 .fd_event
= path_fd_event
,
580 .bus_message_handler
= bus_path_message_handler