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_dependency(UNIT(m), UNIT_BEFORE, UNIT(a), true)) < 0)
131 if ((r = unit_add_dependency(UNIT(a), UNIT_REQUIRES, UNIT(m), true)) < 0)
137 static int automount_add_mount_links(Automount *a) {
143 LIST_FOREACH(units_per_type, other, a->meta.manager->units_per_type[UNIT_MOUNT])
144 if ((r = automount_add_one_mount_link(a, (Mount*) other)) < 0)
150 static int automount_verify(Automount *a) {
155 if (a->meta.load_state != UNIT_LOADED)
158 if (!(e = unit_name_from_path(a->where, ".automount")))
161 b = unit_has_name(UNIT(a), e);
165 log_error("%s's Where setting doesn't match unit name. Refusing.", a->meta.id);
172 static int automount_load(Unit *u) {
174 Automount *a = AUTOMOUNT(u);
177 assert(u->meta.load_state == UNIT_STUB);
179 /* Load a .automount file */
180 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
183 if (u->meta.load_state == UNIT_LOADED) {
186 if (!(a->where = unit_name_to_path(u->meta.id)))
189 path_kill_slashes(a->where);
191 if ((r = automount_add_mount_links(a)) < 0)
194 if ((r = unit_load_related_unit(u, ".mount", (Unit**) &a->mount)) < 0)
197 if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(a->mount), true)) < 0)
201 return automount_verify(a);
204 static void automount_set_state(Automount *a, AutomountState state) {
205 AutomountState old_state;
208 old_state = a->state;
211 if (state != AUTOMOUNT_WAITING &&
212 state != AUTOMOUNT_RUNNING)
215 if (state != old_state)
216 log_debug("%s changed %s -> %s",
218 automount_state_to_string(old_state),
219 automount_state_to_string(state));
221 unit_notify(UNIT(a), state_translation_table[old_state], state_translation_table[state]);
224 static int automount_coldplug(Unit *u) {
225 Automount *a = AUTOMOUNT(u);
229 assert(a->state == AUTOMOUNT_DEAD);
231 if (a->deserialized_state != a->state) {
233 if ((r = open_dev_autofs(u->meta.manager)) < 0)
236 if (a->deserialized_state == AUTOMOUNT_WAITING ||
237 a->deserialized_state == AUTOMOUNT_RUNNING) {
239 assert(a->pipe_fd >= 0);
241 if ((r = unit_watch_fd(UNIT(a), a->pipe_fd, EPOLLIN, &a->pipe_watch)) < 0)
245 automount_set_state(a, a->deserialized_state);
251 static void automount_dump(Unit *u, FILE *f, const char *prefix) {
252 Automount *a = AUTOMOUNT(u);
257 "%sAutomount State: %s\n"
259 "%sDirectoryMode: %04o\n",
260 prefix, automount_state_to_string(a->state),
262 prefix, a->directory_mode);
265 static void automount_enter_dead(Automount *a, bool success) {
271 automount_set_state(a, a->failure ? AUTOMOUNT_MAINTENANCE : AUTOMOUNT_DEAD);
274 static int open_dev_autofs(Manager *m) {
275 struct autofs_dev_ioctl param;
279 if (m->dev_autofs_fd >= 0)
280 return m->dev_autofs_fd;
282 if ((m->dev_autofs_fd = open("/dev/autofs", O_CLOEXEC|O_RDONLY)) < 0) {
283 log_error("Failed to open /dev/autofs: %s", strerror(errno));
287 init_autofs_dev_ioctl(¶m);
288 if (ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, ¶m) < 0) {
289 close_nointr_nofail(m->dev_autofs_fd);
290 m->dev_autofs_fd = -1;
294 log_debug("Autofs kernel version %i.%i", param.ver_major, param.ver_minor);
296 return m->dev_autofs_fd;
299 static int open_ioctl_fd(int dev_autofs_fd, const char *where, dev_t devid) {
300 struct autofs_dev_ioctl *param;
304 assert(dev_autofs_fd >= 0);
307 l = sizeof(struct autofs_dev_ioctl) + strlen(where) + 1;
309 if (!(param = malloc(l)))
312 init_autofs_dev_ioctl(param);
315 param->openmount.devid = devid;
316 strcpy(param->path, where);
318 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_OPENMOUNT, param) < 0) {
323 if (param->ioctlfd < 0) {
328 fd_cloexec(param->ioctlfd, true);
336 static int autofs_protocol(int dev_autofs_fd, int ioctl_fd) {
337 uint32_t major, minor;
338 struct autofs_dev_ioctl param;
340 assert(dev_autofs_fd >= 0);
341 assert(ioctl_fd >= 0);
343 init_autofs_dev_ioctl(¶m);
344 param.ioctlfd = ioctl_fd;
346 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOVER, ¶m) < 0)
349 major = param.protover.version;
351 init_autofs_dev_ioctl(¶m);
352 param.ioctlfd = ioctl_fd;
354 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOSUBVER, ¶m) < 0)
357 minor = param.protosubver.sub_version;
359 log_debug("Autofs protocol version %i.%i", major, minor);
363 static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, time_t sec) {
364 struct autofs_dev_ioctl param;
366 assert(dev_autofs_fd >= 0);
367 assert(ioctl_fd >= 0);
369 init_autofs_dev_ioctl(¶m);
370 param.ioctlfd = ioctl_fd;
371 param.timeout.timeout = sec;
373 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, ¶m) < 0)
379 static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, int status) {
380 struct autofs_dev_ioctl param;
382 assert(dev_autofs_fd >= 0);
383 assert(ioctl_fd >= 0);
385 init_autofs_dev_ioctl(¶m);
386 param.ioctlfd = ioctl_fd;
389 param.fail.token = token;
390 param.fail.status = status;
392 param.ready.token = token;
394 if (ioctl(dev_autofs_fd, status ? AUTOFS_DEV_IOCTL_FAIL : AUTOFS_DEV_IOCTL_READY, ¶m) < 0)
400 int automount_send_ready(Automount *a, int status) {
407 if (set_isempty(a->tokens))
410 if ((ioctl_fd = open_ioctl_fd(a->meta.manager->dev_autofs_fd, a->where, a->dev_id)) < 0) {
416 log_debug("Sending failure: %s", strerror(-status));
418 log_debug("Sending success.");
420 /* Autofs thankfully does not hand out 0 as a token */
421 while ((token = PTR_TO_UINT(set_steal_first(a->tokens)))) {
424 /* Autofs fun fact II:
426 * if you pass a positive status code here, the kernel will
429 if ((k = autofs_send_ready(a->meta.manager->dev_autofs_fd,
440 close_nointr_nofail(ioctl_fd);
445 static void automount_enter_waiting(Automount *a) {
446 int p[2] = { -1, -1 };
447 char name[32], options[128];
448 bool mounted = false;
449 int r, ioctl_fd = -1, dev_autofs_fd;
453 assert(a->pipe_fd < 0);
457 set_clear(a->tokens);
459 if ((dev_autofs_fd = open_dev_autofs(a->meta.manager)) < 0) {
464 /* We knowingly ignore the results of this call */
465 mkdir_p(a->where, 0555);
467 if (pipe2(p, O_NONBLOCK|O_CLOEXEC) < 0) {
472 snprintf(options, sizeof(options), "fd=%i,pgrp=%u,minproto=5,maxproto=5,direct", p[1], (unsigned) getpgrp());
473 char_array_0(options);
475 snprintf(name, sizeof(name), "systemd-%u", (unsigned) getpid());
478 if (mount(name, a->where, "autofs", 0, options) < 0) {
485 close_nointr_nofail(p[1]);
488 if (stat(a->where, &st) < 0) {
493 if ((ioctl_fd = open_ioctl_fd(dev_autofs_fd, a->where, st.st_dev)) < 0) {
498 if ((r = autofs_protocol(dev_autofs_fd, ioctl_fd)) < 0)
501 if ((r = autofs_set_timeout(dev_autofs_fd, ioctl_fd, 300)) < 0)
506 * Unless we close the ioctl fd here, for some weird reason
507 * the direct mount will not receive events from the
510 close_nointr_nofail(ioctl_fd);
513 if ((r = unit_watch_fd(UNIT(a), p[0], EPOLLIN, &a->pipe_watch)) < 0)
517 a->dev_id = st.st_dev;
519 automount_set_state(a, AUTOMOUNT_WAITING);
524 assert_se(close_pipe(p) == 0);
527 close_nointr_nofail(ioctl_fd);
530 repeat_unmout(a->where);
532 log_error("Failed to initialize automounter: %s", strerror(-r));
533 automount_enter_dead(a, false);
536 static void automount_enter_runnning(Automount *a) {
543 mkdir_p(a->where, a->directory_mode);
545 /* Before we do anything, let's see if somebody is playing games with us? */
546 if (lstat(a->where, &st) < 0) {
547 log_warning("%s failed stat automount point: %m", a->meta.id);
551 if (!S_ISDIR(st.st_mode) || st.st_dev != a->dev_id)
552 log_info("%s's automount point already active?", a->meta.id);
553 else if ((r = manager_add_job(a->meta.manager, JOB_START, UNIT(a->mount), JOB_REPLACE, true, NULL)) < 0) {
554 log_warning("%s failed to queue mount startup job: %s", a->meta.id, strerror(-r));
558 automount_set_state(a, AUTOMOUNT_RUNNING);
562 automount_enter_dead(a, false);
565 static int automount_start(Unit *u) {
566 Automount *a = AUTOMOUNT(u);
570 assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_MAINTENANCE);
572 if (path_is_mount_point(a->where)) {
573 log_error("Path %s is already a mount point, refusing start for %s", a->where, u->meta.id);
577 if (a->mount->meta.load_state != UNIT_LOADED)
581 automount_enter_waiting(a);
585 static int automount_stop(Unit *u) {
586 Automount *a = AUTOMOUNT(u);
590 assert(a->state == AUTOMOUNT_WAITING || a->state == AUTOMOUNT_RUNNING);
592 automount_enter_dead(a, true);
596 static int automount_serialize(Unit *u, FILE *f, FDSet *fds) {
597 Automount *a = AUTOMOUNT(u);
605 unit_serialize_item(u, f, "state", automount_state_to_string(a->state));
606 unit_serialize_item(u, f, "failure", yes_no(a->failure));
607 unit_serialize_item_format(u, f, "dev-id", "%u", (unsigned) a->dev_id);
609 SET_FOREACH(p, a->tokens, i)
610 unit_serialize_item_format(u, f, "token", "%u", PTR_TO_UINT(p));
612 if (a->pipe_fd >= 0) {
615 if ((copy = fdset_put_dup(fds, a->pipe_fd)) < 0)
618 unit_serialize_item_format(u, f, "pipe-fd", "%i", copy);
624 static int automount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
625 Automount *a = AUTOMOUNT(u);
631 if (streq(key, "state")) {
632 AutomountState state;
634 if ((state = automount_state_from_string(value)) < 0)
635 log_debug("Failed to parse state value %s", value);
637 a->deserialized_state = state;
638 } else if (streq(key, "failure")) {
641 if ((b = parse_boolean(value)) < 0)
642 log_debug("Failed to parse failure value %s", value);
644 a->failure = b || a->failure;
645 } else if (streq(key, "dev-id")) {
648 if (safe_atou(value, &d) < 0)
649 log_debug("Failed to parse dev-id value %s", value);
651 a->dev_id = (unsigned) d;
652 } else if (streq(key, "token")) {
655 if (safe_atou(value, &token) < 0)
656 log_debug("Failed to parse token value %s", value);
659 if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func)))
662 if ((r = set_put(a->tokens, UINT_TO_PTR(token))) < 0)
665 } else if (streq(key, "pipe-fd")) {
668 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
669 log_debug("Failed to parse pipe-fd value %s", value);
672 close_nointr_nofail(a->pipe_fd);
674 a->pipe_fd = fdset_remove(fds, fd);
677 log_debug("Unknown serialization key '%s'", key);
682 static UnitActiveState automount_active_state(Unit *u) {
685 return state_translation_table[AUTOMOUNT(u)->state];
688 static const char *automount_sub_state_to_string(Unit *u) {
691 return automount_state_to_string(AUTOMOUNT(u)->state);
694 static bool automount_check_gc(Unit *u) {
695 Automount *a = AUTOMOUNT(u);
699 return UNIT_VTABLE(UNIT(a->mount))->check_gc(UNIT(a->mount));
702 static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
703 Automount *a = AUTOMOUNT(u);
704 union autofs_v5_packet_union packet;
709 assert(fd == a->pipe_fd);
711 if (events != EPOLLIN) {
712 log_error("Got invalid poll event on pipe.");
716 if ((l = loop_read(a->pipe_fd, &packet, sizeof(packet), true)) != sizeof(packet)) {
717 log_error("Invalid read from pipe: %s", l < 0 ? strerror(-l) : "short read");
721 switch (packet.hdr.type) {
723 case autofs_ptype_missing_direct:
724 log_debug("Got direct mount request for %s", packet.v5_packet.name);
727 if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func))) {
728 log_error("Failed to allocate token set.");
732 if ((r = set_put(a->tokens, UINT_TO_PTR(packet.v5_packet.wait_queue_token))) < 0) {
733 log_error("Failed to remember token: %s", strerror(-r));
737 automount_enter_runnning(a);
741 log_error("Received unknown automount request %i", packet.hdr.type);
748 automount_enter_dead(a, false);
751 static void automount_shutdown(Manager *m) {
754 if (m->dev_autofs_fd >= 0)
755 close_nointr_nofail(m->dev_autofs_fd);
758 static const char* const automount_state_table[_AUTOMOUNT_STATE_MAX] = {
759 [AUTOMOUNT_DEAD] = "dead",
760 [AUTOMOUNT_WAITING] = "waiting",
761 [AUTOMOUNT_RUNNING] = "running",
762 [AUTOMOUNT_MAINTENANCE] = "maintenance"
765 DEFINE_STRING_TABLE_LOOKUP(automount_state, AutomountState);
767 const UnitVTable automount_vtable = {
768 .suffix = ".automount",
771 .no_instances = true,
773 .init = automount_init,
774 .load = automount_load,
775 .done = automount_done,
777 .coldplug = automount_coldplug,
779 .dump = automount_dump,
781 .start = automount_start,
782 .stop = automount_stop,
784 .serialize = automount_serialize,
785 .deserialize_item = automount_deserialize_item,
787 .active_state = automount_active_state,
788 .sub_state_to_string = automount_sub_state_to_string,
790 .check_gc = automount_check_gc,
792 .fd_event = automount_fd_event,
794 .bus_message_handler = bus_automount_message_handler,
796 .shutdown = automount_shutdown