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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser 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"
35 #include "unit-name.h"
39 #include "path-util.h"
40 #include "dbus-automount.h"
42 #include "bus-error.h"
43 #include "formats-util.h"
44 #include "process-util.h"
46 static const UnitActiveState state_translation_table
[_AUTOMOUNT_STATE_MAX
] = {
47 [AUTOMOUNT_DEAD
] = UNIT_INACTIVE
,
48 [AUTOMOUNT_WAITING
] = UNIT_ACTIVE
,
49 [AUTOMOUNT_RUNNING
] = UNIT_ACTIVE
,
50 [AUTOMOUNT_FAILED
] = UNIT_FAILED
53 static int open_dev_autofs(Manager
*m
);
54 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
);
56 static void automount_init(Unit
*u
) {
57 Automount
*a
= AUTOMOUNT(u
);
60 assert(u
->load_state
== UNIT_STUB
);
63 a
->directory_mode
= 0755;
64 UNIT(a
)->ignore_on_isolate
= true;
67 static void repeat_unmount(const char *path
) {
71 /* If there are multiple mounts on a mount point, this
74 if (umount2(path
, MNT_DETACH
) >= 0)
78 log_error_errno(errno
, "Failed to unmount: %m");
84 static void unmount_autofs(Automount
*a
) {
90 automount_send_ready(a
, -EHOSTDOWN
);
92 a
->pipe_event_source
= sd_event_source_unref(a
->pipe_event_source
);
93 a
->pipe_fd
= safe_close(a
->pipe_fd
);
95 /* If we reload/reexecute things we keep the mount point
98 (UNIT(a
)->manager
->exit_code
!= MANAGER_RELOAD
&&
99 UNIT(a
)->manager
->exit_code
!= MANAGER_REEXECUTE
))
100 repeat_unmount(a
->where
);
103 static void automount_done(Unit
*u
) {
104 Automount
*a
= AUTOMOUNT(u
);
117 static int automount_add_mount_links(Automount
*a
) {
118 _cleanup_free_
char *parent
= NULL
;
123 r
= path_get_parent(a
->where
, &parent
);
127 return unit_require_mounts_for(UNIT(a
), parent
);
130 static int automount_add_default_dependencies(Automount
*a
) {
135 if (UNIT(a
)->manager
->running_as
!= SYSTEMD_SYSTEM
)
138 r
= unit_add_two_dependencies_by_name(UNIT(a
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, NULL
, true);
145 static int automount_verify(Automount
*a
) {
147 _cleanup_free_
char *e
= NULL
;
150 if (UNIT(a
)->load_state
!= UNIT_LOADED
)
153 if (path_equal(a
->where
, "/")) {
154 log_unit_error(UNIT(a
)->id
, "Cannot have an automount unit for the root directory. Refusing.");
158 e
= unit_name_from_path(a
->where
, ".automount");
162 b
= unit_has_name(UNIT(a
), e
);
165 log_unit_error(UNIT(a
)->id
, "%s's Where setting doesn't match unit name. Refusing.", UNIT(a
)->id
);
172 static int automount_load(Unit
*u
) {
173 Automount
*a
= AUTOMOUNT(u
);
177 assert(u
->load_state
== UNIT_STUB
);
179 /* Load a .automount file */
180 r
= unit_load_fragment_and_dropin_optional(u
);
184 if (u
->load_state
== UNIT_LOADED
) {
188 a
->where
= unit_name_to_path(u
->id
);
193 path_kill_slashes(a
->where
);
195 r
= unit_load_related_unit(u
, ".mount", &x
);
199 r
= unit_add_two_dependencies(u
, UNIT_BEFORE
, UNIT_TRIGGERS
, x
, true);
203 r
= automount_add_mount_links(a
);
207 if (UNIT(a
)->default_dependencies
) {
208 r
= automount_add_default_dependencies(a
);
214 return automount_verify(a
);
217 static void automount_set_state(Automount
*a
, AutomountState state
) {
218 AutomountState old_state
;
221 old_state
= a
->state
;
224 if (state
!= AUTOMOUNT_WAITING
&&
225 state
!= AUTOMOUNT_RUNNING
)
228 if (state
!= old_state
)
229 log_unit_debug(UNIT(a
)->id
,
230 "%s changed %s -> %s",
232 automount_state_to_string(old_state
),
233 automount_state_to_string(state
));
235 unit_notify(UNIT(a
), state_translation_table
[old_state
], state_translation_table
[state
], true);
238 static int automount_coldplug(Unit
*u
, Hashmap
*deferred_work
) {
239 Automount
*a
= AUTOMOUNT(u
);
243 assert(a
->state
== AUTOMOUNT_DEAD
);
245 if (a
->deserialized_state
!= a
->state
) {
247 r
= open_dev_autofs(u
->manager
);
251 if (a
->deserialized_state
== AUTOMOUNT_WAITING
||
252 a
->deserialized_state
== AUTOMOUNT_RUNNING
) {
254 assert(a
->pipe_fd
>= 0);
256 r
= sd_event_add_io(u
->manager
->event
, &a
->pipe_event_source
, a
->pipe_fd
, EPOLLIN
, automount_dispatch_io
, u
);
261 automount_set_state(a
, a
->deserialized_state
);
267 static void automount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
268 Automount
*a
= AUTOMOUNT(u
);
273 "%sAutomount State: %s\n"
276 "%sDirectoryMode: %04o\n",
277 prefix
, automount_state_to_string(a
->state
),
278 prefix
, automount_result_to_string(a
->result
),
280 prefix
, a
->directory_mode
);
283 static void automount_enter_dead(Automount
*a
, AutomountResult f
) {
286 if (f
!= AUTOMOUNT_SUCCESS
)
289 automount_set_state(a
, a
->result
!= AUTOMOUNT_SUCCESS
? AUTOMOUNT_FAILED
: AUTOMOUNT_DEAD
);
292 static int open_dev_autofs(Manager
*m
) {
293 struct autofs_dev_ioctl param
;
297 if (m
->dev_autofs_fd
>= 0)
298 return m
->dev_autofs_fd
;
300 label_fix("/dev/autofs", false, false);
302 m
->dev_autofs_fd
= open("/dev/autofs", O_CLOEXEC
|O_RDONLY
);
303 if (m
->dev_autofs_fd
< 0)
304 return log_error_errno(errno
, "Failed to open /dev/autofs: %m");
306 init_autofs_dev_ioctl(¶m
);
307 if (ioctl(m
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_VERSION
, ¶m
) < 0) {
308 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
312 log_debug("Autofs kernel version %i.%i", param
.ver_major
, param
.ver_minor
);
314 return m
->dev_autofs_fd
;
317 static int open_ioctl_fd(int dev_autofs_fd
, const char *where
, dev_t devid
) {
318 struct autofs_dev_ioctl
*param
;
321 assert(dev_autofs_fd
>= 0);
324 l
= sizeof(struct autofs_dev_ioctl
) + strlen(where
) + 1;
327 init_autofs_dev_ioctl(param
);
330 param
->openmount
.devid
= devid
;
331 strcpy(param
->path
, where
);
333 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_OPENMOUNT
, param
) < 0)
336 if (param
->ioctlfd
< 0)
339 fd_cloexec(param
->ioctlfd
, true);
340 return param
->ioctlfd
;
343 static int autofs_protocol(int dev_autofs_fd
, int ioctl_fd
) {
344 uint32_t major
, minor
;
345 struct autofs_dev_ioctl param
;
347 assert(dev_autofs_fd
>= 0);
348 assert(ioctl_fd
>= 0);
350 init_autofs_dev_ioctl(¶m
);
351 param
.ioctlfd
= ioctl_fd
;
353 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOVER
, ¶m
) < 0)
356 major
= param
.protover
.version
;
358 init_autofs_dev_ioctl(¶m
);
359 param
.ioctlfd
= ioctl_fd
;
361 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOSUBVER
, ¶m
) < 0)
364 minor
= param
.protosubver
.sub_version
;
366 log_debug("Autofs protocol version %i.%i", major
, minor
);
370 static int autofs_set_timeout(int dev_autofs_fd
, int ioctl_fd
, time_t sec
) {
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
;
378 param
.timeout
.timeout
= sec
;
380 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_TIMEOUT
, ¶m
) < 0)
386 static int autofs_send_ready(int dev_autofs_fd
, int ioctl_fd
, uint32_t token
, int status
) {
387 struct autofs_dev_ioctl param
;
389 assert(dev_autofs_fd
>= 0);
390 assert(ioctl_fd
>= 0);
392 init_autofs_dev_ioctl(¶m
);
393 param
.ioctlfd
= ioctl_fd
;
396 param
.fail
.token
= token
;
397 param
.fail
.status
= status
;
399 param
.ready
.token
= token
;
401 if (ioctl(dev_autofs_fd
, status
? AUTOFS_DEV_IOCTL_FAIL
: AUTOFS_DEV_IOCTL_READY
, ¶m
) < 0)
407 int automount_send_ready(Automount
*a
, int status
) {
408 _cleanup_close_
int ioctl_fd
= -1;
415 if (set_isempty(a
->tokens
))
418 ioctl_fd
= open_ioctl_fd(UNIT(a
)->manager
->dev_autofs_fd
, a
->where
, a
->dev_id
);
423 log_unit_debug_errno(UNIT(a
)->id
, status
, "Sending failure: %m");
425 log_unit_debug(UNIT(a
)->id
, "Sending success.");
429 /* Autofs thankfully does not hand out 0 as a token */
430 while ((token
= PTR_TO_UINT(set_steal_first(a
->tokens
)))) {
433 /* Autofs fun fact II:
435 * if you pass a positive status code here, the kernel will
438 k
= autofs_send_ready(UNIT(a
)->manager
->dev_autofs_fd
,
449 static void automount_enter_waiting(Automount
*a
) {
450 _cleanup_close_
int ioctl_fd
= -1;
451 int p
[2] = { -1, -1 };
452 char name
[sizeof("systemd-")-1 + DECIMAL_STR_MAX(pid_t
) + 1];
453 char options
[sizeof("fd=,pgrp=,minproto=5,maxproto=5,direct")-1
454 + DECIMAL_STR_MAX(int) + DECIMAL_STR_MAX(gid_t
) + 1];
455 bool mounted
= false;
456 int r
, dev_autofs_fd
;
460 assert(a
->pipe_fd
< 0);
464 set_clear(a
->tokens
);
466 dev_autofs_fd
= open_dev_autofs(UNIT(a
)->manager
);
467 if (dev_autofs_fd
< 0) {
472 /* We knowingly ignore the results of this call */
473 mkdir_p_label(a
->where
, 0555);
475 warn_if_dir_nonempty(a
->meta
.id
, a
->where
);
477 if (pipe2(p
, O_NONBLOCK
|O_CLOEXEC
) < 0) {
482 xsprintf(options
, "fd=%i,pgrp="PID_FMT
",minproto=5,maxproto=5,direct", p
[1], getpgrp());
483 xsprintf(name
, "systemd-"PID_FMT
, getpid());
484 if (mount(name
, a
->where
, "autofs", 0, options
) < 0) {
491 p
[1] = safe_close(p
[1]);
493 if (stat(a
->where
, &st
) < 0) {
498 ioctl_fd
= open_ioctl_fd(dev_autofs_fd
, a
->where
, st
.st_dev
);
504 r
= autofs_protocol(dev_autofs_fd
, ioctl_fd
);
508 r
= autofs_set_timeout(dev_autofs_fd
, ioctl_fd
, 300);
514 * Unless we close the ioctl fd here, for some weird reason
515 * the direct mount will not receive events from the
518 r
= sd_event_add_io(UNIT(a
)->manager
->event
, &a
->pipe_event_source
, p
[0], EPOLLIN
, automount_dispatch_io
, a
);
523 a
->dev_id
= st
.st_dev
;
525 automount_set_state(a
, AUTOMOUNT_WAITING
);
533 repeat_unmount(a
->where
);
535 log_unit_error(UNIT(a
)->id
,
536 "Failed to initialize automounter: %s", strerror(-r
));
537 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
540 static void automount_enter_runnning(Automount
*a
) {
541 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
547 /* We don't take mount requests anymore if we are supposed to
548 * shut down anyway */
549 if (unit_stop_pending(UNIT(a
))) {
550 log_unit_debug(UNIT(a
)->id
,
551 "Suppressing automount request on %s since unit stop is scheduled.", UNIT(a
)->id
);
552 automount_send_ready(a
, -EHOSTDOWN
);
556 mkdir_p_label(a
->where
, a
->directory_mode
);
558 /* Before we do anything, let's see if somebody is playing games with us? */
559 if (lstat(a
->where
, &st
) < 0) {
560 log_unit_warning(UNIT(a
)->id
,
561 "%s failed to stat automount point: %m", UNIT(a
)->id
);
565 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
!= a
->dev_id
)
566 log_unit_info(UNIT(a
)->id
,
567 "%s's automount point already active?", UNIT(a
)->id
);
569 r
= manager_add_job(UNIT(a
)->manager
, JOB_START
, UNIT_TRIGGER(UNIT(a
)),
570 JOB_REPLACE
, true, &error
, NULL
);
572 log_unit_warning(UNIT(a
)->id
,
573 "%s failed to queue mount startup job: %s",
574 UNIT(a
)->id
, bus_error_message(&error
, r
));
579 automount_set_state(a
, AUTOMOUNT_RUNNING
);
583 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
586 static int automount_start(Unit
*u
) {
587 Automount
*a
= AUTOMOUNT(u
);
590 assert(a
->state
== AUTOMOUNT_DEAD
|| a
->state
== AUTOMOUNT_FAILED
);
592 if (path_is_mount_point(a
->where
, false) > 0) {
593 log_unit_error(u
->id
, "Path %s is already a mount point, refusing start for %s", a
->where
, u
->id
);
597 if (UNIT_TRIGGER(u
)->load_state
!= UNIT_LOADED
)
600 a
->result
= AUTOMOUNT_SUCCESS
;
601 automount_enter_waiting(a
);
605 static int automount_stop(Unit
*u
) {
606 Automount
*a
= AUTOMOUNT(u
);
609 assert(a
->state
== AUTOMOUNT_WAITING
|| a
->state
== AUTOMOUNT_RUNNING
);
611 automount_enter_dead(a
, AUTOMOUNT_SUCCESS
);
615 static int automount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
616 Automount
*a
= AUTOMOUNT(u
);
624 unit_serialize_item(u
, f
, "state", automount_state_to_string(a
->state
));
625 unit_serialize_item(u
, f
, "result", automount_result_to_string(a
->result
));
626 unit_serialize_item_format(u
, f
, "dev-id", "%u", (unsigned) a
->dev_id
);
628 SET_FOREACH(p
, a
->tokens
, i
)
629 unit_serialize_item_format(u
, f
, "token", "%u", PTR_TO_UINT(p
));
631 if (a
->pipe_fd
>= 0) {
634 copy
= fdset_put_dup(fds
, a
->pipe_fd
);
638 unit_serialize_item_format(u
, f
, "pipe-fd", "%i", copy
);
644 static int automount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
645 Automount
*a
= AUTOMOUNT(u
);
651 if (streq(key
, "state")) {
652 AutomountState state
;
654 state
= automount_state_from_string(value
);
656 log_unit_debug(u
->id
, "Failed to parse state value %s", value
);
658 a
->deserialized_state
= state
;
659 } else if (streq(key
, "result")) {
662 f
= automount_result_from_string(value
);
664 log_unit_debug(u
->id
, "Failed to parse result value %s", value
);
665 else if (f
!= AUTOMOUNT_SUCCESS
)
668 } else if (streq(key
, "dev-id")) {
671 if (safe_atou(value
, &d
) < 0)
672 log_unit_debug(u
->id
, "Failed to parse dev-id value %s", value
);
674 a
->dev_id
= (unsigned) d
;
675 } else if (streq(key
, "token")) {
678 if (safe_atou(value
, &token
) < 0)
679 log_unit_debug(u
->id
, "Failed to parse token value %s", value
);
682 if (!(a
->tokens
= set_new(NULL
)))
685 r
= set_put(a
->tokens
, UINT_TO_PTR(token
));
689 } else if (streq(key
, "pipe-fd")) {
692 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
693 log_unit_debug(u
->id
, "Failed to parse pipe-fd value %s", value
);
695 safe_close(a
->pipe_fd
);
696 a
->pipe_fd
= fdset_remove(fds
, fd
);
699 log_unit_debug(u
->id
, "Unknown serialization key '%s'", key
);
704 static UnitActiveState
automount_active_state(Unit
*u
) {
707 return state_translation_table
[AUTOMOUNT(u
)->state
];
710 static const char *automount_sub_state_to_string(Unit
*u
) {
713 return automount_state_to_string(AUTOMOUNT(u
)->state
);
716 static bool automount_check_gc(Unit
*u
) {
719 if (!UNIT_TRIGGER(u
))
722 return UNIT_VTABLE(UNIT_TRIGGER(u
))->check_gc(UNIT_TRIGGER(u
));
725 static int automount_dispatch_io(sd_event_source
*s
, int fd
, uint32_t events
, void *userdata
) {
726 union autofs_v5_packet_union packet
;
727 Automount
*a
= AUTOMOUNT(userdata
);
731 assert(fd
== a
->pipe_fd
);
733 if (events
!= EPOLLIN
) {
734 log_unit_error(UNIT(a
)->id
, "%s: got invalid poll event %"PRIu32
" on pipe (fd=%d)",
735 UNIT(a
)->id
, events
, fd
);
739 r
= loop_read_exact(a
->pipe_fd
, &packet
, sizeof(packet
), true);
741 log_unit_error_errno(UNIT(a
)->id
, r
, "Invalid read from pipe: %m");
745 switch (packet
.hdr
.type
) {
747 case autofs_ptype_missing_direct
:
749 if (packet
.v5_packet
.pid
> 0) {
750 _cleanup_free_
char *p
= NULL
;
752 get_process_comm(packet
.v5_packet
.pid
, &p
);
753 log_unit_info(UNIT(a
)->id
,
754 "Got automount request for %s, triggered by %"PRIu32
" (%s)",
755 a
->where
, packet
.v5_packet
.pid
, strna(p
));
757 log_unit_debug(UNIT(a
)->id
, "Got direct mount request on %s", a
->where
);
759 r
= set_ensure_allocated(&a
->tokens
, NULL
);
761 log_unit_error(UNIT(a
)->id
, "Failed to allocate token set.");
765 r
= set_put(a
->tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
));
767 log_unit_error_errno(UNIT(a
)->id
, r
, "Failed to remember token: %m");
771 automount_enter_runnning(a
);
775 log_unit_error(UNIT(a
)->id
, "Received unknown automount request %i", packet
.hdr
.type
);
782 automount_enter_dead(a
, AUTOMOUNT_FAILURE_RESOURCES
);
786 static void automount_shutdown(Manager
*m
) {
789 m
->dev_autofs_fd
= safe_close(m
->dev_autofs_fd
);
792 static void automount_reset_failed(Unit
*u
) {
793 Automount
*a
= AUTOMOUNT(u
);
797 if (a
->state
== AUTOMOUNT_FAILED
)
798 automount_set_state(a
, AUTOMOUNT_DEAD
);
800 a
->result
= AUTOMOUNT_SUCCESS
;
803 static bool automount_supported(Manager
*m
) {
804 static int supported
= -1;
809 supported
= access("/dev/autofs", F_OK
) >= 0;
814 static const char* const automount_state_table
[_AUTOMOUNT_STATE_MAX
] = {
815 [AUTOMOUNT_DEAD
] = "dead",
816 [AUTOMOUNT_WAITING
] = "waiting",
817 [AUTOMOUNT_RUNNING
] = "running",
818 [AUTOMOUNT_FAILED
] = "failed"
821 DEFINE_STRING_TABLE_LOOKUP(automount_state
, AutomountState
);
823 static const char* const automount_result_table
[_AUTOMOUNT_RESULT_MAX
] = {
824 [AUTOMOUNT_SUCCESS
] = "success",
825 [AUTOMOUNT_FAILURE_RESOURCES
] = "resources"
828 DEFINE_STRING_TABLE_LOOKUP(automount_result
, AutomountResult
);
830 const UnitVTable automount_vtable
= {
831 .object_size
= sizeof(Automount
),
839 .no_instances
= true,
841 .init
= automount_init
,
842 .load
= automount_load
,
843 .done
= automount_done
,
845 .coldplug
= automount_coldplug
,
847 .dump
= automount_dump
,
849 .start
= automount_start
,
850 .stop
= automount_stop
,
852 .serialize
= automount_serialize
,
853 .deserialize_item
= automount_deserialize_item
,
855 .active_state
= automount_active_state
,
856 .sub_state_to_string
= automount_sub_state_to_string
,
858 .check_gc
= automount_check_gc
,
860 .reset_failed
= automount_reset_failed
,
862 .bus_interface
= "org.freedesktop.systemd1.Automount",
863 .bus_vtable
= bus_automount_vtable
,
865 .shutdown
= automount_shutdown
,
866 .supported
= automount_supported
,
868 .status_message_formats
= {
869 .finished_start_job
= {
870 [JOB_DONE
] = "Set up automount %s.",
871 [JOB_FAILED
] = "Failed to set up automount %s.",
872 [JOB_DEPENDENCY
] = "Dependency failed for %s.",
874 .finished_stop_job
= {
875 [JOB_DONE
] = "Unset automount %s.",
876 [JOB_FAILED
] = "Failed to unset automount %s.",