1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include <sys/mount.h>
27 #include <sys/epoll.h>
29 #include <linux/auto_fs4.h>
30 #include <linux/auto_dev-ioctl.h>
33 #include "automount.h"
34 #include "load-fragment.h"
35 #include "load-dropin.h"
36 #include "unit-name.h"
37 #include "dbus-automount.h"
38 #include "bus-errors.h"
42 #include "path-util.h"
43 #include "dbus-common.h"
45 static const UnitActiveState state_translation_table[_AUTOMOUNT_STATE_MAX] = {
46 [AUTOMOUNT_DEAD] = UNIT_INACTIVE,
47 [AUTOMOUNT_WAITING] = UNIT_ACTIVE,
48 [AUTOMOUNT_RUNNING] = UNIT_ACTIVE,
49 [AUTOMOUNT_FAILED] = UNIT_FAILED
52 static int open_dev_autofs(Manager *m);
54 static void automount_init(Unit *u) {
55 Automount *a = AUTOMOUNT(u);
58 assert(u->load_state == UNIT_STUB);
60 a->pipe_watch.fd = a->pipe_fd = -1;
61 a->pipe_watch.type = WATCH_INVALID;
63 a->directory_mode = 0755;
65 UNIT(a)->ignore_on_isolate = true;
68 static void repeat_unmout(const char *path) {
72 /* If there are multiple mounts on a mount point, this
75 if (umount2(path, MNT_DETACH) >= 0)
79 log_error("Failed to unmount: %m");
85 static void unmount_autofs(Automount *a) {
91 automount_send_ready(a, -EHOSTDOWN);
93 unit_unwatch_fd(UNIT(a), &a->pipe_watch);
94 close_nointr_nofail(a->pipe_fd);
97 /* If we reload/reexecute things we keep the mount point
100 (UNIT(a)->manager->exit_code != MANAGER_RELOAD &&
101 UNIT(a)->manager->exit_code != MANAGER_REEXECUTE))
102 repeat_unmout(a->where);
105 static void automount_done(Unit *u) {
106 Automount *a = AUTOMOUNT(u);
111 unit_ref_unset(&a->mount);
120 int automount_add_one_mount_link(Automount *a, Mount *m) {
126 if (UNIT(a)->load_state != UNIT_LOADED ||
127 UNIT(m)->load_state != UNIT_LOADED)
130 if (!path_startswith(a->where, m->where))
133 if (path_equal(a->where, m->where))
136 r = unit_add_two_dependencies(UNIT(a), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true);
143 static int automount_add_mount_links(Automount *a) {
149 LIST_FOREACH(units_by_type, other, UNIT(a)->manager->units_by_type[UNIT_MOUNT]) {
150 r = automount_add_one_mount_link(a, MOUNT(other));
158 static int automount_add_default_dependencies(Automount *a) {
163 if (UNIT(a)->manager->running_as != SYSTEMD_SYSTEM)
166 r = unit_add_two_dependencies_by_name(UNIT(a), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
173 static int automount_verify(Automount *a) {
178 if (UNIT(a)->load_state != UNIT_LOADED)
181 if (path_equal(a->where, "/")) {
182 log_error("Cannot have an automount unit for the root directory. Refusing.");
186 e = unit_name_from_path(a->where, ".automount");
190 b = unit_has_name(UNIT(a), e);
194 log_error("%s's Where setting doesn't match unit name. Refusing.", UNIT(a)->id);
201 static int automount_load(Unit *u) {
203 Automount *a = AUTOMOUNT(u);
206 assert(u->load_state == UNIT_STUB);
208 /* Load a .automount file */
209 r = unit_load_fragment_and_dropin_optional(u);
213 if (u->load_state == UNIT_LOADED) {
217 a->where = unit_name_to_path(u->id);
222 path_kill_slashes(a->where);
224 r = automount_add_mount_links(a);
228 r = unit_load_related_unit(u, ".mount", &x);
232 unit_ref_set(&a->mount, x);
234 r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(a->mount), true);
238 if (UNIT(a)->default_dependencies) {
239 r = automount_add_default_dependencies(a);
245 return automount_verify(a);
248 static void automount_set_state(Automount *a, AutomountState state) {
249 AutomountState old_state;
252 old_state = a->state;
255 if (state != AUTOMOUNT_WAITING &&
256 state != AUTOMOUNT_RUNNING)
259 if (state != old_state)
260 log_debug("%s changed %s -> %s",
262 automount_state_to_string(old_state),
263 automount_state_to_string(state));
265 unit_notify(UNIT(a), state_translation_table[old_state], state_translation_table[state], true);
268 static int automount_coldplug(Unit *u) {
269 Automount *a = AUTOMOUNT(u);
273 assert(a->state == AUTOMOUNT_DEAD);
275 if (a->deserialized_state != a->state) {
277 r = open_dev_autofs(u->manager);
281 if (a->deserialized_state == AUTOMOUNT_WAITING ||
282 a->deserialized_state == AUTOMOUNT_RUNNING) {
284 assert(a->pipe_fd >= 0);
286 r = unit_watch_fd(UNIT(a), a->pipe_fd, EPOLLIN, &a->pipe_watch);
291 automount_set_state(a, a->deserialized_state);
297 static void automount_dump(Unit *u, FILE *f, const char *prefix) {
298 Automount *a = AUTOMOUNT(u);
303 "%sAutomount State: %s\n"
306 "%sDirectoryMode: %04o\n",
307 prefix, automount_state_to_string(a->state),
308 prefix, automount_result_to_string(a->result),
310 prefix, a->directory_mode);
313 static void automount_enter_dead(Automount *a, AutomountResult f) {
316 if (f != AUTOMOUNT_SUCCESS)
319 automount_set_state(a, a->result != AUTOMOUNT_SUCCESS ? AUTOMOUNT_FAILED : AUTOMOUNT_DEAD);
322 static int open_dev_autofs(Manager *m) {
323 struct autofs_dev_ioctl param;
327 if (m->dev_autofs_fd >= 0)
328 return m->dev_autofs_fd;
330 label_fix("/dev/autofs", false, false);
332 m->dev_autofs_fd = open("/dev/autofs", O_CLOEXEC|O_RDONLY);
333 if (m->dev_autofs_fd < 0) {
334 log_error("Failed to open /dev/autofs: %s", strerror(errno));
338 init_autofs_dev_ioctl(¶m);
339 if (ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, ¶m) < 0) {
340 close_nointr_nofail(m->dev_autofs_fd);
341 m->dev_autofs_fd = -1;
345 log_debug("Autofs kernel version %i.%i", param.ver_major, param.ver_minor);
347 return m->dev_autofs_fd;
350 static int open_ioctl_fd(int dev_autofs_fd, const char *where, dev_t devid) {
351 struct autofs_dev_ioctl *param;
354 assert(dev_autofs_fd >= 0);
357 l = sizeof(struct autofs_dev_ioctl) + strlen(where) + 1;
360 init_autofs_dev_ioctl(param);
363 param->openmount.devid = devid;
364 strcpy(param->path, where);
366 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_OPENMOUNT, param) < 0)
369 if (param->ioctlfd < 0)
372 fd_cloexec(param->ioctlfd, true);
373 return param->ioctlfd;
376 static int autofs_protocol(int dev_autofs_fd, int ioctl_fd) {
377 uint32_t major, minor;
378 struct autofs_dev_ioctl param;
380 assert(dev_autofs_fd >= 0);
381 assert(ioctl_fd >= 0);
383 init_autofs_dev_ioctl(¶m);
384 param.ioctlfd = ioctl_fd;
386 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOVER, ¶m) < 0)
389 major = param.protover.version;
391 init_autofs_dev_ioctl(¶m);
392 param.ioctlfd = ioctl_fd;
394 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOSUBVER, ¶m) < 0)
397 minor = param.protosubver.sub_version;
399 log_debug("Autofs protocol version %i.%i", major, minor);
403 static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, time_t sec) {
404 struct autofs_dev_ioctl param;
406 assert(dev_autofs_fd >= 0);
407 assert(ioctl_fd >= 0);
409 init_autofs_dev_ioctl(¶m);
410 param.ioctlfd = ioctl_fd;
411 param.timeout.timeout = sec;
413 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, ¶m) < 0)
419 static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, int status) {
420 struct autofs_dev_ioctl param;
422 assert(dev_autofs_fd >= 0);
423 assert(ioctl_fd >= 0);
425 init_autofs_dev_ioctl(¶m);
426 param.ioctlfd = ioctl_fd;
429 param.fail.token = token;
430 param.fail.status = status;
432 param.ready.token = token;
434 if (ioctl(dev_autofs_fd, status ? AUTOFS_DEV_IOCTL_FAIL : AUTOFS_DEV_IOCTL_READY, ¶m) < 0)
440 int automount_send_ready(Automount *a, int status) {
447 if (set_isempty(a->tokens))
450 ioctl_fd = open_ioctl_fd(UNIT(a)->manager->dev_autofs_fd, a->where, a->dev_id);
457 log_debug("Sending failure: %s", strerror(-status));
459 log_debug("Sending success.");
463 /* Autofs thankfully does not hand out 0 as a token */
464 while ((token = PTR_TO_UINT(set_steal_first(a->tokens)))) {
467 /* Autofs fun fact II:
469 * if you pass a positive status code here, the kernel will
472 k = autofs_send_ready(UNIT(a)->manager->dev_autofs_fd,
482 close_nointr_nofail(ioctl_fd);
487 static void automount_enter_waiting(Automount *a) {
488 int p[2] = { -1, -1 };
489 char name[32], options[128];
490 bool mounted = false;
491 int r, ioctl_fd = -1, dev_autofs_fd;
495 assert(a->pipe_fd < 0);
499 set_clear(a->tokens);
501 dev_autofs_fd = open_dev_autofs(UNIT(a)->manager);
502 if (dev_autofs_fd < 0) {
507 /* We knowingly ignore the results of this call */
508 mkdir_p_label(a->where, 0555);
510 if (dir_is_empty(a->where) <= 0)
511 log_notice("%s: Directory %s to mount over is not empty, mounting anyway. (To see the over-mounted files, please manually mount the underlying file system to a secondary location.)", a->meta.id, a->where);
513 if (pipe2(p, O_NONBLOCK|O_CLOEXEC) < 0) {
518 snprintf(options, sizeof(options), "fd=%i,pgrp=%u,minproto=5,maxproto=5,direct", p[1], (unsigned) getpgrp());
519 char_array_0(options);
521 snprintf(name, sizeof(name), "systemd-%u", (unsigned) getpid());
524 if (mount(name, a->where, "autofs", 0, options) < 0) {
531 close_nointr_nofail(p[1]);
534 if (stat(a->where, &st) < 0) {
539 ioctl_fd = open_ioctl_fd(dev_autofs_fd, a->where, st.st_dev);
545 r = autofs_protocol(dev_autofs_fd, ioctl_fd);
549 r = autofs_set_timeout(dev_autofs_fd, ioctl_fd, 300);
555 * Unless we close the ioctl fd here, for some weird reason
556 * the direct mount will not receive events from the
559 close_nointr_nofail(ioctl_fd);
562 r = unit_watch_fd(UNIT(a), p[0], EPOLLIN, &a->pipe_watch);
567 a->dev_id = st.st_dev;
569 automount_set_state(a, AUTOMOUNT_WAITING);
574 assert_se(close_pipe(p) == 0);
577 close_nointr_nofail(ioctl_fd);
580 repeat_unmout(a->where);
582 log_error("Failed to initialize automounter: %s", strerror(-r));
583 automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
586 static void automount_enter_runnning(Automount *a) {
589 _cleanup_dbus_error_free_ DBusError error;
592 assert(UNIT_DEREF(a->mount));
594 dbus_error_init(&error);
596 /* We don't take mount requests anymore if we are supposed to
597 * shut down anyway */
598 if (unit_pending_inactive(UNIT(a))) {
599 log_debug("Suppressing automount request on %s since unit stop is scheduled.", UNIT(a)->id);
600 automount_send_ready(a, -EHOSTDOWN);
604 mkdir_p_label(a->where, a->directory_mode);
606 /* Before we do anything, let's see if somebody is playing games with us? */
607 if (lstat(a->where, &st) < 0) {
608 log_warning("%s failed to stat automount point: %m", UNIT(a)->id);
612 if (!S_ISDIR(st.st_mode) || st.st_dev != a->dev_id)
613 log_info("%s's automount point already active?", UNIT(a)->id);
614 else if ((r = manager_add_job(UNIT(a)->manager, JOB_START, UNIT_DEREF(a->mount), JOB_REPLACE, true, &error, NULL)) < 0) {
615 log_warning("%s failed to queue mount startup job: %s", UNIT(a)->id, bus_error(&error, r));
619 automount_set_state(a, AUTOMOUNT_RUNNING);
623 automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
626 static int automount_start(Unit *u) {
627 Automount *a = AUTOMOUNT(u);
630 assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_FAILED);
632 if (path_is_mount_point(a->where, false)) {
633 log_error("Path %s is already a mount point, refusing start for %s", a->where, u->id);
637 if (UNIT_DEREF(a->mount)->load_state != UNIT_LOADED)
640 a->result = AUTOMOUNT_SUCCESS;
641 automount_enter_waiting(a);
645 static int automount_stop(Unit *u) {
646 Automount *a = AUTOMOUNT(u);
649 assert(a->state == AUTOMOUNT_WAITING || a->state == AUTOMOUNT_RUNNING);
651 automount_enter_dead(a, AUTOMOUNT_SUCCESS);
655 static int automount_serialize(Unit *u, FILE *f, FDSet *fds) {
656 Automount *a = AUTOMOUNT(u);
664 unit_serialize_item(u, f, "state", automount_state_to_string(a->state));
665 unit_serialize_item(u, f, "result", automount_result_to_string(a->result));
666 unit_serialize_item_format(u, f, "dev-id", "%u", (unsigned) a->dev_id);
668 SET_FOREACH(p, a->tokens, i)
669 unit_serialize_item_format(u, f, "token", "%u", PTR_TO_UINT(p));
671 if (a->pipe_fd >= 0) {
674 copy = fdset_put_dup(fds, a->pipe_fd);
678 unit_serialize_item_format(u, f, "pipe-fd", "%i", copy);
684 static int automount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
685 Automount *a = AUTOMOUNT(u);
691 if (streq(key, "state")) {
692 AutomountState state;
694 state = automount_state_from_string(value);
696 log_debug("Failed to parse state value %s", value);
698 a->deserialized_state = state;
699 } else if (streq(key, "result")) {
702 f = automount_result_from_string(value);
704 log_debug("Failed to parse result value %s", value);
705 else if (f != AUTOMOUNT_SUCCESS)
708 } else if (streq(key, "dev-id")) {
711 if (safe_atou(value, &d) < 0)
712 log_debug("Failed to parse dev-id value %s", value);
714 a->dev_id = (unsigned) d;
715 } else if (streq(key, "token")) {
718 if (safe_atou(value, &token) < 0)
719 log_debug("Failed to parse token value %s", value);
722 if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func)))
725 r = set_put(a->tokens, UINT_TO_PTR(token));
729 } else if (streq(key, "pipe-fd")) {
732 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
733 log_debug("Failed to parse pipe-fd value %s", value);
736 close_nointr_nofail(a->pipe_fd);
738 a->pipe_fd = fdset_remove(fds, fd);
741 log_debug("Unknown serialization key '%s'", key);
746 static UnitActiveState automount_active_state(Unit *u) {
749 return state_translation_table[AUTOMOUNT(u)->state];
752 static const char *automount_sub_state_to_string(Unit *u) {
755 return automount_state_to_string(AUTOMOUNT(u)->state);
758 static bool automount_check_gc(Unit *u) {
759 Automount *a = AUTOMOUNT(u);
763 if (!UNIT_DEREF(a->mount))
766 return UNIT_VTABLE(UNIT_DEREF(a->mount))->check_gc(UNIT_DEREF(a->mount));
769 static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
770 Automount *a = AUTOMOUNT(u);
771 union autofs_v5_packet_union packet;
776 assert(fd == a->pipe_fd);
778 if (events != EPOLLIN) {
779 log_error("Got invalid poll event on pipe.");
783 l = loop_read(a->pipe_fd, &packet, sizeof(packet), true);
784 if (l != sizeof(packet)) {
785 log_error("Invalid read from pipe: %s", l < 0 ? strerror(-l) : "short read");
789 switch (packet.hdr.type) {
791 case autofs_ptype_missing_direct:
793 if (packet.v5_packet.pid > 0) {
794 _cleanup_free_ char *p = NULL;
796 get_process_comm(packet.v5_packet.pid, &p);
797 log_debug("Got direct mount request on %s, triggered by %lu (%s)",
798 a->where, (unsigned long) packet.v5_packet.pid, strna(p));
800 log_debug("Got direct mount request on %s", a->where);
802 r = set_ensure_allocated(&a->tokens, trivial_hash_func, trivial_compare_func);
804 log_error("Failed to allocate token set.");
808 r = set_put(a->tokens, UINT_TO_PTR(packet.v5_packet.wait_queue_token));
810 log_error("Failed to remember token: %s", strerror(-r));
814 automount_enter_runnning(a);
818 log_error("Received unknown automount request %i", packet.hdr.type);
825 automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
828 static void automount_shutdown(Manager *m) {
831 if (m->dev_autofs_fd >= 0)
832 close_nointr_nofail(m->dev_autofs_fd);
835 static void automount_reset_failed(Unit *u) {
836 Automount *a = AUTOMOUNT(u);
840 if (a->state == AUTOMOUNT_FAILED)
841 automount_set_state(a, AUTOMOUNT_DEAD);
843 a->result = AUTOMOUNT_SUCCESS;
846 static const char* const automount_state_table[_AUTOMOUNT_STATE_MAX] = {
847 [AUTOMOUNT_DEAD] = "dead",
848 [AUTOMOUNT_WAITING] = "waiting",
849 [AUTOMOUNT_RUNNING] = "running",
850 [AUTOMOUNT_FAILED] = "failed"
853 DEFINE_STRING_TABLE_LOOKUP(automount_state, AutomountState);
855 static const char* const automount_result_table[_AUTOMOUNT_RESULT_MAX] = {
856 [AUTOMOUNT_SUCCESS] = "success",
857 [AUTOMOUNT_FAILURE_RESOURCES] = "resources"
860 DEFINE_STRING_TABLE_LOOKUP(automount_result, AutomountResult);
862 const UnitVTable automount_vtable = {
863 .object_size = sizeof(Automount),
870 .no_instances = true,
872 .init = automount_init,
873 .load = automount_load,
874 .done = automount_done,
876 .coldplug = automount_coldplug,
878 .dump = automount_dump,
880 .start = automount_start,
881 .stop = automount_stop,
883 .serialize = automount_serialize,
884 .deserialize_item = automount_deserialize_item,
886 .active_state = automount_active_state,
887 .sub_state_to_string = automount_sub_state_to_string,
889 .check_gc = automount_check_gc,
891 .fd_event = automount_fd_event,
893 .reset_failed = automount_reset_failed,
895 .bus_interface = "org.freedesktop.systemd1.Automount",
896 .bus_message_handler = bus_automount_message_handler,
897 .bus_invalidating_properties = bus_automount_invalidating_properties,
899 .shutdown = automount_shutdown,
901 .status_message_formats = {
902 .finished_start_job = {
903 [JOB_DONE] = "Set up automount %s.",
904 [JOB_FAILED] = "Failed to set up automount %s.",
905 [JOB_DEPENDENCY] = "Dependency failed for %s.",
907 .finished_stop_job = {
908 [JOB_DONE] = "Unset automount %s.",
909 [JOB_FAILED] = "Failed to unset automount %s.",