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/>.
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"
41 static const UnitActiveState state_translation_table[_AUTOMOUNT_STATE_MAX] = {
42 [AUTOMOUNT_DEAD] = UNIT_INACTIVE,
43 [AUTOMOUNT_WAITING] = UNIT_ACTIVE,
44 [AUTOMOUNT_RUNNING] = UNIT_ACTIVE,
45 [AUTOMOUNT_MAINTENANCE] = UNIT_MAINTENANCE
48 static int open_dev_autofs(Manager *m);
50 static void automount_init(Unit *u) {
51 Automount *a = AUTOMOUNT(u);
54 assert(u->meta.load_state == UNIT_STUB);
56 a->pipe_watch.fd = a->pipe_fd = -1;
57 a->pipe_watch.type = WATCH_INVALID;
59 a->directory_mode = 0755;
62 static void repeat_unmout(const char *path) {
66 /* If there are multiple mounts on a mount point, this
69 if (umount2(path, MNT_DETACH) >= 0)
73 log_error("Failed to unmount: %m");
79 static void unmount_autofs(Automount *a) {
85 automount_send_ready(a, -EHOSTDOWN);
87 unit_unwatch_fd(UNIT(a), &a->pipe_watch);
88 close_nointr_nofail(a->pipe_fd);
91 /* If we reload/reexecute things we keep the mount point
94 (a->meta.manager->exit_code != MANAGER_RELOAD &&
95 a->meta.manager->exit_code != MANAGER_REEXECUTE))
96 repeat_unmout(a->where);
99 static void automount_done(Unit *u) {
100 Automount *a = AUTOMOUNT(u);
114 int automount_add_one_mount_link(Automount *a, Mount *m) {
120 if (a->meta.load_state != UNIT_LOADED ||
121 m->meta.load_state != UNIT_LOADED)
124 if (!path_startswith(a->where, m->where))
127 if (path_equal(a->where, m->where))
130 if ((r = unit_add_two_dependencies(UNIT(a), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
136 static int automount_add_mount_links(Automount *a) {
142 LIST_FOREACH(units_per_type, other, a->meta.manager->units_per_type[UNIT_MOUNT])
143 if ((r = automount_add_one_mount_link(a, (Mount*) other)) < 0)
149 static int automount_verify(Automount *a) {
154 if (a->meta.load_state != UNIT_LOADED)
157 if (path_equal(a->where, "/")) {
158 log_error("Cannot have an automount unit for the root directory. Refusing.");
162 if (!(e = unit_name_from_path(a->where, ".automount")))
165 b = unit_has_name(UNIT(a), e);
169 log_error("%s's Where setting doesn't match unit name. Refusing.", a->meta.id);
176 static int automount_load(Unit *u) {
178 Automount *a = AUTOMOUNT(u);
181 assert(u->meta.load_state == UNIT_STUB);
183 /* Load a .automount file */
184 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
187 if (u->meta.load_state == UNIT_LOADED) {
190 if (!(a->where = unit_name_to_path(u->meta.id)))
193 path_kill_slashes(a->where);
195 if ((r = automount_add_mount_links(a)) < 0)
198 if ((r = unit_load_related_unit(u, ".mount", (Unit**) &a->mount)) < 0)
201 if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(a->mount), true)) < 0)
204 if (a->meta.default_dependencies &&
205 a->meta.manager->running_as == MANAGER_SYSTEM)
206 if ((r = unit_add_two_dependencies_by_name(UNIT(a), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true)) < 0)
210 return automount_verify(a);
213 static void automount_set_state(Automount *a, AutomountState state) {
214 AutomountState old_state;
217 old_state = a->state;
220 if (state != AUTOMOUNT_WAITING &&
221 state != AUTOMOUNT_RUNNING)
224 if (state != old_state)
225 log_debug("%s changed %s -> %s",
227 automount_state_to_string(old_state),
228 automount_state_to_string(state));
230 unit_notify(UNIT(a), state_translation_table[old_state], state_translation_table[state]);
233 static int automount_coldplug(Unit *u) {
234 Automount *a = AUTOMOUNT(u);
238 assert(a->state == AUTOMOUNT_DEAD);
240 if (a->deserialized_state != a->state) {
242 if ((r = open_dev_autofs(u->meta.manager)) < 0)
245 if (a->deserialized_state == AUTOMOUNT_WAITING ||
246 a->deserialized_state == AUTOMOUNT_RUNNING) {
248 assert(a->pipe_fd >= 0);
250 if ((r = unit_watch_fd(UNIT(a), a->pipe_fd, EPOLLIN, &a->pipe_watch)) < 0)
254 automount_set_state(a, a->deserialized_state);
260 static void automount_dump(Unit *u, FILE *f, const char *prefix) {
261 Automount *a = AUTOMOUNT(u);
266 "%sAutomount State: %s\n"
268 "%sDirectoryMode: %04o\n",
269 prefix, automount_state_to_string(a->state),
271 prefix, a->directory_mode);
274 static void automount_enter_dead(Automount *a, bool success) {
280 automount_set_state(a, a->failure ? AUTOMOUNT_MAINTENANCE : AUTOMOUNT_DEAD);
283 static int open_dev_autofs(Manager *m) {
284 struct autofs_dev_ioctl param;
288 if (m->dev_autofs_fd >= 0)
289 return m->dev_autofs_fd;
291 if ((m->dev_autofs_fd = open("/dev/autofs", O_CLOEXEC|O_RDONLY)) < 0) {
292 log_error("Failed to open /dev/autofs: %s", strerror(errno));
296 init_autofs_dev_ioctl(¶m);
297 if (ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, ¶m) < 0) {
298 close_nointr_nofail(m->dev_autofs_fd);
299 m->dev_autofs_fd = -1;
303 log_debug("Autofs kernel version %i.%i", param.ver_major, param.ver_minor);
305 return m->dev_autofs_fd;
308 static int open_ioctl_fd(int dev_autofs_fd, const char *where, dev_t devid) {
309 struct autofs_dev_ioctl *param;
313 assert(dev_autofs_fd >= 0);
316 l = sizeof(struct autofs_dev_ioctl) + strlen(where) + 1;
318 if (!(param = malloc(l)))
321 init_autofs_dev_ioctl(param);
324 param->openmount.devid = devid;
325 strcpy(param->path, where);
327 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_OPENMOUNT, param) < 0) {
332 if (param->ioctlfd < 0) {
337 fd_cloexec(param->ioctlfd, true);
345 static int autofs_protocol(int dev_autofs_fd, int ioctl_fd) {
346 uint32_t major, minor;
347 struct autofs_dev_ioctl param;
349 assert(dev_autofs_fd >= 0);
350 assert(ioctl_fd >= 0);
352 init_autofs_dev_ioctl(¶m);
353 param.ioctlfd = ioctl_fd;
355 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOVER, ¶m) < 0)
358 major = param.protover.version;
360 init_autofs_dev_ioctl(¶m);
361 param.ioctlfd = ioctl_fd;
363 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOSUBVER, ¶m) < 0)
366 minor = param.protosubver.sub_version;
368 log_debug("Autofs protocol version %i.%i", major, minor);
372 static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, time_t sec) {
373 struct autofs_dev_ioctl param;
375 assert(dev_autofs_fd >= 0);
376 assert(ioctl_fd >= 0);
378 init_autofs_dev_ioctl(¶m);
379 param.ioctlfd = ioctl_fd;
380 param.timeout.timeout = sec;
382 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, ¶m) < 0)
388 static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, int status) {
389 struct autofs_dev_ioctl param;
391 assert(dev_autofs_fd >= 0);
392 assert(ioctl_fd >= 0);
394 init_autofs_dev_ioctl(¶m);
395 param.ioctlfd = ioctl_fd;
398 param.fail.token = token;
399 param.fail.status = status;
401 param.ready.token = token;
403 if (ioctl(dev_autofs_fd, status ? AUTOFS_DEV_IOCTL_FAIL : AUTOFS_DEV_IOCTL_READY, ¶m) < 0)
409 int automount_send_ready(Automount *a, int status) {
416 if (set_isempty(a->tokens))
419 if ((ioctl_fd = open_ioctl_fd(a->meta.manager->dev_autofs_fd, a->where, a->dev_id)) < 0) {
425 log_debug("Sending failure: %s", strerror(-status));
427 log_debug("Sending success.");
429 /* Autofs thankfully does not hand out 0 as a token */
430 while ((token = PTR_TO_UINT(set_steal_first(a->tokens)))) {
433 /* Autofs fun fact II:
435 * if you pass a positive status code here, the kernel will
438 if ((k = autofs_send_ready(a->meta.manager->dev_autofs_fd,
449 close_nointr_nofail(ioctl_fd);
454 static void automount_enter_waiting(Automount *a) {
455 int p[2] = { -1, -1 };
456 char name[32], options[128];
457 bool mounted = false;
458 int r, ioctl_fd = -1, dev_autofs_fd;
462 assert(a->pipe_fd < 0);
466 set_clear(a->tokens);
468 if ((dev_autofs_fd = open_dev_autofs(a->meta.manager)) < 0) {
473 /* We knowingly ignore the results of this call */
474 mkdir_p(a->where, 0555);
476 if (pipe2(p, O_NONBLOCK|O_CLOEXEC) < 0) {
481 snprintf(options, sizeof(options), "fd=%i,pgrp=%u,minproto=5,maxproto=5,direct", p[1], (unsigned) getpgrp());
482 char_array_0(options);
484 snprintf(name, sizeof(name), "systemd-%u", (unsigned) getpid());
487 if (mount(name, a->where, "autofs", 0, options) < 0) {
494 close_nointr_nofail(p[1]);
497 if (stat(a->where, &st) < 0) {
502 if ((ioctl_fd = open_ioctl_fd(dev_autofs_fd, a->where, st.st_dev)) < 0) {
507 if ((r = autofs_protocol(dev_autofs_fd, ioctl_fd)) < 0)
510 if ((r = autofs_set_timeout(dev_autofs_fd, ioctl_fd, 300)) < 0)
515 * Unless we close the ioctl fd here, for some weird reason
516 * the direct mount will not receive events from the
519 close_nointr_nofail(ioctl_fd);
522 if ((r = unit_watch_fd(UNIT(a), p[0], EPOLLIN, &a->pipe_watch)) < 0)
526 a->dev_id = st.st_dev;
528 automount_set_state(a, AUTOMOUNT_WAITING);
533 assert_se(close_pipe(p) == 0);
536 close_nointr_nofail(ioctl_fd);
539 repeat_unmout(a->where);
541 log_error("Failed to initialize automounter: %s", strerror(-r));
542 automount_enter_dead(a, false);
545 static void automount_enter_runnning(Automount *a) {
553 dbus_error_init(&error);
555 mkdir_p(a->where, a->directory_mode);
557 /* Before we do anything, let's see if somebody is playing games with us? */
558 if (lstat(a->where, &st) < 0) {
559 log_warning("%s failed stat automount point: %m", a->meta.id);
563 if (!S_ISDIR(st.st_mode) || st.st_dev != a->dev_id)
564 log_info("%s's automount point already active?", a->meta.id);
565 else if ((r = manager_add_job(a->meta.manager, JOB_START, UNIT(a->mount), JOB_REPLACE, true, &error, NULL)) < 0) {
566 log_warning("%s failed to queue mount startup job: %s", a->meta.id, bus_error(&error, r));
570 automount_set_state(a, AUTOMOUNT_RUNNING);
574 automount_enter_dead(a, false);
575 dbus_error_free(&error);
578 static int automount_start(Unit *u) {
579 Automount *a = AUTOMOUNT(u);
583 assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_MAINTENANCE);
585 if (path_is_mount_point(a->where)) {
586 log_error("Path %s is already a mount point, refusing start for %s", a->where, u->meta.id);
590 if (a->mount->meta.load_state != UNIT_LOADED)
594 automount_enter_waiting(a);
598 static int automount_stop(Unit *u) {
599 Automount *a = AUTOMOUNT(u);
603 assert(a->state == AUTOMOUNT_WAITING || a->state == AUTOMOUNT_RUNNING);
605 automount_enter_dead(a, true);
609 static int automount_serialize(Unit *u, FILE *f, FDSet *fds) {
610 Automount *a = AUTOMOUNT(u);
618 unit_serialize_item(u, f, "state", automount_state_to_string(a->state));
619 unit_serialize_item(u, f, "failure", yes_no(a->failure));
620 unit_serialize_item_format(u, f, "dev-id", "%u", (unsigned) a->dev_id);
622 SET_FOREACH(p, a->tokens, i)
623 unit_serialize_item_format(u, f, "token", "%u", PTR_TO_UINT(p));
625 if (a->pipe_fd >= 0) {
628 if ((copy = fdset_put_dup(fds, a->pipe_fd)) < 0)
631 unit_serialize_item_format(u, f, "pipe-fd", "%i", copy);
637 static int automount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
638 Automount *a = AUTOMOUNT(u);
644 if (streq(key, "state")) {
645 AutomountState state;
647 if ((state = automount_state_from_string(value)) < 0)
648 log_debug("Failed to parse state value %s", value);
650 a->deserialized_state = state;
651 } else if (streq(key, "failure")) {
654 if ((b = parse_boolean(value)) < 0)
655 log_debug("Failed to parse failure value %s", value);
657 a->failure = b || a->failure;
658 } else if (streq(key, "dev-id")) {
661 if (safe_atou(value, &d) < 0)
662 log_debug("Failed to parse dev-id value %s", value);
664 a->dev_id = (unsigned) d;
665 } else if (streq(key, "token")) {
668 if (safe_atou(value, &token) < 0)
669 log_debug("Failed to parse token value %s", value);
672 if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func)))
675 if ((r = set_put(a->tokens, UINT_TO_PTR(token))) < 0)
678 } else if (streq(key, "pipe-fd")) {
681 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
682 log_debug("Failed to parse pipe-fd value %s", value);
685 close_nointr_nofail(a->pipe_fd);
687 a->pipe_fd = fdset_remove(fds, fd);
690 log_debug("Unknown serialization key '%s'", key);
695 static UnitActiveState automount_active_state(Unit *u) {
698 return state_translation_table[AUTOMOUNT(u)->state];
701 static const char *automount_sub_state_to_string(Unit *u) {
704 return automount_state_to_string(AUTOMOUNT(u)->state);
707 static bool automount_check_gc(Unit *u) {
708 Automount *a = AUTOMOUNT(u);
712 return UNIT_VTABLE(UNIT(a->mount))->check_gc(UNIT(a->mount));
715 static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
716 Automount *a = AUTOMOUNT(u);
717 union autofs_v5_packet_union packet;
722 assert(fd == a->pipe_fd);
724 if (events != EPOLLIN) {
725 log_error("Got invalid poll event on pipe.");
729 if ((l = loop_read(a->pipe_fd, &packet, sizeof(packet), true)) != sizeof(packet)) {
730 log_error("Invalid read from pipe: %s", l < 0 ? strerror(-l) : "short read");
734 switch (packet.hdr.type) {
736 case autofs_ptype_missing_direct:
737 log_debug("Got direct mount request for %s", packet.v5_packet.name);
740 if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func))) {
741 log_error("Failed to allocate token set.");
745 if ((r = set_put(a->tokens, UINT_TO_PTR(packet.v5_packet.wait_queue_token))) < 0) {
746 log_error("Failed to remember token: %s", strerror(-r));
750 automount_enter_runnning(a);
754 log_error("Received unknown automount request %i", packet.hdr.type);
761 automount_enter_dead(a, false);
764 static void automount_shutdown(Manager *m) {
767 if (m->dev_autofs_fd >= 0)
768 close_nointr_nofail(m->dev_autofs_fd);
771 static const char* const automount_state_table[_AUTOMOUNT_STATE_MAX] = {
772 [AUTOMOUNT_DEAD] = "dead",
773 [AUTOMOUNT_WAITING] = "waiting",
774 [AUTOMOUNT_RUNNING] = "running",
775 [AUTOMOUNT_MAINTENANCE] = "maintenance"
778 DEFINE_STRING_TABLE_LOOKUP(automount_state, AutomountState);
780 const UnitVTable automount_vtable = {
781 .suffix = ".automount",
784 .no_instances = true,
786 .init = automount_init,
787 .load = automount_load,
788 .done = automount_done,
790 .coldplug = automount_coldplug,
792 .dump = automount_dump,
794 .start = automount_start,
795 .stop = automount_stop,
797 .serialize = automount_serialize,
798 .deserialize_item = automount_deserialize_item,
800 .active_state = automount_active_state,
801 .sub_state_to_string = automount_sub_state_to_string,
803 .check_gc = automount_check_gc,
805 .fd_event = automount_fd_event,
807 .bus_message_handler = bus_automount_message_handler,
809 .shutdown = automount_shutdown