1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/epoll.h>
23 #include <sys/inotify.h>
26 #include "bus-error.h"
28 #include "dbus-path.h"
31 #include "glob-util.h"
36 #include "stat-util.h"
37 #include "string-table.h"
38 #include "string-util.h"
39 #include "unit-name.h"
42 static const UnitActiveState state_translation_table
[_PATH_STATE_MAX
] = {
43 [PATH_DEAD
] = UNIT_INACTIVE
,
44 [PATH_WAITING
] = UNIT_ACTIVE
,
45 [PATH_RUNNING
] = UNIT_ACTIVE
,
46 [PATH_FAILED
] = UNIT_FAILED
49 static int path_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
);
51 int path_spec_watch(PathSpec
*s
, sd_event_io_handler_t handler
) {
53 static const int flags_table
[_PATH_TYPE_MAX
] = {
54 [PATH_EXISTS
] = IN_DELETE_SELF
|IN_MOVE_SELF
|IN_ATTRIB
,
55 [PATH_EXISTS_GLOB
] = IN_DELETE_SELF
|IN_MOVE_SELF
|IN_ATTRIB
,
56 [PATH_CHANGED
] = IN_DELETE_SELF
|IN_MOVE_SELF
|IN_ATTRIB
|IN_CLOSE_WRITE
|IN_CREATE
|IN_DELETE
|IN_MOVED_FROM
|IN_MOVED_TO
,
57 [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
,
58 [PATH_DIRECTORY_NOT_EMPTY
] = IN_DELETE_SELF
|IN_MOVE_SELF
|IN_ATTRIB
|IN_CREATE
|IN_MOVED_TO
62 char *slash
, *oldslash
= NULL
;
71 s
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
72 if (s
->inotify_fd
< 0) {
77 r
= sd_event_add_io(s
->unit
->manager
->event
, &s
->event_source
, s
->inotify_fd
, EPOLLIN
, handler
, s
);
81 (void) sd_event_source_set_description(s
->event_source
, "path");
83 /* This function assumes the path was passed through path_kill_slashes()! */
84 assert(!strstr(s
->path
, "//"));
86 for (slash
= strchr(s
->path
, '/'); ; slash
= strchr(slash
+1, '/')) {
92 cut
= slash
+ (slash
== s
->path
);
96 flags
= IN_MOVE_SELF
| IN_DELETE_SELF
| IN_ATTRIB
| IN_CREATE
| IN_MOVED_TO
;
98 flags
= flags_table
[s
->type
];
100 r
= inotify_add_watch(s
->inotify_fd
, s
->path
, flags
);
102 if (IN_SET(errno
, EACCES
, ENOENT
)) {
108 r
= log_warning_errno(errno
, "Failed to add watch on %s: %s", s
->path
, errno
== ENOSPC
? "too many watches" : strerror(-r
));
115 /* Path exists, we don't need to watch parent too closely. */
117 char *cut2
= oldslash
+ (oldslash
== s
->path
);
121 (void) inotify_add_watch(s
->inotify_fd
, s
->path
, IN_MOVE_SELF
);
122 /* Error is ignored, the worst can happen is we get spurious events. */
134 /* whole path has been iterated over */
141 r
= log_error_errno(errno
, "Failed to add watch on any of the components of %s: %m", s
->path
);
142 /* either EACCESS or ENOENT */
149 path_spec_unwatch(s
);
153 void path_spec_unwatch(PathSpec
*s
) {
156 s
->event_source
= sd_event_source_unref(s
->event_source
);
157 s
->inotify_fd
= safe_close(s
->inotify_fd
);
160 int path_spec_fd_event(PathSpec
*s
, uint32_t revents
) {
161 union inotify_event_buffer buffer
;
162 struct inotify_event
*e
;
166 if (revents
!= EPOLLIN
) {
167 log_error("Got invalid poll event on inotify.");
171 l
= read(s
->inotify_fd
, &buffer
, sizeof(buffer
));
173 if (IN_SET(errno
, EAGAIN
, EINTR
))
176 return log_error_errno(errno
, "Failed to read inotify event: %m");
179 FOREACH_INOTIFY_EVENT(e
, buffer
, l
) {
180 if (IN_SET(s
->type
, PATH_CHANGED
, PATH_MODIFIED
) &&
181 s
->primary_wd
== e
->wd
)
188 static bool path_spec_check_good(PathSpec
*s
, bool initial
) {
194 good
= access(s
->path
, F_OK
) >= 0;
197 case PATH_EXISTS_GLOB
:
198 good
= glob_exists(s
->path
) > 0;
201 case PATH_DIRECTORY_NOT_EMPTY
: {
204 k
= dir_is_empty(s
->path
);
205 good
= !(k
== -ENOENT
|| k
> 0);
210 case PATH_MODIFIED
: {
213 b
= access(s
->path
, F_OK
) >= 0;
214 good
= !initial
&& b
!= s
->previous_exists
;
215 s
->previous_exists
= b
;
226 static void path_spec_mkdir(PathSpec
*s
, mode_t mode
) {
229 if (IN_SET(s
->type
, PATH_EXISTS
, PATH_EXISTS_GLOB
))
232 r
= mkdir_p_label(s
->path
, mode
);
234 log_warning_errno(r
, "mkdir(%s) failed: %m", s
->path
);
237 static void path_spec_dump(PathSpec
*s
, FILE *f
, const char *prefix
) {
241 path_type_to_string(s
->type
),
245 void path_spec_done(PathSpec
*s
) {
247 assert(s
->inotify_fd
== -1);
252 static void path_init(Unit
*u
) {
256 assert(u
->load_state
== UNIT_STUB
);
258 p
->directory_mode
= 0755;
261 void path_free_specs(Path
*p
) {
266 while ((s
= p
->specs
)) {
267 path_spec_unwatch(s
);
268 LIST_REMOVE(spec
, p
->specs
, s
);
274 static void path_done(Unit
*u
) {
282 static int path_add_mount_dependencies(Path
*p
) {
288 LIST_FOREACH(spec
, s
, p
->specs
) {
289 r
= unit_require_mounts_for(UNIT(p
), s
->path
, UNIT_DEPENDENCY_FILE
);
297 static int path_verify(Path
*p
) {
300 if (UNIT(p
)->load_state
!= UNIT_LOADED
)
304 log_unit_error(UNIT(p
), "Path unit lacks path setting. Refusing.");
311 static int path_add_default_dependencies(Path
*p
) {
316 if (!UNIT(p
)->default_dependencies
)
319 r
= unit_add_dependency_by_name(UNIT(p
), UNIT_BEFORE
, SPECIAL_PATHS_TARGET
, NULL
, true, UNIT_DEPENDENCY_DEFAULT
);
323 if (MANAGER_IS_SYSTEM(UNIT(p
)->manager
)) {
324 r
= unit_add_two_dependencies_by_name(UNIT(p
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, NULL
, true, UNIT_DEPENDENCY_DEFAULT
);
329 return unit_add_two_dependencies_by_name(UNIT(p
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true, UNIT_DEPENDENCY_DEFAULT
);
332 static int path_add_trigger_dependencies(Path
*p
) {
338 if (!hashmap_isempty(UNIT(p
)->dependencies
[UNIT_TRIGGERS
]))
341 r
= unit_load_related_unit(UNIT(p
), ".service", &x
);
345 return unit_add_two_dependencies(UNIT(p
), UNIT_BEFORE
, UNIT_TRIGGERS
, x
, true, UNIT_DEPENDENCY_IMPLICIT
);
348 static int path_load(Unit
*u
) {
353 assert(u
->load_state
== UNIT_STUB
);
355 r
= unit_load_fragment_and_dropin(u
);
359 if (u
->load_state
== UNIT_LOADED
) {
361 r
= path_add_trigger_dependencies(p
);
365 r
= path_add_mount_dependencies(p
);
369 r
= path_add_default_dependencies(p
);
374 return path_verify(p
);
377 static void path_dump(Unit
*u
, FILE *f
, const char *prefix
) {
385 trigger
= UNIT_TRIGGER(u
);
391 "%sMakeDirectory: %s\n"
392 "%sDirectoryMode: %04o\n",
393 prefix
, path_state_to_string(p
->state
),
394 prefix
, path_result_to_string(p
->result
),
395 prefix
, trigger
? trigger
->id
: "n/a",
396 prefix
, yes_no(p
->make_directory
),
397 prefix
, p
->directory_mode
);
399 LIST_FOREACH(spec
, s
, p
->specs
)
400 path_spec_dump(s
, f
, prefix
);
403 static void path_unwatch(Path
*p
) {
408 LIST_FOREACH(spec
, s
, p
->specs
)
409 path_spec_unwatch(s
);
412 static int path_watch(Path
*p
) {
418 LIST_FOREACH(spec
, s
, p
->specs
) {
419 r
= path_spec_watch(s
, path_dispatch_io
);
427 static void path_set_state(Path
*p
, PathState state
) {
431 old_state
= p
->state
;
434 if (state
!= PATH_WAITING
&&
435 (state
!= PATH_RUNNING
|| p
->inotify_triggered
))
438 if (state
!= old_state
)
439 log_unit_debug(UNIT(p
), "Changed %s -> %s", path_state_to_string(old_state
), path_state_to_string(state
));
441 unit_notify(UNIT(p
), state_translation_table
[old_state
], state_translation_table
[state
], true);
444 static void path_enter_waiting(Path
*p
, bool initial
, bool recheck
);
446 static int path_coldplug(Unit
*u
) {
450 assert(p
->state
== PATH_DEAD
);
452 if (p
->deserialized_state
!= p
->state
) {
454 if (IN_SET(p
->deserialized_state
, PATH_WAITING
, PATH_RUNNING
))
455 path_enter_waiting(p
, true, true);
457 path_set_state(p
, p
->deserialized_state
);
463 static void path_enter_dead(Path
*p
, PathResult f
) {
466 if (p
->result
== PATH_SUCCESS
)
469 if (p
->result
!= PATH_SUCCESS
)
470 log_unit_warning(UNIT(p
), "Failed with result '%s'.", path_result_to_string(p
->result
));
472 path_set_state(p
, p
->result
!= PATH_SUCCESS
? PATH_FAILED
: PATH_DEAD
);
475 static void path_enter_running(Path
*p
) {
476 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
482 /* Don't start job if we are supposed to go down */
483 if (unit_stop_pending(UNIT(p
)))
486 trigger
= UNIT_TRIGGER(UNIT(p
));
488 log_unit_error(UNIT(p
), "Unit to trigger vanished.");
489 path_enter_dead(p
, PATH_FAILURE_RESOURCES
);
493 r
= manager_add_job(UNIT(p
)->manager
, JOB_START
, trigger
, JOB_REPLACE
, &error
, NULL
);
497 p
->inotify_triggered
= false;
503 path_set_state(p
, PATH_RUNNING
);
507 log_unit_warning(UNIT(p
), "Failed to queue unit startup job: %s", bus_error_message(&error
, r
));
508 path_enter_dead(p
, PATH_FAILURE_RESOURCES
);
511 static bool path_check_good(Path
*p
, bool initial
) {
517 LIST_FOREACH(spec
, s
, p
->specs
) {
518 good
= path_spec_check_good(s
, initial
);
527 static void path_enter_waiting(Path
*p
, bool initial
, bool recheck
) {
531 if (path_check_good(p
, initial
)) {
532 log_unit_debug(UNIT(p
), "Got triggered.");
533 path_enter_running(p
);
541 /* Hmm, so now we have created inotify watches, but the file
542 * might have appeared/been removed by now, so we must
546 if (path_check_good(p
, false)) {
547 log_unit_debug(UNIT(p
), "Got triggered.");
548 path_enter_running(p
);
552 path_set_state(p
, PATH_WAITING
);
556 log_unit_warning_errno(UNIT(p
), r
, "Failed to enter waiting state: %m");
557 path_enter_dead(p
, PATH_FAILURE_RESOURCES
);
560 static void path_mkdir(Path
*p
) {
565 if (!p
->make_directory
)
568 LIST_FOREACH(spec
, s
, p
->specs
)
569 path_spec_mkdir(s
, p
->directory_mode
);
572 static int path_start(Unit
*u
) {
578 assert(IN_SET(p
->state
, PATH_DEAD
, PATH_FAILED
));
580 trigger
= UNIT_TRIGGER(u
);
581 if (!trigger
|| trigger
->load_state
!= UNIT_LOADED
) {
582 log_unit_error(u
, "Refusing to start, unit to trigger not loaded.");
586 r
= unit_start_limit_test(u
);
588 path_enter_dead(p
, PATH_FAILURE_START_LIMIT_HIT
);
592 r
= unit_acquire_invocation_id(u
);
598 p
->result
= PATH_SUCCESS
;
599 path_enter_waiting(p
, true, true);
604 static int path_stop(Unit
*u
) {
608 assert(IN_SET(p
->state
, PATH_WAITING
, PATH_RUNNING
));
610 path_enter_dead(p
, PATH_SUCCESS
);
614 static int path_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
621 unit_serialize_item(u
, f
, "state", path_state_to_string(p
->state
));
622 unit_serialize_item(u
, f
, "result", path_result_to_string(p
->result
));
627 static int path_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
635 if (streq(key
, "state")) {
638 state
= path_state_from_string(value
);
640 log_unit_debug(u
, "Failed to parse state value: %s", value
);
642 p
->deserialized_state
= state
;
644 } else if (streq(key
, "result")) {
647 f
= path_result_from_string(value
);
649 log_unit_debug(u
, "Failed to parse result value: %s", value
);
650 else if (f
!= PATH_SUCCESS
)
654 log_unit_debug(u
, "Unknown serialization key: %s", key
);
659 _pure_
static UnitActiveState
path_active_state(Unit
*u
) {
662 return state_translation_table
[PATH(u
)->state
];
665 _pure_
static const char *path_sub_state_to_string(Unit
*u
) {
668 return path_state_to_string(PATH(u
)->state
);
671 static int path_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
672 PathSpec
*s
= userdata
;
682 if (!IN_SET(p
->state
, PATH_WAITING
, PATH_RUNNING
))
685 /* log_debug("inotify wakeup on %s.", u->id); */
687 LIST_FOREACH(spec
, s
, p
->specs
)
688 if (path_spec_owns_inotify_fd(s
, fd
))
692 log_error("Got event on unknown fd.");
696 changed
= path_spec_fd_event(s
, revents
);
700 /* If we are already running, then remember that one event was
701 * dispatched so that we restart the service only if something
702 * actually changed on disk */
703 p
->inotify_triggered
= true;
706 path_enter_running(p
);
708 path_enter_waiting(p
, false, true);
713 path_enter_dead(p
, PATH_FAILURE_RESOURCES
);
717 static void path_trigger_notify(Unit
*u
, Unit
*other
) {
723 /* Invoked whenever the unit we trigger changes state or gains
726 if (other
->load_state
!= UNIT_LOADED
)
729 if (p
->state
== PATH_RUNNING
&&
730 UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other
))) {
731 log_unit_debug(UNIT(p
), "Got notified about unit deactivation.");
733 /* Hmm, so inotify was triggered since the
734 * last activation, so I guess we need to
735 * recheck what is going on. */
736 path_enter_waiting(p
, false, p
->inotify_triggered
);
740 static void path_reset_failed(Unit
*u
) {
745 if (p
->state
== PATH_FAILED
)
746 path_set_state(p
, PATH_DEAD
);
748 p
->result
= PATH_SUCCESS
;
751 static const char* const path_type_table
[_PATH_TYPE_MAX
] = {
752 [PATH_EXISTS
] = "PathExists",
753 [PATH_EXISTS_GLOB
] = "PathExistsGlob",
754 [PATH_DIRECTORY_NOT_EMPTY
] = "DirectoryNotEmpty",
755 [PATH_CHANGED
] = "PathChanged",
756 [PATH_MODIFIED
] = "PathModified",
759 DEFINE_STRING_TABLE_LOOKUP(path_type
, PathType
);
761 static const char* const path_result_table
[_PATH_RESULT_MAX
] = {
762 [PATH_SUCCESS
] = "success",
763 [PATH_FAILURE_RESOURCES
] = "resources",
764 [PATH_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
767 DEFINE_STRING_TABLE_LOOKUP(path_result
, PathResult
);
769 const UnitVTable path_vtable
= {
770 .object_size
= sizeof(Path
),
776 .private_section
= "Path",
778 .can_transient
= true,
784 .coldplug
= path_coldplug
,
791 .serialize
= path_serialize
,
792 .deserialize_item
= path_deserialize_item
,
794 .active_state
= path_active_state
,
795 .sub_state_to_string
= path_sub_state_to_string
,
797 .trigger_notify
= path_trigger_notify
,
799 .reset_failed
= path_reset_failed
,
801 .bus_vtable
= bus_path_vtable
,
802 .bus_set_property
= bus_path_set_property
,