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
) {
62 /* If there are multiple mounts on a mount point, this
65 if (umount2(path
, MNT_DETACH
) >= 0)
69 log_error("Failed to unmount: %m");
75 static void unmount_autofs(Automount
*a
) {
81 automount_send_ready(a
, -EHOSTDOWN
);
83 unit_unwatch_fd(UNIT(a
), &a
->pipe_watch
);
84 close_nointr_nofail(a
->pipe_fd
);
87 /* If we reload/reexecute things we keep the mount point
90 (UNIT(a
)->meta
.manager
->exit_code
!= MANAGER_RELOAD
&&
91 UNIT(a
)->meta
.manager
->exit_code
!= MANAGER_REEXECUTE
))
92 repeat_unmout(a
->where
);
95 static void automount_done(Unit
*u
) {
96 Automount
*a
= AUTOMOUNT(u
);
110 int automount_add_one_mount_link(Automount
*a
, Mount
*m
) {
116 if (a
->meta
.load_state
!= UNIT_LOADED
||
117 m
->meta
.load_state
!= UNIT_LOADED
)
120 if (!path_startswith(a
->where
, m
->where
))
123 if (path_equal(a
->where
, m
->where
))
126 if ((r
= unit_add_dependency(UNIT(m
), UNIT_BEFORE
, UNIT(a
), true)) < 0)
129 if ((r
= unit_add_dependency(UNIT(a
), UNIT_REQUIRES
, UNIT(m
), true)) < 0)
135 static int automount_add_mount_links(Automount
*a
) {
141 LIST_FOREACH(units_per_type
, other
, a
->meta
.manager
->units_per_type
[UNIT_MOUNT
])
142 if ((r
= automount_add_one_mount_link(a
, (Mount
*) other
)) < 0)
148 static int automount_verify(Automount
*a
) {
153 if (UNIT(a
)->meta
.load_state
!= UNIT_LOADED
)
156 if (!(e
= unit_name_from_path(a
->where
, ".automount")))
159 b
= unit_has_name(UNIT(a
), e
);
163 log_error("%s's Where setting doesn't match unit name. Refusing.", UNIT(a
)->meta
.id
);
170 static int automount_load(Unit
*u
) {
172 Automount
*a
= AUTOMOUNT(u
);
175 assert(u
->meta
.load_state
== UNIT_STUB
);
177 /* Load a .automount file */
178 if ((r
= unit_load_fragment_and_dropin_optional(u
)) < 0)
181 if (u
->meta
.load_state
== UNIT_LOADED
) {
184 if (!(a
->where
= unit_name_to_path(u
->meta
.id
)))
187 path_kill_slashes(a
->where
);
189 if ((r
= automount_add_mount_links(a
)) < 0)
192 if ((r
= unit_load_related_unit(u
, ".mount", (Unit
**) &a
->mount
)) < 0)
195 if ((r
= unit_add_dependency(u
, UNIT_BEFORE
, UNIT(a
->mount
), true)) < 0)
199 return automount_verify(a
);
202 static void automount_set_state(Automount
*a
, AutomountState state
) {
203 AutomountState old_state
;
206 old_state
= a
->state
;
209 if (state
!= AUTOMOUNT_WAITING
&&
210 state
!= AUTOMOUNT_RUNNING
)
213 if (state
!= old_state
)
214 log_debug("%s changed %s -> %s",
216 automount_state_to_string(old_state
),
217 automount_state_to_string(state
));
219 unit_notify(UNIT(a
), state_translation_table
[old_state
], state_translation_table
[state
]);
222 static int automount_coldplug(Unit
*u
) {
223 Automount
*a
= AUTOMOUNT(u
);
227 assert(a
->state
== AUTOMOUNT_DEAD
);
229 if (a
->deserialized_state
!= a
->state
) {
231 if ((r
= open_dev_autofs(u
->meta
.manager
)) < 0)
234 if (a
->deserialized_state
== AUTOMOUNT_WAITING
||
235 a
->deserialized_state
== AUTOMOUNT_RUNNING
) {
237 assert(a
->pipe_fd
>= 0);
239 if ((r
= unit_watch_fd(UNIT(a
), a
->pipe_fd
, EPOLLIN
, &a
->pipe_watch
)) < 0)
243 automount_set_state(a
, a
->deserialized_state
);
249 static void automount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
250 Automount
*a
= AUTOMOUNT(u
);
255 "%sAutomount State: %s\n"
257 prefix
, automount_state_to_string(a
->state
),
261 static void automount_enter_dead(Automount
*a
, bool success
) {
267 automount_set_state(a
, a
->failure
? AUTOMOUNT_MAINTAINANCE
: AUTOMOUNT_DEAD
);
270 static int open_dev_autofs(Manager
*m
) {
271 struct autofs_dev_ioctl param
;
275 if (m
->dev_autofs_fd
>= 0)
276 return m
->dev_autofs_fd
;
278 if ((m
->dev_autofs_fd
= open("/dev/autofs", O_CLOEXEC
|O_RDONLY
)) < 0) {
279 log_error("Failed to open /dev/autofs: %s", strerror(errno
));
283 init_autofs_dev_ioctl(¶m
);
284 if (ioctl(m
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_VERSION
, ¶m
) < 0) {
285 close_nointr_nofail(m
->dev_autofs_fd
);
286 m
->dev_autofs_fd
= -1;
290 log_debug("Autofs kernel version %i.%i", param
.ver_major
, param
.ver_minor
);
292 return m
->dev_autofs_fd
;
295 static int open_ioctl_fd(int dev_autofs_fd
, const char *where
, dev_t devid
) {
296 struct autofs_dev_ioctl
*param
;
300 assert(dev_autofs_fd
>= 0);
303 l
= sizeof(struct autofs_dev_ioctl
) + strlen(where
) + 1;
305 if (!(param
= malloc(l
)))
308 init_autofs_dev_ioctl(param
);
311 param
->openmount
.devid
= devid
;
312 strcpy(param
->path
, where
);
314 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_OPENMOUNT
, param
) < 0) {
319 if (param
->ioctlfd
< 0) {
324 fd_cloexec(param
->ioctlfd
, true);
332 static int autofs_protocol(int dev_autofs_fd
, int ioctl_fd
) {
333 uint32_t major
, minor
;
334 struct autofs_dev_ioctl param
;
336 assert(dev_autofs_fd
>= 0);
337 assert(ioctl_fd
>= 0);
339 init_autofs_dev_ioctl(¶m
);
340 param
.ioctlfd
= ioctl_fd
;
342 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOVER
, ¶m
) < 0)
345 major
= param
.protover
.version
;
347 init_autofs_dev_ioctl(¶m
);
348 param
.ioctlfd
= ioctl_fd
;
350 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOSUBVER
, ¶m
) < 0)
353 minor
= param
.protosubver
.sub_version
;
355 log_debug("Autofs protocol version %i.%i", major
, minor
);
359 static int autofs_set_timeout(int dev_autofs_fd
, int ioctl_fd
, time_t sec
) {
360 struct autofs_dev_ioctl param
;
362 assert(dev_autofs_fd
>= 0);
363 assert(ioctl_fd
>= 0);
365 init_autofs_dev_ioctl(¶m
);
366 param
.ioctlfd
= ioctl_fd
;
367 param
.timeout
.timeout
= sec
;
369 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_TIMEOUT
, ¶m
) < 0)
375 static int autofs_send_ready(int dev_autofs_fd
, int ioctl_fd
, uint32_t token
, int status
) {
376 struct autofs_dev_ioctl param
;
378 assert(dev_autofs_fd
>= 0);
379 assert(ioctl_fd
>= 0);
381 init_autofs_dev_ioctl(¶m
);
382 param
.ioctlfd
= ioctl_fd
;
385 param
.fail
.token
= token
;
386 param
.fail
.status
= status
;
388 param
.ready
.token
= token
;
390 if (ioctl(dev_autofs_fd
, status
? AUTOFS_DEV_IOCTL_FAIL
: AUTOFS_DEV_IOCTL_READY
, ¶m
) < 0)
396 int automount_send_ready(Automount
*a
, int status
) {
403 if (set_isempty(a
->tokens
))
406 if ((ioctl_fd
= open_ioctl_fd(UNIT(a
)->meta
.manager
->dev_autofs_fd
, a
->where
, a
->dev_id
)) < 0) {
412 log_debug("Sending failure: %s", strerror(-status
));
414 log_debug("Sending success.");
416 /* Autofs thankfully does not hand out 0 as a token */
417 while ((token
= PTR_TO_UINT(set_steal_first(a
->tokens
)))) {
420 /* Autofs fun fact II:
422 * if you pass a positive status code here, the kernel will
425 if ((k
= autofs_send_ready(UNIT(a
)->meta
.manager
->dev_autofs_fd
,
436 close_nointr_nofail(ioctl_fd
);
441 static void automount_enter_waiting(Automount
*a
) {
442 int p
[2] = { -1, -1 };
443 char name
[32], options
[128];
444 bool mounted
= false;
445 int r
, ioctl_fd
= -1, dev_autofs_fd
;
449 assert(a
->pipe_fd
< 0);
453 set_clear(a
->tokens
);
455 if ((dev_autofs_fd
= open_dev_autofs(UNIT(a
)->meta
.manager
)) < 0) {
460 /* We knowingly ignore the results of this call */
461 mkdir_p(a
->where
, 0555);
463 if (pipe2(p
, O_NONBLOCK
|O_CLOEXEC
) < 0) {
468 snprintf(options
, sizeof(options
), "fd=%i,pgrp=%u,minproto=5,maxproto=5,direct", p
[1], (unsigned) getpgrp());
469 char_array_0(options
);
471 snprintf(name
, sizeof(name
), "systemd-%u", (unsigned) getpid());
474 if (mount(name
, a
->where
, "autofs", 0, options
) < 0) {
481 close_nointr_nofail(p
[1]);
484 if (stat(a
->where
, &st
) < 0) {
489 if ((ioctl_fd
= open_ioctl_fd(dev_autofs_fd
, a
->where
, st
.st_dev
)) < 0) {
494 if ((r
= autofs_protocol(dev_autofs_fd
, ioctl_fd
)) < 0)
497 if ((r
= autofs_set_timeout(dev_autofs_fd
, ioctl_fd
, 300)) < 0)
502 * Unless we close the ioctl fd here, for some weird reason
503 * the direct mount will not receive events from the
506 close_nointr_nofail(ioctl_fd
);
509 if ((r
= unit_watch_fd(UNIT(a
), p
[0], EPOLLIN
, &a
->pipe_watch
)) < 0)
513 a
->dev_id
= st
.st_dev
;
515 automount_set_state(a
, AUTOMOUNT_WAITING
);
520 assert_se(close_pipe(p
) == 0);
523 close_nointr_nofail(ioctl_fd
);
526 repeat_unmout(a
->where
);
528 log_error("Failed to initialize automounter: %s", strerror(-r
));
529 automount_enter_dead(a
, false);
532 static void automount_enter_runnning(Automount
*a
) {
539 /* Before we do anything, let's see if somebody is playing games with us? */
541 if (stat(a
->where
, &st
) < 0) {
542 log_warning("%s failed stat automount point: %m", a
->meta
.id
);
546 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
!= a
->dev_id
)
547 log_info("%s's automount point already active?", a
->meta
.id
);
548 else if ((r
= manager_add_job(UNIT(a
)->meta
.manager
, JOB_START
, UNIT(a
->mount
), JOB_REPLACE
, true, NULL
)) < 0) {
549 log_warning("%s failed to queue mount startup job: %s", a
->meta
.id
, strerror(-r
));
553 automount_set_state(a
, AUTOMOUNT_RUNNING
);
557 automount_enter_dead(a
, false);
560 static int automount_start(Unit
*u
) {
561 Automount
*a
= AUTOMOUNT(u
);
565 if (path_is_mount_point(a
->where
)) {
566 log_error("Path %s is already a mount point, refusing start for %s", a
->where
, u
->meta
.id
);
570 assert(a
->state
== AUTOMOUNT_DEAD
|| a
->state
== AUTOMOUNT_MAINTAINANCE
);
573 automount_enter_waiting(a
);
577 static int automount_stop(Unit
*u
) {
578 Automount
*a
= AUTOMOUNT(u
);
582 assert(a
->state
== AUTOMOUNT_WAITING
|| a
->state
== AUTOMOUNT_RUNNING
);
584 automount_enter_dead(a
, true);
588 static int automount_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
589 Automount
*a
= AUTOMOUNT(u
);
597 unit_serialize_item(u
, f
, "state", automount_state_to_string(a
->state
));
598 unit_serialize_item(u
, f
, "failure", yes_no(a
->failure
));
599 unit_serialize_item_format(u
, f
, "dev-id", "%u", (unsigned) a
->dev_id
);
601 SET_FOREACH(p
, a
->tokens
, i
)
602 unit_serialize_item_format(u
, f
, "token", "%u", PTR_TO_UINT(p
));
604 if (a
->pipe_fd
>= 0) {
607 if ((copy
= fdset_put_dup(fds
, a
->pipe_fd
)) < 0)
610 unit_serialize_item_format(u
, f
, "pipe-fd", "%i", copy
);
616 static int automount_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
617 Automount
*a
= AUTOMOUNT(u
);
623 if (streq(key
, "state")) {
624 AutomountState state
;
626 if ((state
= automount_state_from_string(value
)) < 0)
627 log_debug("Failed to parse state value %s", value
);
629 a
->deserialized_state
= state
;
630 } else if (streq(key
, "failure")) {
633 if ((b
= parse_boolean(value
)) < 0)
634 log_debug("Failed to parse failure value %s", value
);
636 a
->failure
= b
|| a
->failure
;
637 } else if (streq(key
, "dev-id")) {
640 if (safe_atou(value
, &d
) < 0)
641 log_debug("Failed to parse dev-id value %s", value
);
643 a
->dev_id
= (unsigned) d
;
644 } else if (streq(key
, "token")) {
647 if (safe_atou(value
, &token
) < 0)
648 log_debug("Failed to parse token value %s", value
);
651 if (!(a
->tokens
= set_new(trivial_hash_func
, trivial_compare_func
)))
654 if ((r
= set_put(a
->tokens
, UINT_TO_PTR(token
))) < 0)
657 } else if (streq(key
, "pipe-fd")) {
660 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
661 log_debug("Failed to parse pipe-fd value %s", value
);
664 close_nointr_nofail(a
->pipe_fd
);
666 a
->pipe_fd
= fdset_remove(fds
, fd
);
669 log_debug("Unknown serialization key '%s'", key
);
674 static UnitActiveState
automount_active_state(Unit
*u
) {
677 return state_translation_table
[AUTOMOUNT(u
)->state
];
680 static const char *automount_sub_state_to_string(Unit
*u
) {
683 return automount_state_to_string(AUTOMOUNT(u
)->state
);
686 static bool automount_check_gc(Unit
*u
) {
687 Automount
*a
= AUTOMOUNT(u
);
691 return UNIT_VTABLE(UNIT(a
->mount
))->check_gc(UNIT(a
->mount
));
694 static void automount_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
695 Automount
*a
= AUTOMOUNT(u
);
696 union autofs_v5_packet_union packet
;
701 assert(fd
== a
->pipe_fd
);
703 if (events
!= EPOLLIN
) {
704 log_error("Got invalid poll event on pipe.");
708 if ((l
= loop_read(a
->pipe_fd
, &packet
, sizeof(packet
))) != sizeof(packet
)) {
709 log_error("Invalid read from pipe: %s", l
< 0 ? strerror(-l
) : "short read");
713 switch (packet
.hdr
.type
) {
715 case autofs_ptype_missing_direct
:
716 log_debug("Got direct mount request for %s", packet
.v5_packet
.name
);
719 if (!(a
->tokens
= set_new(trivial_hash_func
, trivial_compare_func
))) {
720 log_error("Failed to allocate token set.");
724 if ((r
= set_put(a
->tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
))) < 0) {
725 log_error("Failed to remember token: %s", strerror(-r
));
729 automount_enter_runnning(a
);
733 log_error("Received unknown automount request %i", packet
.hdr
.type
);
740 automount_enter_dead(a
, false);
743 static void automount_shutdown(Manager
*m
) {
746 if (m
->dev_autofs_fd
>= 0)
747 close_nointr_nofail(m
->dev_autofs_fd
);
750 static const char* const automount_state_table
[_AUTOMOUNT_STATE_MAX
] = {
751 [AUTOMOUNT_DEAD
] = "dead",
752 [AUTOMOUNT_WAITING
] = "waiting",
753 [AUTOMOUNT_RUNNING
] = "running",
754 [AUTOMOUNT_MAINTAINANCE
] = "maintainance"
757 DEFINE_STRING_TABLE_LOOKUP(automount_state
, AutomountState
);
759 const UnitVTable automount_vtable
= {
760 .suffix
= ".automount",
763 .no_instances
= true,
765 .init
= automount_init
,
766 .load
= automount_load
,
767 .done
= automount_done
,
769 .coldplug
= automount_coldplug
,
771 .dump
= automount_dump
,
773 .start
= automount_start
,
774 .stop
= automount_stop
,
776 .serialize
= automount_serialize
,
777 .deserialize_item
= automount_deserialize_item
,
779 .active_state
= automount_active_state
,
780 .sub_state_to_string
= automount_sub_state_to_string
,
782 .check_gc
= automount_check_gc
,
784 .fd_event
= automount_fd_event
,
786 .bus_message_handler
= bus_automount_message_handler
,
788 .shutdown
= automount_shutdown