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"
35 #include "load-fragment.h"
36 #include "load-dropin.h"
37 #include "unit-name.h"
38 #include "dbus-automount.h"
39 #include "bus-errors.h"
43 #include "path-util.h"
44 #include "dbus-common.h"
46 static const UnitActiveState state_translation_table[_AUTOMOUNT_STATE_MAX] = {
47 [AUTOMOUNT_DEAD] = UNIT_INACTIVE,
48 [AUTOMOUNT_WAITING] = UNIT_ACTIVE,
49 [AUTOMOUNT_RUNNING] = UNIT_ACTIVE,
50 [AUTOMOUNT_FAILED] = UNIT_FAILED
53 static int open_dev_autofs(Manager *m);
55 static void automount_init(Unit *u) {
56 Automount *a = AUTOMOUNT(u);
59 assert(u->load_state == UNIT_STUB);
61 a->pipe_watch.fd = a->pipe_fd = -1;
62 a->pipe_watch.type = WATCH_INVALID;
64 a->directory_mode = 0755;
66 UNIT(a)->ignore_on_isolate = true;
69 static void repeat_unmout(const char *path) {
73 /* If there are multiple mounts on a mount point, this
76 if (umount2(path, MNT_DETACH) >= 0)
80 log_error("Failed to unmount: %m");
86 static void unmount_autofs(Automount *a) {
92 automount_send_ready(a, -EHOSTDOWN);
94 unit_unwatch_fd(UNIT(a), &a->pipe_watch);
95 close_nointr_nofail(a->pipe_fd);
98 /* If we reload/reexecute things we keep the mount point
101 (UNIT(a)->manager->exit_code != MANAGER_RELOAD &&
102 UNIT(a)->manager->exit_code != MANAGER_REEXECUTE))
103 repeat_unmout(a->where);
106 static void automount_done(Unit *u) {
107 Automount *a = AUTOMOUNT(u);
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_unit(UNIT(a)->id, "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_unit(UNIT(a)->id, "%s's Where setting doesn't match unit name. Refusing.", UNIT(a)->id);
201 static int automount_load(Unit *u) {
202 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 = unit_load_related_unit(u, ".mount", &x);
228 r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, x, true);
232 r = automount_add_mount_links(a);
236 if (UNIT(a)->default_dependencies) {
237 r = automount_add_default_dependencies(a);
243 return automount_verify(a);
246 static void automount_set_state(Automount *a, AutomountState state) {
247 AutomountState old_state;
250 old_state = a->state;
253 if (state != AUTOMOUNT_WAITING &&
254 state != AUTOMOUNT_RUNNING)
257 if (state != old_state)
258 log_debug_unit(UNIT(a)->id,
259 "%s changed %s -> %s",
261 automount_state_to_string(old_state),
262 automount_state_to_string(state));
264 unit_notify(UNIT(a), state_translation_table[old_state], state_translation_table[state], true);
267 static int automount_coldplug(Unit *u) {
268 Automount *a = AUTOMOUNT(u);
272 assert(a->state == AUTOMOUNT_DEAD);
274 if (a->deserialized_state != a->state) {
276 r = open_dev_autofs(u->manager);
280 if (a->deserialized_state == AUTOMOUNT_WAITING ||
281 a->deserialized_state == AUTOMOUNT_RUNNING) {
283 assert(a->pipe_fd >= 0);
285 r = unit_watch_fd(UNIT(a), a->pipe_fd, EPOLLIN, &a->pipe_watch);
290 automount_set_state(a, a->deserialized_state);
296 static void automount_dump(Unit *u, FILE *f, const char *prefix) {
297 Automount *a = AUTOMOUNT(u);
302 "%sAutomount State: %s\n"
305 "%sDirectoryMode: %04o\n",
306 prefix, automount_state_to_string(a->state),
307 prefix, automount_result_to_string(a->result),
309 prefix, a->directory_mode);
312 static void automount_enter_dead(Automount *a, AutomountResult f) {
315 if (f != AUTOMOUNT_SUCCESS)
318 automount_set_state(a, a->result != AUTOMOUNT_SUCCESS ? AUTOMOUNT_FAILED : AUTOMOUNT_DEAD);
321 static int open_dev_autofs(Manager *m) {
322 struct autofs_dev_ioctl param;
326 if (m->dev_autofs_fd >= 0)
327 return m->dev_autofs_fd;
329 label_fix("/dev/autofs", false, false);
331 m->dev_autofs_fd = open("/dev/autofs", O_CLOEXEC|O_RDONLY);
332 if (m->dev_autofs_fd < 0) {
333 log_error("Failed to open /dev/autofs: %s", strerror(errno));
337 init_autofs_dev_ioctl(¶m);
338 if (ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, ¶m) < 0) {
339 close_nointr_nofail(m->dev_autofs_fd);
340 m->dev_autofs_fd = -1;
344 log_debug("Autofs kernel version %i.%i", param.ver_major, param.ver_minor);
346 return m->dev_autofs_fd;
349 static int open_ioctl_fd(int dev_autofs_fd, const char *where, dev_t devid) {
350 struct autofs_dev_ioctl *param;
353 assert(dev_autofs_fd >= 0);
356 l = sizeof(struct autofs_dev_ioctl) + strlen(where) + 1;
359 init_autofs_dev_ioctl(param);
362 param->openmount.devid = devid;
363 strcpy(param->path, where);
365 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_OPENMOUNT, param) < 0)
368 if (param->ioctlfd < 0)
371 fd_cloexec(param->ioctlfd, true);
372 return param->ioctlfd;
375 static int autofs_protocol(int dev_autofs_fd, int ioctl_fd) {
376 uint32_t major, minor;
377 struct autofs_dev_ioctl param;
379 assert(dev_autofs_fd >= 0);
380 assert(ioctl_fd >= 0);
382 init_autofs_dev_ioctl(¶m);
383 param.ioctlfd = ioctl_fd;
385 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOVER, ¶m) < 0)
388 major = param.protover.version;
390 init_autofs_dev_ioctl(¶m);
391 param.ioctlfd = ioctl_fd;
393 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOSUBVER, ¶m) < 0)
396 minor = param.protosubver.sub_version;
398 log_debug("Autofs protocol version %i.%i", major, minor);
402 static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, time_t sec) {
403 struct autofs_dev_ioctl param;
405 assert(dev_autofs_fd >= 0);
406 assert(ioctl_fd >= 0);
408 init_autofs_dev_ioctl(¶m);
409 param.ioctlfd = ioctl_fd;
410 param.timeout.timeout = sec;
412 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, ¶m) < 0)
418 static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, int status) {
419 struct autofs_dev_ioctl param;
421 assert(dev_autofs_fd >= 0);
422 assert(ioctl_fd >= 0);
424 init_autofs_dev_ioctl(¶m);
425 param.ioctlfd = ioctl_fd;
428 param.fail.token = token;
429 param.fail.status = status;
431 param.ready.token = token;
433 if (ioctl(dev_autofs_fd, status ? AUTOFS_DEV_IOCTL_FAIL : AUTOFS_DEV_IOCTL_READY, ¶m) < 0)
439 int automount_send_ready(Automount *a, int status) {
446 if (set_isempty(a->tokens))
449 ioctl_fd = open_ioctl_fd(UNIT(a)->manager->dev_autofs_fd, a->where, a->dev_id);
456 log_debug_unit(UNIT(a)->id, "Sending failure: %s", strerror(-status));
458 log_debug_unit(UNIT(a)->id, "Sending success.");
462 /* Autofs thankfully does not hand out 0 as a token */
463 while ((token = PTR_TO_UINT(set_steal_first(a->tokens)))) {
466 /* Autofs fun fact II:
468 * if you pass a positive status code here, the kernel will
471 k = autofs_send_ready(UNIT(a)->manager->dev_autofs_fd,
481 close_nointr_nofail(ioctl_fd);
486 static void automount_enter_waiting(Automount *a) {
487 int p[2] = { -1, -1 };
488 char name[32], options[128];
489 bool mounted = false;
490 int r, ioctl_fd = -1, dev_autofs_fd;
494 assert(a->pipe_fd < 0);
498 set_clear(a->tokens);
500 dev_autofs_fd = open_dev_autofs(UNIT(a)->manager);
501 if (dev_autofs_fd < 0) {
506 /* We knowingly ignore the results of this call */
507 mkdir_p_label(a->where, 0555);
509 warn_if_dir_nonempty(a->meta.id, a->where);
511 if (pipe2(p, O_NONBLOCK|O_CLOEXEC) < 0) {
516 snprintf(options, sizeof(options), "fd=%i,pgrp=%u,minproto=5,maxproto=5,direct", p[1], (unsigned) getpgrp());
517 char_array_0(options);
519 snprintf(name, sizeof(name), "systemd-%u", (unsigned) getpid());
522 if (mount(name, a->where, "autofs", 0, options) < 0) {
529 close_nointr_nofail(p[1]);
532 if (stat(a->where, &st) < 0) {
537 ioctl_fd = open_ioctl_fd(dev_autofs_fd, a->where, st.st_dev);
543 r = autofs_protocol(dev_autofs_fd, ioctl_fd);
547 r = autofs_set_timeout(dev_autofs_fd, ioctl_fd, 300);
553 * Unless we close the ioctl fd here, for some weird reason
554 * the direct mount will not receive events from the
557 close_nointr_nofail(ioctl_fd);
560 r = unit_watch_fd(UNIT(a), p[0], EPOLLIN, &a->pipe_watch);
565 a->dev_id = st.st_dev;
567 automount_set_state(a, AUTOMOUNT_WAITING);
572 assert_se(close_pipe(p) == 0);
575 close_nointr_nofail(ioctl_fd);
578 repeat_unmout(a->where);
580 log_error_unit(UNIT(a)->id,
581 "Failed to initialize automounter: %s", strerror(-r));
582 automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
585 static void automount_enter_runnning(Automount *a) {
586 _cleanup_dbus_error_free_ DBusError error;
592 dbus_error_init(&error);
594 /* We don't take mount requests anymore if we are supposed to
595 * shut down anyway */
596 if (unit_stop_pending(UNIT(a))) {
597 log_debug_unit(UNIT(a)->id,
598 "Suppressing automount request on %s since unit stop is scheduled.", UNIT(a)->id);
599 automount_send_ready(a, -EHOSTDOWN);
603 mkdir_p_label(a->where, a->directory_mode);
605 /* Before we do anything, let's see if somebody is playing games with us? */
606 if (lstat(a->where, &st) < 0) {
607 log_warning_unit(UNIT(a)->id,
608 "%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_unit(UNIT(a)->id,
614 "%s's automount point already active?", UNIT(a)->id);
616 r = manager_add_job(UNIT(a)->manager, JOB_START, UNIT_TRIGGER(UNIT(a)),
617 JOB_REPLACE, true, &error, NULL);
619 log_warning_unit(UNIT(a)->id,
620 "%s failed to queue mount startup job: %s",
621 UNIT(a)->id, bus_error(&error, r));
626 automount_set_state(a, AUTOMOUNT_RUNNING);
630 automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
633 static int automount_start(Unit *u) {
634 Automount *a = AUTOMOUNT(u);
637 assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_FAILED);
639 if (path_is_mount_point(a->where, false)) {
640 log_error_unit(u->id,
641 "Path %s is already a mount point, refusing start for %s",
646 if (UNIT_TRIGGER(u)->load_state != UNIT_LOADED)
649 a->result = AUTOMOUNT_SUCCESS;
650 automount_enter_waiting(a);
654 static int automount_stop(Unit *u) {
655 Automount *a = AUTOMOUNT(u);
658 assert(a->state == AUTOMOUNT_WAITING || a->state == AUTOMOUNT_RUNNING);
660 automount_enter_dead(a, AUTOMOUNT_SUCCESS);
664 static int automount_serialize(Unit *u, FILE *f, FDSet *fds) {
665 Automount *a = AUTOMOUNT(u);
673 unit_serialize_item(u, f, "state", automount_state_to_string(a->state));
674 unit_serialize_item(u, f, "result", automount_result_to_string(a->result));
675 unit_serialize_item_format(u, f, "dev-id", "%u", (unsigned) a->dev_id);
677 SET_FOREACH(p, a->tokens, i)
678 unit_serialize_item_format(u, f, "token", "%u", PTR_TO_UINT(p));
680 if (a->pipe_fd >= 0) {
683 copy = fdset_put_dup(fds, a->pipe_fd);
687 unit_serialize_item_format(u, f, "pipe-fd", "%i", copy);
693 static int automount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
694 Automount *a = AUTOMOUNT(u);
700 if (streq(key, "state")) {
701 AutomountState state;
703 state = automount_state_from_string(value);
705 log_debug_unit(u->id, "Failed to parse state value %s", value);
707 a->deserialized_state = state;
708 } else if (streq(key, "result")) {
711 f = automount_result_from_string(value);
713 log_debug_unit(u->id, "Failed to parse result value %s", value);
714 else if (f != AUTOMOUNT_SUCCESS)
717 } else if (streq(key, "dev-id")) {
720 if (safe_atou(value, &d) < 0)
721 log_debug_unit(u->id, "Failed to parse dev-id value %s", value);
723 a->dev_id = (unsigned) d;
724 } else if (streq(key, "token")) {
727 if (safe_atou(value, &token) < 0)
728 log_debug_unit(u->id, "Failed to parse token value %s", value);
731 if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func)))
734 r = set_put(a->tokens, UINT_TO_PTR(token));
738 } else if (streq(key, "pipe-fd")) {
741 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
742 log_debug_unit(u->id, "Failed to parse pipe-fd value %s", value);
745 close_nointr_nofail(a->pipe_fd);
747 a->pipe_fd = fdset_remove(fds, fd);
750 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
755 static UnitActiveState automount_active_state(Unit *u) {
758 return state_translation_table[AUTOMOUNT(u)->state];
761 static const char *automount_sub_state_to_string(Unit *u) {
764 return automount_state_to_string(AUTOMOUNT(u)->state);
767 static bool automount_check_gc(Unit *u) {
770 if (!UNIT_TRIGGER(u))
773 return UNIT_VTABLE(UNIT_TRIGGER(u))->check_gc(UNIT_TRIGGER(u));
776 static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
777 Automount *a = AUTOMOUNT(u);
778 union autofs_v5_packet_union packet;
783 assert(fd == a->pipe_fd);
785 if (events != EPOLLIN) {
786 log_error_unit(u->id, "Got invalid poll event on pipe.");
790 l = loop_read(a->pipe_fd, &packet, sizeof(packet), true);
791 if (l != sizeof(packet)) {
792 log_error_unit(u->id, "Invalid read from pipe: %s", l < 0 ? strerror(-l) : "short read");
796 switch (packet.hdr.type) {
798 case autofs_ptype_missing_direct:
800 if (packet.v5_packet.pid > 0) {
801 _cleanup_free_ char *p = NULL;
803 get_process_comm(packet.v5_packet.pid, &p);
804 log_debug_unit(u->id,
805 "Got direct mount request on %s, triggered by %lu (%s)",
806 a->where, (unsigned long) packet.v5_packet.pid, strna(p));
808 log_debug_unit(u->id, "Got direct mount request on %s", a->where);
810 r = set_ensure_allocated(&a->tokens, trivial_hash_func, trivial_compare_func);
812 log_error_unit(u->id, "Failed to allocate token set.");
816 r = set_put(a->tokens, UINT_TO_PTR(packet.v5_packet.wait_queue_token));
818 log_error_unit(u->id, "Failed to remember token: %s", strerror(-r));
822 automount_enter_runnning(a);
826 log_error_unit(u->id, "Received unknown automount request %i", packet.hdr.type);
833 automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
836 static void automount_shutdown(Manager *m) {
839 if (m->dev_autofs_fd >= 0)
840 close_nointr_nofail(m->dev_autofs_fd);
843 static void automount_reset_failed(Unit *u) {
844 Automount *a = AUTOMOUNT(u);
848 if (a->state == AUTOMOUNT_FAILED)
849 automount_set_state(a, AUTOMOUNT_DEAD);
851 a->result = AUTOMOUNT_SUCCESS;
854 static const char* const automount_state_table[_AUTOMOUNT_STATE_MAX] = {
855 [AUTOMOUNT_DEAD] = "dead",
856 [AUTOMOUNT_WAITING] = "waiting",
857 [AUTOMOUNT_RUNNING] = "running",
858 [AUTOMOUNT_FAILED] = "failed"
861 DEFINE_STRING_TABLE_LOOKUP(automount_state, AutomountState);
863 static const char* const automount_result_table[_AUTOMOUNT_RESULT_MAX] = {
864 [AUTOMOUNT_SUCCESS] = "success",
865 [AUTOMOUNT_FAILURE_RESOURCES] = "resources"
868 DEFINE_STRING_TABLE_LOOKUP(automount_result, AutomountResult);
870 const UnitVTable automount_vtable = {
871 .object_size = sizeof(Automount),
878 .no_instances = true,
880 .init = automount_init,
881 .load = automount_load,
882 .done = automount_done,
884 .coldplug = automount_coldplug,
886 .dump = automount_dump,
888 .start = automount_start,
889 .stop = automount_stop,
891 .serialize = automount_serialize,
892 .deserialize_item = automount_deserialize_item,
894 .active_state = automount_active_state,
895 .sub_state_to_string = automount_sub_state_to_string,
897 .check_gc = automount_check_gc,
899 .fd_event = automount_fd_event,
901 .reset_failed = automount_reset_failed,
903 .bus_interface = "org.freedesktop.systemd1.Automount",
904 .bus_message_handler = bus_automount_message_handler,
905 .bus_invalidating_properties = bus_automount_invalidating_properties,
907 .shutdown = automount_shutdown,
909 .status_message_formats = {
910 .finished_start_job = {
911 [JOB_DONE] = "Set up automount %s.",
912 [JOB_FAILED] = "Failed to set up automount %s.",
913 [JOB_DEPENDENCY] = "Dependency failed for %s.",
915 .finished_stop_job = {
916 [JOB_DONE] = "Unset automount %s.",
917 [JOB_FAILED] = "Failed to unset automount %s.",