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