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;
62 a
->meta
.ignore_on_isolate
= true;
65 static void repeat_unmout(const char *path
) {
69 /* If there are multiple mounts on a mount point, this
72 if (umount2(path
, MNT_DETACH
) >= 0)
76 log_error("Failed to unmount: %m");
82 static void unmount_autofs(Automount
*a
) {
88 automount_send_ready(a
, -EHOSTDOWN
);
90 unit_unwatch_fd(UNIT(a
), &a
->pipe_watch
);
91 close_nointr_nofail(a
->pipe_fd
);
94 /* If we reload/reexecute things we keep the mount point
97 (a
->meta
.manager
->exit_code
!= MANAGER_RELOAD
&&
98 a
->meta
.manager
->exit_code
!= MANAGER_REEXECUTE
))
99 repeat_unmout(a
->where
);
102 static void automount_done(Unit
*u
) {
103 Automount
*a
= AUTOMOUNT(u
);
108 unit_ref_unset(&a
->mount
);
117 int automount_add_one_mount_link(Automount
*a
, Mount
*m
) {
123 if (a
->meta
.load_state
!= UNIT_LOADED
||
124 m
->meta
.load_state
!= UNIT_LOADED
)
127 if (!path_startswith(a
->where
, m
->where
))
130 if (path_equal(a
->where
, m
->where
))
133 if ((r
= unit_add_two_dependencies(UNIT(a
), UNIT_AFTER
, UNIT_REQUIRES
, UNIT(m
), true)) < 0)
139 static int automount_add_mount_links(Automount
*a
) {
145 LIST_FOREACH(units_by_type
, other
, a
->meta
.manager
->units_by_type
[UNIT_MOUNT
])
146 if ((r
= automount_add_one_mount_link(a
, (Mount
*) other
)) < 0)
152 static int automount_add_default_dependencies(Automount
*a
) {
157 if (a
->meta
.manager
->running_as
== MANAGER_SYSTEM
) {
159 if ((r
= unit_add_dependency_by_name(UNIT(a
), UNIT_BEFORE
, SPECIAL_BASIC_TARGET
, NULL
, true)) < 0)
162 if ((r
= unit_add_two_dependencies_by_name(UNIT(a
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, NULL
, true)) < 0)
169 static int automount_verify(Automount
*a
) {
174 if (a
->meta
.load_state
!= UNIT_LOADED
)
177 if (path_equal(a
->where
, "/")) {
178 log_error("Cannot have an automount unit for the root directory. Refusing.");
182 if (!(e
= unit_name_from_path(a
->where
, ".automount")))
185 b
= unit_has_name(UNIT(a
), e
);
189 log_error("%s's Where setting doesn't match unit name. Refusing.", a
->meta
.id
);
196 static int automount_load(Unit
*u
) {
198 Automount
*a
= AUTOMOUNT(u
);
201 assert(u
->meta
.load_state
== UNIT_STUB
);
203 /* Load a .automount file */
204 if ((r
= unit_load_fragment_and_dropin_optional(u
)) < 0)
207 if (u
->meta
.load_state
== UNIT_LOADED
) {
211 if (!(a
->where
= unit_name_to_path(u
->meta
.id
)))
214 path_kill_slashes(a
->where
);
216 if ((r
= automount_add_mount_links(a
)) < 0)
219 r
= unit_load_related_unit(u
, ".mount", &x
);
223 unit_ref_set(&a
->mount
, x
);
225 r
= unit_add_two_dependencies(u
, UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT_DEREF(a
->mount
), true);
229 if (a
->meta
.default_dependencies
)
230 if ((r
= automount_add_default_dependencies(a
)) < 0)
234 return automount_verify(a
);
237 static void automount_set_state(Automount
*a
, AutomountState state
) {
238 AutomountState old_state
;
241 old_state
= a
->state
;
244 if (state
!= AUTOMOUNT_WAITING
&&
245 state
!= AUTOMOUNT_RUNNING
)
248 if (state
!= old_state
)
249 log_debug("%s changed %s -> %s",
251 automount_state_to_string(old_state
),
252 automount_state_to_string(state
));
254 unit_notify(UNIT(a
), state_translation_table
[old_state
], state_translation_table
[state
], true);
257 static int automount_coldplug(Unit
*u
) {
258 Automount
*a
= AUTOMOUNT(u
);
262 assert(a
->state
== AUTOMOUNT_DEAD
);
264 if (a
->deserialized_state
!= a
->state
) {
266 if ((r
= open_dev_autofs(u
->meta
.manager
)) < 0)
269 if (a
->deserialized_state
== AUTOMOUNT_WAITING
||
270 a
->deserialized_state
== AUTOMOUNT_RUNNING
) {
272 assert(a
->pipe_fd
>= 0);
274 if ((r
= unit_watch_fd(UNIT(a
), a
->pipe_fd
, EPOLLIN
, &a
->pipe_watch
)) < 0)
278 automount_set_state(a
, a
->deserialized_state
);
284 static void automount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
285 Automount
*a
= AUTOMOUNT(u
);
290 "%sAutomount State: %s\n"
292 "%sDirectoryMode: %04o\n",
293 prefix
, automount_state_to_string(a
->state
),
295 prefix
, a
->directory_mode
);
298 static void automount_enter_dead(Automount
*a
, bool success
) {
304 automount_set_state(a
, a
->failure
? AUTOMOUNT_FAILED
: AUTOMOUNT_DEAD
);
307 static int open_dev_autofs(Manager
*m
) {
308 struct autofs_dev_ioctl param
;
312 if (m
->dev_autofs_fd
>= 0)
313 return m
->dev_autofs_fd
;
315 label_fix("/dev/autofs", false);
317 if ((m
->dev_autofs_fd
= open("/dev/autofs", O_CLOEXEC
|O_RDONLY
)) < 0) {
318 log_error("Failed to open /dev/autofs: %s", strerror(errno
));
322 init_autofs_dev_ioctl(¶m
);
323 if (ioctl(m
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_VERSION
, ¶m
) < 0) {
324 close_nointr_nofail(m
->dev_autofs_fd
);
325 m
->dev_autofs_fd
= -1;
329 log_debug("Autofs kernel version %i.%i", param
.ver_major
, param
.ver_minor
);
331 return m
->dev_autofs_fd
;
334 static int open_ioctl_fd(int dev_autofs_fd
, const char *where
, dev_t devid
) {
335 struct autofs_dev_ioctl
*param
;
339 assert(dev_autofs_fd
>= 0);
342 l
= sizeof(struct autofs_dev_ioctl
) + strlen(where
) + 1;
344 if (!(param
= malloc(l
)))
347 init_autofs_dev_ioctl(param
);
350 param
->openmount
.devid
= devid
;
351 strcpy(param
->path
, where
);
353 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_OPENMOUNT
, param
) < 0) {
358 if (param
->ioctlfd
< 0) {
363 fd_cloexec(param
->ioctlfd
, true);
371 static int autofs_protocol(int dev_autofs_fd
, int ioctl_fd
) {
372 uint32_t major
, minor
;
373 struct autofs_dev_ioctl param
;
375 assert(dev_autofs_fd
>= 0);
376 assert(ioctl_fd
>= 0);
378 init_autofs_dev_ioctl(¶m
);
379 param
.ioctlfd
= ioctl_fd
;
381 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOVER
, ¶m
) < 0)
384 major
= param
.protover
.version
;
386 init_autofs_dev_ioctl(¶m
);
387 param
.ioctlfd
= ioctl_fd
;
389 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOSUBVER
, ¶m
) < 0)
392 minor
= param
.protosubver
.sub_version
;
394 log_debug("Autofs protocol version %i.%i", major
, minor
);
398 static int autofs_set_timeout(int dev_autofs_fd
, int ioctl_fd
, time_t sec
) {
399 struct autofs_dev_ioctl param
;
401 assert(dev_autofs_fd
>= 0);
402 assert(ioctl_fd
>= 0);
404 init_autofs_dev_ioctl(¶m
);
405 param
.ioctlfd
= ioctl_fd
;
406 param
.timeout
.timeout
= sec
;
408 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_TIMEOUT
, ¶m
) < 0)
414 static int autofs_send_ready(int dev_autofs_fd
, int ioctl_fd
, uint32_t token
, int status
) {
415 struct autofs_dev_ioctl param
;
417 assert(dev_autofs_fd
>= 0);
418 assert(ioctl_fd
>= 0);
420 init_autofs_dev_ioctl(¶m
);
421 param
.ioctlfd
= ioctl_fd
;
424 param
.fail
.token
= token
;
425 param
.fail
.status
= status
;
427 param
.ready
.token
= token
;
429 if (ioctl(dev_autofs_fd
, status
? AUTOFS_DEV_IOCTL_FAIL
: AUTOFS_DEV_IOCTL_READY
, ¶m
) < 0)
435 int automount_send_ready(Automount
*a
, int status
) {
442 if (set_isempty(a
->tokens
))
445 if ((ioctl_fd
= open_ioctl_fd(a
->meta
.manager
->dev_autofs_fd
, a
->where
, a
->dev_id
)) < 0) {
451 log_debug("Sending failure: %s", strerror(-status
));
453 log_debug("Sending success.");
457 /* Autofs thankfully does not hand out 0 as a token */
458 while ((token
= PTR_TO_UINT(set_steal_first(a
->tokens
)))) {
461 /* Autofs fun fact II:
463 * if you pass a positive status code here, the kernel will
466 if ((k
= autofs_send_ready(a
->meta
.manager
->dev_autofs_fd
,
475 close_nointr_nofail(ioctl_fd
);
480 static void automount_enter_waiting(Automount
*a
) {
481 int p
[2] = { -1, -1 };
482 char name
[32], options
[128];
483 bool mounted
= false;
484 int r
, ioctl_fd
= -1, dev_autofs_fd
;
488 assert(a
->pipe_fd
< 0);
492 set_clear(a
->tokens
);
494 if ((dev_autofs_fd
= open_dev_autofs(a
->meta
.manager
)) < 0) {
499 /* We knowingly ignore the results of this call */
500 mkdir_p(a
->where
, 0555);
502 if (pipe2(p
, O_NONBLOCK
|O_CLOEXEC
) < 0) {
507 snprintf(options
, sizeof(options
), "fd=%i,pgrp=%u,minproto=5,maxproto=5,direct", p
[1], (unsigned) getpgrp());
508 char_array_0(options
);
510 snprintf(name
, sizeof(name
), "systemd-%u", (unsigned) getpid());
513 if (mount(name
, a
->where
, "autofs", 0, options
) < 0) {
520 close_nointr_nofail(p
[1]);
523 if (stat(a
->where
, &st
) < 0) {
528 if ((ioctl_fd
= open_ioctl_fd(dev_autofs_fd
, a
->where
, st
.st_dev
)) < 0) {
533 if ((r
= autofs_protocol(dev_autofs_fd
, ioctl_fd
)) < 0)
536 if ((r
= autofs_set_timeout(dev_autofs_fd
, ioctl_fd
, 300)) < 0)
541 * Unless we close the ioctl fd here, for some weird reason
542 * the direct mount will not receive events from the
545 close_nointr_nofail(ioctl_fd
);
548 if ((r
= unit_watch_fd(UNIT(a
), p
[0], EPOLLIN
, &a
->pipe_watch
)) < 0)
552 a
->dev_id
= st
.st_dev
;
554 automount_set_state(a
, AUTOMOUNT_WAITING
);
559 assert_se(close_pipe(p
) == 0);
562 close_nointr_nofail(ioctl_fd
);
565 repeat_unmout(a
->where
);
567 log_error("Failed to initialize automounter: %s", strerror(-r
));
568 automount_enter_dead(a
, false);
571 static void automount_enter_runnning(Automount
*a
) {
577 assert(UNIT_DEREF(a
->mount
));
579 dbus_error_init(&error
);
581 /* We don't take mount requests anymore if we are supposed to
582 * shut down anyway */
583 if (unit_pending_inactive(UNIT(a
))) {
584 log_debug("Suppressing automount request on %s since unit stop is scheduled.", a
->meta
.id
);
585 automount_send_ready(a
, -EHOSTDOWN
);
589 mkdir_p(a
->where
, a
->directory_mode
);
591 /* Before we do anything, let's see if somebody is playing games with us? */
592 if (lstat(a
->where
, &st
) < 0) {
593 log_warning("%s failed to stat automount point: %m", a
->meta
.id
);
597 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
!= a
->dev_id
)
598 log_info("%s's automount point already active?", a
->meta
.id
);
599 else if ((r
= manager_add_job(a
->meta
.manager
, JOB_START
, UNIT_DEREF(a
->mount
), JOB_REPLACE
, true, &error
, NULL
)) < 0) {
600 log_warning("%s failed to queue mount startup job: %s", a
->meta
.id
, bus_error(&error
, r
));
604 automount_set_state(a
, AUTOMOUNT_RUNNING
);
608 automount_enter_dead(a
, false);
609 dbus_error_free(&error
);
612 static int automount_start(Unit
*u
) {
613 Automount
*a
= AUTOMOUNT(u
);
617 assert(a
->state
== AUTOMOUNT_DEAD
|| a
->state
== AUTOMOUNT_FAILED
);
619 if (path_is_mount_point(a
->where
, false)) {
620 log_error("Path %s is already a mount point, refusing start for %s", a
->where
, u
->meta
.id
);
624 if (UNIT_DEREF(a
->mount
)->meta
.load_state
!= UNIT_LOADED
)
628 automount_enter_waiting(a
);
632 static int automount_stop(Unit
*u
) {
633 Automount
*a
= AUTOMOUNT(u
);
637 assert(a
->state
== AUTOMOUNT_WAITING
|| a
->state
== AUTOMOUNT_RUNNING
);
639 automount_enter_dead(a
, true);
643 static int automount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
644 Automount
*a
= AUTOMOUNT(u
);
652 unit_serialize_item(u
, f
, "state", automount_state_to_string(a
->state
));
653 unit_serialize_item(u
, f
, "failure", yes_no(a
->failure
));
654 unit_serialize_item_format(u
, f
, "dev-id", "%u", (unsigned) a
->dev_id
);
656 SET_FOREACH(p
, a
->tokens
, i
)
657 unit_serialize_item_format(u
, f
, "token", "%u", PTR_TO_UINT(p
));
659 if (a
->pipe_fd
>= 0) {
662 if ((copy
= fdset_put_dup(fds
, a
->pipe_fd
)) < 0)
665 unit_serialize_item_format(u
, f
, "pipe-fd", "%i", copy
);
671 static int automount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
672 Automount
*a
= AUTOMOUNT(u
);
678 if (streq(key
, "state")) {
679 AutomountState state
;
681 if ((state
= automount_state_from_string(value
)) < 0)
682 log_debug("Failed to parse state value %s", value
);
684 a
->deserialized_state
= state
;
685 } else if (streq(key
, "failure")) {
688 if ((b
= parse_boolean(value
)) < 0)
689 log_debug("Failed to parse failure value %s", value
);
691 a
->failure
= b
|| a
->failure
;
692 } else if (streq(key
, "dev-id")) {
695 if (safe_atou(value
, &d
) < 0)
696 log_debug("Failed to parse dev-id value %s", value
);
698 a
->dev_id
= (unsigned) d
;
699 } else if (streq(key
, "token")) {
702 if (safe_atou(value
, &token
) < 0)
703 log_debug("Failed to parse token value %s", value
);
706 if (!(a
->tokens
= set_new(trivial_hash_func
, trivial_compare_func
)))
709 if ((r
= set_put(a
->tokens
, UINT_TO_PTR(token
))) < 0)
712 } else if (streq(key
, "pipe-fd")) {
715 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
716 log_debug("Failed to parse pipe-fd value %s", value
);
719 close_nointr_nofail(a
->pipe_fd
);
721 a
->pipe_fd
= fdset_remove(fds
, fd
);
724 log_debug("Unknown serialization key '%s'", key
);
729 static UnitActiveState
automount_active_state(Unit
*u
) {
732 return state_translation_table
[AUTOMOUNT(u
)->state
];
735 static const char *automount_sub_state_to_string(Unit
*u
) {
738 return automount_state_to_string(AUTOMOUNT(u
)->state
);
741 static bool automount_check_gc(Unit
*u
) {
742 Automount
*a
= AUTOMOUNT(u
);
746 if (!UNIT_DEREF(a
->mount
))
749 return UNIT_VTABLE(UNIT_DEREF(a
->mount
))->check_gc(UNIT_DEREF(a
->mount
));
752 static void automount_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
753 Automount
*a
= AUTOMOUNT(u
);
754 union autofs_v5_packet_union packet
;
759 assert(fd
== a
->pipe_fd
);
761 if (events
!= EPOLLIN
) {
762 log_error("Got invalid poll event on pipe.");
766 if ((l
= loop_read(a
->pipe_fd
, &packet
, sizeof(packet
), true)) != sizeof(packet
)) {
767 log_error("Invalid read from pipe: %s", l
< 0 ? strerror(-l
) : "short read");
771 switch (packet
.hdr
.type
) {
773 case autofs_ptype_missing_direct
:
775 if (packet
.v5_packet
.pid
> 0) {
778 get_process_comm(packet
.v5_packet
.pid
, &p
);
779 log_debug("Got direct mount request for %s, triggered by %lu (%s)", packet
.v5_packet
.name
, (unsigned long) packet
.v5_packet
.pid
, strna(p
));
783 log_debug("Got direct mount request for %s", packet
.v5_packet
.name
);
786 if (!(a
->tokens
= set_new(trivial_hash_func
, trivial_compare_func
))) {
787 log_error("Failed to allocate token set.");
791 if ((r
= set_put(a
->tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
))) < 0) {
792 log_error("Failed to remember token: %s", strerror(-r
));
796 automount_enter_runnning(a
);
800 log_error("Received unknown automount request %i", packet
.hdr
.type
);
807 automount_enter_dead(a
, false);
810 static void automount_shutdown(Manager
*m
) {
813 if (m
->dev_autofs_fd
>= 0)
814 close_nointr_nofail(m
->dev_autofs_fd
);
817 static void automount_reset_failed(Unit
*u
) {
818 Automount
*a
= AUTOMOUNT(u
);
822 if (a
->state
== AUTOMOUNT_FAILED
)
823 automount_set_state(a
, AUTOMOUNT_DEAD
);
828 static const char* const automount_state_table
[_AUTOMOUNT_STATE_MAX
] = {
829 [AUTOMOUNT_DEAD
] = "dead",
830 [AUTOMOUNT_WAITING
] = "waiting",
831 [AUTOMOUNT_RUNNING
] = "running",
832 [AUTOMOUNT_FAILED
] = "failed"
835 DEFINE_STRING_TABLE_LOOKUP(automount_state
, AutomountState
);
837 const UnitVTable automount_vtable
= {
838 .suffix
= ".automount",
839 .object_size
= sizeof(Automount
),
846 .no_instances
= true,
848 .init
= automount_init
,
849 .load
= automount_load
,
850 .done
= automount_done
,
852 .coldplug
= automount_coldplug
,
854 .dump
= automount_dump
,
856 .start
= automount_start
,
857 .stop
= automount_stop
,
859 .serialize
= automount_serialize
,
860 .deserialize_item
= automount_deserialize_item
,
862 .active_state
= automount_active_state
,
863 .sub_state_to_string
= automount_sub_state_to_string
,
865 .check_gc
= automount_check_gc
,
867 .fd_event
= automount_fd_event
,
869 .reset_failed
= automount_reset_failed
,
871 .bus_interface
= "org.freedesktop.systemd1.Automount",
872 .bus_message_handler
= bus_automount_message_handler
,
874 .shutdown
= automount_shutdown