]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/automount.c
core: use ASSERT_PTR(CAST(u)) everywhere
[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 int k;
476
477 /* Autofs fun fact:
478 *
479 * if you pass a positive status code here, kernels
480 * prior to 4.12 will freeze! Yay! */
481
482 k = autofs_send_ready(UNIT(a)->manager->dev_autofs_fd,
483 ioctl_fd,
484 token,
485 status);
486 if (k < 0)
487 r = k;
488 }
489
490 return r;
491 }
492
493 static void automount_trigger_notify(Unit *u, Unit *other) {
494 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
495 int r;
496
497 assert(other);
498
499 /* Filter out invocations with bogus state */
500 assert(UNIT_IS_LOAD_COMPLETE(other->load_state));
501 assert(other->type == UNIT_MOUNT);
502
503 /* Don't propagate state changes from the mount if we are already down */
504 if (!IN_SET(a->state, AUTOMOUNT_WAITING, AUTOMOUNT_RUNNING))
505 return;
506
507 /* Propagate start limit hit state */
508 if (other->start_limit_hit) {
509 automount_enter_dead(a, AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT);
510 return;
511 }
512
513 /* Don't propagate anything if there's still a job queued */
514 if (other->job)
515 return;
516
517 /* The mount is successfully established */
518 if (IN_SET(MOUNT(other)->state, MOUNT_MOUNTED, MOUNT_REMOUNTING)) {
519 (void) automount_send_ready(a, a->tokens, 0);
520
521 r = automount_start_expire(a);
522 if (r < 0)
523 log_unit_warning_errno(UNIT(a), r, "Failed to start expiration timer, ignoring: %m");
524
525 automount_set_state(a, AUTOMOUNT_RUNNING);
526 }
527
528 if (IN_SET(MOUNT(other)->state,
529 MOUNT_MOUNTING, MOUNT_MOUNTING_DONE,
530 MOUNT_MOUNTED, MOUNT_REMOUNTING,
531 MOUNT_REMOUNTING_SIGTERM, MOUNT_REMOUNTING_SIGKILL,
532 MOUNT_UNMOUNTING_SIGTERM, MOUNT_UNMOUNTING_SIGKILL,
533 MOUNT_FAILED))
534 (void) automount_send_ready(a, a->expire_tokens, -ENODEV);
535
536 if (MOUNT(other)->state == MOUNT_DEAD)
537 (void) automount_send_ready(a, a->expire_tokens, 0);
538
539 /* The mount is in some unhappy state now, let's unfreeze any waiting clients */
540 if (IN_SET(MOUNT(other)->state,
541 MOUNT_DEAD, MOUNT_UNMOUNTING,
542 MOUNT_REMOUNTING_SIGTERM, MOUNT_REMOUNTING_SIGKILL,
543 MOUNT_UNMOUNTING_SIGTERM, MOUNT_UNMOUNTING_SIGKILL,
544 MOUNT_FAILED)) {
545
546 (void) automount_send_ready(a, a->tokens, -ENODEV);
547
548 automount_set_state(a, AUTOMOUNT_WAITING);
549 }
550 }
551
552 static void automount_enter_waiting(Automount *a) {
553 _cleanup_close_pair_ int pipe_fd[2] = EBADF_PAIR;
554 _cleanup_close_ int ioctl_fd = -EBADF;
555 char name[STRLEN("systemd-") + DECIMAL_STR_MAX(pid_t) + 1];
556 _cleanup_free_ char *options = NULL;
557 bool mounted = false;
558 int r, dev_autofs_fd;
559 struct stat st;
560
561 assert(a);
562 assert(a->pipe_fd < 0);
563 assert(a->where);
564
565 set_clear(a->tokens);
566
567 r = unit_fail_if_noncanonical(UNIT(a), a->where);
568 if (r < 0)
569 goto fail;
570
571 (void) mkdir_p_label(a->where, a->directory_mode);
572
573 unit_warn_if_dir_nonempty(UNIT(a), a->where);
574
575 dev_autofs_fd = open_dev_autofs(UNIT(a)->manager);
576 if (dev_autofs_fd < 0)
577 goto fail;
578
579 if (pipe2(pipe_fd, O_CLOEXEC) < 0) {
580 log_unit_warning_errno(UNIT(a), errno, "Failed to allocate autofs pipe: %m");
581 goto fail;
582 }
583 r = fd_nonblock(pipe_fd[0], true);
584 if (r < 0) {
585 log_unit_warning_errno(UNIT(a), r, "Failed to make read side of pipe non-blocking: %m");
586 goto fail;
587 }
588
589 if (asprintf(
590 &options,
591 "fd=%i,pgrp="PID_FMT",minproto=5,maxproto=5,direct%s%s",
592 pipe_fd[1],
593 getpgrp(),
594 isempty(a->extra_options) ? "" : ",",
595 strempty(a->extra_options)) < 0) {
596 log_oom();
597 goto fail;
598 }
599
600 xsprintf(name, "systemd-"PID_FMT, getpid_cached());
601 r = mount_nofollow_verbose(LOG_WARNING, name, a->where, "autofs", 0, options);
602 if (r < 0)
603 goto fail;
604
605 mounted = true;
606
607 pipe_fd[1] = safe_close(pipe_fd[1]);
608
609 if (stat(a->where, &st) < 0) {
610 log_unit_warning_errno(UNIT(a), errno, "Failed to stat new automount point '%s': %m", a->where);
611 goto fail;
612 }
613
614 ioctl_fd = open_ioctl_fd(dev_autofs_fd, a->where, st.st_dev);
615 if (ioctl_fd < 0) {
616 log_unit_warning_errno(UNIT(a), ioctl_fd, "Failed to open automount ioctl fd for '%s': %m", a->where);
617 goto fail;
618 }
619
620 r = autofs_protocol(dev_autofs_fd, ioctl_fd);
621 if (r < 0) {
622 log_unit_warning_errno(UNIT(a), r, "Failed to validate autofs protocol for '%s': %m", a->where);
623 goto fail;
624 }
625
626 r = autofs_set_timeout(dev_autofs_fd, ioctl_fd, a->timeout_idle_usec);
627 if (r < 0) {
628 log_unit_warning_errno(UNIT(a), r, "Failed to set autofs timeout for '%s': %m", a->where);
629 goto fail;
630 }
631
632 r = sd_event_add_io(UNIT(a)->manager->event, &a->pipe_event_source, pipe_fd[0], EPOLLIN, automount_dispatch_io, a);
633 if (r < 0) {
634 log_unit_warning_errno(UNIT(a), r, "Failed to allocate IO event source for autofs mount '%s': %m", a->where);
635 goto fail;
636 }
637
638 (void) sd_event_source_set_description(a->pipe_event_source, "automount-io");
639
640 a->pipe_fd = TAKE_FD(pipe_fd[0]);
641 a->dev_id = st.st_dev;
642
643 automount_set_state(a, AUTOMOUNT_WAITING);
644 return;
645
646 fail:
647 if (mounted) {
648 r = repeat_unmount(a->where, MNT_DETACH|UMOUNT_NOFOLLOW);
649 if (r < 0)
650 log_unit_warning_errno(UNIT(a), r, "Failed to unmount, ignoring: %m");
651 }
652
653 automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
654 }
655
656 static int asynchronous_expire(int dev_autofs_fd, int ioctl_fd) {
657 int r;
658
659 assert(dev_autofs_fd >= 0);
660 assert(ioctl_fd >= 0);
661
662 /* Issue AUTOFS_DEV_IOCTL_EXPIRE in subprocess, asynchronously. Note that we don't keep track of the
663 * child's PID, we are PID1/autoreaper after all, hence when it dies we'll automatically clean it up
664 * anyway. */
665
666 r = safe_fork_full("(sd-expire)",
667 /* stdio_fds= */ NULL,
668 (int[]) { dev_autofs_fd, ioctl_fd },
669 /* n_except_fds= */ 2,
670 FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_REOPEN_LOG,
671 /* pid= */ NULL);
672 if (r != 0)
673 return r;
674
675 /* Child */
676 for (;;) {
677 struct autofs_dev_ioctl param;
678 init_autofs_dev_ioctl(&param);
679 param.ioctlfd = ioctl_fd;
680
681 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_EXPIRE, &param) < 0)
682 break;
683 }
684
685 if (errno != EAGAIN)
686 log_warning_errno(errno, "Failed to expire automount, ignoring: %m");
687
688 _exit(EXIT_SUCCESS);
689 }
690
691 static int automount_dispatch_expire(sd_event_source *source, usec_t usec, void *userdata) {
692 Automount *a = ASSERT_PTR(AUTOMOUNT(userdata));
693 _cleanup_close_ int ioctl_fd = -EBADF;
694 int r;
695
696 assert(source == a->expire_event_source);
697
698 ioctl_fd = open_ioctl_fd(UNIT(a)->manager->dev_autofs_fd, a->where, a->dev_id);
699 if (ioctl_fd < 0)
700 return log_unit_error_errno(UNIT(a), ioctl_fd, "Couldn't open autofs ioctl fd: %m");
701
702 r = asynchronous_expire(UNIT(a)->manager->dev_autofs_fd, ioctl_fd);
703 if (r < 0)
704 return log_unit_error_errno(UNIT(a), r, "Failed to start expire job: %m");
705
706 return automount_start_expire(a);
707 }
708
709 static int automount_start_expire(Automount *a) {
710 usec_t timeout;
711 int r;
712
713 assert(a);
714
715 if (a->timeout_idle_usec == 0)
716 return 0;
717
718 timeout = MAX(a->timeout_idle_usec/3, USEC_PER_SEC);
719
720 if (a->expire_event_source) {
721 r = sd_event_source_set_time_relative(a->expire_event_source, timeout);
722 if (r < 0)
723 return r;
724
725 return sd_event_source_set_enabled(a->expire_event_source, SD_EVENT_ONESHOT);
726 }
727
728 r = sd_event_add_time_relative(
729 UNIT(a)->manager->event,
730 &a->expire_event_source,
731 CLOCK_MONOTONIC, timeout, 0,
732 automount_dispatch_expire, a);
733 if (r < 0)
734 return r;
735
736 (void) sd_event_source_set_description(a->expire_event_source, "automount-expire");
737
738 return 0;
739 }
740
741 static void automount_stop_expire(Automount *a) {
742 assert(a);
743
744 if (!a->expire_event_source)
745 return;
746
747 (void) sd_event_source_set_enabled(a->expire_event_source, SD_EVENT_OFF);
748 }
749
750 static void automount_enter_running(Automount *a) {
751 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
752 Unit *trigger;
753 struct stat st;
754 int r;
755
756 assert(a);
757
758 /* If the user masked our unit in the meantime, fail */
759 if (UNIT(a)->load_state != UNIT_LOADED) {
760 log_unit_error(UNIT(a), "Suppressing automount event since unit is no longer loaded.");
761 goto fail;
762 }
763
764 /* We don't take mount requests anymore if we are supposed to
765 * shut down anyway */
766 if (unit_stop_pending(UNIT(a))) {
767 log_unit_debug(UNIT(a), "Suppressing automount request since unit stop is scheduled.");
768 automount_send_ready(a, a->tokens, -EHOSTDOWN);
769 automount_send_ready(a, a->expire_tokens, -EHOSTDOWN);
770 return;
771 }
772
773 (void) mkdir_p_label(a->where, a->directory_mode);
774
775 /* Before we do anything, let's see if somebody is playing games with us? */
776 if (lstat(a->where, &st) < 0) {
777 log_unit_warning_errno(UNIT(a), errno, "Failed to stat automount point: %m");
778 goto fail;
779 }
780
781 /* The mount unit may have been explicitly started before we got the
782 * autofs request. Ack it to unblock anything waiting on the mount point. */
783 if (!S_ISDIR(st.st_mode) || st.st_dev != a->dev_id) {
784 log_unit_info(UNIT(a), "Automount point already active?");
785 automount_send_ready(a, a->tokens, 0);
786 return;
787 }
788
789 trigger = UNIT_TRIGGER(UNIT(a));
790 if (!trigger) {
791 log_unit_error(UNIT(a), "Unit to trigger vanished.");
792 goto fail;
793 }
794
795 r = manager_add_job(UNIT(a)->manager, JOB_START, trigger, JOB_REPLACE, NULL, &error, NULL);
796 if (r < 0) {
797 log_unit_warning(UNIT(a), "Failed to queue mount startup job: %s", bus_error_message(&error, r));
798 goto fail;
799 }
800
801 automount_set_state(a, AUTOMOUNT_RUNNING);
802 return;
803
804 fail:
805 automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
806 }
807
808 static int automount_start(Unit *u) {
809 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
810 int r;
811
812 assert(IN_SET(a->state, AUTOMOUNT_DEAD, AUTOMOUNT_FAILED));
813
814 if (path_is_mount_point(a->where) > 0)
815 return log_unit_error_errno(u, SYNTHETIC_ERRNO(EEXIST), "Path %s is already a mount point, refusing start.", a->where);
816
817 r = unit_test_trigger_loaded(u);
818 if (r < 0)
819 return r;
820
821 r = unit_acquire_invocation_id(u);
822 if (r < 0)
823 return r;
824
825 a->result = AUTOMOUNT_SUCCESS;
826 automount_enter_waiting(a);
827 return 1;
828 }
829
830 static int automount_stop(Unit *u) {
831 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
832
833 assert(IN_SET(a->state, AUTOMOUNT_WAITING, AUTOMOUNT_RUNNING));
834
835 automount_enter_dead(a, AUTOMOUNT_SUCCESS);
836 return 1;
837 }
838
839 static int automount_serialize(Unit *u, FILE *f, FDSet *fds) {
840 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
841 void *p;
842 int r;
843
844 assert(f);
845 assert(fds);
846
847 (void) serialize_item(f, "state", automount_state_to_string(a->state));
848 (void) serialize_item(f, "result", automount_result_to_string(a->result));
849 (void) serialize_item_format(f, "dev-id", "%lu", (unsigned long) a->dev_id);
850
851 SET_FOREACH(p, a->tokens)
852 (void) serialize_item_format(f, "token", "%u", PTR_TO_UINT(p));
853 SET_FOREACH(p, a->expire_tokens)
854 (void) serialize_item_format(f, "expire-token", "%u", PTR_TO_UINT(p));
855
856 r = serialize_fd(f, fds, "pipe-fd", a->pipe_fd);
857 if (r < 0)
858 return r;
859
860 return 0;
861 }
862
863 static int automount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
864 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
865 int r;
866
867 assert(fds);
868
869 if (streq(key, "state")) {
870 AutomountState state;
871
872 state = automount_state_from_string(value);
873 if (state < 0)
874 log_unit_debug(u, "Failed to parse state value: %s", value);
875 else
876 a->deserialized_state = state;
877 } else if (streq(key, "result")) {
878 AutomountResult f;
879
880 f = automount_result_from_string(value);
881 if (f < 0)
882 log_unit_debug(u, "Failed to parse result value: %s", value);
883 else if (f != AUTOMOUNT_SUCCESS)
884 a->result = f;
885
886 } else if (streq(key, "dev-id")) {
887 unsigned long d;
888
889 if (safe_atolu(value, &d) < 0)
890 log_unit_debug(u, "Failed to parse dev-id value: %s", value);
891 else
892 a->dev_id = (dev_t) d;
893
894 } else if (streq(key, "token")) {
895 unsigned token;
896
897 if (safe_atou(value, &token) < 0)
898 log_unit_debug(u, "Failed to parse token value: %s", value);
899 else {
900 r = set_ensure_put(&a->tokens, NULL, UINT_TO_PTR(token));
901 if (r < 0)
902 log_unit_error_errno(u, r, "Failed to add token to set: %m");
903 }
904 } else if (streq(key, "expire-token")) {
905 unsigned token;
906
907 if (safe_atou(value, &token) < 0)
908 log_unit_debug(u, "Failed to parse token value: %s", value);
909 else {
910 r = set_ensure_put(&a->expire_tokens, NULL, UINT_TO_PTR(token));
911 if (r < 0)
912 log_unit_error_errno(u, r, "Failed to add expire token to set: %m");
913 }
914 } else if (streq(key, "pipe-fd")) {
915 safe_close(a->pipe_fd);
916 a->pipe_fd = deserialize_fd(fds, value);
917 } else
918 log_unit_debug(u, "Unknown serialization key: %s", key);
919
920 return 0;
921 }
922
923 static UnitActiveState automount_active_state(Unit *u) {
924 assert(u);
925
926 return state_translation_table[AUTOMOUNT(u)->state];
927 }
928
929 static const char *automount_sub_state_to_string(Unit *u) {
930 assert(u);
931
932 return automount_state_to_string(AUTOMOUNT(u)->state);
933 }
934
935 static bool automount_may_gc(Unit *u) {
936 Unit *t;
937
938 assert(u);
939
940 t = UNIT_TRIGGER(u);
941 if (!t)
942 return true;
943
944 return UNIT_VTABLE(t)->may_gc(t);
945 }
946
947 static int automount_dispatch_io(sd_event_source *s, int fd, uint32_t events, void *userdata) {
948 Automount *a = ASSERT_PTR(AUTOMOUNT(userdata));
949 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
950 union autofs_v5_packet_union packet;
951 Unit *trigger;
952 int r;
953
954 assert(fd == a->pipe_fd);
955
956 if (events & (EPOLLHUP|EPOLLERR)) {
957 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?");
958 automount_enter_dead(a, AUTOMOUNT_FAILURE_UNMOUNTED);
959 return 0;
960 }
961
962 if (events != EPOLLIN) {
963 log_unit_error(UNIT(a), "Got invalid poll event %"PRIu32" on pipe (fd=%d)", events, fd);
964 goto fail;
965 }
966
967 r = loop_read_exact(a->pipe_fd, &packet, sizeof(packet), true);
968 if (r < 0) {
969 log_unit_error_errno(UNIT(a), r, "Invalid read from pipe: %m");
970 goto fail;
971 }
972
973 switch (packet.hdr.type) {
974
975 case autofs_ptype_missing_direct:
976
977 if (packet.v5_packet.pid > 0) {
978 _cleanup_free_ char *p = NULL;
979
980 (void) pid_get_comm(packet.v5_packet.pid, &p);
981 log_unit_info(UNIT(a), "Got automount request for %s, triggered by %"PRIu32" (%s)", a->where, packet.v5_packet.pid, strna(p));
982 } else
983 log_unit_debug(UNIT(a), "Got direct mount request on %s", a->where);
984
985 r = set_ensure_put(&a->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 automount_enter_running(a);
992 break;
993
994 case autofs_ptype_expire_direct:
995 log_unit_debug(UNIT(a), "Got direct umount request on %s", a->where);
996
997 automount_stop_expire(a);
998
999 r = set_ensure_put(&a->expire_tokens, NULL, UINT_TO_PTR(packet.v5_packet.wait_queue_token));
1000 if (r < 0) {
1001 log_unit_error_errno(UNIT(a), r, "Failed to remember token: %m");
1002 goto fail;
1003 }
1004
1005 trigger = UNIT_TRIGGER(UNIT(a));
1006 if (!trigger) {
1007 log_unit_error(UNIT(a), "Unit to trigger vanished.");
1008 goto fail;
1009 }
1010
1011 r = manager_add_job(UNIT(a)->manager, JOB_STOP, trigger, JOB_REPLACE, NULL, &error, NULL);
1012 if (r < 0) {
1013 log_unit_warning(UNIT(a), "Failed to queue unmount job: %s", bus_error_message(&error, r));
1014 goto fail;
1015 }
1016 break;
1017
1018 default:
1019 log_unit_error(UNIT(a), "Received unknown automount request %i", packet.hdr.type);
1020 break;
1021 }
1022
1023 return 0;
1024
1025 fail:
1026 automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
1027 return 0;
1028 }
1029
1030 static void automount_shutdown(Manager *m) {
1031 assert(m);
1032
1033 m->dev_autofs_fd = safe_close(m->dev_autofs_fd);
1034 }
1035
1036 static void automount_reset_failed(Unit *u) {
1037 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
1038
1039 if (a->state == AUTOMOUNT_FAILED)
1040 automount_set_state(a, AUTOMOUNT_DEAD);
1041
1042 a->result = AUTOMOUNT_SUCCESS;
1043 }
1044
1045 static bool automount_supported(void) {
1046 static int supported = -1;
1047
1048 if (supported < 0)
1049 supported = access("/dev/autofs", F_OK) >= 0;
1050
1051 return supported;
1052 }
1053
1054 static int automount_can_start(Unit *u) {
1055 Automount *a = ASSERT_PTR(AUTOMOUNT(u));
1056 int r;
1057
1058 r = unit_test_start_limit(u);
1059 if (r < 0) {
1060 automount_enter_dead(a, AUTOMOUNT_FAILURE_START_LIMIT_HIT);
1061 return r;
1062 }
1063
1064 return 1;
1065 }
1066
1067 static const char* const automount_result_table[_AUTOMOUNT_RESULT_MAX] = {
1068 [AUTOMOUNT_SUCCESS] = "success",
1069 [AUTOMOUNT_FAILURE_RESOURCES] = "resources",
1070 [AUTOMOUNT_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
1071 [AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT] = "mount-start-limit-hit",
1072 [AUTOMOUNT_FAILURE_UNMOUNTED] = "unmounted",
1073 };
1074
1075 DEFINE_STRING_TABLE_LOOKUP(automount_result, AutomountResult);
1076
1077 const UnitVTable automount_vtable = {
1078 .object_size = sizeof(Automount),
1079
1080 .sections =
1081 "Unit\0"
1082 "Automount\0"
1083 "Install\0",
1084 .private_section = "Automount",
1085
1086 .can_transient = true,
1087 .can_fail = true,
1088 .can_trigger = true,
1089 .exclude_from_switch_root_serialization = true,
1090
1091 .init = automount_init,
1092 .load = automount_load,
1093 .done = automount_done,
1094
1095 .coldplug = automount_coldplug,
1096
1097 .dump = automount_dump,
1098
1099 .start = automount_start,
1100 .stop = automount_stop,
1101
1102 .serialize = automount_serialize,
1103 .deserialize_item = automount_deserialize_item,
1104
1105 .active_state = automount_active_state,
1106 .sub_state_to_string = automount_sub_state_to_string,
1107
1108 .may_gc = automount_may_gc,
1109
1110 .trigger_notify = automount_trigger_notify,
1111
1112 .reset_failed = automount_reset_failed,
1113
1114 .bus_set_property = bus_automount_set_property,
1115
1116 .shutdown = automount_shutdown,
1117 .supported = automount_supported,
1118
1119 .status_message_formats = {
1120 .finished_start_job = {
1121 [JOB_DONE] = "Set up automount %s.",
1122 [JOB_FAILED] = "Failed to set up automount %s.",
1123 },
1124 .finished_stop_job = {
1125 [JOB_DONE] = "Unset automount %s.",
1126 [JOB_FAILED] = "Failed to unset automount %s.",
1127 },
1128 },
1129
1130 .can_start = automount_can_start,
1131 };