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_MAINTAINANCE] = UNIT_INACTIVE,
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;
58 static void repeat_unmout(const char *path) {
63 if (umount2(path, MNT_DETACH) >= 0)
67 log_error("Failed to unmount: %m");
73 static void unmount_autofs(Automount *a) {
79 automount_send_ready(a, -EHOSTDOWN);
81 unit_unwatch_fd(UNIT(a), &a->pipe_watch);
82 close_nointr_nofail(a->pipe_fd);
85 /* If we reload/reexecute things we keep the mount point
88 (UNIT(a)->meta.manager->exit_code != MANAGER_RELOAD &&
89 UNIT(a)->meta.manager->exit_code != MANAGER_REEXECUTE))
90 repeat_unmout(a->where);
93 static void automount_done(Unit *u) {
94 Automount *a = AUTOMOUNT(u);
108 int automount_add_one_mount_link(Automount *a, Mount *m) {
114 if (a->meta.load_state != UNIT_LOADED ||
115 m->meta.load_state != UNIT_LOADED)
118 if (!path_startswith(a->where, m->where))
121 if ((r = unit_add_dependency(UNIT(m), UNIT_BEFORE, UNIT(a), true)) < 0)
124 if ((r = unit_add_dependency(UNIT(a), UNIT_REQUIRES, UNIT(m), true)) < 0)
130 static int automount_add_mount_links(Automount *a) {
136 LIST_FOREACH(units_per_type, other, a->meta.manager->units_per_type[UNIT_MOUNT])
137 if ((r = automount_add_one_mount_link(a, (Mount*) other)) < 0)
143 static int automount_verify(Automount *a) {
148 if (UNIT(a)->meta.load_state != UNIT_LOADED)
151 if (!(e = unit_name_from_path(a->where, ".automount")))
154 b = unit_has_name(UNIT(a), e);
158 log_error("%s's Where setting doesn't match unit name. Refusing.", UNIT(a)->meta.id);
165 static int automount_load(Unit *u) {
167 Automount *a = AUTOMOUNT(u);
170 assert(u->meta.load_state == UNIT_STUB);
172 /* Load a .automount file */
173 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
176 if (u->meta.load_state == UNIT_LOADED) {
179 if (!(a->where = unit_name_to_path(u->meta.id)))
182 path_kill_slashes(a->where);
184 if ((r = automount_add_mount_links(a)) < 0)
187 if ((r = unit_load_related_unit(u, ".mount", (Unit**) &a->mount)) < 0)
190 if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(a->mount), true)) < 0)
194 return automount_verify(a);
197 static void automount_set_state(Automount *a, AutomountState state) {
198 AutomountState old_state;
201 old_state = a->state;
204 if (state != AUTOMOUNT_WAITING &&
205 state != AUTOMOUNT_RUNNING)
208 if (state != old_state)
209 log_debug("%s changed %s -> %s",
211 automount_state_to_string(old_state),
212 automount_state_to_string(state));
214 unit_notify(UNIT(a), state_translation_table[old_state], state_translation_table[state]);
217 static int automount_coldplug(Unit *u) {
218 Automount *a = AUTOMOUNT(u);
222 assert(a->state == AUTOMOUNT_DEAD);
224 if (a->deserialized_state != a->state) {
226 if ((r = open_dev_autofs(u->meta.manager)) < 0)
229 if (a->deserialized_state == AUTOMOUNT_WAITING ||
230 a->deserialized_state == AUTOMOUNT_RUNNING) {
232 assert(a->pipe_fd >= 0);
234 if ((r = unit_watch_fd(UNIT(a), a->pipe_fd, EPOLLIN, &a->pipe_watch)) < 0)
238 automount_set_state(a, a->deserialized_state);
244 static void automount_dump(Unit *u, FILE *f, const char *prefix) {
245 Automount *a = AUTOMOUNT(u);
250 "%sAutomount State: %s\n"
252 prefix, automount_state_to_string(a->state),
256 static void automount_enter_dead(Automount *a, bool success) {
262 automount_set_state(a, a->failure ? AUTOMOUNT_MAINTAINANCE : AUTOMOUNT_DEAD);
265 static int open_dev_autofs(Manager *m) {
266 struct autofs_dev_ioctl param;
270 if (m->dev_autofs_fd >= 0)
271 return m->dev_autofs_fd;
273 if ((m->dev_autofs_fd = open("/dev/autofs", O_CLOEXEC|O_RDONLY)) < 0) {
274 log_error("Failed to open /dev/autofs: %s", strerror(errno));
278 init_autofs_dev_ioctl(¶m);
279 if (ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, ¶m) < 0) {
280 close_nointr_nofail(m->dev_autofs_fd);
281 m->dev_autofs_fd = -1;
285 log_debug("Autofs kernel version %i.%i", param.ver_major, param.ver_minor);
287 return m->dev_autofs_fd;
290 static int open_ioctl_fd(int dev_autofs_fd, const char *where, dev_t devid) {
291 struct autofs_dev_ioctl *param;
295 assert(dev_autofs_fd >= 0);
298 l = sizeof(struct autofs_dev_ioctl) + strlen(where) + 1;
300 if (!(param = malloc(l)))
303 init_autofs_dev_ioctl(param);
306 param->openmount.devid = devid;
307 strcpy(param->path, where);
309 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_OPENMOUNT, param) < 0) {
314 if (param->ioctlfd < 0) {
319 fd_cloexec(param->ioctlfd, true);
327 static int autofs_protocol(int dev_autofs_fd, int ioctl_fd) {
328 uint32_t major, minor;
329 struct autofs_dev_ioctl param;
331 assert(dev_autofs_fd >= 0);
332 assert(ioctl_fd >= 0);
334 init_autofs_dev_ioctl(¶m);
335 param.ioctlfd = ioctl_fd;
337 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOVER, ¶m) < 0)
340 major = param.protover.version;
342 init_autofs_dev_ioctl(¶m);
343 param.ioctlfd = ioctl_fd;
345 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOSUBVER, ¶m) < 0)
348 minor = param.protosubver.sub_version;
350 log_debug("Autofs protocol version %i.%i", major, minor);
354 static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, time_t sec) {
355 struct autofs_dev_ioctl param;
357 assert(dev_autofs_fd >= 0);
358 assert(ioctl_fd >= 0);
360 init_autofs_dev_ioctl(¶m);
361 param.ioctlfd = ioctl_fd;
362 param.timeout.timeout = sec;
364 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, ¶m) < 0)
370 static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, int status) {
371 struct autofs_dev_ioctl param;
373 assert(dev_autofs_fd >= 0);
374 assert(ioctl_fd >= 0);
376 init_autofs_dev_ioctl(¶m);
377 param.ioctlfd = ioctl_fd;
380 param.fail.token = token;
381 param.fail.status = status;
383 param.ready.token = token;
385 if (ioctl(dev_autofs_fd, status ? AUTOFS_DEV_IOCTL_FAIL : AUTOFS_DEV_IOCTL_READY, ¶m) < 0)
391 int automount_send_ready(Automount *a, int status) {
398 if (set_isempty(a->tokens))
401 if ((ioctl_fd = open_ioctl_fd(UNIT(a)->meta.manager->dev_autofs_fd, a->where, a->dev_id)) < 0) {
407 log_debug("Sending failure: %s", strerror(-status));
409 log_debug("Sending success.");
411 /* Autofs thankfully does not hand out 0 as a token */
412 while ((token = PTR_TO_UINT(set_steal_first(a->tokens)))) {
415 /* Autofs fun fact II:
417 * if you pass a positive status code here, the kernel will
420 if ((k = autofs_send_ready(UNIT(a)->meta.manager->dev_autofs_fd,
431 close_nointr_nofail(ioctl_fd);
436 static void automount_enter_waiting(Automount *a) {
437 int p[2] = { -1, -1 };
438 char name[32], options[128];
439 bool mounted = false;
440 int r, ioctl_fd = -1, dev_autofs_fd;
444 assert(a->pipe_fd < 0);
448 set_clear(a->tokens);
450 if ((dev_autofs_fd = open_dev_autofs(UNIT(a)->meta.manager)) < 0) {
455 /* We knowingly ignore the results of this call */
456 mkdir_p(a->where, 0555);
458 if (pipe2(p, O_NONBLOCK|O_CLOEXEC) < 0) {
463 snprintf(options, sizeof(options), "fd=%i,pgrp=%u,minproto=5,maxproto=5,direct", p[1], (unsigned) getpgrp());
464 char_array_0(options);
466 snprintf(name, sizeof(name), "systemd-%u", (unsigned) getpid());
469 if (mount(name, a->where, "autofs", 0, options) < 0) {
476 close_nointr_nofail(p[1]);
479 if (stat(a->where, &st) < 0) {
484 if ((ioctl_fd = open_ioctl_fd(dev_autofs_fd, a->where, st.st_dev)) < 0) {
489 if ((r = autofs_protocol(dev_autofs_fd, ioctl_fd)) < 0)
492 if ((r = autofs_set_timeout(dev_autofs_fd, ioctl_fd, 300)) < 0)
497 * Unless we close the ioctl fd here, for some weird reason
498 * the direct mount will not receive events from the
501 close_nointr_nofail(ioctl_fd);
504 if ((r = unit_watch_fd(UNIT(a), p[0], EPOLLIN, &a->pipe_watch)) < 0)
508 a->dev_id = st.st_dev;
510 automount_set_state(a, AUTOMOUNT_WAITING);
515 assert_se(close_pipe(p) == 0);
518 close_nointr_nofail(ioctl_fd);
521 repeat_unmout(a->where);
523 log_error("Failed to initialize automounter: %s", strerror(-r));
524 automount_enter_dead(a, false);
527 static void automount_enter_runnning(Automount *a) {
534 /* Before we do anything, let's see if somebody is playing games with us? */
536 if (stat(a->where, &st) < 0) {
537 log_warning("%s failed stat automount point: %m", a->meta.id);
541 if (!S_ISDIR(st.st_mode) || st.st_dev != a->dev_id)
542 log_info("%s's automount point already active?", a->meta.id);
543 else if ((r = manager_add_job(UNIT(a)->meta.manager, JOB_START, UNIT(a->mount), JOB_REPLACE, true, NULL)) < 0) {
544 log_warning("%s failed to queue mount startup job: %s", a->meta.id, strerror(-r));
548 automount_set_state(a, AUTOMOUNT_RUNNING);
552 automount_enter_dead(a, false);
555 static int automount_start(Unit *u) {
556 Automount *a = AUTOMOUNT(u);
560 if (path_is_mount_point(a->where)) {
561 log_error("Path %s is already a mount point, refusing start for %s", a->where, u->meta.id);
565 assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_MAINTAINANCE);
568 automount_enter_waiting(a);
572 static int automount_stop(Unit *u) {
573 Automount *a = AUTOMOUNT(u);
577 assert(a->state == AUTOMOUNT_WAITING || a->state == AUTOMOUNT_RUNNING);
579 automount_enter_dead(a, true);
583 static int automount_serialize(Unit *u, FILE *f, FDSet *fds) {
584 Automount *a = AUTOMOUNT(u);
592 unit_serialize_item(u, f, "state", automount_state_to_string(a->state));
593 unit_serialize_item(u, f, "failure", yes_no(a->failure));
594 unit_serialize_item_format(u, f, "dev-id", "%u", (unsigned) a->dev_id);
596 SET_FOREACH(p, a->tokens, i)
597 unit_serialize_item_format(u, f, "token", "%u", PTR_TO_UINT(p));
599 if (a->pipe_fd >= 0) {
602 if ((copy = fdset_put_dup(fds, a->pipe_fd)) < 0)
605 unit_serialize_item_format(u, f, "pipe-fd", "%i", copy);
611 static int automount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
612 Automount *a = AUTOMOUNT(u);
618 if (streq(key, "state")) {
619 AutomountState state;
621 if ((state = automount_state_from_string(value)) < 0)
622 log_debug("Failed to parse state value %s", value);
624 a->deserialized_state = state;
625 } else if (streq(key, "failure")) {
628 if ((b = parse_boolean(value)) < 0)
629 log_debug("Failed to parse failure value %s", value);
631 a->failure = b || a->failure;
632 } else if (streq(key, "dev-id")) {
635 if (safe_atou(value, &d) < 0)
636 log_debug("Failed to parse dev-id value %s", value);
638 a->dev_id = (unsigned) d;
639 } else if (streq(key, "token")) {
642 if (safe_atou(value, &token) < 0)
643 log_debug("Failed to parse token value %s", value);
646 if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func)))
649 if ((r = set_put(a->tokens, UINT_TO_PTR(token))) < 0)
652 } else if (streq(key, "pipe-fd")) {
655 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
656 log_debug("Failed to parse pipe-fd value %s", value);
659 close_nointr_nofail(a->pipe_fd);
661 a->pipe_fd = fdset_remove(fds, fd);
664 log_debug("Unknown serialization key '%s'", key);
669 static UnitActiveState automount_active_state(Unit *u) {
672 return state_translation_table[AUTOMOUNT(u)->state];
675 static const char *automount_sub_state_to_string(Unit *u) {
678 return automount_state_to_string(AUTOMOUNT(u)->state);
681 static bool automount_check_gc(Unit *u) {
682 Automount *a = AUTOMOUNT(u);
686 return UNIT_VTABLE(UNIT(a->mount))->check_gc(UNIT(a->mount));
689 static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
690 Automount *a = AUTOMOUNT(u);
691 union autofs_v5_packet_union packet;
696 assert(fd == a->pipe_fd);
698 if (events != EPOLLIN) {
699 log_error("Got invalid poll event on pipe.");
703 if ((l = loop_read(a->pipe_fd, &packet, sizeof(packet))) != sizeof(packet)) {
704 log_error("Invalid read from pipe: %s", l < 0 ? strerror(-l) : "short read");
708 switch (packet.hdr.type) {
710 case autofs_ptype_missing_direct:
711 log_debug("Got direct mount request for %s", packet.v5_packet.name);
714 if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func))) {
715 log_error("Failed to allocate token set.");
719 if ((r = set_put(a->tokens, UINT_TO_PTR(packet.v5_packet.wait_queue_token))) < 0) {
720 log_error("Failed to remember token: %s", strerror(-r));
724 automount_enter_runnning(a);
728 log_error("Received unknown automount request %i", packet.hdr.type);
735 automount_enter_dead(a, false);
738 static void automount_shutdown(Manager *m) {
741 if (m->dev_autofs_fd >= 0)
742 close_nointr_nofail(m->dev_autofs_fd);
745 static const char* const automount_state_table[_AUTOMOUNT_STATE_MAX] = {
746 [AUTOMOUNT_DEAD] = "dead",
747 [AUTOMOUNT_WAITING] = "waiting",
748 [AUTOMOUNT_RUNNING] = "running",
749 [AUTOMOUNT_MAINTAINANCE] = "maintainance"
752 DEFINE_STRING_TABLE_LOOKUP(automount_state, AutomountState);
754 const UnitVTable automount_vtable = {
755 .suffix = ".automount",
758 .no_instances = true,
760 .init = automount_init,
761 .load = automount_load,
762 .done = automount_done,
764 .coldplug = automount_coldplug,
766 .dump = automount_dump,
768 .start = automount_start,
769 .stop = automount_stop,
771 .serialize = automount_serialize,
772 .deserialize_item = automount_deserialize_item,
774 .active_state = automount_active_state,
775 .sub_state_to_string = automount_sub_state_to_string,
777 .check_gc = automount_check_gc,
779 .fd_event = automount_fd_event,
781 .bus_message_handler = bus_automount_message_handler,
783 .shutdown = automount_shutdown