Debian Codegraph @

src/core/automount.c

raw
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> /* IWYU pragma: keep */
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.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 || IN_SET(f, AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT, AUTOMOUNT_FAILURE_START_LIMIT_HIT))
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 = pidref_safe_fork_full(
659 "(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 /* ret= */ 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 (void) sd_event_source_set_enabled(a->expire_event_source, SD_EVENT_OFF);
738 }
739
740 static void automount_enter_running(Automount *a) {
741 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
742 Unit *trigger;
743 struct stat st;
744 int r;
745
746 assert(a);
747
748 /* If the user masked our unit in the meantime, fail */
749 if (UNIT(a)->load_state != UNIT_LOADED) {
750 log_unit_error(UNIT(a), "Suppressing automount event since unit is no longer loaded.");
751 goto fail;
752 }
753
754 /* We don't take mount requests anymore if we are supposed to
755 * shut down anyway */
756 if (unit_stop_pending(UNIT(a))) {
757 log_unit_debug(UNIT(a), "Suppressing automount request since unit stop is scheduled.");
758 automount_send_ready(a, a->tokens, -EHOSTDOWN);
759 automount_send_ready(a, a->expire_tokens, -EHOSTDOWN);
760 return;
761 }
762
763 (void) mkdir_p_label(a->where, a->directory_mode);
764
765 /* Before we do anything, let's see if somebody is playing games with us? */
766 if (lstat(a->where, &st) < 0) {
767 log_unit_warning_errno(UNIT(a), errno, "Failed to stat automount point: %m");
768 goto fail;
769 }
770
771 /* The mount unit may have been explicitly started before we got the
772 * autofs request. Ack it to unblock anything waiting on the mount point. */
773 if (!S_ISDIR(st.st_mode) || st.st_dev != a->dev_id) {
774 log_unit_info(UNIT(a), "Automount point already active?");
775 automount_send_ready(a, a->tokens, 0);
776 return;
777 }
778
779 trigger = UNIT_TRIGGER(UNIT(a));
780 if (!trigger) {
781 log_unit_error(UNIT(a), "Unit to trigger vanished.");
782 goto fail;
783 }
784
785 r = manager_add_job(UNIT(a)->manager, JOB_START, trigger, JOB_REPLACE, &error, /* ret= */ NULL);
786 if (r < 0) {
787 log_unit_warning(UNIT(a), "Failed to queue mount startup job: %s", bus_error_message(&error, r));
788 goto fail;
789 }
790
791 automount_set_state(a, AUTOMOUNT_RUNNING);
792 return;
793
794 fail:
795 automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
796 }
797
798 static int automount_start(Unit *u) {
799 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
800 int r;
801
802 assert(IN_SET(a->state, AUTOMOUNT_DEAD, AUTOMOUNT_FAILED));
803
804 if (path_is_mount_point(a->where) > 0)
805 return log_unit_error_errno(u, SYNTHETIC_ERRNO(EEXIST), "Path %s is already a mount point, refusing start.", a->where);
806
807 r = unit_acquire_invocation_id(u);
808 if (r < 0)
809 return r;
810
811 a->result = AUTOMOUNT_SUCCESS;
812 automount_enter_waiting(a);
813 return 1;
814 }
815
816 static int automount_stop(Unit *u) {
817 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
818
819 assert(IN_SET(a->state, AUTOMOUNT_WAITING, AUTOMOUNT_RUNNING));
820
821 automount_enter_dead(a, AUTOMOUNT_SUCCESS);
822 return 1;
823 }
824
825 static int automount_serialize(Unit *u, FILE *f, FDSet *fds) {
826 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
827 void *p;
828 int r;
829
830 assert(f);
831 assert(fds);
832
833 (void) serialize_item(f, "state", automount_state_to_string(a->state));
834 (void) serialize_item(f, "result", automount_result_to_string(a->result));
835 (void) serialize_item_format(f, "dev-id", "%lu", (unsigned long) a->dev_id);
836
837 SET_FOREACH(p, a->tokens)
838 (void) serialize_item_format(f, "token", "%u", PTR_TO_UINT(p));
839 SET_FOREACH(p, a->expire_tokens)
840 (void) serialize_item_format(f, "expire-token", "%u", PTR_TO_UINT(p));
841
842 r = serialize_fd(f, fds, "pipe-fd", a->pipe_fd);
843 if (r < 0)
844 return r;
845
846 return 0;
847 }
848
849 static int automount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
850 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
851 int r;
852
853 assert(fds);
854
855 if (streq(key, "state")) {
856 AutomountState state;
857
858 state = automount_state_from_string(value);
859 if (state < 0)
860 log_unit_debug(u, "Failed to parse state value: %s", value);
861 else
862 a->deserialized_state = state;
863 } else if (streq(key, "result")) {
864 AutomountResult f;
865
866 f = automount_result_from_string(value);
867 if (f < 0)
868 log_unit_debug(u, "Failed to parse result value: %s", value);
869 else if (f != AUTOMOUNT_SUCCESS)
870 a->result = f;
871
872 } else if (streq(key, "dev-id")) {
873 unsigned long d;
874
875 if (safe_atolu(value, &d) < 0)
876 log_unit_debug(u, "Failed to parse dev-id value: %s", value);
877 else
878 a->dev_id = (dev_t) d;
879
880 } else if (streq(key, "token")) {
881 unsigned token;
882
883 if (safe_atou(value, &token) < 0)
884 log_unit_debug(u, "Failed to parse token value: %s", value);
885 else {
886 r = set_ensure_put(&a->tokens, NULL, UINT_TO_PTR(token));
887 if (r < 0)
888 log_unit_error_errno(u, r, "Failed to add token to set: %m");
889 }
890 } else if (streq(key, "expire-token")) {
891 unsigned token;
892
893 if (safe_atou(value, &token) < 0)
894 log_unit_debug(u, "Failed to parse token value: %s", value);
895 else {
896 r = set_ensure_put(&a->expire_tokens, NULL, UINT_TO_PTR(token));
897 if (r < 0)
898 log_unit_error_errno(u, r, "Failed to add expire token to set: %m");
899 }
900 } else if (streq(key, "pipe-fd")) {
901 safe_close(a->pipe_fd);
902 a->pipe_fd = deserialize_fd(fds, value);
903 } else
904 log_unit_debug(u, "Unknown serialization key: %s", key);
905
906 return 0;
907 }
908
909 static UnitActiveState automount_active_state(Unit *u) {
910 assert(u);
911
912 return state_translation_table[AUTOMOUNT(u)->state];
913 }
914
915 static const char *automount_sub_state_to_string(Unit *u) {
916 assert(u);
917
918 return automount_state_to_string(AUTOMOUNT(u)->state);
919 }
920
921 static bool automount_may_gc(Unit *u) {
922 Unit *t;
923
924 assert(u);
925
926 t = UNIT_TRIGGER(u);
927 if (!t)
928 return true;
929
930 return UNIT_VTABLE(t)->may_gc(t);
931 }
932
933 static int automount_dispatch_io(sd_event_source *s, int fd, uint32_t events, void *userdata) {
934 Automount *a = ASSERT_PTR(AUTOMOUNT(userdata));
935 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
936 union autofs_v5_packet_union packet;
937 Unit *trigger;
938 int r;
939
940 assert(fd == a->pipe_fd);
941
942 if (events & (EPOLLHUP|EPOLLERR)) {
943 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?");
944 automount_enter_dead(a, AUTOMOUNT_FAILURE_UNMOUNTED);
945 return 0;
946 }
947
948 if (events != EPOLLIN) {
949 log_unit_error(UNIT(a), "Got invalid poll event %"PRIu32" on pipe (fd=%d)", events, fd);
950 goto fail;
951 }
952
953 r = loop_read_exact(a->pipe_fd, &packet, sizeof(packet), true);
954 if (r < 0) {
955 log_unit_error_errno(UNIT(a), r, "Invalid read from pipe: %m");
956 goto fail;
957 }
958
959 switch (packet.hdr.type) {
960
961 case autofs_ptype_missing_direct:
962
963 if (packet.v5_packet.pid > 0) {
964 _cleanup_free_ char *p = NULL;
965
966 (void) pid_get_comm(packet.v5_packet.pid, &p);
967 log_unit_info(UNIT(a), "Got automount request for %s, triggered by %"PRIu32" (%s)", a->where, packet.v5_packet.pid, strna(p));
968 } else
969 log_unit_debug(UNIT(a), "Got direct mount request on %s", a->where);
970
971 r = set_ensure_put(&a->tokens, NULL, UINT_TO_PTR(packet.v5_packet.wait_queue_token));
972 if (r < 0) {
973 log_unit_error_errno(UNIT(a), r, "Failed to remember token: %m");
974 goto fail;
975 }
976
977 automount_enter_running(a);
978 break;
979
980 case autofs_ptype_expire_direct:
981 log_unit_debug(UNIT(a), "Got direct umount request on %s", a->where);
982
983 automount_stop_expire(a);
984
985 r = set_ensure_put(&a->expire_tokens, NULL, UINT_TO_PTR(packet.v5_packet.wait_queue_token));
986 if (r < 0) {
987 log_unit_error_errno(UNIT(a), r, "Failed to remember token: %m");
988 goto fail;
989 }
990
991 trigger = UNIT_TRIGGER(UNIT(a));
992 if (!trigger) {
993 log_unit_error(UNIT(a), "Unit to trigger vanished.");
994 goto fail;
995 }
996
997 r = manager_add_job(UNIT(a)->manager, JOB_STOP, trigger, JOB_REPLACE, &error, /* ret= */ NULL);
998 if (r < 0) {
999 log_unit_warning(UNIT(a), "Failed to queue unmount job: %s", bus_error_message(&error, r));
1000 goto fail;
1001 }
1002 break;
1003
1004 default:
1005 log_unit_error(UNIT(a), "Received unknown automount request %i", packet.hdr.type);
1006 }
1007
1008 return 0;
1009
1010 fail:
1011 automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
1012 return 0;
1013 }
1014
1015 static void automount_shutdown(Manager *m) {
1016 assert(m);
1017
1018 m->dev_autofs_fd = safe_close(m->dev_autofs_fd);
1019 }
1020
1021 static void automount_reset_failed(Unit *u) {
1022 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
1023
1024 if (a->state == AUTOMOUNT_FAILED)
1025 automount_set_state(a, AUTOMOUNT_DEAD);
1026
1027 a->result = AUTOMOUNT_SUCCESS;
1028 }
1029
1030 static bool automount_supported(void) {
1031 static int supported = -1;
1032
1033 if (!unit_type_supported(UNIT_MOUNT))
1034 return false;
1035
1036 if (supported < 0)
1037 supported = access("/dev/autofs", F_OK) >= 0;
1038
1039 return supported;
1040 }
1041
1042 static int automount_test_startable(Unit *u) {
1043 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
1044 int r;
1045
1046 r = unit_test_trigger_loaded(u);
1047 if (r < 0)
1048 return r;
1049
1050 r = unit_test_start_limit(u);
1051 if (r < 0) {
1052 automount_enter_dead(a, AUTOMOUNT_FAILURE_START_LIMIT_HIT);
1053 return r;
1054 }
1055
1056 return true;
1057 }
1058
1059 static const char* const automount_result_table[_AUTOMOUNT_RESULT_MAX] = {
1060 [AUTOMOUNT_SUCCESS] = "success",
1061 [AUTOMOUNT_FAILURE_RESOURCES] = "resources",
1062 [AUTOMOUNT_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
1063 [AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT] = "mount-start-limit-hit",
1064 [AUTOMOUNT_FAILURE_UNMOUNTED] = "unmounted",
1065 };
1066
1067 DEFINE_STRING_TABLE_LOOKUP(automount_result, AutomountResult);
1068
1069 const UnitVTable automount_vtable = {
1070 .object_size = sizeof(Automount),
1071
1072 .sections =
1073 "Unit\0"
1074 "Automount\0"
1075 "Install\0",
1076 .private_section = "Automount",
1077
1078 .can_transient = true,
1079 .can_fail = true,
1080 .can_trigger = true,
1081 .exclude_from_switch_root_serialization = true,
1082
1083 .init = automount_init,
1084 .load = automount_load,
1085 .done = automount_done,
1086
1087 .coldplug = automount_coldplug,
1088
1089 .dump = automount_dump,
1090
1091 .start = automount_start,
1092 .stop = automount_stop,
1093
1094 .serialize = automount_serialize,
1095 .deserialize_item = automount_deserialize_item,
1096
1097 .active_state = automount_active_state,
1098 .sub_state_to_string = automount_sub_state_to_string,
1099
1100 .may_gc = automount_may_gc,
1101
1102 .trigger_notify = automount_trigger_notify,
1103
1104 .reset_failed = automount_reset_failed,
1105
1106 .bus_set_property = bus_automount_set_property,
1107
1108 .shutdown = automount_shutdown,
1109 .supported = automount_supported,
1110
1111 .status_message_formats = {
1112 .finished_start_job = {
1113 [JOB_DONE] = "Set up automount %s.",
1114 [JOB_FAILED] = "Failed to set up automount %s.",
1115 },
1116 .finished_stop_job = {
1117 [JOB_DONE] = "Unset automount %s.",
1118 [JOB_FAILED] = "Failed to unset automount %s.",
1119 },
1120 },
1121
1122 .test_startable = automount_test_startable,
1123 };