]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/automount.c
b85177d04a4c8081ea8a4d36c1ccd30332294a14
[thirdparty/systemd.git] / src / core / automount.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <linux/auto_dev-ioctl.h>
5 #include <sys/mount.h>
6 #include <sys/stat.h>
7 #include <unistd.h>
8
9 #include "sd-bus.h"
10
11 #include "alloc-util.h"
12 #include "automount.h"
13 #include "bus-error.h"
14 #include "dbus-automount.h"
15 #include "dbus-unit.h"
16 #include "errno-util.h"
17 #include "fd-util.h"
18 #include "format-util.h"
19 #include "fstab-util.h"
20 #include "io-util.h"
21 #include "label-util.h"
22 #include "manager.h"
23 #include "mkdir-label.h"
24 #include "mount.h"
25 #include "mount-util.h"
26 #include "mountpoint-util.h"
27 #include "parse-util.h"
28 #include "path-util.h"
29 #include "process-util.h"
30 #include "serialize.h"
31 #include "set.h"
32 #include "special.h"
33 #include "stdio-util.h"
34 #include "string-table.h"
35 #include "string-util.h"
36 #include "unit.h"
37 #include "unit-name.h"
38
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_FAILED] = UNIT_FAILED,
44 };
45
46 static int open_dev_autofs(Manager *m);
47 static int automount_dispatch_io(sd_event_source *s, int fd, uint32_t events, void *userdata);
48 static int automount_start_expire(Automount *a);
49 static void automount_stop_expire(Automount *a);
50 static int automount_send_ready(Automount *a, Set *tokens, int status);
51
52 static void automount_init(Unit *u) {
53 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
54
55 assert(u->load_state == UNIT_STUB);
56
57 a->pipe_fd = -EBADF;
58 a->directory_mode = 0755;
59 UNIT(a)->ignore_on_isolate = true;
60 }
61
62 static void unmount_autofs(Automount *a) {
63 int r;
64
65 assert(a);
66
67 if (a->pipe_fd < 0)
68 return;
69
70 a->pipe_event_source = sd_event_source_disable_unref(a->pipe_event_source);
71 a->pipe_fd = safe_close(a->pipe_fd);
72
73 /* If we reload/reexecute things we keep the mount point around */
74 if (!IN_SET(UNIT(a)->manager->objective, MANAGER_RELOAD, MANAGER_REEXECUTE)) {
75
76 automount_send_ready(a, a->tokens, -EHOSTDOWN);
77 automount_send_ready(a, a->expire_tokens, -EHOSTDOWN);
78
79 if (a->where) {
80 r = repeat_unmount(a->where, MNT_DETACH|UMOUNT_NOFOLLOW);
81 if (r < 0)
82 log_unit_error_errno(UNIT(a), r, "Failed to unmount: %m");
83 }
84 }
85 }
86
87 static void automount_done(Unit *u) {
88 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
89
90 unmount_autofs(a);
91
92 a->where = mfree(a->where);
93 a->extra_options = mfree(a->extra_options);
94
95 a->tokens = set_free(a->tokens);
96 a->expire_tokens = set_free(a->expire_tokens);
97
98 a->expire_event_source = sd_event_source_disable_unref(a->expire_event_source);
99 }
100
101 static int automount_add_trigger_dependencies(Automount *a) {
102 Unit *x;
103 int r;
104
105 assert(a);
106
107 r = unit_load_related_unit(UNIT(a), ".mount", &x);
108 if (r < 0)
109 return r;
110
111 return unit_add_two_dependencies(UNIT(a), UNIT_BEFORE, UNIT_TRIGGERS, x, true, UNIT_DEPENDENCY_IMPLICIT);
112 }
113
114 static int automount_add_mount_dependencies(Automount *a) {
115 _cleanup_free_ char *parent = NULL;
116 int r;
117
118 assert(a);
119
120 r = path_extract_directory(a->where, &parent);
121 if (r < 0)
122 return r;
123
124 return unit_add_mounts_for(UNIT(a), parent, UNIT_DEPENDENCY_IMPLICIT, UNIT_MOUNT_REQUIRES);
125 }
126
127 static int automount_add_default_dependencies(Automount *a) {
128 int r;
129
130 assert(a);
131
132 if (!UNIT(a)->default_dependencies)
133 return 0;
134
135 if (!MANAGER_IS_SYSTEM(UNIT(a)->manager))
136 return 0;
137
138 r = unit_add_dependency_by_name(UNIT(a), UNIT_BEFORE, SPECIAL_LOCAL_FS_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
139 if (r < 0)
140 return r;
141
142 r = unit_add_dependency_by_name(UNIT(a), UNIT_AFTER, SPECIAL_LOCAL_FS_PRE_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
143 if (r < 0)
144 return r;
145
146 r = unit_add_two_dependencies_by_name(UNIT(a), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
147 if (r < 0)
148 return r;
149
150 return 0;
151 }
152
153 static int automount_verify(Automount *a) {
154 static const char *const reserved_options[] = {
155 "fd\0",
156 "pgrp\0",
157 "minproto\0",
158 "maxproto\0",
159 "direct\0",
160 "indirect\0",
161 };
162
163 _cleanup_free_ char *e = NULL;
164 int r;
165
166 assert(a);
167 assert(UNIT(a)->load_state == UNIT_LOADED);
168
169 if (path_equal(a->where, "/"))
170 return log_unit_error_errno(UNIT(a), SYNTHETIC_ERRNO(ENOEXEC), "Cannot have an automount unit for the root directory. Refusing.");
171
172 r = unit_name_from_path(a->where, ".automount", &e);
173 if (r < 0)
174 return log_unit_error_errno(UNIT(a), r, "Failed to generate unit name from path: %m");
175
176 if (!unit_has_name(UNIT(a), e))
177 return log_unit_error_errno(UNIT(a), SYNTHETIC_ERRNO(ENOEXEC), "Where= setting doesn't match unit name. Refusing.");
178
179 FOREACH_ELEMENT(reserved_option, reserved_options)
180 if (fstab_test_option(a->extra_options, *reserved_option))
181 return log_unit_error_errno(
182 UNIT(a),
183 SYNTHETIC_ERRNO(ENOEXEC),
184 "ExtraOptions= setting may not contain reserved option %s.",
185 *reserved_option);
186
187 return 0;
188 }
189
190 static int automount_set_where(Automount *a) {
191 int r;
192
193 assert(a);
194
195 if (a->where)
196 return 0;
197
198 r = unit_name_to_path(UNIT(a)->id, &a->where);
199 if (r < 0)
200 return r;
201
202 path_simplify(a->where);
203 return 1;
204 }
205
206 static int automount_add_extras(Automount *a) {
207 int r;
208
209 r = automount_set_where(a);
210 if (r < 0)
211 return r;
212
213 r = automount_add_trigger_dependencies(a);
214 if (r < 0)
215 return r;
216
217 r = automount_add_mount_dependencies(a);
218 if (r < 0)
219 return r;
220
221 return automount_add_default_dependencies(a);
222 }
223
224 static int automount_load(Unit *u) {
225 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
226 int r;
227
228 assert(u->load_state == UNIT_STUB);
229
230 /* Load a .automount file */
231 r = unit_load_fragment_and_dropin(u, true);
232 if (r < 0)
233 return r;
234
235 if (u->load_state != UNIT_LOADED)
236 return 0;
237
238 r = automount_add_extras(a);
239 if (r < 0)
240 return r;
241
242 return automount_verify(a);
243 }
244
245 static void automount_set_state(Automount *a, AutomountState state) {
246 AutomountState old_state;
247
248 assert(a);
249
250 if (a->state != state)
251 bus_unit_send_pending_change_signal(UNIT(a), false);
252
253 old_state = a->state;
254 a->state = state;
255
256 if (state != AUTOMOUNT_RUNNING)
257 automount_stop_expire(a);
258
259 if (!IN_SET(state, AUTOMOUNT_WAITING, AUTOMOUNT_RUNNING))
260 unmount_autofs(a);
261
262 if (state != old_state)
263 log_unit_debug(UNIT(a), "Changed %s -> %s", automount_state_to_string(old_state), automount_state_to_string(state));
264
265 unit_notify(UNIT(a), state_translation_table[old_state], state_translation_table[state], /* reload_success = */ true);
266 }
267
268 static int automount_coldplug(Unit *u) {
269 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
270 int r;
271
272 assert(a->state == AUTOMOUNT_DEAD);
273
274 if (a->deserialized_state == a->state)
275 return 0;
276
277 if (IN_SET(a->deserialized_state, AUTOMOUNT_WAITING, AUTOMOUNT_RUNNING)) {
278
279 r = automount_set_where(a);
280 if (r < 0)
281 return r;
282
283 r = open_dev_autofs(u->manager);
284 if (r < 0)
285 return r;
286
287 assert(a->pipe_fd >= 0);
288
289 r = sd_event_add_io(u->manager->event, &a->pipe_event_source, a->pipe_fd, EPOLLIN, automount_dispatch_io, u);
290 if (r < 0)
291 return r;
292
293 (void) sd_event_source_set_description(a->pipe_event_source, "automount-io");
294 if (a->deserialized_state == AUTOMOUNT_RUNNING) {
295 r = automount_start_expire(a);
296 if (r < 0)
297 log_unit_warning_errno(UNIT(a), r, "Failed to start expiration timer, ignoring: %m");
298 }
299
300 automount_set_state(a, a->deserialized_state);
301 }
302
303 return 0;
304 }
305
306 static void automount_dump(Unit *u, FILE *f, const char *prefix) {
307 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
308
309 fprintf(f,
310 "%sAutomount State: %s\n"
311 "%sResult: %s\n"
312 "%sWhere: %s\n"
313 "%sExtraOptions: %s\n"
314 "%sDirectoryMode: %04o\n"
315 "%sTimeoutIdleUSec: %s\n",
316 prefix, automount_state_to_string(a->state),
317 prefix, automount_result_to_string(a->result),
318 prefix, a->where,
319 prefix, a->extra_options,
320 prefix, a->directory_mode,
321 prefix, FORMAT_TIMESPAN(a->timeout_idle_usec, USEC_PER_SEC));
322 }
323
324 static void automount_enter_dead(Automount *a, AutomountResult f) {
325 assert(a);
326
327 if (a->result == AUTOMOUNT_SUCCESS)
328 a->result = f;
329
330 unit_log_result(UNIT(a), a->result == AUTOMOUNT_SUCCESS, automount_result_to_string(a->result));
331 automount_set_state(a, a->result != AUTOMOUNT_SUCCESS ? AUTOMOUNT_FAILED : AUTOMOUNT_DEAD);
332 }
333
334 static int open_dev_autofs(Manager *m) {
335 struct autofs_dev_ioctl param;
336 int r;
337
338 assert(m);
339
340 if (m->dev_autofs_fd >= 0)
341 return m->dev_autofs_fd;
342
343 (void) label_fix("/dev/autofs", 0);
344
345 m->dev_autofs_fd = open("/dev/autofs", O_CLOEXEC|O_RDONLY);
346 if (m->dev_autofs_fd < 0)
347 return log_error_errno(errno, "Failed to open %s: %m", "/dev/autofs");
348
349 init_autofs_dev_ioctl(&param);
350 r = RET_NERRNO(ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, &param));
351 if (r < 0) {
352 m->dev_autofs_fd = safe_close(m->dev_autofs_fd);
353 return log_error_errno(r, "Failed to issue AUTOFS_DEV_IOCTL_VERSION ioctl: %m");
354 }
355
356 log_debug("Autofs kernel version %u.%u", param.ver_major, param.ver_minor);
357
358 return m->dev_autofs_fd;
359 }
360
361 static int open_ioctl_fd(int dev_autofs_fd, const char *where, dev_t devid) {
362 struct autofs_dev_ioctl *param;
363 size_t l;
364
365 assert(dev_autofs_fd >= 0);
366 assert(where);
367
368 l = sizeof(struct autofs_dev_ioctl) + strlen(where) + 1;
369 param = alloca_safe(l);
370
371 init_autofs_dev_ioctl(param);
372 param->size = l;
373 param->ioctlfd = -EBADF;
374 param->openmount.devid = devid;
375 strcpy(param->path, where);
376
377 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_OPENMOUNT, param) < 0)
378 return -errno;
379
380 if (param->ioctlfd < 0)
381 return -EIO;
382
383 (void) fd_cloexec(param->ioctlfd, true);
384 return param->ioctlfd;
385 }
386
387 static int autofs_protocol(int dev_autofs_fd, int ioctl_fd) {
388 uint32_t major, minor;
389 struct autofs_dev_ioctl param;
390
391 assert(dev_autofs_fd >= 0);
392 assert(ioctl_fd >= 0);
393
394 init_autofs_dev_ioctl(&param);
395 param.ioctlfd = ioctl_fd;
396
397 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOVER, &param) < 0)
398 return -errno;
399
400 major = param.protover.version;
401
402 init_autofs_dev_ioctl(&param);
403 param.ioctlfd = ioctl_fd;
404
405 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOSUBVER, &param) < 0)
406 return -errno;
407
408 minor = param.protosubver.sub_version;
409
410 log_debug("Autofs protocol version %u.%u", major, minor);
411 return 0;
412 }
413
414 static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, usec_t usec) {
415 struct autofs_dev_ioctl param;
416
417 assert(dev_autofs_fd >= 0);
418 assert(ioctl_fd >= 0);
419
420 init_autofs_dev_ioctl(&param);
421 param.ioctlfd = ioctl_fd;
422
423 if (usec == USEC_INFINITY)
424 param.timeout.timeout = 0;
425 else
426 /* Convert to seconds, rounding up. */
427 param.timeout.timeout = DIV_ROUND_UP(usec, USEC_PER_SEC);
428
429 return RET_NERRNO(ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, &param));
430 }
431
432 static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, int status) {
433 struct autofs_dev_ioctl param;
434
435 assert(dev_autofs_fd >= 0);
436 assert(ioctl_fd >= 0);
437
438 init_autofs_dev_ioctl(&param);
439 param.ioctlfd = ioctl_fd;
440
441 if (status != 0) {
442 param.fail.token = token;
443 param.fail.status = status;
444 } else
445 param.ready.token = token;
446
447 return RET_NERRNO(ioctl(dev_autofs_fd, status ? AUTOFS_DEV_IOCTL_FAIL : AUTOFS_DEV_IOCTL_READY, &param));
448 }
449
450 static int automount_send_ready(Automount *a, Set *tokens, int status) {
451 _cleanup_close_ int ioctl_fd = -EBADF;
452 unsigned token;
453 int r;
454
455 assert(a);
456 assert(status <= 0);
457
458 if (set_isempty(tokens))
459 return 0;
460
461 ioctl_fd = open_ioctl_fd(UNIT(a)->manager->dev_autofs_fd, a->where, a->dev_id);
462 if (ioctl_fd < 0)
463 return ioctl_fd;
464
465 if (status != 0)
466 log_unit_debug_errno(UNIT(a), status, "Sending failure: %m");
467 else
468 log_unit_debug(UNIT(a), "Sending success.");
469
470 r = 0;
471
472 /* Autofs thankfully does not hand out 0 as a token */
473 while ((token = PTR_TO_UINT(set_steal_first(tokens))))
474 /* Autofs fun fact:
475 *
476 * if you pass a positive status code here, kernels prior to 4.12 will freeze! Yay! */
477 RET_GATHER(r, autofs_send_ready(UNIT(a)->manager->dev_autofs_fd,
478 ioctl_fd,
479 token,
480 status));
481
482 return r;
483 }
484
485 static void automount_trigger_notify(Unit *u, Unit *other) {
486 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
487 int r;
488
489 assert(other);
490
491 /* Filter out invocations with bogus state */
492 assert(UNIT_IS_LOAD_COMPLETE(other->load_state));
493 assert(other->type == UNIT_MOUNT);
494
495 /* Don't propagate state changes from the mount if we are already down */
496 if (!IN_SET(a->state, AUTOMOUNT_WAITING, AUTOMOUNT_RUNNING))
497 return;
498
499 /* Propagate start limit hit state */
500 if (other->start_limit_hit) {
501 automount_enter_dead(a, AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT);
502 return;
503 }
504
505 /* Don't propagate anything if there's still a job queued */
506 if (other->job)
507 return;
508
509 /* The mount is successfully established */
510 if (IN_SET(MOUNT(other)->state, MOUNT_MOUNTED, MOUNT_REMOUNTING)) {
511 (void) automount_send_ready(a, a->tokens, 0);
512
513 r = automount_start_expire(a);
514 if (r < 0)
515 log_unit_warning_errno(UNIT(a), r, "Failed to start expiration timer, ignoring: %m");
516
517 automount_set_state(a, AUTOMOUNT_RUNNING);
518 }
519
520 if (IN_SET(MOUNT(other)->state,
521 MOUNT_MOUNTING, MOUNT_MOUNTING_DONE,
522 MOUNT_MOUNTED, MOUNT_REMOUNTING,
523 MOUNT_REMOUNTING_SIGTERM, MOUNT_REMOUNTING_SIGKILL,
524 MOUNT_UNMOUNTING_SIGTERM, MOUNT_UNMOUNTING_SIGKILL,
525 MOUNT_FAILED))
526 (void) automount_send_ready(a, a->expire_tokens, -ENODEV);
527
528 if (MOUNT(other)->state == MOUNT_DEAD)
529 (void) automount_send_ready(a, a->expire_tokens, 0);
530
531 /* The mount is in some unhappy state now, let's unfreeze any waiting clients */
532 if (IN_SET(MOUNT(other)->state,
533 MOUNT_DEAD, MOUNT_UNMOUNTING,
534 MOUNT_REMOUNTING_SIGTERM, MOUNT_REMOUNTING_SIGKILL,
535 MOUNT_UNMOUNTING_SIGTERM, MOUNT_UNMOUNTING_SIGKILL,
536 MOUNT_FAILED)) {
537
538 (void) automount_send_ready(a, a->tokens, -ENODEV);
539
540 automount_set_state(a, AUTOMOUNT_WAITING);
541 }
542 }
543
544 static void automount_enter_waiting(Automount *a) {
545 _cleanup_close_pair_ int pipe_fd[2] = EBADF_PAIR;
546 _cleanup_close_ int ioctl_fd = -EBADF;
547 char name[STRLEN("systemd-") + DECIMAL_STR_MAX(pid_t) + 1];
548 _cleanup_free_ char *options = NULL;
549 bool mounted = false;
550 int r, dev_autofs_fd;
551 struct stat st;
552
553 assert(a);
554 assert(a->pipe_fd < 0);
555 assert(a->where);
556
557 set_clear(a->tokens);
558
559 r = unit_fail_if_noncanonical_mount_path(UNIT(a), a->where);
560 if (r < 0)
561 goto fail;
562
563 (void) mkdir_p_label(a->where, a->directory_mode);
564
565 unit_warn_if_dir_nonempty(UNIT(a), a->where);
566
567 dev_autofs_fd = open_dev_autofs(UNIT(a)->manager);
568 if (dev_autofs_fd < 0)
569 goto fail;
570
571 if (pipe2(pipe_fd, O_CLOEXEC) < 0) {
572 log_unit_warning_errno(UNIT(a), errno, "Failed to allocate autofs pipe: %m");
573 goto fail;
574 }
575 r = fd_nonblock(pipe_fd[0], true);
576 if (r < 0) {
577 log_unit_warning_errno(UNIT(a), r, "Failed to make read side of pipe non-blocking: %m");
578 goto fail;
579 }
580
581 if (asprintf(
582 &options,
583 "fd=%i,pgrp="PID_FMT",minproto=5,maxproto=5,direct%s%s",
584 pipe_fd[1],
585 getpgrp(),
586 isempty(a->extra_options) ? "" : ",",
587 strempty(a->extra_options)) < 0) {
588 log_oom();
589 goto fail;
590 }
591
592 xsprintf(name, "systemd-"PID_FMT, getpid_cached());
593 r = mount_nofollow_verbose(LOG_WARNING, name, a->where, "autofs", 0, options);
594 if (r < 0)
595 goto fail;
596
597 mounted = true;
598
599 pipe_fd[1] = safe_close(pipe_fd[1]);
600
601 if (stat(a->where, &st) < 0) {
602 log_unit_warning_errno(UNIT(a), errno, "Failed to stat new automount point '%s': %m", a->where);
603 goto fail;
604 }
605
606 ioctl_fd = open_ioctl_fd(dev_autofs_fd, a->where, st.st_dev);
607 if (ioctl_fd < 0) {
608 log_unit_warning_errno(UNIT(a), ioctl_fd, "Failed to open automount ioctl fd for '%s': %m", a->where);
609 goto fail;
610 }
611
612 r = autofs_protocol(dev_autofs_fd, ioctl_fd);
613 if (r < 0) {
614 log_unit_warning_errno(UNIT(a), r, "Failed to validate autofs protocol for '%s': %m", a->where);
615 goto fail;
616 }
617
618 r = autofs_set_timeout(dev_autofs_fd, ioctl_fd, a->timeout_idle_usec);
619 if (r < 0) {
620 log_unit_warning_errno(UNIT(a), r, "Failed to set autofs timeout for '%s': %m", a->where);
621 goto fail;
622 }
623
624 r = sd_event_add_io(UNIT(a)->manager->event, &a->pipe_event_source, pipe_fd[0], EPOLLIN, automount_dispatch_io, a);
625 if (r < 0) {
626 log_unit_warning_errno(UNIT(a), r, "Failed to allocate IO event source for autofs mount '%s': %m", a->where);
627 goto fail;
628 }
629
630 (void) sd_event_source_set_description(a->pipe_event_source, "automount-io");
631
632 a->pipe_fd = TAKE_FD(pipe_fd[0]);
633 a->dev_id = st.st_dev;
634
635 automount_set_state(a, AUTOMOUNT_WAITING);
636 return;
637
638 fail:
639 if (mounted) {
640 r = repeat_unmount(a->where, MNT_DETACH|UMOUNT_NOFOLLOW);
641 if (r < 0)
642 log_unit_warning_errno(UNIT(a), r, "Failed to unmount, ignoring: %m");
643 }
644
645 automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
646 }
647
648 static int asynchronous_expire(int dev_autofs_fd, int ioctl_fd) {
649 int r;
650
651 assert(dev_autofs_fd >= 0);
652 assert(ioctl_fd >= 0);
653
654 /* Issue AUTOFS_DEV_IOCTL_EXPIRE in subprocess, asynchronously. Note that we don't keep track of the
655 * child's PID, we are PID1/autoreaper after all, hence when it dies we'll automatically clean it up
656 * anyway. */
657
658 r = safe_fork_full("(sd-expire)",
659 /* stdio_fds= */ NULL,
660 (int[]) { dev_autofs_fd, ioctl_fd },
661 /* n_except_fds= */ 2,
662 FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_REOPEN_LOG,
663 /* ret_pid= */ NULL);
664 if (r != 0)
665 return r;
666
667 /* Child */
668 for (;;) {
669 struct autofs_dev_ioctl param;
670 init_autofs_dev_ioctl(&param);
671 param.ioctlfd = ioctl_fd;
672
673 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_EXPIRE, &param) < 0)
674 break;
675 }
676
677 if (errno != EAGAIN)
678 log_warning_errno(errno, "Failed to expire automount, ignoring: %m");
679
680 _exit(EXIT_SUCCESS);
681 }
682
683 static int automount_dispatch_expire(sd_event_source *source, usec_t usec, void *userdata) {
684 Automount *a = ASSERT_PTR(AUTOMOUNT(userdata));
685 _cleanup_close_ int ioctl_fd = -EBADF;
686 int r;
687
688 assert(source == a->expire_event_source);
689
690 ioctl_fd = open_ioctl_fd(UNIT(a)->manager->dev_autofs_fd, a->where, a->dev_id);
691 if (ioctl_fd < 0)
692 return log_unit_error_errno(UNIT(a), ioctl_fd, "Couldn't open autofs ioctl fd: %m");
693
694 r = asynchronous_expire(UNIT(a)->manager->dev_autofs_fd, ioctl_fd);
695 if (r < 0)
696 return log_unit_error_errno(UNIT(a), r, "Failed to start expire job: %m");
697
698 return automount_start_expire(a);
699 }
700
701 static int automount_start_expire(Automount *a) {
702 usec_t timeout;
703 int r;
704
705 assert(a);
706
707 if (a->timeout_idle_usec == 0)
708 return 0;
709
710 timeout = MAX(a->timeout_idle_usec/3, USEC_PER_SEC);
711
712 if (a->expire_event_source) {
713 r = sd_event_source_set_time_relative(a->expire_event_source, timeout);
714 if (r < 0)
715 return r;
716
717 return sd_event_source_set_enabled(a->expire_event_source, SD_EVENT_ONESHOT);
718 }
719
720 r = sd_event_add_time_relative(
721 UNIT(a)->manager->event,
722 &a->expire_event_source,
723 CLOCK_MONOTONIC, timeout, 0,
724 automount_dispatch_expire, a);
725 if (r < 0)
726 return r;
727
728 (void) sd_event_source_set_description(a->expire_event_source, "automount-expire");
729
730 return 0;
731 }
732
733 static void automount_stop_expire(Automount *a) {
734 assert(a);
735
736 (void) sd_event_source_set_enabled(a->expire_event_source, SD_EVENT_OFF);
737 }
738
739 static void automount_enter_running(Automount *a) {
740 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
741 Unit *trigger;
742 struct stat st;
743 int r;
744
745 assert(a);
746
747 /* If the user masked our unit in the meantime, fail */
748 if (UNIT(a)->load_state != UNIT_LOADED) {
749 log_unit_error(UNIT(a), "Suppressing automount event since unit is no longer loaded.");
750 goto fail;
751 }
752
753 /* We don't take mount requests anymore if we are supposed to
754 * shut down anyway */
755 if (unit_stop_pending(UNIT(a))) {
756 log_unit_debug(UNIT(a), "Suppressing automount request since unit stop is scheduled.");
757 automount_send_ready(a, a->tokens, -EHOSTDOWN);
758 automount_send_ready(a, a->expire_tokens, -EHOSTDOWN);
759 return;
760 }
761
762 (void) mkdir_p_label(a->where, a->directory_mode);
763
764 /* Before we do anything, let's see if somebody is playing games with us? */
765 if (lstat(a->where, &st) < 0) {
766 log_unit_warning_errno(UNIT(a), errno, "Failed to stat automount point: %m");
767 goto fail;
768 }
769
770 /* The mount unit may have been explicitly started before we got the
771 * autofs request. Ack it to unblock anything waiting on the mount point. */
772 if (!S_ISDIR(st.st_mode) || st.st_dev != a->dev_id) {
773 log_unit_info(UNIT(a), "Automount point already active?");
774 automount_send_ready(a, a->tokens, 0);
775 return;
776 }
777
778 trigger = UNIT_TRIGGER(UNIT(a));
779 if (!trigger) {
780 log_unit_error(UNIT(a), "Unit to trigger vanished.");
781 goto fail;
782 }
783
784 r = manager_add_job(UNIT(a)->manager, JOB_START, trigger, JOB_REPLACE, &error, /* ret = */ NULL);
785 if (r < 0) {
786 log_unit_warning(UNIT(a), "Failed to queue mount startup job: %s", bus_error_message(&error, r));
787 goto fail;
788 }
789
790 automount_set_state(a, AUTOMOUNT_RUNNING);
791 return;
792
793 fail:
794 automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
795 }
796
797 static int automount_start(Unit *u) {
798 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
799 int r;
800
801 assert(IN_SET(a->state, AUTOMOUNT_DEAD, AUTOMOUNT_FAILED));
802
803 if (path_is_mount_point(a->where) > 0)
804 return log_unit_error_errno(u, SYNTHETIC_ERRNO(EEXIST), "Path %s is already a mount point, refusing start.", a->where);
805
806 r = unit_test_trigger_loaded(u);
807 if (r < 0)
808 return r;
809
810 r = unit_acquire_invocation_id(u);
811 if (r < 0)
812 return r;
813
814 a->result = AUTOMOUNT_SUCCESS;
815 automount_enter_waiting(a);
816 return 1;
817 }
818
819 static int automount_stop(Unit *u) {
820 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
821
822 assert(IN_SET(a->state, AUTOMOUNT_WAITING, AUTOMOUNT_RUNNING));
823
824 automount_enter_dead(a, AUTOMOUNT_SUCCESS);
825 return 1;
826 }
827
828 static int automount_serialize(Unit *u, FILE *f, FDSet *fds) {
829 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
830 void *p;
831 int r;
832
833 assert(f);
834 assert(fds);
835
836 (void) serialize_item(f, "state", automount_state_to_string(a->state));
837 (void) serialize_item(f, "result", automount_result_to_string(a->result));
838 (void) serialize_item_format(f, "dev-id", "%lu", (unsigned long) a->dev_id);
839
840 SET_FOREACH(p, a->tokens)
841 (void) serialize_item_format(f, "token", "%u", PTR_TO_UINT(p));
842 SET_FOREACH(p, a->expire_tokens)
843 (void) serialize_item_format(f, "expire-token", "%u", PTR_TO_UINT(p));
844
845 r = serialize_fd(f, fds, "pipe-fd", a->pipe_fd);
846 if (r < 0)
847 return r;
848
849 return 0;
850 }
851
852 static int automount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
853 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
854 int r;
855
856 assert(fds);
857
858 if (streq(key, "state")) {
859 AutomountState state;
860
861 state = automount_state_from_string(value);
862 if (state < 0)
863 log_unit_debug(u, "Failed to parse state value: %s", value);
864 else
865 a->deserialized_state = state;
866 } else if (streq(key, "result")) {
867 AutomountResult f;
868
869 f = automount_result_from_string(value);
870 if (f < 0)
871 log_unit_debug(u, "Failed to parse result value: %s", value);
872 else if (f != AUTOMOUNT_SUCCESS)
873 a->result = f;
874
875 } else if (streq(key, "dev-id")) {
876 unsigned long d;
877
878 if (safe_atolu(value, &d) < 0)
879 log_unit_debug(u, "Failed to parse dev-id value: %s", value);
880 else
881 a->dev_id = (dev_t) d;
882
883 } else if (streq(key, "token")) {
884 unsigned token;
885
886 if (safe_atou(value, &token) < 0)
887 log_unit_debug(u, "Failed to parse token value: %s", value);
888 else {
889 r = set_ensure_put(&a->tokens, NULL, UINT_TO_PTR(token));
890 if (r < 0)
891 log_unit_error_errno(u, r, "Failed to add token to set: %m");
892 }
893 } else if (streq(key, "expire-token")) {
894 unsigned token;
895
896 if (safe_atou(value, &token) < 0)
897 log_unit_debug(u, "Failed to parse token value: %s", value);
898 else {
899 r = set_ensure_put(&a->expire_tokens, NULL, UINT_TO_PTR(token));
900 if (r < 0)
901 log_unit_error_errno(u, r, "Failed to add expire token to set: %m");
902 }
903 } else if (streq(key, "pipe-fd")) {
904 safe_close(a->pipe_fd);
905 a->pipe_fd = deserialize_fd(fds, value);
906 } else
907 log_unit_debug(u, "Unknown serialization key: %s", key);
908
909 return 0;
910 }
911
912 static UnitActiveState automount_active_state(Unit *u) {
913 assert(u);
914
915 return state_translation_table[AUTOMOUNT(u)->state];
916 }
917
918 static const char *automount_sub_state_to_string(Unit *u) {
919 assert(u);
920
921 return automount_state_to_string(AUTOMOUNT(u)->state);
922 }
923
924 static bool automount_may_gc(Unit *u) {
925 Unit *t;
926
927 assert(u);
928
929 t = UNIT_TRIGGER(u);
930 if (!t)
931 return true;
932
933 return UNIT_VTABLE(t)->may_gc(t);
934 }
935
936 static int automount_dispatch_io(sd_event_source *s, int fd, uint32_t events, void *userdata) {
937 Automount *a = ASSERT_PTR(AUTOMOUNT(userdata));
938 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
939 union autofs_v5_packet_union packet;
940 Unit *trigger;
941 int r;
942
943 assert(fd == a->pipe_fd);
944
945 if (events & (EPOLLHUP|EPOLLERR)) {
946 log_unit_error(UNIT(a), "Got hangup/error on autofs pipe from kernel. Likely our automount point has been unmounted by someone or something else?");
947 automount_enter_dead(a, AUTOMOUNT_FAILURE_UNMOUNTED);
948 return 0;
949 }
950
951 if (events != EPOLLIN) {
952 log_unit_error(UNIT(a), "Got invalid poll event %"PRIu32" on pipe (fd=%d)", events, fd);
953 goto fail;
954 }
955
956 r = loop_read_exact(a->pipe_fd, &packet, sizeof(packet), true);
957 if (r < 0) {
958 log_unit_error_errno(UNIT(a), r, "Invalid read from pipe: %m");
959 goto fail;
960 }
961
962 switch (packet.hdr.type) {
963
964 case autofs_ptype_missing_direct:
965
966 if (packet.v5_packet.pid > 0) {
967 _cleanup_free_ char *p = NULL;
968
969 (void) pid_get_comm(packet.v5_packet.pid, &p);
970 log_unit_info(UNIT(a), "Got automount request for %s, triggered by %"PRIu32" (%s)", a->where, packet.v5_packet.pid, strna(p));
971 } else
972 log_unit_debug(UNIT(a), "Got direct mount request on %s", a->where);
973
974 r = set_ensure_put(&a->tokens, NULL, UINT_TO_PTR(packet.v5_packet.wait_queue_token));
975 if (r < 0) {
976 log_unit_error_errno(UNIT(a), r, "Failed to remember token: %m");
977 goto fail;
978 }
979
980 automount_enter_running(a);
981 break;
982
983 case autofs_ptype_expire_direct:
984 log_unit_debug(UNIT(a), "Got direct umount request on %s", a->where);
985
986 automount_stop_expire(a);
987
988 r = set_ensure_put(&a->expire_tokens, NULL, UINT_TO_PTR(packet.v5_packet.wait_queue_token));
989 if (r < 0) {
990 log_unit_error_errno(UNIT(a), r, "Failed to remember token: %m");
991 goto fail;
992 }
993
994 trigger = UNIT_TRIGGER(UNIT(a));
995 if (!trigger) {
996 log_unit_error(UNIT(a), "Unit to trigger vanished.");
997 goto fail;
998 }
999
1000 r = manager_add_job(UNIT(a)->manager, JOB_STOP, trigger, JOB_REPLACE, &error, /* ret = */ NULL);
1001 if (r < 0) {
1002 log_unit_warning(UNIT(a), "Failed to queue unmount job: %s", bus_error_message(&error, r));
1003 goto fail;
1004 }
1005 break;
1006
1007 default:
1008 log_unit_error(UNIT(a), "Received unknown automount request %i", packet.hdr.type);
1009 }
1010
1011 return 0;
1012
1013 fail:
1014 automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
1015 return 0;
1016 }
1017
1018 static void automount_shutdown(Manager *m) {
1019 assert(m);
1020
1021 m->dev_autofs_fd = safe_close(m->dev_autofs_fd);
1022 }
1023
1024 static void automount_reset_failed(Unit *u) {
1025 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
1026
1027 if (a->state == AUTOMOUNT_FAILED)
1028 automount_set_state(a, AUTOMOUNT_DEAD);
1029
1030 a->result = AUTOMOUNT_SUCCESS;
1031 }
1032
1033 static bool automount_supported(void) {
1034 static int supported = -1;
1035
1036 if (supported < 0)
1037 supported = access("/dev/autofs", F_OK) >= 0;
1038
1039 return supported;
1040 }
1041
1042 static int automount_can_start(Unit *u) {
1043 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
1044 int r;
1045
1046 r = unit_test_start_limit(u);
1047 if (r < 0) {
1048 automount_enter_dead(a, AUTOMOUNT_FAILURE_START_LIMIT_HIT);
1049 return r;
1050 }
1051
1052 return 1;
1053 }
1054
1055 static const char* const automount_result_table[_AUTOMOUNT_RESULT_MAX] = {
1056 [AUTOMOUNT_SUCCESS] = "success",
1057 [AUTOMOUNT_FAILURE_RESOURCES] = "resources",
1058 [AUTOMOUNT_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
1059 [AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT] = "mount-start-limit-hit",
1060 [AUTOMOUNT_FAILURE_UNMOUNTED] = "unmounted",
1061 };
1062
1063 DEFINE_STRING_TABLE_LOOKUP(automount_result, AutomountResult);
1064
1065 const UnitVTable automount_vtable = {
1066 .object_size = sizeof(Automount),
1067
1068 .sections =
1069 "Unit\0"
1070 "Automount\0"
1071 "Install\0",
1072 .private_section = "Automount",
1073
1074 .can_transient = true,
1075 .can_fail = true,
1076 .can_trigger = true,
1077 .exclude_from_switch_root_serialization = true,
1078
1079 .init = automount_init,
1080 .load = automount_load,
1081 .done = automount_done,
1082
1083 .coldplug = automount_coldplug,
1084
1085 .dump = automount_dump,
1086
1087 .start = automount_start,
1088 .stop = automount_stop,
1089
1090 .serialize = automount_serialize,
1091 .deserialize_item = automount_deserialize_item,
1092
1093 .active_state = automount_active_state,
1094 .sub_state_to_string = automount_sub_state_to_string,
1095
1096 .may_gc = automount_may_gc,
1097
1098 .trigger_notify = automount_trigger_notify,
1099
1100 .reset_failed = automount_reset_failed,
1101
1102 .bus_set_property = bus_automount_set_property,
1103
1104 .shutdown = automount_shutdown,
1105 .supported = automount_supported,
1106
1107 .status_message_formats = {
1108 .finished_start_job = {
1109 [JOB_DONE] = "Set up automount %s.",
1110 [JOB_FAILED] = "Failed to set up automount %s.",
1111 },
1112 .finished_stop_job = {
1113 [JOB_DONE] = "Unset automount %s.",
1114 [JOB_FAILED] = "Failed to unset automount %s.",
1115 },
1116 },
1117
1118 .can_start = automount_can_start,
1119 };