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