1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
10 #include <sys/inotify.h>
13 #include "bus-error.h"
15 #include "dbus-path.h"
18 #include "glob-util.h"
23 #include "stat-util.h"
24 #include "string-table.h"
25 #include "string-util.h"
26 #include "unit-name.h"
29 static const UnitActiveState state_translation_table
[_PATH_STATE_MAX
] = {
30 [PATH_DEAD
] = UNIT_INACTIVE
,
31 [PATH_WAITING
] = UNIT_ACTIVE
,
32 [PATH_RUNNING
] = UNIT_ACTIVE
,
33 [PATH_FAILED
] = UNIT_FAILED
36 static int path_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
);
38 int path_spec_watch(PathSpec
*s
, sd_event_io_handler_t handler
) {
40 static const int flags_table
[_PATH_TYPE_MAX
] = {
41 [PATH_EXISTS
] = IN_DELETE_SELF
|IN_MOVE_SELF
|IN_ATTRIB
,
42 [PATH_EXISTS_GLOB
] = IN_DELETE_SELF
|IN_MOVE_SELF
|IN_ATTRIB
,
43 [PATH_CHANGED
] = IN_DELETE_SELF
|IN_MOVE_SELF
|IN_ATTRIB
|IN_CLOSE_WRITE
|IN_CREATE
|IN_DELETE
|IN_MOVED_FROM
|IN_MOVED_TO
,
44 [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
,
45 [PATH_DIRECTORY_NOT_EMPTY
] = IN_DELETE_SELF
|IN_MOVE_SELF
|IN_ATTRIB
|IN_CREATE
|IN_MOVED_TO
49 char *slash
, *oldslash
= NULL
;
58 s
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
59 if (s
->inotify_fd
< 0) {
64 r
= sd_event_add_io(s
->unit
->manager
->event
, &s
->event_source
, s
->inotify_fd
, EPOLLIN
, handler
, s
);
68 (void) sd_event_source_set_description(s
->event_source
, "path");
70 /* This function assumes the path was passed through path_simplify()! */
71 assert(!strstr(s
->path
, "//"));
73 for (slash
= strchr(s
->path
, '/'); ; slash
= strchr(slash
+1, '/')) {
79 cut
= slash
+ (slash
== s
->path
);
83 flags
= IN_MOVE_SELF
| IN_DELETE_SELF
| IN_ATTRIB
| IN_CREATE
| IN_MOVED_TO
;
85 flags
= flags_table
[s
->type
];
87 r
= inotify_add_watch(s
->inotify_fd
, s
->path
, flags
);
89 if (IN_SET(errno
, EACCES
, ENOENT
)) {
95 r
= log_warning_errno(errno
, "Failed to add watch on %s: %s", s
->path
, errno
== ENOSPC
? "too many watches" : strerror(-r
));
102 /* Path exists, we don't need to watch parent too closely. */
104 char *cut2
= oldslash
+ (oldslash
== s
->path
);
108 (void) inotify_add_watch(s
->inotify_fd
, s
->path
, IN_MOVE_SELF
);
109 /* Error is ignored, the worst can happen is we get spurious events. */
121 /* whole path has been iterated over */
128 r
= log_error_errno(errno
, "Failed to add watch on any of the components of %s: %m", s
->path
);
129 /* either EACCESS or ENOENT */
136 path_spec_unwatch(s
);
140 void path_spec_unwatch(PathSpec
*s
) {
143 s
->event_source
= sd_event_source_unref(s
->event_source
);
144 s
->inotify_fd
= safe_close(s
->inotify_fd
);
147 int path_spec_fd_event(PathSpec
*s
, uint32_t revents
) {
148 union inotify_event_buffer buffer
;
149 struct inotify_event
*e
;
153 if (revents
!= EPOLLIN
) {
154 log_error("Got invalid poll event on inotify.");
158 l
= read(s
->inotify_fd
, &buffer
, sizeof(buffer
));
160 if (IN_SET(errno
, EAGAIN
, EINTR
))
163 return log_error_errno(errno
, "Failed to read inotify event: %m");
166 FOREACH_INOTIFY_EVENT(e
, buffer
, l
) {
167 if (IN_SET(s
->type
, PATH_CHANGED
, PATH_MODIFIED
) &&
168 s
->primary_wd
== e
->wd
)
175 static bool path_spec_check_good(PathSpec
*s
, bool initial
) {
181 good
= access(s
->path
, F_OK
) >= 0;
184 case PATH_EXISTS_GLOB
:
185 good
= glob_exists(s
->path
) > 0;
188 case PATH_DIRECTORY_NOT_EMPTY
: {
191 k
= dir_is_empty(s
->path
);
192 good
= !(k
== -ENOENT
|| k
> 0);
197 case PATH_MODIFIED
: {
200 b
= access(s
->path
, F_OK
) >= 0;
201 good
= !initial
&& b
!= s
->previous_exists
;
202 s
->previous_exists
= b
;
213 static void path_spec_mkdir(PathSpec
*s
, mode_t mode
) {
216 if (IN_SET(s
->type
, PATH_EXISTS
, PATH_EXISTS_GLOB
))
219 r
= mkdir_p_label(s
->path
, mode
);
221 log_warning_errno(r
, "mkdir(%s) failed: %m", s
->path
);
224 static void path_spec_dump(PathSpec
*s
, FILE *f
, const char *prefix
) {
228 path_type_to_string(s
->type
),
232 void path_spec_done(PathSpec
*s
) {
234 assert(s
->inotify_fd
== -1);
239 static void path_init(Unit
*u
) {
243 assert(u
->load_state
== UNIT_STUB
);
245 p
->directory_mode
= 0755;
248 void path_free_specs(Path
*p
) {
253 while ((s
= p
->specs
)) {
254 path_spec_unwatch(s
);
255 LIST_REMOVE(spec
, p
->specs
, s
);
261 static void path_done(Unit
*u
) {
269 static int path_add_mount_dependencies(Path
*p
) {
275 LIST_FOREACH(spec
, s
, p
->specs
) {
276 r
= unit_require_mounts_for(UNIT(p
), s
->path
, UNIT_DEPENDENCY_FILE
);
284 static int path_verify(Path
*p
) {
287 if (UNIT(p
)->load_state
!= UNIT_LOADED
)
291 log_unit_error(UNIT(p
), "Path unit lacks path setting. Refusing.");
298 static int path_add_default_dependencies(Path
*p
) {
303 if (!UNIT(p
)->default_dependencies
)
306 r
= unit_add_dependency_by_name(UNIT(p
), UNIT_BEFORE
, SPECIAL_PATHS_TARGET
, NULL
, true, UNIT_DEPENDENCY_DEFAULT
);
310 if (MANAGER_IS_SYSTEM(UNIT(p
)->manager
)) {
311 r
= unit_add_two_dependencies_by_name(UNIT(p
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, NULL
, true, UNIT_DEPENDENCY_DEFAULT
);
316 return unit_add_two_dependencies_by_name(UNIT(p
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true, UNIT_DEPENDENCY_DEFAULT
);
319 static int path_add_trigger_dependencies(Path
*p
) {
325 if (!hashmap_isempty(UNIT(p
)->dependencies
[UNIT_TRIGGERS
]))
328 r
= unit_load_related_unit(UNIT(p
), ".service", &x
);
332 return unit_add_two_dependencies(UNIT(p
), UNIT_BEFORE
, UNIT_TRIGGERS
, x
, true, UNIT_DEPENDENCY_IMPLICIT
);
335 static int path_load(Unit
*u
) {
340 assert(u
->load_state
== UNIT_STUB
);
342 r
= unit_load_fragment_and_dropin(u
);
346 if (u
->load_state
== UNIT_LOADED
) {
348 r
= path_add_trigger_dependencies(p
);
352 r
= path_add_mount_dependencies(p
);
356 r
= path_add_default_dependencies(p
);
361 return path_verify(p
);
364 static void path_dump(Unit
*u
, FILE *f
, const char *prefix
) {
372 trigger
= UNIT_TRIGGER(u
);
378 "%sMakeDirectory: %s\n"
379 "%sDirectoryMode: %04o\n",
380 prefix
, path_state_to_string(p
->state
),
381 prefix
, path_result_to_string(p
->result
),
382 prefix
, trigger
? trigger
->id
: "n/a",
383 prefix
, yes_no(p
->make_directory
),
384 prefix
, p
->directory_mode
);
386 LIST_FOREACH(spec
, s
, p
->specs
)
387 path_spec_dump(s
, f
, prefix
);
390 static void path_unwatch(Path
*p
) {
395 LIST_FOREACH(spec
, s
, p
->specs
)
396 path_spec_unwatch(s
);
399 static int path_watch(Path
*p
) {
405 LIST_FOREACH(spec
, s
, p
->specs
) {
406 r
= path_spec_watch(s
, path_dispatch_io
);
414 static void path_set_state(Path
*p
, PathState state
) {
418 old_state
= p
->state
;
421 if (state
!= PATH_WAITING
&&
422 (state
!= PATH_RUNNING
|| p
->inotify_triggered
))
425 if (state
!= old_state
)
426 log_unit_debug(UNIT(p
), "Changed %s -> %s", path_state_to_string(old_state
), path_state_to_string(state
));
428 unit_notify(UNIT(p
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
431 static void path_enter_waiting(Path
*p
, bool initial
, bool recheck
);
433 static int path_coldplug(Unit
*u
) {
437 assert(p
->state
== PATH_DEAD
);
439 if (p
->deserialized_state
!= p
->state
) {
441 if (IN_SET(p
->deserialized_state
, PATH_WAITING
, PATH_RUNNING
))
442 path_enter_waiting(p
, true, true);
444 path_set_state(p
, p
->deserialized_state
);
450 static void path_enter_dead(Path
*p
, PathResult f
) {
453 if (p
->result
== PATH_SUCCESS
)
456 if (p
->result
!= PATH_SUCCESS
)
457 log_unit_warning(UNIT(p
), "Failed with result '%s'.", path_result_to_string(p
->result
));
459 path_set_state(p
, p
->result
!= PATH_SUCCESS
? PATH_FAILED
: PATH_DEAD
);
462 static void path_enter_running(Path
*p
) {
463 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
469 /* Don't start job if we are supposed to go down */
470 if (unit_stop_pending(UNIT(p
)))
473 trigger
= UNIT_TRIGGER(UNIT(p
));
475 log_unit_error(UNIT(p
), "Unit to trigger vanished.");
476 path_enter_dead(p
, PATH_FAILURE_RESOURCES
);
480 r
= manager_add_job(UNIT(p
)->manager
, JOB_START
, trigger
, JOB_REPLACE
, &error
, NULL
);
484 p
->inotify_triggered
= false;
490 path_set_state(p
, PATH_RUNNING
);
494 log_unit_warning(UNIT(p
), "Failed to queue unit startup job: %s", bus_error_message(&error
, r
));
495 path_enter_dead(p
, PATH_FAILURE_RESOURCES
);
498 static bool path_check_good(Path
*p
, bool initial
) {
504 LIST_FOREACH(spec
, s
, p
->specs
) {
505 good
= path_spec_check_good(s
, initial
);
514 static void path_enter_waiting(Path
*p
, bool initial
, bool recheck
) {
518 if (path_check_good(p
, initial
)) {
519 log_unit_debug(UNIT(p
), "Got triggered.");
520 path_enter_running(p
);
528 /* Hmm, so now we have created inotify watches, but the file
529 * might have appeared/been removed by now, so we must
533 if (path_check_good(p
, false)) {
534 log_unit_debug(UNIT(p
), "Got triggered.");
535 path_enter_running(p
);
539 path_set_state(p
, PATH_WAITING
);
543 log_unit_warning_errno(UNIT(p
), r
, "Failed to enter waiting state: %m");
544 path_enter_dead(p
, PATH_FAILURE_RESOURCES
);
547 static void path_mkdir(Path
*p
) {
552 if (!p
->make_directory
)
555 LIST_FOREACH(spec
, s
, p
->specs
)
556 path_spec_mkdir(s
, p
->directory_mode
);
559 static int path_start(Unit
*u
) {
565 assert(IN_SET(p
->state
, PATH_DEAD
, PATH_FAILED
));
567 trigger
= UNIT_TRIGGER(u
);
568 if (!trigger
|| trigger
->load_state
!= UNIT_LOADED
) {
569 log_unit_error(u
, "Refusing to start, unit to trigger not loaded.");
573 r
= unit_start_limit_test(u
);
575 path_enter_dead(p
, PATH_FAILURE_START_LIMIT_HIT
);
579 r
= unit_acquire_invocation_id(u
);
585 p
->result
= PATH_SUCCESS
;
586 path_enter_waiting(p
, true, true);
591 static int path_stop(Unit
*u
) {
595 assert(IN_SET(p
->state
, PATH_WAITING
, PATH_RUNNING
));
597 path_enter_dead(p
, PATH_SUCCESS
);
601 static int path_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
608 unit_serialize_item(u
, f
, "state", path_state_to_string(p
->state
));
609 unit_serialize_item(u
, f
, "result", path_result_to_string(p
->result
));
614 static int path_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
622 if (streq(key
, "state")) {
625 state
= path_state_from_string(value
);
627 log_unit_debug(u
, "Failed to parse state value: %s", value
);
629 p
->deserialized_state
= state
;
631 } else if (streq(key
, "result")) {
634 f
= path_result_from_string(value
);
636 log_unit_debug(u
, "Failed to parse result value: %s", value
);
637 else if (f
!= PATH_SUCCESS
)
641 log_unit_debug(u
, "Unknown serialization key: %s", key
);
646 _pure_
static UnitActiveState
path_active_state(Unit
*u
) {
649 return state_translation_table
[PATH(u
)->state
];
652 _pure_
static const char *path_sub_state_to_string(Unit
*u
) {
655 return path_state_to_string(PATH(u
)->state
);
658 static int path_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
659 PathSpec
*s
= userdata
;
669 if (!IN_SET(p
->state
, PATH_WAITING
, PATH_RUNNING
))
672 /* log_debug("inotify wakeup on %s.", u->id); */
674 LIST_FOREACH(spec
, s
, p
->specs
)
675 if (path_spec_owns_inotify_fd(s
, fd
))
679 log_error("Got event on unknown fd.");
683 changed
= path_spec_fd_event(s
, revents
);
687 /* If we are already running, then remember that one event was
688 * dispatched so that we restart the service only if something
689 * actually changed on disk */
690 p
->inotify_triggered
= true;
693 path_enter_running(p
);
695 path_enter_waiting(p
, false, true);
700 path_enter_dead(p
, PATH_FAILURE_RESOURCES
);
704 static void path_trigger_notify(Unit
*u
, Unit
*other
) {
710 /* Invoked whenever the unit we trigger changes state or gains
713 if (other
->load_state
!= UNIT_LOADED
)
716 if (p
->state
== PATH_RUNNING
&&
717 UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other
))) {
718 log_unit_debug(UNIT(p
), "Got notified about unit deactivation.");
720 /* Hmm, so inotify was triggered since the
721 * last activation, so I guess we need to
722 * recheck what is going on. */
723 path_enter_waiting(p
, false, p
->inotify_triggered
);
727 static void path_reset_failed(Unit
*u
) {
732 if (p
->state
== PATH_FAILED
)
733 path_set_state(p
, PATH_DEAD
);
735 p
->result
= PATH_SUCCESS
;
738 static const char* const path_type_table
[_PATH_TYPE_MAX
] = {
739 [PATH_EXISTS
] = "PathExists",
740 [PATH_EXISTS_GLOB
] = "PathExistsGlob",
741 [PATH_DIRECTORY_NOT_EMPTY
] = "DirectoryNotEmpty",
742 [PATH_CHANGED
] = "PathChanged",
743 [PATH_MODIFIED
] = "PathModified",
746 DEFINE_STRING_TABLE_LOOKUP(path_type
, PathType
);
748 static const char* const path_result_table
[_PATH_RESULT_MAX
] = {
749 [PATH_SUCCESS
] = "success",
750 [PATH_FAILURE_RESOURCES
] = "resources",
751 [PATH_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
754 DEFINE_STRING_TABLE_LOOKUP(path_result
, PathResult
);
756 const UnitVTable path_vtable
= {
757 .object_size
= sizeof(Path
),
763 .private_section
= "Path",
765 .can_transient
= true,
771 .coldplug
= path_coldplug
,
778 .serialize
= path_serialize
,
779 .deserialize_item
= path_deserialize_item
,
781 .active_state
= path_active_state
,
782 .sub_state_to_string
= path_sub_state_to_string
,
784 .trigger_notify
= path_trigger_notify
,
786 .reset_failed
= path_reset_failed
,
788 .bus_vtable
= bus_path_vtable
,
789 .bus_set_property
= bus_path_set_property
,