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