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"
38 #include "bus-errors.h"
41 static const UnitActiveState state_translation_table
[_AUTOMOUNT_STATE_MAX
] = {
42 [AUTOMOUNT_DEAD
] = UNIT_INACTIVE
,
43 [AUTOMOUNT_WAITING
] = UNIT_ACTIVE
,
44 [AUTOMOUNT_RUNNING
] = UNIT_ACTIVE
,
45 [AUTOMOUNT_MAINTENANCE
] = UNIT_MAINTENANCE
48 static int open_dev_autofs(Manager
*m
);
50 static void automount_init(Unit
*u
) {
51 Automount
*a
= AUTOMOUNT(u
);
54 assert(u
->meta
.load_state
== UNIT_STUB
);
56 a
->pipe_watch
.fd
= a
->pipe_fd
= -1;
57 a
->pipe_watch
.type
= WATCH_INVALID
;
59 a
->directory_mode
= 0755;
62 static void repeat_unmout(const char *path
) {
66 /* If there are multiple mounts on a mount point, this
69 if (umount2(path
, MNT_DETACH
) >= 0)
73 log_error("Failed to unmount: %m");
79 static void unmount_autofs(Automount
*a
) {
85 automount_send_ready(a
, -EHOSTDOWN
);
87 unit_unwatch_fd(UNIT(a
), &a
->pipe_watch
);
88 close_nointr_nofail(a
->pipe_fd
);
91 /* If we reload/reexecute things we keep the mount point
94 (a
->meta
.manager
->exit_code
!= MANAGER_RELOAD
&&
95 a
->meta
.manager
->exit_code
!= MANAGER_REEXECUTE
))
96 repeat_unmout(a
->where
);
99 static void automount_done(Unit
*u
) {
100 Automount
*a
= AUTOMOUNT(u
);
114 int automount_add_one_mount_link(Automount
*a
, Mount
*m
) {
120 if (a
->meta
.load_state
!= UNIT_LOADED
||
121 m
->meta
.load_state
!= UNIT_LOADED
)
124 if (!path_startswith(a
->where
, m
->where
))
127 if (path_equal(a
->where
, m
->where
))
130 if ((r
= unit_add_two_dependencies(UNIT(a
), UNIT_AFTER
, UNIT_REQUIRES
, UNIT(m
), true)) < 0)
136 static int automount_add_mount_links(Automount
*a
) {
142 LIST_FOREACH(units_per_type
, other
, a
->meta
.manager
->units_per_type
[UNIT_MOUNT
])
143 if ((r
= automount_add_one_mount_link(a
, (Mount
*) other
)) < 0)
149 static int automount_verify(Automount
*a
) {
154 if (a
->meta
.load_state
!= UNIT_LOADED
)
157 if (!(e
= unit_name_from_path(a
->where
, ".automount")))
160 b
= unit_has_name(UNIT(a
), e
);
164 log_error("%s's Where setting doesn't match unit name. Refusing.", a
->meta
.id
);
171 static int automount_load(Unit
*u
) {
173 Automount
*a
= AUTOMOUNT(u
);
176 assert(u
->meta
.load_state
== UNIT_STUB
);
178 /* Load a .automount file */
179 if ((r
= unit_load_fragment_and_dropin_optional(u
)) < 0)
182 if (u
->meta
.load_state
== UNIT_LOADED
) {
185 if (!(a
->where
= unit_name_to_path(u
->meta
.id
)))
188 path_kill_slashes(a
->where
);
190 if ((r
= automount_add_mount_links(a
)) < 0)
193 if ((r
= unit_load_related_unit(u
, ".mount", (Unit
**) &a
->mount
)) < 0)
196 if ((r
= unit_add_dependency(u
, UNIT_BEFORE
, UNIT(a
->mount
), true)) < 0)
199 if (a
->meta
.default_dependencies
)
200 if ((r
= unit_add_two_dependencies_by_name(UNIT(a
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, NULL
, true)) < 0)
204 return automount_verify(a
);
207 static void automount_set_state(Automount
*a
, AutomountState state
) {
208 AutomountState old_state
;
211 old_state
= a
->state
;
214 if (state
!= AUTOMOUNT_WAITING
&&
215 state
!= AUTOMOUNT_RUNNING
)
218 if (state
!= old_state
)
219 log_debug("%s changed %s -> %s",
221 automount_state_to_string(old_state
),
222 automount_state_to_string(state
));
224 unit_notify(UNIT(a
), state_translation_table
[old_state
], state_translation_table
[state
]);
227 static int automount_coldplug(Unit
*u
) {
228 Automount
*a
= AUTOMOUNT(u
);
232 assert(a
->state
== AUTOMOUNT_DEAD
);
234 if (a
->deserialized_state
!= a
->state
) {
236 if ((r
= open_dev_autofs(u
->meta
.manager
)) < 0)
239 if (a
->deserialized_state
== AUTOMOUNT_WAITING
||
240 a
->deserialized_state
== AUTOMOUNT_RUNNING
) {
242 assert(a
->pipe_fd
>= 0);
244 if ((r
= unit_watch_fd(UNIT(a
), a
->pipe_fd
, EPOLLIN
, &a
->pipe_watch
)) < 0)
248 automount_set_state(a
, a
->deserialized_state
);
254 static void automount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
255 Automount
*a
= AUTOMOUNT(u
);
260 "%sAutomount State: %s\n"
262 "%sDirectoryMode: %04o\n",
263 prefix
, automount_state_to_string(a
->state
),
265 prefix
, a
->directory_mode
);
268 static void automount_enter_dead(Automount
*a
, bool success
) {
274 automount_set_state(a
, a
->failure
? AUTOMOUNT_MAINTENANCE
: AUTOMOUNT_DEAD
);
277 static int open_dev_autofs(Manager
*m
) {
278 struct autofs_dev_ioctl param
;
282 if (m
->dev_autofs_fd
>= 0)
283 return m
->dev_autofs_fd
;
285 if ((m
->dev_autofs_fd
= open("/dev/autofs", O_CLOEXEC
|O_RDONLY
)) < 0) {
286 log_error("Failed to open /dev/autofs: %s", strerror(errno
));
290 init_autofs_dev_ioctl(¶m
);
291 if (ioctl(m
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_VERSION
, ¶m
) < 0) {
292 close_nointr_nofail(m
->dev_autofs_fd
);
293 m
->dev_autofs_fd
= -1;
297 log_debug("Autofs kernel version %i.%i", param
.ver_major
, param
.ver_minor
);
299 return m
->dev_autofs_fd
;
302 static int open_ioctl_fd(int dev_autofs_fd
, const char *where
, dev_t devid
) {
303 struct autofs_dev_ioctl
*param
;
307 assert(dev_autofs_fd
>= 0);
310 l
= sizeof(struct autofs_dev_ioctl
) + strlen(where
) + 1;
312 if (!(param
= malloc(l
)))
315 init_autofs_dev_ioctl(param
);
318 param
->openmount
.devid
= devid
;
319 strcpy(param
->path
, where
);
321 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_OPENMOUNT
, param
) < 0) {
326 if (param
->ioctlfd
< 0) {
331 fd_cloexec(param
->ioctlfd
, true);
339 static int autofs_protocol(int dev_autofs_fd
, int ioctl_fd
) {
340 uint32_t major
, minor
;
341 struct autofs_dev_ioctl param
;
343 assert(dev_autofs_fd
>= 0);
344 assert(ioctl_fd
>= 0);
346 init_autofs_dev_ioctl(¶m
);
347 param
.ioctlfd
= ioctl_fd
;
349 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOVER
, ¶m
) < 0)
352 major
= param
.protover
.version
;
354 init_autofs_dev_ioctl(¶m
);
355 param
.ioctlfd
= ioctl_fd
;
357 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOSUBVER
, ¶m
) < 0)
360 minor
= param
.protosubver
.sub_version
;
362 log_debug("Autofs protocol version %i.%i", major
, minor
);
366 static int autofs_set_timeout(int dev_autofs_fd
, int ioctl_fd
, time_t sec
) {
367 struct autofs_dev_ioctl param
;
369 assert(dev_autofs_fd
>= 0);
370 assert(ioctl_fd
>= 0);
372 init_autofs_dev_ioctl(¶m
);
373 param
.ioctlfd
= ioctl_fd
;
374 param
.timeout
.timeout
= sec
;
376 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_TIMEOUT
, ¶m
) < 0)
382 static int autofs_send_ready(int dev_autofs_fd
, int ioctl_fd
, uint32_t token
, int status
) {
383 struct autofs_dev_ioctl param
;
385 assert(dev_autofs_fd
>= 0);
386 assert(ioctl_fd
>= 0);
388 init_autofs_dev_ioctl(¶m
);
389 param
.ioctlfd
= ioctl_fd
;
392 param
.fail
.token
= token
;
393 param
.fail
.status
= status
;
395 param
.ready
.token
= token
;
397 if (ioctl(dev_autofs_fd
, status
? AUTOFS_DEV_IOCTL_FAIL
: AUTOFS_DEV_IOCTL_READY
, ¶m
) < 0)
403 int automount_send_ready(Automount
*a
, int status
) {
410 if (set_isempty(a
->tokens
))
413 if ((ioctl_fd
= open_ioctl_fd(a
->meta
.manager
->dev_autofs_fd
, a
->where
, a
->dev_id
)) < 0) {
419 log_debug("Sending failure: %s", strerror(-status
));
421 log_debug("Sending success.");
423 /* Autofs thankfully does not hand out 0 as a token */
424 while ((token
= PTR_TO_UINT(set_steal_first(a
->tokens
)))) {
427 /* Autofs fun fact II:
429 * if you pass a positive status code here, the kernel will
432 if ((k
= autofs_send_ready(a
->meta
.manager
->dev_autofs_fd
,
443 close_nointr_nofail(ioctl_fd
);
448 static void automount_enter_waiting(Automount
*a
) {
449 int p
[2] = { -1, -1 };
450 char name
[32], options
[128];
451 bool mounted
= false;
452 int r
, ioctl_fd
= -1, dev_autofs_fd
;
456 assert(a
->pipe_fd
< 0);
460 set_clear(a
->tokens
);
462 if ((dev_autofs_fd
= open_dev_autofs(a
->meta
.manager
)) < 0) {
467 /* We knowingly ignore the results of this call */
468 mkdir_p(a
->where
, 0555);
470 if (pipe2(p
, O_NONBLOCK
|O_CLOEXEC
) < 0) {
475 snprintf(options
, sizeof(options
), "fd=%i,pgrp=%u,minproto=5,maxproto=5,direct", p
[1], (unsigned) getpgrp());
476 char_array_0(options
);
478 snprintf(name
, sizeof(name
), "systemd-%u", (unsigned) getpid());
481 if (mount(name
, a
->where
, "autofs", 0, options
) < 0) {
488 close_nointr_nofail(p
[1]);
491 if (stat(a
->where
, &st
) < 0) {
496 if ((ioctl_fd
= open_ioctl_fd(dev_autofs_fd
, a
->where
, st
.st_dev
)) < 0) {
501 if ((r
= autofs_protocol(dev_autofs_fd
, ioctl_fd
)) < 0)
504 if ((r
= autofs_set_timeout(dev_autofs_fd
, ioctl_fd
, 300)) < 0)
509 * Unless we close the ioctl fd here, for some weird reason
510 * the direct mount will not receive events from the
513 close_nointr_nofail(ioctl_fd
);
516 if ((r
= unit_watch_fd(UNIT(a
), p
[0], EPOLLIN
, &a
->pipe_watch
)) < 0)
520 a
->dev_id
= st
.st_dev
;
522 automount_set_state(a
, AUTOMOUNT_WAITING
);
527 assert_se(close_pipe(p
) == 0);
530 close_nointr_nofail(ioctl_fd
);
533 repeat_unmout(a
->where
);
535 log_error("Failed to initialize automounter: %s", strerror(-r
));
536 automount_enter_dead(a
, false);
539 static void automount_enter_runnning(Automount
*a
) {
547 dbus_error_init(&error
);
549 mkdir_p(a
->where
, a
->directory_mode
);
551 /* Before we do anything, let's see if somebody is playing games with us? */
552 if (lstat(a
->where
, &st
) < 0) {
553 log_warning("%s failed stat automount point: %m", a
->meta
.id
);
557 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
!= a
->dev_id
)
558 log_info("%s's automount point already active?", a
->meta
.id
);
559 else if ((r
= manager_add_job(a
->meta
.manager
, JOB_START
, UNIT(a
->mount
), JOB_REPLACE
, true, &error
, NULL
)) < 0) {
560 log_warning("%s failed to queue mount startup job: %s", a
->meta
.id
, bus_error(&error
, r
));
564 automount_set_state(a
, AUTOMOUNT_RUNNING
);
568 automount_enter_dead(a
, false);
569 dbus_error_free(&error
);
572 static int automount_start(Unit
*u
) {
573 Automount
*a
= AUTOMOUNT(u
);
577 assert(a
->state
== AUTOMOUNT_DEAD
|| a
->state
== AUTOMOUNT_MAINTENANCE
);
579 if (path_is_mount_point(a
->where
)) {
580 log_error("Path %s is already a mount point, refusing start for %s", a
->where
, u
->meta
.id
);
584 if (a
->mount
->meta
.load_state
!= UNIT_LOADED
)
588 automount_enter_waiting(a
);
592 static int automount_stop(Unit
*u
) {
593 Automount
*a
= AUTOMOUNT(u
);
597 assert(a
->state
== AUTOMOUNT_WAITING
|| a
->state
== AUTOMOUNT_RUNNING
);
599 automount_enter_dead(a
, true);
603 static int automount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
604 Automount
*a
= AUTOMOUNT(u
);
612 unit_serialize_item(u
, f
, "state", automount_state_to_string(a
->state
));
613 unit_serialize_item(u
, f
, "failure", yes_no(a
->failure
));
614 unit_serialize_item_format(u
, f
, "dev-id", "%u", (unsigned) a
->dev_id
);
616 SET_FOREACH(p
, a
->tokens
, i
)
617 unit_serialize_item_format(u
, f
, "token", "%u", PTR_TO_UINT(p
));
619 if (a
->pipe_fd
>= 0) {
622 if ((copy
= fdset_put_dup(fds
, a
->pipe_fd
)) < 0)
625 unit_serialize_item_format(u
, f
, "pipe-fd", "%i", copy
);
631 static int automount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
632 Automount
*a
= AUTOMOUNT(u
);
638 if (streq(key
, "state")) {
639 AutomountState state
;
641 if ((state
= automount_state_from_string(value
)) < 0)
642 log_debug("Failed to parse state value %s", value
);
644 a
->deserialized_state
= state
;
645 } else if (streq(key
, "failure")) {
648 if ((b
= parse_boolean(value
)) < 0)
649 log_debug("Failed to parse failure value %s", value
);
651 a
->failure
= b
|| a
->failure
;
652 } else if (streq(key
, "dev-id")) {
655 if (safe_atou(value
, &d
) < 0)
656 log_debug("Failed to parse dev-id value %s", value
);
658 a
->dev_id
= (unsigned) d
;
659 } else if (streq(key
, "token")) {
662 if (safe_atou(value
, &token
) < 0)
663 log_debug("Failed to parse token value %s", value
);
666 if (!(a
->tokens
= set_new(trivial_hash_func
, trivial_compare_func
)))
669 if ((r
= set_put(a
->tokens
, UINT_TO_PTR(token
))) < 0)
672 } else if (streq(key
, "pipe-fd")) {
675 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
676 log_debug("Failed to parse pipe-fd value %s", value
);
679 close_nointr_nofail(a
->pipe_fd
);
681 a
->pipe_fd
= fdset_remove(fds
, fd
);
684 log_debug("Unknown serialization key '%s'", key
);
689 static UnitActiveState
automount_active_state(Unit
*u
) {
692 return state_translation_table
[AUTOMOUNT(u
)->state
];
695 static const char *automount_sub_state_to_string(Unit
*u
) {
698 return automount_state_to_string(AUTOMOUNT(u
)->state
);
701 static bool automount_check_gc(Unit
*u
) {
702 Automount
*a
= AUTOMOUNT(u
);
706 return UNIT_VTABLE(UNIT(a
->mount
))->check_gc(UNIT(a
->mount
));
709 static void automount_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
710 Automount
*a
= AUTOMOUNT(u
);
711 union autofs_v5_packet_union packet
;
716 assert(fd
== a
->pipe_fd
);
718 if (events
!= EPOLLIN
) {
719 log_error("Got invalid poll event on pipe.");
723 if ((l
= loop_read(a
->pipe_fd
, &packet
, sizeof(packet
), true)) != sizeof(packet
)) {
724 log_error("Invalid read from pipe: %s", l
< 0 ? strerror(-l
) : "short read");
728 switch (packet
.hdr
.type
) {
730 case autofs_ptype_missing_direct
:
731 log_debug("Got direct mount request for %s", packet
.v5_packet
.name
);
734 if (!(a
->tokens
= set_new(trivial_hash_func
, trivial_compare_func
))) {
735 log_error("Failed to allocate token set.");
739 if ((r
= set_put(a
->tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
))) < 0) {
740 log_error("Failed to remember token: %s", strerror(-r
));
744 automount_enter_runnning(a
);
748 log_error("Received unknown automount request %i", packet
.hdr
.type
);
755 automount_enter_dead(a
, false);
758 static void automount_shutdown(Manager
*m
) {
761 if (m
->dev_autofs_fd
>= 0)
762 close_nointr_nofail(m
->dev_autofs_fd
);
765 static const char* const automount_state_table
[_AUTOMOUNT_STATE_MAX
] = {
766 [AUTOMOUNT_DEAD
] = "dead",
767 [AUTOMOUNT_WAITING
] = "waiting",
768 [AUTOMOUNT_RUNNING
] = "running",
769 [AUTOMOUNT_MAINTENANCE
] = "maintenance"
772 DEFINE_STRING_TABLE_LOOKUP(automount_state
, AutomountState
);
774 const UnitVTable automount_vtable
= {
775 .suffix
= ".automount",
778 .no_instances
= true,
780 .init
= automount_init
,
781 .load
= automount_load
,
782 .done
= automount_done
,
784 .coldplug
= automount_coldplug
,
786 .dump
= automount_dump
,
788 .start
= automount_start
,
789 .stop
= automount_stop
,
791 .serialize
= automount_serialize
,
792 .deserialize_item
= automount_deserialize_item
,
794 .active_state
= automount_active_state
,
795 .sub_state_to_string
= automount_sub_state_to_string
,
797 .check_gc
= automount_check_gc
,
799 .fd_event
= automount_fd_event
,
801 .bus_message_handler
= bus_automount_message_handler
,
803 .shutdown
= automount_shutdown