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