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"
39 static const UnitActiveState state_translation_table[_AUTOMOUNT_STATE_MAX] = {
40 [AUTOMOUNT_DEAD] = UNIT_INACTIVE,
41 [AUTOMOUNT_WAITING] = UNIT_ACTIVE,
42 [AUTOMOUNT_RUNNING] = UNIT_ACTIVE,
43 [AUTOMOUNT_MAINTENANCE] = UNIT_MAINTENANCE
46 static int open_dev_autofs(Manager *m);
48 static void automount_init(Unit *u) {
49 Automount *a = AUTOMOUNT(u);
52 assert(u->meta.load_state == UNIT_STUB);
54 a->pipe_watch.fd = a->pipe_fd = -1;
55 a->pipe_watch.type = WATCH_INVALID;
57 a->directory_mode = 0755;
60 static void repeat_unmout(const char *path) {
64 /* If there are multiple mounts on a mount point, this
67 if (umount2(path, MNT_DETACH) >= 0)
71 log_error("Failed to unmount: %m");
77 static void unmount_autofs(Automount *a) {
83 automount_send_ready(a, -EHOSTDOWN);
85 unit_unwatch_fd(UNIT(a), &a->pipe_watch);
86 close_nointr_nofail(a->pipe_fd);
89 /* If we reload/reexecute things we keep the mount point
92 (a->meta.manager->exit_code != MANAGER_RELOAD &&
93 a->meta.manager->exit_code != MANAGER_REEXECUTE))
94 repeat_unmout(a->where);
97 static void automount_done(Unit *u) {
98 Automount *a = AUTOMOUNT(u);
112 int automount_add_one_mount_link(Automount *a, Mount *m) {
118 if (a->meta.load_state != UNIT_LOADED ||
119 m->meta.load_state != UNIT_LOADED)
122 if (!path_startswith(a->where, m->where))
125 if (path_equal(a->where, m->where))
128 if ((r = unit_add_two_dependencies(UNIT(a), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
134 static int automount_add_mount_links(Automount *a) {
140 LIST_FOREACH(units_per_type, other, a->meta.manager->units_per_type[UNIT_MOUNT])
141 if ((r = automount_add_one_mount_link(a, (Mount*) other)) < 0)
147 static int automount_verify(Automount *a) {
152 if (a->meta.load_state != UNIT_LOADED)
155 if (!(e = unit_name_from_path(a->where, ".automount")))
158 b = unit_has_name(UNIT(a), e);
162 log_error("%s's Where setting doesn't match unit name. Refusing.", a->meta.id);
169 static int automount_load(Unit *u) {
171 Automount *a = AUTOMOUNT(u);
174 assert(u->meta.load_state == UNIT_STUB);
176 /* Load a .automount file */
177 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
180 if (u->meta.load_state == UNIT_LOADED) {
183 if (!(a->where = unit_name_to_path(u->meta.id)))
186 path_kill_slashes(a->where);
188 if ((r = automount_add_mount_links(a)) < 0)
191 if ((r = unit_load_related_unit(u, ".mount", (Unit**) &a->mount)) < 0)
194 if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(a->mount), true)) < 0)
198 return automount_verify(a);
201 static void automount_set_state(Automount *a, AutomountState state) {
202 AutomountState old_state;
205 old_state = a->state;
208 if (state != AUTOMOUNT_WAITING &&
209 state != AUTOMOUNT_RUNNING)
212 if (state != old_state)
213 log_debug("%s changed %s -> %s",
215 automount_state_to_string(old_state),
216 automount_state_to_string(state));
218 unit_notify(UNIT(a), state_translation_table[old_state], state_translation_table[state]);
221 static int automount_coldplug(Unit *u) {
222 Automount *a = AUTOMOUNT(u);
226 assert(a->state == AUTOMOUNT_DEAD);
228 if (a->deserialized_state != a->state) {
230 if ((r = open_dev_autofs(u->meta.manager)) < 0)
233 if (a->deserialized_state == AUTOMOUNT_WAITING ||
234 a->deserialized_state == AUTOMOUNT_RUNNING) {
236 assert(a->pipe_fd >= 0);
238 if ((r = unit_watch_fd(UNIT(a), a->pipe_fd, EPOLLIN, &a->pipe_watch)) < 0)
242 automount_set_state(a, a->deserialized_state);
248 static void automount_dump(Unit *u, FILE *f, const char *prefix) {
249 Automount *a = AUTOMOUNT(u);
254 "%sAutomount State: %s\n"
256 "%sDirectoryMode: %04o\n",
257 prefix, automount_state_to_string(a->state),
259 prefix, a->directory_mode);
262 static void automount_enter_dead(Automount *a, bool success) {
268 automount_set_state(a, a->failure ? AUTOMOUNT_MAINTENANCE : AUTOMOUNT_DEAD);
271 static int open_dev_autofs(Manager *m) {
272 struct autofs_dev_ioctl param;
276 if (m->dev_autofs_fd >= 0)
277 return m->dev_autofs_fd;
279 if ((m->dev_autofs_fd = open("/dev/autofs", O_CLOEXEC|O_RDONLY)) < 0) {
280 log_error("Failed to open /dev/autofs: %s", strerror(errno));
284 init_autofs_dev_ioctl(¶m);
285 if (ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, ¶m) < 0) {
286 close_nointr_nofail(m->dev_autofs_fd);
287 m->dev_autofs_fd = -1;
291 log_debug("Autofs kernel version %i.%i", param.ver_major, param.ver_minor);
293 return m->dev_autofs_fd;
296 static int open_ioctl_fd(int dev_autofs_fd, const char *where, dev_t devid) {
297 struct autofs_dev_ioctl *param;
301 assert(dev_autofs_fd >= 0);
304 l = sizeof(struct autofs_dev_ioctl) + strlen(where) + 1;
306 if (!(param = malloc(l)))
309 init_autofs_dev_ioctl(param);
312 param->openmount.devid = devid;
313 strcpy(param->path, where);
315 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_OPENMOUNT, param) < 0) {
320 if (param->ioctlfd < 0) {
325 fd_cloexec(param->ioctlfd, true);
333 static int autofs_protocol(int dev_autofs_fd, int ioctl_fd) {
334 uint32_t major, minor;
335 struct autofs_dev_ioctl param;
337 assert(dev_autofs_fd >= 0);
338 assert(ioctl_fd >= 0);
340 init_autofs_dev_ioctl(¶m);
341 param.ioctlfd = ioctl_fd;
343 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOVER, ¶m) < 0)
346 major = param.protover.version;
348 init_autofs_dev_ioctl(¶m);
349 param.ioctlfd = ioctl_fd;
351 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOSUBVER, ¶m) < 0)
354 minor = param.protosubver.sub_version;
356 log_debug("Autofs protocol version %i.%i", major, minor);
360 static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, time_t sec) {
361 struct autofs_dev_ioctl param;
363 assert(dev_autofs_fd >= 0);
364 assert(ioctl_fd >= 0);
366 init_autofs_dev_ioctl(¶m);
367 param.ioctlfd = ioctl_fd;
368 param.timeout.timeout = sec;
370 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, ¶m) < 0)
376 static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, int status) {
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;
386 param.fail.token = token;
387 param.fail.status = status;
389 param.ready.token = token;
391 if (ioctl(dev_autofs_fd, status ? AUTOFS_DEV_IOCTL_FAIL : AUTOFS_DEV_IOCTL_READY, ¶m) < 0)
397 int automount_send_ready(Automount *a, int status) {
404 if (set_isempty(a->tokens))
407 if ((ioctl_fd = open_ioctl_fd(a->meta.manager->dev_autofs_fd, a->where, a->dev_id)) < 0) {
413 log_debug("Sending failure: %s", strerror(-status));
415 log_debug("Sending success.");
417 /* Autofs thankfully does not hand out 0 as a token */
418 while ((token = PTR_TO_UINT(set_steal_first(a->tokens)))) {
421 /* Autofs fun fact II:
423 * if you pass a positive status code here, the kernel will
426 if ((k = autofs_send_ready(a->meta.manager->dev_autofs_fd,
437 close_nointr_nofail(ioctl_fd);
442 static void automount_enter_waiting(Automount *a) {
443 int p[2] = { -1, -1 };
444 char name[32], options[128];
445 bool mounted = false;
446 int r, ioctl_fd = -1, dev_autofs_fd;
450 assert(a->pipe_fd < 0);
454 set_clear(a->tokens);
456 if ((dev_autofs_fd = open_dev_autofs(a->meta.manager)) < 0) {
461 /* We knowingly ignore the results of this call */
462 mkdir_p(a->where, 0555);
464 if (pipe2(p, O_NONBLOCK|O_CLOEXEC) < 0) {
469 snprintf(options, sizeof(options), "fd=%i,pgrp=%u,minproto=5,maxproto=5,direct", p[1], (unsigned) getpgrp());
470 char_array_0(options);
472 snprintf(name, sizeof(name), "systemd-%u", (unsigned) getpid());
475 if (mount(name, a->where, "autofs", 0, options) < 0) {
482 close_nointr_nofail(p[1]);
485 if (stat(a->where, &st) < 0) {
490 if ((ioctl_fd = open_ioctl_fd(dev_autofs_fd, a->where, st.st_dev)) < 0) {
495 if ((r = autofs_protocol(dev_autofs_fd, ioctl_fd)) < 0)
498 if ((r = autofs_set_timeout(dev_autofs_fd, ioctl_fd, 300)) < 0)
503 * Unless we close the ioctl fd here, for some weird reason
504 * the direct mount will not receive events from the
507 close_nointr_nofail(ioctl_fd);
510 if ((r = unit_watch_fd(UNIT(a), p[0], EPOLLIN, &a->pipe_watch)) < 0)
514 a->dev_id = st.st_dev;
516 automount_set_state(a, AUTOMOUNT_WAITING);
521 assert_se(close_pipe(p) == 0);
524 close_nointr_nofail(ioctl_fd);
527 repeat_unmout(a->where);
529 log_error("Failed to initialize automounter: %s", strerror(-r));
530 automount_enter_dead(a, false);
533 static void automount_enter_runnning(Automount *a) {
540 mkdir_p(a->where, a->directory_mode);
542 /* Before we do anything, let's see if somebody is playing games with us? */
543 if (lstat(a->where, &st) < 0) {
544 log_warning("%s failed stat automount point: %m", a->meta.id);
548 if (!S_ISDIR(st.st_mode) || st.st_dev != a->dev_id)
549 log_info("%s's automount point already active?", a->meta.id);
550 else if ((r = manager_add_job(a->meta.manager, JOB_START, UNIT(a->mount), JOB_REPLACE, true, NULL)) < 0) {
551 log_warning("%s failed to queue mount startup job: %s", a->meta.id, strerror(-r));
555 automount_set_state(a, AUTOMOUNT_RUNNING);
559 automount_enter_dead(a, false);
562 static int automount_start(Unit *u) {
563 Automount *a = AUTOMOUNT(u);
567 assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_MAINTENANCE);
569 if (path_is_mount_point(a->where)) {
570 log_error("Path %s is already a mount point, refusing start for %s", a->where, u->meta.id);
574 if (a->mount->meta.load_state != UNIT_LOADED)
578 automount_enter_waiting(a);
582 static int automount_stop(Unit *u) {
583 Automount *a = AUTOMOUNT(u);
587 assert(a->state == AUTOMOUNT_WAITING || a->state == AUTOMOUNT_RUNNING);
589 automount_enter_dead(a, true);
593 static int automount_serialize(Unit *u, FILE *f, FDSet *fds) {
594 Automount *a = AUTOMOUNT(u);
602 unit_serialize_item(u, f, "state", automount_state_to_string(a->state));
603 unit_serialize_item(u, f, "failure", yes_no(a->failure));
604 unit_serialize_item_format(u, f, "dev-id", "%u", (unsigned) a->dev_id);
606 SET_FOREACH(p, a->tokens, i)
607 unit_serialize_item_format(u, f, "token", "%u", PTR_TO_UINT(p));
609 if (a->pipe_fd >= 0) {
612 if ((copy = fdset_put_dup(fds, a->pipe_fd)) < 0)
615 unit_serialize_item_format(u, f, "pipe-fd", "%i", copy);
621 static int automount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
622 Automount *a = AUTOMOUNT(u);
628 if (streq(key, "state")) {
629 AutomountState state;
631 if ((state = automount_state_from_string(value)) < 0)
632 log_debug("Failed to parse state value %s", value);
634 a->deserialized_state = state;
635 } else if (streq(key, "failure")) {
638 if ((b = parse_boolean(value)) < 0)
639 log_debug("Failed to parse failure value %s", value);
641 a->failure = b || a->failure;
642 } else if (streq(key, "dev-id")) {
645 if (safe_atou(value, &d) < 0)
646 log_debug("Failed to parse dev-id value %s", value);
648 a->dev_id = (unsigned) d;
649 } else if (streq(key, "token")) {
652 if (safe_atou(value, &token) < 0)
653 log_debug("Failed to parse token value %s", value);
656 if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func)))
659 if ((r = set_put(a->tokens, UINT_TO_PTR(token))) < 0)
662 } else if (streq(key, "pipe-fd")) {
665 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
666 log_debug("Failed to parse pipe-fd value %s", value);
669 close_nointr_nofail(a->pipe_fd);
671 a->pipe_fd = fdset_remove(fds, fd);
674 log_debug("Unknown serialization key '%s'", key);
679 static UnitActiveState automount_active_state(Unit *u) {
682 return state_translation_table[AUTOMOUNT(u)->state];
685 static const char *automount_sub_state_to_string(Unit *u) {
688 return automount_state_to_string(AUTOMOUNT(u)->state);
691 static bool automount_check_gc(Unit *u) {
692 Automount *a = AUTOMOUNT(u);
696 return UNIT_VTABLE(UNIT(a->mount))->check_gc(UNIT(a->mount));
699 static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
700 Automount *a = AUTOMOUNT(u);
701 union autofs_v5_packet_union packet;
706 assert(fd == a->pipe_fd);
708 if (events != EPOLLIN) {
709 log_error("Got invalid poll event on pipe.");
713 if ((l = loop_read(a->pipe_fd, &packet, sizeof(packet), true)) != sizeof(packet)) {
714 log_error("Invalid read from pipe: %s", l < 0 ? strerror(-l) : "short read");
718 switch (packet.hdr.type) {
720 case autofs_ptype_missing_direct:
721 log_debug("Got direct mount request for %s", packet.v5_packet.name);
724 if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func))) {
725 log_error("Failed to allocate token set.");
729 if ((r = set_put(a->tokens, UINT_TO_PTR(packet.v5_packet.wait_queue_token))) < 0) {
730 log_error("Failed to remember token: %s", strerror(-r));
734 automount_enter_runnning(a);
738 log_error("Received unknown automount request %i", packet.hdr.type);
745 automount_enter_dead(a, false);
748 static void automount_shutdown(Manager *m) {
751 if (m->dev_autofs_fd >= 0)
752 close_nointr_nofail(m->dev_autofs_fd);
755 static const char* const automount_state_table[_AUTOMOUNT_STATE_MAX] = {
756 [AUTOMOUNT_DEAD] = "dead",
757 [AUTOMOUNT_WAITING] = "waiting",
758 [AUTOMOUNT_RUNNING] = "running",
759 [AUTOMOUNT_MAINTENANCE] = "maintenance"
762 DEFINE_STRING_TABLE_LOOKUP(automount_state, AutomountState);
764 const UnitVTable automount_vtable = {
765 .suffix = ".automount",
768 .no_instances = true,
770 .init = automount_init,
771 .load = automount_load,
772 .done = automount_done,
774 .coldplug = automount_coldplug,
776 .dump = automount_dump,
778 .start = automount_start,
779 .stop = automount_stop,
781 .serialize = automount_serialize,
782 .deserialize_item = automount_deserialize_item,
784 .active_state = automount_active_state,
785 .sub_state_to_string = automount_sub_state_to_string,
787 .check_gc = automount_check_gc,
789 .fd_event = automount_fd_event,
791 .bus_message_handler = bus_automount_message_handler,
793 .shutdown = automount_shutdown