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"
38 static const UnitActiveState state_translation_table[_AUTOMOUNT_STATE_MAX] = {
39 [AUTOMOUNT_DEAD] = UNIT_INACTIVE,
40 [AUTOMOUNT_WAITING] = UNIT_ACTIVE,
41 [AUTOMOUNT_RUNNING] = UNIT_ACTIVE,
42 [AUTOMOUNT_MAINTAINANCE] = UNIT_INACTIVE,
45 static const char* const state_string_table[_AUTOMOUNT_STATE_MAX] = {
46 [AUTOMOUNT_DEAD] = "dead",
47 [AUTOMOUNT_WAITING] = "waiting",
48 [AUTOMOUNT_RUNNING] = "running",
49 [AUTOMOUNT_MAINTAINANCE] = "maintainance"
52 static char *automount_name_from_where(const char *where) {
55 if (streq(where, "/"))
56 return strdup("-.automount");
58 return unit_name_build_escape(where+1, NULL, ".automount");
61 static void automount_init(Unit *u) {
62 Automount *a = AUTOMOUNT(u);
65 assert(u->meta.load_state == UNIT_STUB);
67 a->pipe_watch.fd = a->pipe_fd = -1;
70 static void repeat_unmout(const char *path) {
75 if (umount2(path, MNT_DETACH) >= 0)
79 log_error("Failed to unmount: %m");
85 static void unmount_autofs(Automount *a) {
91 automount_send_ready(a, -EHOSTDOWN);
93 unit_unwatch_fd(UNIT(a), &a->pipe_watch);
94 close_nointr_nofail(a->pipe_fd);
97 repeat_unmout(a->where);
100 static void automount_done(Unit *u) {
101 Automount *a = AUTOMOUNT(u);
114 static int automount_verify(Automount *a) {
119 if (UNIT(a)->meta.load_state != UNIT_LOADED)
123 log_error("%s lacks Where setting. Refusing.", UNIT(a)->meta.id);
127 path_kill_slashes(a->where);
129 if (!(e = automount_name_from_where(a->where)))
132 b = unit_has_name(UNIT(a), e);
136 log_error("%s's Where setting doesn't match unit name. Refusing.", UNIT(a)->meta.id);
143 static int automount_load(Unit *u) {
145 Automount *a = AUTOMOUNT(u);
148 assert(u->meta.load_state == UNIT_STUB);
150 /* Load a .automount file */
151 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
154 if (u->meta.load_state == UNIT_LOADED) {
156 if ((r = unit_load_related_unit(u, ".mount", (Unit**) &a->mount)) < 0)
159 if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(a->mount))) < 0)
163 return automount_verify(a);
166 static void automount_set_state(Automount *a, AutomountState state) {
167 AutomountState old_state;
170 old_state = a->state;
173 if (state != AUTOMOUNT_WAITING &&
174 state != AUTOMOUNT_RUNNING)
177 if (state != old_state)
178 log_debug("%s changed %s → %s", UNIT(a)->meta.id, state_string_table[old_state], state_string_table[state]);
180 unit_notify(UNIT(a), state_translation_table[old_state], state_translation_table[state]);
183 static void automount_dump(Unit *u, FILE *f, const char *prefix) {
184 Automount *s = AUTOMOUNT(u);
189 "%sAutomount State: %s\n",
190 prefix, state_string_table[s->state]);
193 static void automount_enter_dead(Automount *a, bool success) {
199 automount_set_state(a, a->failure ? AUTOMOUNT_MAINTAINANCE : AUTOMOUNT_DEAD);
202 static int open_dev_autofs(Manager *m) {
203 struct autofs_dev_ioctl param;
207 if (m->dev_autofs_fd >= 0)
208 return m->dev_autofs_fd;
210 if ((m->dev_autofs_fd = open("/dev/autofs", O_RDONLY)) < 0) {
211 log_error("Failed to open /dev/autofs: %s", strerror(errno));
215 init_autofs_dev_ioctl(¶m);
216 if (ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, ¶m) < 0) {
217 close_nointr_nofail(m->dev_autofs_fd);
218 m->dev_autofs_fd = -1;
222 log_debug("Autofs kernel version %i.%i", param.ver_major, param.ver_minor);
224 return m->dev_autofs_fd;
227 static int open_ioctl_fd(int dev_autofs_fd, const char *where, dev_t devid) {
228 struct autofs_dev_ioctl *param;
232 assert(dev_autofs_fd >= 0);
235 l = sizeof(struct autofs_dev_ioctl) + strlen(where) + 1;
237 if (!(param = malloc(l)))
240 init_autofs_dev_ioctl(param);
243 param->openmount.devid = devid;
244 strcpy(param->path, where);
246 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_OPENMOUNT, param) < 0) {
251 if (param->ioctlfd < 0) {
263 static int autofs_protocol(int dev_autofs_fd, int ioctl_fd) {
264 uint32_t major, minor;
265 struct autofs_dev_ioctl param;
267 assert(dev_autofs_fd >= 0);
268 assert(ioctl_fd >= 0);
270 init_autofs_dev_ioctl(¶m);
271 param.ioctlfd = ioctl_fd;
273 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOVER, ¶m) < 0)
276 major = param.protover.version;
278 init_autofs_dev_ioctl(¶m);
279 param.ioctlfd = ioctl_fd;
281 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOSUBVER, ¶m) < 0)
284 minor = param.protosubver.sub_version;
286 log_debug("Autofs protocol version %i.%i", major, minor);
290 static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, time_t sec) {
291 struct autofs_dev_ioctl param;
293 assert(dev_autofs_fd >= 0);
294 assert(ioctl_fd >= 0);
296 init_autofs_dev_ioctl(¶m);
297 param.ioctlfd = ioctl_fd;
298 param.timeout.timeout = sec;
300 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, ¶m) < 0)
306 static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, int status) {
307 struct autofs_dev_ioctl param;
309 assert(dev_autofs_fd >= 0);
310 assert(ioctl_fd >= 0);
312 init_autofs_dev_ioctl(¶m);
313 param.ioctlfd = ioctl_fd;
316 param.fail.token = token;
317 param.fail.status = status;
319 param.ready.token = token;
321 if (ioctl(dev_autofs_fd, status ? AUTOFS_DEV_IOCTL_FAIL : AUTOFS_DEV_IOCTL_READY, ¶m) < 0)
327 int automount_send_ready(Automount *a, int status) {
334 if (set_isempty(a->tokens))
337 if ((ioctl_fd = open_ioctl_fd(UNIT(a)->meta.manager->dev_autofs_fd, a->where, a->dev_id)) < 0) {
343 log_debug("Sending failure: %s", strerror(-status));
345 log_debug("Sending success.");
347 /* Autofs thankfully does not hand out 0 as a token */
348 while ((token = PTR_TO_UINT(set_steal_first(a->tokens)))) {
351 /* Autofs fun fact II:
353 * if you pass a positive status code here, the kernel will
356 if ((k = autofs_send_ready(UNIT(a)->meta.manager->dev_autofs_fd,
367 close_nointr_nofail(ioctl_fd);
372 static void automount_enter_waiting(Automount *a) {
373 int p[2] = { -1, -1 };
374 char name[32], options[128];
375 bool mounted = false;
376 int r, ioctl_fd = -1, dev_autofs_fd;
380 assert(a->pipe_fd < 0);
384 set_clear(a->tokens);
385 else if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func))) {
390 if ((dev_autofs_fd = open_dev_autofs(UNIT(a)->meta.manager)) < 0) {
395 /* We knowingly ignore the results of this call */
396 mkdir_p(a->where, 0555);
398 if (pipe2(p, O_NONBLOCK) < 0) {
403 snprintf(options, sizeof(options), "fd=%i,pgrp=%u,minproto=5,maxproto=5,direct", p[1], (unsigned) getpgrp());
404 char_array_0(options);
406 snprintf(name, sizeof(name), "systemd-%u", (unsigned) getpid());
409 if (mount(name, a->where, "autofs", 0, options) < 0) {
416 close_nointr_nofail(p[1]);
419 if (stat(a->where, &st) < 0) {
424 if ((ioctl_fd = open_ioctl_fd(dev_autofs_fd, a->where, st.st_dev)) < 0) {
429 if ((r = autofs_protocol(dev_autofs_fd, ioctl_fd)) < 0)
432 if ((r = autofs_set_timeout(dev_autofs_fd, ioctl_fd, 300)) < 0)
437 * Unless we close the ioctl fd here, for some weird reason
438 * the direct mount will not receive events from the
441 close_nointr_nofail(ioctl_fd);
444 if ((r = unit_watch_fd(UNIT(a), p[0], EPOLLIN, &a->pipe_watch)) < 0)
448 a->dev_id = st.st_dev;
450 automount_set_state(a, AUTOMOUNT_WAITING);
455 assert_se(close_pipe(p) == 0);
458 close_nointr_nofail(ioctl_fd);
461 repeat_unmout(a->where);
463 log_error("Failed to initialize automounter: %s", strerror(-r));
464 automount_enter_dead(a, false);
467 static void automount_enter_runnning(Automount *a) {
474 /* Before we do anything, let's see if somebody is playing games with us? */
476 if (stat(a->where, &st) < 0) {
477 log_warning("%s failed stat automount point: %m", a->meta.id);
481 if (!S_ISDIR(st.st_mode) || st.st_dev != a->dev_id)
482 log_info("%s's automount point already active?", a->meta.id);
483 else if ((r = manager_add_job(UNIT(a)->meta.manager, JOB_START, UNIT(a->mount), JOB_REPLACE, true, NULL)) < 0) {
484 log_warning("%s failed to queue mount startup job: %s", a->meta.id, strerror(-r));
488 automount_set_state(a, AUTOMOUNT_RUNNING);
492 automount_enter_dead(a, false);
495 static int automount_start(Unit *u) {
496 Automount *a = AUTOMOUNT(u);
500 if (path_is_mount_point(a->where)) {
501 log_error("Path %s is already a mount point, refusing start for %s", a->where, u->meta.id);
505 assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_MAINTAINANCE);
508 automount_enter_waiting(a);
512 static int automount_stop(Unit *u) {
513 Automount *a = AUTOMOUNT(u);
517 assert(a->state == AUTOMOUNT_WAITING || a->state == AUTOMOUNT_RUNNING);
519 automount_enter_dead(a, true);
523 static UnitActiveState automount_active_state(Unit *u) {
525 return state_translation_table[AUTOMOUNT(u)->state];
528 static const char *automount_sub_state_to_string(Unit *u) {
531 return state_string_table[AUTOMOUNT(u)->state];
534 static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
535 union autofs_v5_packet_union packet;
539 Automount *a = AUTOMOUNT(u);
542 assert(fd == a->pipe_fd);
544 if (events != EPOLLIN) {
545 log_error("Got invalid poll event on pipe.");
549 if ((l = loop_read(a->pipe_fd, &packet, sizeof(packet))) != sizeof(packet)) {
550 log_error("Invalid read from pipe: %s", l < 0 ? strerror(-l) : "short read");
554 switch (packet.hdr.type) {
556 case autofs_ptype_missing_direct:
557 log_debug("Got direct mount request for %s", packet.v5_packet.name);
559 if ((r = set_put(a->tokens, UINT_TO_PTR(packet.v5_packet.wait_queue_token))) < 0) {
560 log_error("Failed to remember token: %s", strerror(-r));
564 automount_enter_runnning(a);
568 log_error("Received unknown automount request %i", packet.hdr.type);
575 automount_enter_dead(a, false);
578 static void automount_shutdown(Manager *m) {
581 if (m->dev_autofs_fd >= 0)
582 close_nointr_nofail(m->dev_autofs_fd);
585 const UnitVTable automount_vtable = {
586 .suffix = ".automount",
589 .no_instances = true,
591 .init = automount_init,
592 .load = automount_load,
593 .done = automount_done,
595 .dump = automount_dump,
597 .start = automount_start,
598 .stop = automount_stop,
600 .active_state = automount_active_state,
601 .sub_state_to_string = automount_sub_state_to_string,
603 .fd_event = automount_fd_event,
605 .shutdown = automount_shutdown