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