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"
39 static const UnitActiveState state_translation_table
[_AUTOMOUNT_STATE_MAX
] = {
40 [AUTOMOUNT_DEAD
] = UNIT_INACTIVE
,
41 [AUTOMOUNT_WAITING
] = UNIT_ACTIVE
,
42 [AUTOMOUNT_RUNNING
] = UNIT_ACTIVE
,
43 [AUTOMOUNT_MAINTAINANCE
] = UNIT_INACTIVE
,
46 static int open_dev_autofs(Manager
*m
);
48 static void automount_init(Unit
*u
) {
49 Automount
*a
= AUTOMOUNT(u
);
52 assert(u
->meta
.load_state
== UNIT_STUB
);
54 a
->pipe_watch
.fd
= a
->pipe_fd
= -1;
55 a
->pipe_watch
.type
= WATCH_INVALID
;
58 static void repeat_unmout(const char *path
) {
63 if (umount2(path
, MNT_DETACH
) >= 0)
67 log_error("Failed to unmount: %m");
73 static void unmount_autofs(Automount
*a
) {
79 automount_send_ready(a
, -EHOSTDOWN
);
81 unit_unwatch_fd(UNIT(a
), &a
->pipe_watch
);
82 close_nointr_nofail(a
->pipe_fd
);
85 /* If we reload/reexecute things we keep the mount point
88 (UNIT(a
)->meta
.manager
->exit_code
!= MANAGER_RELOAD
&&
89 UNIT(a
)->meta
.manager
->exit_code
!= MANAGER_REEXECUTE
))
90 repeat_unmout(a
->where
);
93 static void automount_done(Unit
*u
) {
94 Automount
*a
= AUTOMOUNT(u
);
108 static int automount_verify(Automount
*a
) {
113 if (UNIT(a
)->meta
.load_state
!= UNIT_LOADED
)
116 if (!(e
= unit_name_from_path(a
->where
, ".automount")))
119 b
= unit_has_name(UNIT(a
), e
);
123 log_error("%s's Where setting doesn't match unit name. Refusing.", UNIT(a
)->meta
.id
);
130 static int automount_load(Unit
*u
) {
132 Automount
*a
= AUTOMOUNT(u
);
135 assert(u
->meta
.load_state
== UNIT_STUB
);
137 /* Load a .automount file */
138 if ((r
= unit_load_fragment_and_dropin_optional(u
)) < 0)
141 if (u
->meta
.load_state
== UNIT_LOADED
) {
144 if (!(a
->where
= unit_name_to_path(u
->meta
.id
)))
147 path_kill_slashes(a
->where
);
149 if ((r
= unit_load_related_unit(u
, ".mount", (Unit
**) &a
->mount
)) < 0)
152 if ((r
= unit_add_dependency(u
, UNIT_BEFORE
, UNIT(a
->mount
), true)) < 0)
156 return automount_verify(a
);
159 static void automount_set_state(Automount
*a
, AutomountState state
) {
160 AutomountState old_state
;
163 old_state
= a
->state
;
166 if (state
!= AUTOMOUNT_WAITING
&&
167 state
!= AUTOMOUNT_RUNNING
)
170 if (state
!= old_state
)
171 log_debug("%s changed %s → %s",
173 automount_state_to_string(old_state
),
174 automount_state_to_string(state
));
176 unit_notify(UNIT(a
), state_translation_table
[old_state
], state_translation_table
[state
]);
179 static int automount_coldplug(Unit
*u
) {
180 Automount
*a
= AUTOMOUNT(u
);
184 assert(a
->state
== AUTOMOUNT_DEAD
);
186 if (a
->deserialized_state
!= a
->state
) {
188 if ((r
= open_dev_autofs(u
->meta
.manager
)) < 0)
191 if (a
->deserialized_state
== AUTOMOUNT_WAITING
||
192 a
->deserialized_state
== AUTOMOUNT_RUNNING
) {
194 assert(a
->pipe_fd
>= 0);
196 if ((r
= unit_watch_fd(UNIT(a
), a
->pipe_fd
, EPOLLIN
, &a
->pipe_watch
)) < 0)
200 automount_set_state(a
, a
->deserialized_state
);
206 static void automount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
207 Automount
*a
= AUTOMOUNT(u
);
212 "%sAutomount State: %s\n"
214 prefix
, automount_state_to_string(a
->state
),
218 static void automount_enter_dead(Automount
*a
, bool success
) {
224 automount_set_state(a
, a
->failure
? AUTOMOUNT_MAINTAINANCE
: AUTOMOUNT_DEAD
);
227 static int open_dev_autofs(Manager
*m
) {
228 struct autofs_dev_ioctl param
;
232 if (m
->dev_autofs_fd
>= 0)
233 return m
->dev_autofs_fd
;
235 if ((m
->dev_autofs_fd
= open("/dev/autofs", O_CLOEXEC
|O_RDONLY
)) < 0) {
236 log_error("Failed to open /dev/autofs: %s", strerror(errno
));
240 init_autofs_dev_ioctl(¶m
);
241 if (ioctl(m
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_VERSION
, ¶m
) < 0) {
242 close_nointr_nofail(m
->dev_autofs_fd
);
243 m
->dev_autofs_fd
= -1;
247 log_debug("Autofs kernel version %i.%i", param
.ver_major
, param
.ver_minor
);
249 return m
->dev_autofs_fd
;
252 static int open_ioctl_fd(int dev_autofs_fd
, const char *where
, dev_t devid
) {
253 struct autofs_dev_ioctl
*param
;
257 assert(dev_autofs_fd
>= 0);
260 l
= sizeof(struct autofs_dev_ioctl
) + strlen(where
) + 1;
262 if (!(param
= malloc(l
)))
265 init_autofs_dev_ioctl(param
);
268 param
->openmount
.devid
= devid
;
269 strcpy(param
->path
, where
);
271 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_OPENMOUNT
, param
) < 0) {
276 if (param
->ioctlfd
< 0) {
281 fd_cloexec(param
->ioctlfd
, true);
289 static int autofs_protocol(int dev_autofs_fd
, int ioctl_fd
) {
290 uint32_t major
, minor
;
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
;
299 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOVER
, ¶m
) < 0)
302 major
= param
.protover
.version
;
304 init_autofs_dev_ioctl(¶m
);
305 param
.ioctlfd
= ioctl_fd
;
307 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOSUBVER
, ¶m
) < 0)
310 minor
= param
.protosubver
.sub_version
;
312 log_debug("Autofs protocol version %i.%i", major
, minor
);
316 static int autofs_set_timeout(int dev_autofs_fd
, int ioctl_fd
, time_t sec
) {
317 struct autofs_dev_ioctl param
;
319 assert(dev_autofs_fd
>= 0);
320 assert(ioctl_fd
>= 0);
322 init_autofs_dev_ioctl(¶m
);
323 param
.ioctlfd
= ioctl_fd
;
324 param
.timeout
.timeout
= sec
;
326 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_TIMEOUT
, ¶m
) < 0)
332 static int autofs_send_ready(int dev_autofs_fd
, int ioctl_fd
, uint32_t token
, int status
) {
333 struct autofs_dev_ioctl param
;
335 assert(dev_autofs_fd
>= 0);
336 assert(ioctl_fd
>= 0);
338 init_autofs_dev_ioctl(¶m
);
339 param
.ioctlfd
= ioctl_fd
;
342 param
.fail
.token
= token
;
343 param
.fail
.status
= status
;
345 param
.ready
.token
= token
;
347 if (ioctl(dev_autofs_fd
, status
? AUTOFS_DEV_IOCTL_FAIL
: AUTOFS_DEV_IOCTL_READY
, ¶m
) < 0)
353 int automount_send_ready(Automount
*a
, int status
) {
360 if (set_isempty(a
->tokens
))
363 if ((ioctl_fd
= open_ioctl_fd(UNIT(a
)->meta
.manager
->dev_autofs_fd
, a
->where
, a
->dev_id
)) < 0) {
369 log_debug("Sending failure: %s", strerror(-status
));
371 log_debug("Sending success.");
373 /* Autofs thankfully does not hand out 0 as a token */
374 while ((token
= PTR_TO_UINT(set_steal_first(a
->tokens
)))) {
377 /* Autofs fun fact II:
379 * if you pass a positive status code here, the kernel will
382 if ((k
= autofs_send_ready(UNIT(a
)->meta
.manager
->dev_autofs_fd
,
393 close_nointr_nofail(ioctl_fd
);
398 static void automount_enter_waiting(Automount
*a
) {
399 int p
[2] = { -1, -1 };
400 char name
[32], options
[128];
401 bool mounted
= false;
402 int r
, ioctl_fd
= -1, dev_autofs_fd
;
406 assert(a
->pipe_fd
< 0);
410 set_clear(a
->tokens
);
412 if ((dev_autofs_fd
= open_dev_autofs(UNIT(a
)->meta
.manager
)) < 0) {
417 /* We knowingly ignore the results of this call */
418 mkdir_p(a
->where
, 0555);
420 if (pipe2(p
, O_NONBLOCK
|O_CLOEXEC
) < 0) {
425 snprintf(options
, sizeof(options
), "fd=%i,pgrp=%u,minproto=5,maxproto=5,direct", p
[1], (unsigned) getpgrp());
426 char_array_0(options
);
428 snprintf(name
, sizeof(name
), "systemd-%u", (unsigned) getpid());
431 if (mount(name
, a
->where
, "autofs", 0, options
) < 0) {
438 close_nointr_nofail(p
[1]);
441 if (stat(a
->where
, &st
) < 0) {
446 if ((ioctl_fd
= open_ioctl_fd(dev_autofs_fd
, a
->where
, st
.st_dev
)) < 0) {
451 if ((r
= autofs_protocol(dev_autofs_fd
, ioctl_fd
)) < 0)
454 if ((r
= autofs_set_timeout(dev_autofs_fd
, ioctl_fd
, 300)) < 0)
459 * Unless we close the ioctl fd here, for some weird reason
460 * the direct mount will not receive events from the
463 close_nointr_nofail(ioctl_fd
);
466 if ((r
= unit_watch_fd(UNIT(a
), p
[0], EPOLLIN
, &a
->pipe_watch
)) < 0)
470 a
->dev_id
= st
.st_dev
;
472 automount_set_state(a
, AUTOMOUNT_WAITING
);
477 assert_se(close_pipe(p
) == 0);
480 close_nointr_nofail(ioctl_fd
);
483 repeat_unmout(a
->where
);
485 log_error("Failed to initialize automounter: %s", strerror(-r
));
486 automount_enter_dead(a
, false);
489 static void automount_enter_runnning(Automount
*a
) {
496 /* Before we do anything, let's see if somebody is playing games with us? */
498 if (stat(a
->where
, &st
) < 0) {
499 log_warning("%s failed stat automount point: %m", a
->meta
.id
);
503 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
!= a
->dev_id
)
504 log_info("%s's automount point already active?", a
->meta
.id
);
505 else if ((r
= manager_add_job(UNIT(a
)->meta
.manager
, JOB_START
, UNIT(a
->mount
), JOB_REPLACE
, true, NULL
)) < 0) {
506 log_warning("%s failed to queue mount startup job: %s", a
->meta
.id
, strerror(-r
));
510 automount_set_state(a
, AUTOMOUNT_RUNNING
);
514 automount_enter_dead(a
, false);
517 static int automount_start(Unit
*u
) {
518 Automount
*a
= AUTOMOUNT(u
);
522 if (path_is_mount_point(a
->where
)) {
523 log_error("Path %s is already a mount point, refusing start for %s", a
->where
, u
->meta
.id
);
527 assert(a
->state
== AUTOMOUNT_DEAD
|| a
->state
== AUTOMOUNT_MAINTAINANCE
);
530 automount_enter_waiting(a
);
534 static int automount_stop(Unit
*u
) {
535 Automount
*a
= AUTOMOUNT(u
);
539 assert(a
->state
== AUTOMOUNT_WAITING
|| a
->state
== AUTOMOUNT_RUNNING
);
541 automount_enter_dead(a
, true);
545 static int automount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
546 Automount
*a
= AUTOMOUNT(u
);
554 unit_serialize_item(u
, f
, "state", automount_state_to_string(a
->state
));
555 unit_serialize_item(u
, f
, "failure", yes_no(a
->failure
));
556 unit_serialize_item_format(u
, f
, "dev-id", "%u", (unsigned) a
->dev_id
);
558 SET_FOREACH(p
, a
->tokens
, i
)
559 unit_serialize_item_format(u
, f
, "token", "%u", PTR_TO_UINT(p
));
561 if (a
->pipe_fd
>= 0) {
564 if ((copy
= fdset_put_dup(fds
, a
->pipe_fd
)) < 0)
567 unit_serialize_item_format(u
, f
, "pipe-fd", "%i", copy
);
573 static int automount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
574 Automount
*a
= AUTOMOUNT(u
);
580 if (streq(key
, "state")) {
581 AutomountState state
;
583 if ((state
= automount_state_from_string(value
)) < 0)
584 log_debug("Failed to parse state value %s", value
);
586 a
->deserialized_state
= state
;
587 } else if (streq(key
, "failure")) {
590 if ((b
= parse_boolean(value
)) < 0)
591 log_debug("Failed to parse failure value %s", value
);
593 a
->failure
= b
|| a
->failure
;
594 } else if (streq(key
, "dev-id")) {
597 if (safe_atou(value
, &d
) < 0)
598 log_debug("Failed to parse dev-id value %s", value
);
600 a
->dev_id
= (unsigned) d
;
601 } else if (streq(key
, "token")) {
604 if (safe_atou(value
, &token
) < 0)
605 log_debug("Failed to parse token value %s", value
);
608 if (!(a
->tokens
= set_new(trivial_hash_func
, trivial_compare_func
)))
611 if ((r
= set_put(a
->tokens
, UINT_TO_PTR(token
))) < 0)
614 } else if (streq(key
, "pipe-fd")) {
617 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
618 log_debug("Failed to parse pipe-fd value %s", value
);
621 close_nointr_nofail(a
->pipe_fd
);
623 a
->pipe_fd
= fdset_remove(fds
, fd
);
626 log_debug("Unknown serialization key '%s'", key
);
631 static UnitActiveState
automount_active_state(Unit
*u
) {
634 return state_translation_table
[AUTOMOUNT(u
)->state
];
637 static const char *automount_sub_state_to_string(Unit
*u
) {
640 return automount_state_to_string(AUTOMOUNT(u
)->state
);
643 static bool automount_check_gc(Unit
*u
) {
644 Automount
*a
= AUTOMOUNT(u
);
648 return UNIT_VTABLE(UNIT(a
->mount
))->check_gc(UNIT(a
->mount
));
651 static void automount_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
652 Automount
*a
= AUTOMOUNT(u
);
653 union autofs_v5_packet_union packet
;
658 assert(fd
== a
->pipe_fd
);
660 if (events
!= EPOLLIN
) {
661 log_error("Got invalid poll event on pipe.");
665 if ((l
= loop_read(a
->pipe_fd
, &packet
, sizeof(packet
))) != sizeof(packet
)) {
666 log_error("Invalid read from pipe: %s", l
< 0 ? strerror(-l
) : "short read");
670 switch (packet
.hdr
.type
) {
672 case autofs_ptype_missing_direct
:
673 log_debug("Got direct mount request for %s", packet
.v5_packet
.name
);
676 if (!(a
->tokens
= set_new(trivial_hash_func
, trivial_compare_func
))) {
677 log_error("Failed to allocate token set.");
681 if ((r
= set_put(a
->tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
))) < 0) {
682 log_error("Failed to remember token: %s", strerror(-r
));
686 automount_enter_runnning(a
);
690 log_error("Received unknown automount request %i", packet
.hdr
.type
);
697 automount_enter_dead(a
, false);
700 static void automount_shutdown(Manager
*m
) {
703 if (m
->dev_autofs_fd
>= 0)
704 close_nointr_nofail(m
->dev_autofs_fd
);
707 static const char* const automount_state_table
[_AUTOMOUNT_STATE_MAX
] = {
708 [AUTOMOUNT_DEAD
] = "dead",
709 [AUTOMOUNT_WAITING
] = "waiting",
710 [AUTOMOUNT_RUNNING
] = "running",
711 [AUTOMOUNT_MAINTAINANCE
] = "maintainance"
714 DEFINE_STRING_TABLE_LOOKUP(automount_state
, AutomountState
);
716 const UnitVTable automount_vtable
= {
717 .suffix
= ".automount",
720 .no_instances
= true,
722 .init
= automount_init
,
723 .load
= automount_load
,
724 .done
= automount_done
,
726 .coldplug
= automount_coldplug
,
728 .dump
= automount_dump
,
730 .start
= automount_start
,
731 .stop
= automount_stop
,
733 .serialize
= automount_serialize
,
734 .deserialize_item
= automount_deserialize_item
,
736 .active_state
= automount_active_state
,
737 .sub_state_to_string
= automount_sub_state_to_string
,
739 .check_gc
= automount_check_gc
,
741 .fd_event
= automount_fd_event
,
743 .bus_message_handler
= bus_automount_message_handler
,
745 .shutdown
= automount_shutdown