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