1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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"
42 static const UnitActiveState state_translation_table
[_AUTOMOUNT_STATE_MAX
] = {
43 [AUTOMOUNT_DEAD
] = UNIT_INACTIVE
,
44 [AUTOMOUNT_WAITING
] = UNIT_ACTIVE
,
45 [AUTOMOUNT_RUNNING
] = UNIT_ACTIVE
,
46 [AUTOMOUNT_FAILED
] = UNIT_FAILED
49 static int open_dev_autofs(Manager
*m
);
51 static void automount_init(Unit
*u
) {
52 Automount
*a
= AUTOMOUNT(u
);
55 assert(u
->meta
.load_state
== UNIT_STUB
);
57 a
->pipe_watch
.fd
= a
->pipe_fd
= -1;
58 a
->pipe_watch
.type
= WATCH_INVALID
;
60 a
->directory_mode
= 0755;
63 static void repeat_unmout(const char *path
) {
67 /* If there are multiple mounts on a mount point, this
70 if (umount2(path
, MNT_DETACH
) >= 0)
74 log_error("Failed to unmount: %m");
80 static void unmount_autofs(Automount
*a
) {
86 automount_send_ready(a
, -EHOSTDOWN
);
88 unit_unwatch_fd(UNIT(a
), &a
->pipe_watch
);
89 close_nointr_nofail(a
->pipe_fd
);
92 /* If we reload/reexecute things we keep the mount point
95 (a
->meta
.manager
->exit_code
!= MANAGER_RELOAD
&&
96 a
->meta
.manager
->exit_code
!= MANAGER_REEXECUTE
))
97 repeat_unmout(a
->where
);
100 static void automount_done(Unit
*u
) {
101 Automount
*a
= AUTOMOUNT(u
);
115 int automount_add_one_mount_link(Automount
*a
, Mount
*m
) {
121 if (a
->meta
.load_state
!= UNIT_LOADED
||
122 m
->meta
.load_state
!= UNIT_LOADED
)
125 if (!path_startswith(a
->where
, m
->where
))
128 if (path_equal(a
->where
, m
->where
))
131 if ((r
= unit_add_two_dependencies(UNIT(a
), UNIT_AFTER
, 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_add_default_dependencies(Automount
*a
) {
155 if (a
->meta
.manager
->running_as
== MANAGER_SYSTEM
) {
157 if ((r
= unit_add_two_dependencies_by_name(UNIT(a
), UNIT_BEFORE
, UNIT_CONFLICTED_BY
, SPECIAL_UMOUNT_TARGET
, NULL
, true)) < 0)
164 static int automount_verify(Automount
*a
) {
169 if (a
->meta
.load_state
!= UNIT_LOADED
)
172 if (path_equal(a
->where
, "/")) {
173 log_error("Cannot have an automount unit for the root directory. Refusing.");
177 if (!(e
= unit_name_from_path(a
->where
, ".automount")))
180 b
= unit_has_name(UNIT(a
), e
);
184 log_error("%s's Where setting doesn't match unit name. Refusing.", a
->meta
.id
);
191 static int automount_load(Unit
*u
) {
193 Automount
*a
= AUTOMOUNT(u
);
196 assert(u
->meta
.load_state
== UNIT_STUB
);
198 /* Load a .automount file */
199 if ((r
= unit_load_fragment_and_dropin_optional(u
)) < 0)
202 if (u
->meta
.load_state
== UNIT_LOADED
) {
205 if (!(a
->where
= unit_name_to_path(u
->meta
.id
)))
208 path_kill_slashes(a
->where
);
210 if ((r
= automount_add_mount_links(a
)) < 0)
213 if ((r
= unit_load_related_unit(u
, ".mount", (Unit
**) &a
->mount
)) < 0)
216 if ((r
= unit_add_dependency(u
, UNIT_BEFORE
, UNIT(a
->mount
), true)) < 0)
219 if (a
->meta
.default_dependencies
)
220 if ((r
= automount_add_default_dependencies(a
)) < 0)
224 return automount_verify(a
);
227 static void automount_set_state(Automount
*a
, AutomountState state
) {
228 AutomountState old_state
;
231 old_state
= a
->state
;
234 if (state
!= AUTOMOUNT_WAITING
&&
235 state
!= AUTOMOUNT_RUNNING
)
238 if (state
!= old_state
)
239 log_debug("%s changed %s -> %s",
241 automount_state_to_string(old_state
),
242 automount_state_to_string(state
));
244 unit_notify(UNIT(a
), state_translation_table
[old_state
], state_translation_table
[state
]);
247 static int automount_coldplug(Unit
*u
) {
248 Automount
*a
= AUTOMOUNT(u
);
252 assert(a
->state
== AUTOMOUNT_DEAD
);
254 if (a
->deserialized_state
!= a
->state
) {
256 if ((r
= open_dev_autofs(u
->meta
.manager
)) < 0)
259 if (a
->deserialized_state
== AUTOMOUNT_WAITING
||
260 a
->deserialized_state
== AUTOMOUNT_RUNNING
) {
262 assert(a
->pipe_fd
>= 0);
264 if ((r
= unit_watch_fd(UNIT(a
), a
->pipe_fd
, EPOLLIN
, &a
->pipe_watch
)) < 0)
268 automount_set_state(a
, a
->deserialized_state
);
274 static void automount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
275 Automount
*a
= AUTOMOUNT(u
);
280 "%sAutomount State: %s\n"
282 "%sDirectoryMode: %04o\n",
283 prefix
, automount_state_to_string(a
->state
),
285 prefix
, a
->directory_mode
);
288 static void automount_enter_dead(Automount
*a
, bool success
) {
294 automount_set_state(a
, a
->failure
? AUTOMOUNT_FAILED
: AUTOMOUNT_DEAD
);
297 static int open_dev_autofs(Manager
*m
) {
298 struct autofs_dev_ioctl param
;
302 if (m
->dev_autofs_fd
>= 0)
303 return m
->dev_autofs_fd
;
305 label_fix("/dev/autofs");
307 if ((m
->dev_autofs_fd
= open("/dev/autofs", O_CLOEXEC
|O_RDONLY
)) < 0) {
308 log_error("Failed to open /dev/autofs: %s", strerror(errno
));
312 init_autofs_dev_ioctl(¶m
);
313 if (ioctl(m
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_VERSION
, ¶m
) < 0) {
314 close_nointr_nofail(m
->dev_autofs_fd
);
315 m
->dev_autofs_fd
= -1;
319 log_debug("Autofs kernel version %i.%i", param
.ver_major
, param
.ver_minor
);
321 return m
->dev_autofs_fd
;
324 static int open_ioctl_fd(int dev_autofs_fd
, const char *where
, dev_t devid
) {
325 struct autofs_dev_ioctl
*param
;
329 assert(dev_autofs_fd
>= 0);
332 l
= sizeof(struct autofs_dev_ioctl
) + strlen(where
) + 1;
334 if (!(param
= malloc(l
)))
337 init_autofs_dev_ioctl(param
);
340 param
->openmount
.devid
= devid
;
341 strcpy(param
->path
, where
);
343 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_OPENMOUNT
, param
) < 0) {
348 if (param
->ioctlfd
< 0) {
353 fd_cloexec(param
->ioctlfd
, true);
361 static int autofs_protocol(int dev_autofs_fd
, int ioctl_fd
) {
362 uint32_t major
, minor
;
363 struct autofs_dev_ioctl param
;
365 assert(dev_autofs_fd
>= 0);
366 assert(ioctl_fd
>= 0);
368 init_autofs_dev_ioctl(¶m
);
369 param
.ioctlfd
= ioctl_fd
;
371 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOVER
, ¶m
) < 0)
374 major
= param
.protover
.version
;
376 init_autofs_dev_ioctl(¶m
);
377 param
.ioctlfd
= ioctl_fd
;
379 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOSUBVER
, ¶m
) < 0)
382 minor
= param
.protosubver
.sub_version
;
384 log_debug("Autofs protocol version %i.%i", major
, minor
);
388 static int autofs_set_timeout(int dev_autofs_fd
, int ioctl_fd
, time_t sec
) {
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
;
396 param
.timeout
.timeout
= sec
;
398 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_TIMEOUT
, ¶m
) < 0)
404 static int autofs_send_ready(int dev_autofs_fd
, int ioctl_fd
, uint32_t token
, int status
) {
405 struct autofs_dev_ioctl param
;
407 assert(dev_autofs_fd
>= 0);
408 assert(ioctl_fd
>= 0);
410 init_autofs_dev_ioctl(¶m
);
411 param
.ioctlfd
= ioctl_fd
;
414 param
.fail
.token
= token
;
415 param
.fail
.status
= status
;
417 param
.ready
.token
= token
;
419 if (ioctl(dev_autofs_fd
, status
? AUTOFS_DEV_IOCTL_FAIL
: AUTOFS_DEV_IOCTL_READY
, ¶m
) < 0)
425 int automount_send_ready(Automount
*a
, int status
) {
432 if (set_isempty(a
->tokens
))
435 if ((ioctl_fd
= open_ioctl_fd(a
->meta
.manager
->dev_autofs_fd
, a
->where
, a
->dev_id
)) < 0) {
441 log_debug("Sending failure: %s", strerror(-status
));
443 log_debug("Sending success.");
447 /* Autofs thankfully does not hand out 0 as a token */
448 while ((token
= PTR_TO_UINT(set_steal_first(a
->tokens
)))) {
451 /* Autofs fun fact II:
453 * if you pass a positive status code here, the kernel will
456 if ((k
= autofs_send_ready(a
->meta
.manager
->dev_autofs_fd
,
465 close_nointr_nofail(ioctl_fd
);
470 static void automount_enter_waiting(Automount
*a
) {
471 int p
[2] = { -1, -1 };
472 char name
[32], options
[128];
473 bool mounted
= false;
474 int r
, ioctl_fd
= -1, dev_autofs_fd
;
478 assert(a
->pipe_fd
< 0);
482 set_clear(a
->tokens
);
484 if ((dev_autofs_fd
= open_dev_autofs(a
->meta
.manager
)) < 0) {
489 /* We knowingly ignore the results of this call */
490 mkdir_p(a
->where
, 0555);
492 if (pipe2(p
, O_NONBLOCK
|O_CLOEXEC
) < 0) {
497 snprintf(options
, sizeof(options
), "fd=%i,pgrp=%u,minproto=5,maxproto=5,direct", p
[1], (unsigned) getpgrp());
498 char_array_0(options
);
500 snprintf(name
, sizeof(name
), "systemd-%u", (unsigned) getpid());
503 if (mount(name
, a
->where
, "autofs", 0, options
) < 0) {
510 close_nointr_nofail(p
[1]);
513 if (stat(a
->where
, &st
) < 0) {
518 if ((ioctl_fd
= open_ioctl_fd(dev_autofs_fd
, a
->where
, st
.st_dev
)) < 0) {
523 if ((r
= autofs_protocol(dev_autofs_fd
, ioctl_fd
)) < 0)
526 if ((r
= autofs_set_timeout(dev_autofs_fd
, ioctl_fd
, 300)) < 0)
531 * Unless we close the ioctl fd here, for some weird reason
532 * the direct mount will not receive events from the
535 close_nointr_nofail(ioctl_fd
);
538 if ((r
= unit_watch_fd(UNIT(a
), p
[0], EPOLLIN
, &a
->pipe_watch
)) < 0)
542 a
->dev_id
= st
.st_dev
;
544 automount_set_state(a
, AUTOMOUNT_WAITING
);
549 assert_se(close_pipe(p
) == 0);
552 close_nointr_nofail(ioctl_fd
);
555 repeat_unmout(a
->where
);
557 log_error("Failed to initialize automounter: %s", strerror(-r
));
558 automount_enter_dead(a
, false);
561 static void automount_enter_runnning(Automount
*a
) {
569 dbus_error_init(&error
);
571 /* We don't take mount requests anymore if we are supposed to
572 * shut down anyway */
573 if (a
->meta
.job
&& a
->meta
.job
->type
== JOB_STOP
) {
574 automount_send_ready(a
, -EHOSTDOWN
);
578 mkdir_p(a
->where
, a
->directory_mode
);
580 /* Before we do anything, let's see if somebody is playing games with us? */
581 if (lstat(a
->where
, &st
) < 0) {
582 log_warning("%s failed stat automount point: %m", a
->meta
.id
);
586 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
!= a
->dev_id
)
587 log_info("%s's automount point already active?", a
->meta
.id
);
588 else if ((r
= manager_add_job(a
->meta
.manager
, JOB_START
, UNIT(a
->mount
), JOB_REPLACE
, true, &error
, NULL
)) < 0) {
589 log_warning("%s failed to queue mount startup job: %s", a
->meta
.id
, bus_error(&error
, r
));
593 automount_set_state(a
, AUTOMOUNT_RUNNING
);
597 automount_enter_dead(a
, false);
598 dbus_error_free(&error
);
601 static int automount_start(Unit
*u
) {
602 Automount
*a
= AUTOMOUNT(u
);
606 assert(a
->state
== AUTOMOUNT_DEAD
|| a
->state
== AUTOMOUNT_FAILED
);
608 if (path_is_mount_point(a
->where
)) {
609 log_error("Path %s is already a mount point, refusing start for %s", a
->where
, u
->meta
.id
);
613 if (a
->mount
->meta
.load_state
!= UNIT_LOADED
)
617 automount_enter_waiting(a
);
621 static int automount_stop(Unit
*u
) {
622 Automount
*a
= AUTOMOUNT(u
);
626 assert(a
->state
== AUTOMOUNT_WAITING
|| a
->state
== AUTOMOUNT_RUNNING
);
628 automount_enter_dead(a
, true);
632 static int automount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
633 Automount
*a
= AUTOMOUNT(u
);
641 unit_serialize_item(u
, f
, "state", automount_state_to_string(a
->state
));
642 unit_serialize_item(u
, f
, "failure", yes_no(a
->failure
));
643 unit_serialize_item_format(u
, f
, "dev-id", "%u", (unsigned) a
->dev_id
);
645 SET_FOREACH(p
, a
->tokens
, i
)
646 unit_serialize_item_format(u
, f
, "token", "%u", PTR_TO_UINT(p
));
648 if (a
->pipe_fd
>= 0) {
651 if ((copy
= fdset_put_dup(fds
, a
->pipe_fd
)) < 0)
654 unit_serialize_item_format(u
, f
, "pipe-fd", "%i", copy
);
660 static int automount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
661 Automount
*a
= AUTOMOUNT(u
);
667 if (streq(key
, "state")) {
668 AutomountState state
;
670 if ((state
= automount_state_from_string(value
)) < 0)
671 log_debug("Failed to parse state value %s", value
);
673 a
->deserialized_state
= state
;
674 } else if (streq(key
, "failure")) {
677 if ((b
= parse_boolean(value
)) < 0)
678 log_debug("Failed to parse failure value %s", value
);
680 a
->failure
= b
|| a
->failure
;
681 } else if (streq(key
, "dev-id")) {
684 if (safe_atou(value
, &d
) < 0)
685 log_debug("Failed to parse dev-id value %s", value
);
687 a
->dev_id
= (unsigned) d
;
688 } else if (streq(key
, "token")) {
691 if (safe_atou(value
, &token
) < 0)
692 log_debug("Failed to parse token value %s", value
);
695 if (!(a
->tokens
= set_new(trivial_hash_func
, trivial_compare_func
)))
698 if ((r
= set_put(a
->tokens
, UINT_TO_PTR(token
))) < 0)
701 } else if (streq(key
, "pipe-fd")) {
704 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
705 log_debug("Failed to parse pipe-fd value %s", value
);
708 close_nointr_nofail(a
->pipe_fd
);
710 a
->pipe_fd
= fdset_remove(fds
, fd
);
713 log_debug("Unknown serialization key '%s'", key
);
718 static UnitActiveState
automount_active_state(Unit
*u
) {
721 return state_translation_table
[AUTOMOUNT(u
)->state
];
724 static const char *automount_sub_state_to_string(Unit
*u
) {
727 return automount_state_to_string(AUTOMOUNT(u
)->state
);
730 static bool automount_check_gc(Unit
*u
) {
731 Automount
*a
= AUTOMOUNT(u
);
738 return UNIT_VTABLE(UNIT(a
->mount
))->check_gc(UNIT(a
->mount
));
741 static void automount_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
742 Automount
*a
= AUTOMOUNT(u
);
743 union autofs_v5_packet_union packet
;
748 assert(fd
== a
->pipe_fd
);
750 if (events
!= EPOLLIN
) {
751 log_error("Got invalid poll event on pipe.");
755 if ((l
= loop_read(a
->pipe_fd
, &packet
, sizeof(packet
), true)) != sizeof(packet
)) {
756 log_error("Invalid read from pipe: %s", l
< 0 ? strerror(-l
) : "short read");
760 switch (packet
.hdr
.type
) {
762 case autofs_ptype_missing_direct
:
764 if (packet
.v5_packet
.pid
> 0) {
767 get_process_name(packet
.v5_packet
.pid
, &p
);
768 log_debug("Got direct mount request for %s, triggered by %lu (%s)", packet
.v5_packet
.name
, (unsigned long) packet
.v5_packet
.pid
, strna(p
));
772 log_debug("Got direct mount request for %s", packet
.v5_packet
.name
);
775 if (!(a
->tokens
= set_new(trivial_hash_func
, trivial_compare_func
))) {
776 log_error("Failed to allocate token set.");
780 if ((r
= set_put(a
->tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
))) < 0) {
781 log_error("Failed to remember token: %s", strerror(-r
));
785 automount_enter_runnning(a
);
789 log_error("Received unknown automount request %i", packet
.hdr
.type
);
796 automount_enter_dead(a
, false);
799 static void automount_shutdown(Manager
*m
) {
802 if (m
->dev_autofs_fd
>= 0)
803 close_nointr_nofail(m
->dev_autofs_fd
);
806 static void automount_reset_failed(Unit
*u
) {
807 Automount
*a
= AUTOMOUNT(u
);
811 if (a
->state
== AUTOMOUNT_FAILED
)
812 automount_set_state(a
, AUTOMOUNT_DEAD
);
817 static const char* const automount_state_table
[_AUTOMOUNT_STATE_MAX
] = {
818 [AUTOMOUNT_DEAD
] = "dead",
819 [AUTOMOUNT_WAITING
] = "waiting",
820 [AUTOMOUNT_RUNNING
] = "running",
821 [AUTOMOUNT_FAILED
] = "failed"
824 DEFINE_STRING_TABLE_LOOKUP(automount_state
, AutomountState
);
826 const UnitVTable automount_vtable
= {
827 .suffix
= ".automount",
830 .no_instances
= true,
832 .init
= automount_init
,
833 .load
= automount_load
,
834 .done
= automount_done
,
836 .coldplug
= automount_coldplug
,
838 .dump
= automount_dump
,
840 .start
= automount_start
,
841 .stop
= automount_stop
,
843 .serialize
= automount_serialize
,
844 .deserialize_item
= automount_deserialize_item
,
846 .active_state
= automount_active_state
,
847 .sub_state_to_string
= automount_sub_state_to_string
,
849 .check_gc
= automount_check_gc
,
851 .fd_event
= automount_fd_event
,
853 .reset_failed
= automount_reset_failed
,
855 .bus_interface
= "org.freedesktop.systemd1.Automount",
856 .bus_message_handler
= bus_automount_message_handler
,
858 .shutdown
= automount_shutdown