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 const char* const state_string_table
[_AUTOMOUNT_STATE_MAX
] = {
47 [AUTOMOUNT_DEAD
] = "dead",
48 [AUTOMOUNT_WAITING
] = "waiting",
49 [AUTOMOUNT_RUNNING
] = "running",
50 [AUTOMOUNT_MAINTAINANCE
] = "maintainance"
53 static char *automount_name_from_where(const char *where
) {
56 if (streq(where
, "/"))
57 return strdup("-.automount");
59 return unit_name_build_escape(where
+1, NULL
, ".automount");
62 static void automount_init(Unit
*u
) {
63 Automount
*a
= AUTOMOUNT(u
);
66 assert(u
->meta
.load_state
== UNIT_STUB
);
68 a
->pipe_watch
.fd
= a
->pipe_fd
= -1;
71 static void repeat_unmout(const char *path
) {
76 if (umount2(path
, MNT_DETACH
) >= 0)
80 log_error("Failed to unmount: %m");
86 static void unmount_autofs(Automount
*a
) {
92 automount_send_ready(a
, -EHOSTDOWN
);
94 unit_unwatch_fd(UNIT(a
), &a
->pipe_watch
);
95 close_nointr_nofail(a
->pipe_fd
);
98 repeat_unmout(a
->where
);
101 static void automount_done(Unit
*u
) {
102 Automount
*a
= AUTOMOUNT(u
);
115 static int automount_verify(Automount
*a
) {
120 if (UNIT(a
)->meta
.load_state
!= UNIT_LOADED
)
124 log_error("%s lacks Where setting. Refusing.", UNIT(a
)->meta
.id
);
128 path_kill_slashes(a
->where
);
130 if (!(e
= automount_name_from_where(a
->where
)))
133 b
= unit_has_name(UNIT(a
), e
);
137 log_error("%s's Where setting doesn't match unit name. Refusing.", UNIT(a
)->meta
.id
);
144 static int automount_load(Unit
*u
) {
146 Automount
*a
= AUTOMOUNT(u
);
149 assert(u
->meta
.load_state
== UNIT_STUB
);
151 /* Load a .automount file */
152 if ((r
= unit_load_fragment_and_dropin_optional(u
)) < 0)
155 if (u
->meta
.load_state
== UNIT_LOADED
) {
157 if ((r
= unit_load_related_unit(u
, ".mount", (Unit
**) &a
->mount
)) < 0)
160 if ((r
= unit_add_dependency(u
, UNIT_BEFORE
, UNIT(a
->mount
))) < 0)
164 return automount_verify(a
);
167 static void automount_set_state(Automount
*a
, AutomountState state
) {
168 AutomountState old_state
;
171 old_state
= a
->state
;
174 if (state
!= AUTOMOUNT_WAITING
&&
175 state
!= AUTOMOUNT_RUNNING
)
178 if (state
!= old_state
)
179 log_debug("%s changed %s → %s", UNIT(a
)->meta
.id
, state_string_table
[old_state
], state_string_table
[state
]);
181 unit_notify(UNIT(a
), state_translation_table
[old_state
], state_translation_table
[state
]);
184 static void automount_dump(Unit
*u
, FILE *f
, const char *prefix
) {
185 Automount
*s
= AUTOMOUNT(u
);
190 "%sAutomount State: %s\n",
191 prefix
, state_string_table
[s
->state
]);
194 static void automount_enter_dead(Automount
*a
, bool success
) {
200 automount_set_state(a
, a
->failure
? AUTOMOUNT_MAINTAINANCE
: AUTOMOUNT_DEAD
);
203 static int open_dev_autofs(Manager
*m
) {
204 struct autofs_dev_ioctl param
;
208 if (m
->dev_autofs_fd
>= 0)
209 return m
->dev_autofs_fd
;
211 if ((m
->dev_autofs_fd
= open("/dev/autofs", O_RDONLY
)) < 0) {
212 log_error("Failed to open /dev/autofs: %s", strerror(errno
));
216 init_autofs_dev_ioctl(¶m
);
217 if (ioctl(m
->dev_autofs_fd
, AUTOFS_DEV_IOCTL_VERSION
, ¶m
) < 0) {
218 close_nointr_nofail(m
->dev_autofs_fd
);
219 m
->dev_autofs_fd
= -1;
223 log_debug("Autofs kernel version %i.%i", param
.ver_major
, param
.ver_minor
);
225 return m
->dev_autofs_fd
;
228 static int open_ioctl_fd(int dev_autofs_fd
, const char *where
, dev_t devid
) {
229 struct autofs_dev_ioctl
*param
;
233 assert(dev_autofs_fd
>= 0);
236 l
= sizeof(struct autofs_dev_ioctl
) + strlen(where
) + 1;
238 if (!(param
= malloc(l
)))
241 init_autofs_dev_ioctl(param
);
244 param
->openmount
.devid
= devid
;
245 strcpy(param
->path
, where
);
247 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_OPENMOUNT
, param
) < 0) {
252 if (param
->ioctlfd
< 0) {
264 static int autofs_protocol(int dev_autofs_fd
, int ioctl_fd
) {
265 uint32_t major
, minor
;
266 struct autofs_dev_ioctl param
;
268 assert(dev_autofs_fd
>= 0);
269 assert(ioctl_fd
>= 0);
271 init_autofs_dev_ioctl(¶m
);
272 param
.ioctlfd
= ioctl_fd
;
274 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOVER
, ¶m
) < 0)
277 major
= param
.protover
.version
;
279 init_autofs_dev_ioctl(¶m
);
280 param
.ioctlfd
= ioctl_fd
;
282 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_PROTOSUBVER
, ¶m
) < 0)
285 minor
= param
.protosubver
.sub_version
;
287 log_debug("Autofs protocol version %i.%i", major
, minor
);
291 static int autofs_set_timeout(int dev_autofs_fd
, int ioctl_fd
, time_t sec
) {
292 struct autofs_dev_ioctl param
;
294 assert(dev_autofs_fd
>= 0);
295 assert(ioctl_fd
>= 0);
297 init_autofs_dev_ioctl(¶m
);
298 param
.ioctlfd
= ioctl_fd
;
299 param
.timeout
.timeout
= sec
;
301 if (ioctl(dev_autofs_fd
, AUTOFS_DEV_IOCTL_TIMEOUT
, ¶m
) < 0)
307 static int autofs_send_ready(int dev_autofs_fd
, int ioctl_fd
, uint32_t token
, int status
) {
308 struct autofs_dev_ioctl param
;
310 assert(dev_autofs_fd
>= 0);
311 assert(ioctl_fd
>= 0);
313 init_autofs_dev_ioctl(¶m
);
314 param
.ioctlfd
= ioctl_fd
;
317 param
.fail
.token
= token
;
318 param
.fail
.status
= status
;
320 param
.ready
.token
= token
;
322 if (ioctl(dev_autofs_fd
, status
? AUTOFS_DEV_IOCTL_FAIL
: AUTOFS_DEV_IOCTL_READY
, ¶m
) < 0)
328 int automount_send_ready(Automount
*a
, int status
) {
335 if (set_isempty(a
->tokens
))
338 if ((ioctl_fd
= open_ioctl_fd(UNIT(a
)->meta
.manager
->dev_autofs_fd
, a
->where
, a
->dev_id
)) < 0) {
344 log_debug("Sending failure: %s", strerror(-status
));
346 log_debug("Sending success.");
348 /* Autofs thankfully does not hand out 0 as a token */
349 while ((token
= PTR_TO_UINT(set_steal_first(a
->tokens
)))) {
352 /* Autofs fun fact II:
354 * if you pass a positive status code here, the kernel will
357 if ((k
= autofs_send_ready(UNIT(a
)->meta
.manager
->dev_autofs_fd
,
368 close_nointr_nofail(ioctl_fd
);
373 static void automount_enter_waiting(Automount
*a
) {
374 int p
[2] = { -1, -1 };
375 char name
[32], options
[128];
376 bool mounted
= false;
377 int r
, ioctl_fd
= -1, dev_autofs_fd
;
381 assert(a
->pipe_fd
< 0);
385 set_clear(a
->tokens
);
386 else if (!(a
->tokens
= set_new(trivial_hash_func
, trivial_compare_func
))) {
391 if ((dev_autofs_fd
= open_dev_autofs(UNIT(a
)->meta
.manager
)) < 0) {
396 /* We knowingly ignore the results of this call */
397 mkdir_p(a
->where
, 0555);
399 if (pipe2(p
, O_NONBLOCK
) < 0) {
404 snprintf(options
, sizeof(options
), "fd=%i,pgrp=%u,minproto=5,maxproto=5,direct", p
[1], (unsigned) getpgrp());
405 char_array_0(options
);
407 snprintf(name
, sizeof(name
), "systemd-%u", (unsigned) getpid());
410 if (mount(name
, a
->where
, "autofs", 0, options
) < 0) {
417 close_nointr_nofail(p
[1]);
420 if (stat(a
->where
, &st
) < 0) {
425 if ((ioctl_fd
= open_ioctl_fd(dev_autofs_fd
, a
->where
, st
.st_dev
)) < 0) {
430 if ((r
= autofs_protocol(dev_autofs_fd
, ioctl_fd
)) < 0)
433 if ((r
= autofs_set_timeout(dev_autofs_fd
, ioctl_fd
, 300)) < 0)
438 * Unless we close the ioctl fd here, for some weird reason
439 * the direct mount will not receive events from the
442 close_nointr_nofail(ioctl_fd
);
445 if ((r
= unit_watch_fd(UNIT(a
), p
[0], EPOLLIN
, &a
->pipe_watch
)) < 0)
449 a
->dev_id
= st
.st_dev
;
451 automount_set_state(a
, AUTOMOUNT_WAITING
);
456 assert_se(close_pipe(p
) == 0);
459 close_nointr_nofail(ioctl_fd
);
462 repeat_unmout(a
->where
);
464 log_error("Failed to initialize automounter: %s", strerror(-r
));
465 automount_enter_dead(a
, false);
468 static void automount_enter_runnning(Automount
*a
) {
475 /* Before we do anything, let's see if somebody is playing games with us? */
477 if (stat(a
->where
, &st
) < 0) {
478 log_warning("%s failed stat automount point: %m", a
->meta
.id
);
482 if (!S_ISDIR(st
.st_mode
) || st
.st_dev
!= a
->dev_id
)
483 log_info("%s's automount point already active?", a
->meta
.id
);
484 else if ((r
= manager_add_job(UNIT(a
)->meta
.manager
, JOB_START
, UNIT(a
->mount
), JOB_REPLACE
, true, NULL
)) < 0) {
485 log_warning("%s failed to queue mount startup job: %s", a
->meta
.id
, strerror(-r
));
489 automount_set_state(a
, AUTOMOUNT_RUNNING
);
493 automount_enter_dead(a
, false);
496 static int automount_start(Unit
*u
) {
497 Automount
*a
= AUTOMOUNT(u
);
501 if (path_is_mount_point(a
->where
)) {
502 log_error("Path %s is already a mount point, refusing start for %s", a
->where
, u
->meta
.id
);
506 assert(a
->state
== AUTOMOUNT_DEAD
|| a
->state
== AUTOMOUNT_MAINTAINANCE
);
509 automount_enter_waiting(a
);
513 static int automount_stop(Unit
*u
) {
514 Automount
*a
= AUTOMOUNT(u
);
518 assert(a
->state
== AUTOMOUNT_WAITING
|| a
->state
== AUTOMOUNT_RUNNING
);
520 automount_enter_dead(a
, true);
524 static UnitActiveState
automount_active_state(Unit
*u
) {
526 return state_translation_table
[AUTOMOUNT(u
)->state
];
529 static const char *automount_sub_state_to_string(Unit
*u
) {
532 return state_string_table
[AUTOMOUNT(u
)->state
];
535 static void automount_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
536 union autofs_v5_packet_union packet
;
540 Automount
*a
= AUTOMOUNT(u
);
543 assert(fd
== a
->pipe_fd
);
545 if (events
!= EPOLLIN
) {
546 log_error("Got invalid poll event on pipe.");
550 if ((l
= loop_read(a
->pipe_fd
, &packet
, sizeof(packet
))) != sizeof(packet
)) {
551 log_error("Invalid read from pipe: %s", l
< 0 ? strerror(-l
) : "short read");
555 switch (packet
.hdr
.type
) {
557 case autofs_ptype_missing_direct
:
558 log_debug("Got direct mount request for %s", packet
.v5_packet
.name
);
560 if ((r
= set_put(a
->tokens
, UINT_TO_PTR(packet
.v5_packet
.wait_queue_token
))) < 0) {
561 log_error("Failed to remember token: %s", strerror(-r
));
565 automount_enter_runnning(a
);
569 log_error("Received unknown automount request %i", packet
.hdr
.type
);
576 automount_enter_dead(a
, false);
579 static void automount_shutdown(Manager
*m
) {
582 if (m
->dev_autofs_fd
>= 0)
583 close_nointr_nofail(m
->dev_autofs_fd
);
586 const UnitVTable automount_vtable
= {
587 .suffix
= ".automount",
590 .no_instances
= true,
592 .init
= automount_init
,
593 .load
= automount_load
,
594 .done
= automount_done
,
596 .dump
= automount_dump
,
598 .start
= automount_start
,
599 .stop
= automount_stop
,
601 .active_state
= automount_active_state
,
602 .sub_state_to_string
= automount_sub_state_to_string
,
604 .fd_event
= automount_fd_event
,
606 .bus_message_handler
= bus_automount_message_handler
,
608 .shutdown
= automount_shutdown