]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/automount.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[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 unit_log_success(UNIT(a));
320 else
321 unit_log_failure(UNIT(a), automount_result_to_string(a->result));
322
323 automount_set_state(a, a->result != AUTOMOUNT_SUCCESS ? AUTOMOUNT_FAILED : AUTOMOUNT_DEAD);
324 }
325
326 static int open_dev_autofs(Manager *m) {
327 struct autofs_dev_ioctl param;
328
329 assert(m);
330
331 if (m->dev_autofs_fd >= 0)
332 return m->dev_autofs_fd;
333
334 (void) label_fix("/dev/autofs", 0);
335
336 m->dev_autofs_fd = open("/dev/autofs", O_CLOEXEC|O_RDONLY);
337 if (m->dev_autofs_fd < 0)
338 return log_error_errno(errno, "Failed to open /dev/autofs: %m");
339
340 init_autofs_dev_ioctl(&param);
341 if (ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, &param) < 0) {
342 m->dev_autofs_fd = safe_close(m->dev_autofs_fd);
343 return -errno;
344 }
345
346 log_debug("Autofs kernel version %i.%i", param.ver_major, param.ver_minor);
347
348 return m->dev_autofs_fd;
349 }
350
351 static int open_ioctl_fd(int dev_autofs_fd, const char *where, dev_t devid) {
352 struct autofs_dev_ioctl *param;
353 size_t l;
354
355 assert(dev_autofs_fd >= 0);
356 assert(where);
357
358 l = sizeof(struct autofs_dev_ioctl) + strlen(where) + 1;
359 param = alloca(l);
360
361 init_autofs_dev_ioctl(param);
362 param->size = l;
363 param->ioctlfd = -1;
364 param->openmount.devid = devid;
365 strcpy(param->path, where);
366
367 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_OPENMOUNT, param) < 0)
368 return -errno;
369
370 if (param->ioctlfd < 0)
371 return -EIO;
372
373 (void) fd_cloexec(param->ioctlfd, true);
374 return param->ioctlfd;
375 }
376
377 static int autofs_protocol(int dev_autofs_fd, int ioctl_fd) {
378 uint32_t major, minor;
379 struct autofs_dev_ioctl param;
380
381 assert(dev_autofs_fd >= 0);
382 assert(ioctl_fd >= 0);
383
384 init_autofs_dev_ioctl(&param);
385 param.ioctlfd = ioctl_fd;
386
387 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOVER, &param) < 0)
388 return -errno;
389
390 major = param.protover.version;
391
392 init_autofs_dev_ioctl(&param);
393 param.ioctlfd = ioctl_fd;
394
395 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOSUBVER, &param) < 0)
396 return -errno;
397
398 minor = param.protosubver.sub_version;
399
400 log_debug("Autofs protocol version %i.%i", major, minor);
401 return 0;
402 }
403
404 static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, usec_t usec) {
405 struct autofs_dev_ioctl param;
406
407 assert(dev_autofs_fd >= 0);
408 assert(ioctl_fd >= 0);
409
410 init_autofs_dev_ioctl(&param);
411 param.ioctlfd = ioctl_fd;
412
413 if (usec == USEC_INFINITY)
414 param.timeout.timeout = 0;
415 else
416 /* Convert to seconds, rounding up. */
417 param.timeout.timeout = DIV_ROUND_UP(usec, USEC_PER_SEC);
418
419 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, &param) < 0)
420 return -errno;
421
422 return 0;
423 }
424
425 static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, int status) {
426 struct autofs_dev_ioctl param;
427
428 assert(dev_autofs_fd >= 0);
429 assert(ioctl_fd >= 0);
430
431 init_autofs_dev_ioctl(&param);
432 param.ioctlfd = ioctl_fd;
433
434 if (status != 0) {
435 param.fail.token = token;
436 param.fail.status = status;
437 } else
438 param.ready.token = token;
439
440 if (ioctl(dev_autofs_fd, status ? AUTOFS_DEV_IOCTL_FAIL : AUTOFS_DEV_IOCTL_READY, &param) < 0)
441 return -errno;
442
443 return 0;
444 }
445
446 static int automount_send_ready(Automount *a, Set *tokens, int status) {
447 _cleanup_close_ int ioctl_fd = -1;
448 unsigned token;
449 int r;
450
451 assert(a);
452 assert(status <= 0);
453
454 if (set_isempty(tokens))
455 return 0;
456
457 ioctl_fd = open_ioctl_fd(UNIT(a)->manager->dev_autofs_fd, a->where, a->dev_id);
458 if (ioctl_fd < 0)
459 return ioctl_fd;
460
461 if (status != 0)
462 log_unit_debug_errno(UNIT(a), status, "Sending failure: %m");
463 else
464 log_unit_debug(UNIT(a), "Sending success.");
465
466 r = 0;
467
468 /* Autofs thankfully does not hand out 0 as a token */
469 while ((token = PTR_TO_UINT(set_steal_first(tokens)))) {
470 int k;
471
472 /* Autofs fun fact:
473 *
474 * if you pass a positive status code here, kernels
475 * prior to 4.12 will freeze! Yay! */
476
477 k = autofs_send_ready(UNIT(a)->manager->dev_autofs_fd,
478 ioctl_fd,
479 token,
480 status);
481 if (k < 0)
482 r = k;
483 }
484
485 return r;
486 }
487
488 static void automount_trigger_notify(Unit *u, Unit *other) {
489 Automount *a = AUTOMOUNT(u);
490 int r;
491
492 assert(a);
493 assert(other);
494
495 /* Filter out invocations with bogus state */
496 if (other->load_state != UNIT_LOADED || other->type != UNIT_MOUNT)
497 return;
498
499 /* Don't propagate state changes from the mount if we are already down */
500 if (!IN_SET(a->state, AUTOMOUNT_WAITING, AUTOMOUNT_RUNNING))
501 return;
502
503 /* Propagate start limit hit state */
504 if (other->start_limit_hit) {
505 automount_enter_dead(a, AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT);
506 return;
507 }
508
509 /* Don't propagate anything if there's still a job queued */
510 if (other->job)
511 return;
512
513 /* The mount is successfully established */
514 if (IN_SET(MOUNT(other)->state, MOUNT_MOUNTED, MOUNT_REMOUNTING)) {
515 (void) automount_send_ready(a, a->tokens, 0);
516
517 r = automount_start_expire(a);
518 if (r < 0)
519 log_unit_warning_errno(UNIT(a), r, "Failed to start expiration timer, ignoring: %m");
520
521 automount_set_state(a, AUTOMOUNT_RUNNING);
522 }
523
524 if (IN_SET(MOUNT(other)->state,
525 MOUNT_MOUNTING, MOUNT_MOUNTING_DONE,
526 MOUNT_MOUNTED, MOUNT_REMOUNTING,
527 MOUNT_REMOUNTING_SIGTERM, MOUNT_REMOUNTING_SIGKILL,
528 MOUNT_UNMOUNTING_SIGTERM, MOUNT_UNMOUNTING_SIGKILL,
529 MOUNT_FAILED)) {
530
531 (void) automount_send_ready(a, a->expire_tokens, -ENODEV);
532 }
533
534 if (MOUNT(other)->state == MOUNT_DEAD)
535 (void) automount_send_ready(a, a->expire_tokens, 0);
536
537 /* The mount is in some unhappy state now, let's unfreeze any waiting clients */
538 if (IN_SET(MOUNT(other)->state,
539 MOUNT_DEAD, MOUNT_UNMOUNTING,
540 MOUNT_REMOUNTING_SIGTERM, MOUNT_REMOUNTING_SIGKILL,
541 MOUNT_UNMOUNTING_SIGTERM, MOUNT_UNMOUNTING_SIGKILL,
542 MOUNT_FAILED)) {
543
544 (void) automount_send_ready(a, a->tokens, -ENODEV);
545
546 automount_set_state(a, AUTOMOUNT_WAITING);
547 }
548 }
549
550 static void automount_enter_waiting(Automount *a) {
551 _cleanup_close_ int ioctl_fd = -1;
552 int p[2] = { -1, -1 };
553 char name[STRLEN("systemd-") + DECIMAL_STR_MAX(pid_t) + 1];
554 char options[STRLEN("fd=,pgrp=,minproto=5,maxproto=5,direct")
555 + DECIMAL_STR_MAX(int) + DECIMAL_STR_MAX(gid_t) + 1];
556 bool mounted = false;
557 int r, dev_autofs_fd;
558 struct stat st;
559
560 assert(a);
561 assert(a->pipe_fd < 0);
562 assert(a->where);
563
564 set_clear(a->tokens);
565
566 r = unit_fail_if_noncanonical(UNIT(a), a->where);
567 if (r < 0)
568 goto fail;
569
570 (void) mkdir_p_label(a->where, 0555);
571
572 unit_warn_if_dir_nonempty(UNIT(a), a->where);
573
574 dev_autofs_fd = open_dev_autofs(UNIT(a)->manager);
575 if (dev_autofs_fd < 0) {
576 r = dev_autofs_fd;
577 goto fail;
578 }
579
580 if (pipe2(p, O_NONBLOCK|O_CLOEXEC) < 0) {
581 r = -errno;
582 goto fail;
583 }
584
585 xsprintf(options, "fd=%i,pgrp="PID_FMT",minproto=5,maxproto=5,direct", p[1], getpgrp());
586 xsprintf(name, "systemd-"PID_FMT, getpid_cached());
587 if (mount(name, a->where, "autofs", 0, options) < 0) {
588 r = -errno;
589 goto fail;
590 }
591
592 mounted = true;
593
594 p[1] = safe_close(p[1]);
595
596 if (stat(a->where, &st) < 0) {
597 r = -errno;
598 goto fail;
599 }
600
601 ioctl_fd = open_ioctl_fd(dev_autofs_fd, a->where, st.st_dev);
602 if (ioctl_fd < 0) {
603 r = ioctl_fd;
604 goto fail;
605 }
606
607 r = autofs_protocol(dev_autofs_fd, ioctl_fd);
608 if (r < 0)
609 goto fail;
610
611 r = autofs_set_timeout(dev_autofs_fd, ioctl_fd, a->timeout_idle_usec);
612 if (r < 0)
613 goto fail;
614
615 r = sd_event_add_io(UNIT(a)->manager->event, &a->pipe_event_source, p[0], EPOLLIN, automount_dispatch_io, a);
616 if (r < 0)
617 goto fail;
618
619 (void) sd_event_source_set_description(a->pipe_event_source, "automount-io");
620
621 a->pipe_fd = p[0];
622 a->dev_id = st.st_dev;
623
624 automount_set_state(a, AUTOMOUNT_WAITING);
625
626 return;
627
628 fail:
629 log_unit_error_errno(UNIT(a), r, "Failed to initialize automounter: %m");
630
631 safe_close_pair(p);
632
633 if (mounted) {
634 r = repeat_unmount(a->where, MNT_DETACH);
635 if (r < 0)
636 log_error_errno(r, "Failed to unmount, ignoring: %m");
637 }
638
639 automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
640 }
641
642 static void *expire_thread(void *p) {
643 struct autofs_dev_ioctl param;
644 _cleanup_(expire_data_freep) struct expire_data *data = (struct expire_data*)p;
645 int r;
646
647 assert(data->dev_autofs_fd >= 0);
648 assert(data->ioctl_fd >= 0);
649
650 init_autofs_dev_ioctl(&param);
651 param.ioctlfd = data->ioctl_fd;
652
653 do {
654 r = ioctl(data->dev_autofs_fd, AUTOFS_DEV_IOCTL_EXPIRE, &param);
655 } while (r >= 0);
656
657 if (errno != EAGAIN)
658 log_warning_errno(errno, "Failed to expire automount, ignoring: %m");
659
660 return NULL;
661 }
662
663 static int automount_dispatch_expire(sd_event_source *source, usec_t usec, void *userdata) {
664 Automount *a = AUTOMOUNT(userdata);
665 _cleanup_(expire_data_freep) struct expire_data *data = NULL;
666 int r;
667
668 assert(a);
669 assert(source == a->expire_event_source);
670
671 data = new0(struct expire_data, 1);
672 if (!data)
673 return log_oom();
674
675 data->ioctl_fd = -1;
676
677 data->dev_autofs_fd = fcntl(UNIT(a)->manager->dev_autofs_fd, F_DUPFD_CLOEXEC, 3);
678 if (data->dev_autofs_fd < 0)
679 return log_unit_error_errno(UNIT(a), errno, "Failed to duplicate autofs fd: %m");
680
681 data->ioctl_fd = open_ioctl_fd(UNIT(a)->manager->dev_autofs_fd, a->where, a->dev_id);
682 if (data->ioctl_fd < 0)
683 return log_unit_error_errno(UNIT(a), data->ioctl_fd, "Couldn't open autofs ioctl fd: %m");
684
685 r = asynchronous_job(expire_thread, data);
686 if (r < 0)
687 return log_unit_error_errno(UNIT(a), r, "Failed to start expire job: %m");
688
689 data = NULL;
690
691 return automount_start_expire(a);
692 }
693
694 static int automount_start_expire(Automount *a) {
695 int r;
696 usec_t timeout;
697
698 assert(a);
699
700 if (a->timeout_idle_usec == 0)
701 return 0;
702
703 timeout = now(CLOCK_MONOTONIC) + MAX(a->timeout_idle_usec/3, USEC_PER_SEC);
704
705 if (a->expire_event_source) {
706 r = sd_event_source_set_time(a->expire_event_source, timeout);
707 if (r < 0)
708 return r;
709
710 return sd_event_source_set_enabled(a->expire_event_source, SD_EVENT_ONESHOT);
711 }
712
713 r = sd_event_add_time(
714 UNIT(a)->manager->event,
715 &a->expire_event_source,
716 CLOCK_MONOTONIC, timeout, 0,
717 automount_dispatch_expire, a);
718 if (r < 0)
719 return r;
720
721 (void) sd_event_source_set_description(a->expire_event_source, "automount-expire");
722
723 return 0;
724 }
725
726 static void automount_stop_expire(Automount *a) {
727 assert(a);
728
729 if (!a->expire_event_source)
730 return;
731
732 (void) sd_event_source_set_enabled(a->expire_event_source, SD_EVENT_OFF);
733 }
734
735 static void automount_enter_running(Automount *a) {
736 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
737 Unit *trigger;
738 struct stat st;
739 int r;
740
741 assert(a);
742
743 /* If the user masked our unit in the meantime, fail */
744 if (UNIT(a)->load_state != UNIT_LOADED) {
745 log_unit_error(UNIT(a), "Suppressing automount event since unit is no longer loaded.");
746 goto fail;
747 }
748
749 /* We don't take mount requests anymore if we are supposed to
750 * shut down anyway */
751 if (unit_stop_pending(UNIT(a))) {
752 log_unit_debug(UNIT(a), "Suppressing automount request since unit stop is scheduled.");
753 automount_send_ready(a, a->tokens, -EHOSTDOWN);
754 automount_send_ready(a, a->expire_tokens, -EHOSTDOWN);
755 return;
756 }
757
758 mkdir_p_label(a->where, a->directory_mode);
759
760 /* Before we do anything, let's see if somebody is playing games with us? */
761 if (lstat(a->where, &st) < 0) {
762 log_unit_warning_errno(UNIT(a), errno, "Failed to stat automount point: %m");
763 goto fail;
764 }
765
766 /* The mount unit may have been explicitly started before we got the
767 * autofs request. Ack it to unblock anything waiting on the mount point. */
768 if (!S_ISDIR(st.st_mode) || st.st_dev != a->dev_id) {
769 log_unit_info(UNIT(a), "Automount point already active?");
770 automount_send_ready(a, a->tokens, 0);
771 return;
772 }
773
774 trigger = UNIT_TRIGGER(UNIT(a));
775 if (!trigger) {
776 log_unit_error(UNIT(a), "Unit to trigger vanished.");
777 goto fail;
778 }
779
780 r = manager_add_job(UNIT(a)->manager, JOB_START, trigger, JOB_REPLACE, &error, NULL);
781 if (r < 0) {
782 log_unit_warning(UNIT(a), "Failed to queue mount startup job: %s", bus_error_message(&error, r));
783 goto fail;
784 }
785
786 automount_set_state(a, AUTOMOUNT_RUNNING);
787 return;
788
789 fail:
790 automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
791 }
792
793 static int automount_start(Unit *u) {
794 Automount *a = AUTOMOUNT(u);
795 Unit *trigger;
796 int r;
797
798 assert(a);
799 assert(IN_SET(a->state, AUTOMOUNT_DEAD, AUTOMOUNT_FAILED));
800
801 if (path_is_mount_point(a->where, NULL, 0) > 0) {
802 log_unit_error(u, "Path %s is already a mount point, refusing start.", a->where);
803 return -EEXIST;
804 }
805
806 trigger = UNIT_TRIGGER(u);
807 if (!trigger || trigger->load_state != UNIT_LOADED) {
808 log_unit_error(u, "Refusing to start, unit to trigger not loaded.");
809 return -ENOENT;
810 }
811
812 r = unit_start_limit_test(u);
813 if (r < 0) {
814 automount_enter_dead(a, AUTOMOUNT_FAILURE_START_LIMIT_HIT);
815 return r;
816 }
817
818 r = unit_acquire_invocation_id(u);
819 if (r < 0)
820 return r;
821
822 a->result = AUTOMOUNT_SUCCESS;
823 automount_enter_waiting(a);
824 return 1;
825 }
826
827 static int automount_stop(Unit *u) {
828 Automount *a = AUTOMOUNT(u);
829
830 assert(a);
831 assert(IN_SET(a->state, AUTOMOUNT_WAITING, AUTOMOUNT_RUNNING));
832
833 automount_enter_dead(a, AUTOMOUNT_SUCCESS);
834 return 1;
835 }
836
837 static int automount_serialize(Unit *u, FILE *f, FDSet *fds) {
838 Automount *a = AUTOMOUNT(u);
839 Iterator i;
840 void *p;
841 int r;
842
843 assert(a);
844 assert(f);
845 assert(fds);
846
847 (void) serialize_item(f, "state", automount_state_to_string(a->state));
848 (void) serialize_item(f, "result", automount_result_to_string(a->result));
849 (void) serialize_item_format(f, "dev-id", "%lu", (unsigned long) a->dev_id);
850
851 SET_FOREACH(p, a->tokens, i)
852 (void) serialize_item_format(f, "token", "%u", PTR_TO_UINT(p));
853 SET_FOREACH(p, a->expire_tokens, i)
854 (void) serialize_item_format(f, "expire-token", "%u", PTR_TO_UINT(p));
855
856 r = serialize_fd(f, fds, "pipe-fd", a->pipe_fd);
857 if (r < 0)
858 return r;
859
860 return 0;
861 }
862
863 static int automount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
864 Automount *a = AUTOMOUNT(u);
865 int r;
866
867 assert(a);
868 assert(fds);
869
870 if (streq(key, "state")) {
871 AutomountState state;
872
873 state = automount_state_from_string(value);
874 if (state < 0)
875 log_unit_debug(u, "Failed to parse state value: %s", value);
876 else
877 a->deserialized_state = state;
878 } else if (streq(key, "result")) {
879 AutomountResult f;
880
881 f = automount_result_from_string(value);
882 if (f < 0)
883 log_unit_debug(u, "Failed to parse result value: %s", value);
884 else if (f != AUTOMOUNT_SUCCESS)
885 a->result = f;
886
887 } else if (streq(key, "dev-id")) {
888 unsigned long d;
889
890 if (safe_atolu(value, &d) < 0)
891 log_unit_debug(u, "Failed to parse dev-id value: %s", value);
892 else
893 a->dev_id = (dev_t) d;
894
895 } else if (streq(key, "token")) {
896 unsigned token;
897
898 if (safe_atou(value, &token) < 0)
899 log_unit_debug(u, "Failed to parse token value: %s", value);
900 else {
901 r = set_ensure_allocated(&a->tokens, NULL);
902 if (r < 0) {
903 log_oom();
904 return 0;
905 }
906
907 r = set_put(a->tokens, UINT_TO_PTR(token));
908 if (r < 0)
909 log_unit_error_errno(u, r, "Failed to add token to set: %m");
910 }
911 } else if (streq(key, "expire-token")) {
912 unsigned token;
913
914 if (safe_atou(value, &token) < 0)
915 log_unit_debug(u, "Failed to parse token value: %s", value);
916 else {
917 r = set_ensure_allocated(&a->expire_tokens, NULL);
918 if (r < 0) {
919 log_oom();
920 return 0;
921 }
922
923 r = set_put(a->expire_tokens, UINT_TO_PTR(token));
924 if (r < 0)
925 log_unit_error_errno(u, r, "Failed to add expire token to set: %m");
926 }
927 } else if (streq(key, "pipe-fd")) {
928 int fd;
929
930 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
931 log_unit_debug(u, "Failed to parse pipe-fd value: %s", value);
932 else {
933 safe_close(a->pipe_fd);
934 a->pipe_fd = fdset_remove(fds, fd);
935 }
936 } else
937 log_unit_debug(u, "Unknown serialization key: %s", key);
938
939 return 0;
940 }
941
942 static UnitActiveState automount_active_state(Unit *u) {
943 assert(u);
944
945 return state_translation_table[AUTOMOUNT(u)->state];
946 }
947
948 static const char *automount_sub_state_to_string(Unit *u) {
949 assert(u);
950
951 return automount_state_to_string(AUTOMOUNT(u)->state);
952 }
953
954 static bool automount_may_gc(Unit *u) {
955 Unit *t;
956
957 assert(u);
958
959 t = UNIT_TRIGGER(u);
960 if (!t)
961 return true;
962
963 return UNIT_VTABLE(t)->may_gc(t);
964 }
965
966 static int automount_dispatch_io(sd_event_source *s, int fd, uint32_t events, void *userdata) {
967 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
968 union autofs_v5_packet_union packet;
969 Automount *a = AUTOMOUNT(userdata);
970 Unit *trigger;
971 int r;
972
973 assert(a);
974 assert(fd == a->pipe_fd);
975
976 if (events != EPOLLIN) {
977 log_unit_error(UNIT(a), "Got invalid poll event %"PRIu32" on pipe (fd=%d)", events, fd);
978 goto fail;
979 }
980
981 r = loop_read_exact(a->pipe_fd, &packet, sizeof(packet), true);
982 if (r < 0) {
983 log_unit_error_errno(UNIT(a), r, "Invalid read from pipe: %m");
984 goto fail;
985 }
986
987 switch (packet.hdr.type) {
988
989 case autofs_ptype_missing_direct:
990
991 if (packet.v5_packet.pid > 0) {
992 _cleanup_free_ char *p = NULL;
993
994 get_process_comm(packet.v5_packet.pid, &p);
995 log_unit_info(UNIT(a), "Got automount request for %s, triggered by %"PRIu32" (%s)", a->where, packet.v5_packet.pid, strna(p));
996 } else
997 log_unit_debug(UNIT(a), "Got direct mount request on %s", a->where);
998
999 r = set_ensure_allocated(&a->tokens, NULL);
1000 if (r < 0) {
1001 log_unit_error(UNIT(a), "Failed to allocate token set.");
1002 goto fail;
1003 }
1004
1005 r = set_put(a->tokens, UINT_TO_PTR(packet.v5_packet.wait_queue_token));
1006 if (r < 0) {
1007 log_unit_error_errno(UNIT(a), r, "Failed to remember token: %m");
1008 goto fail;
1009 }
1010
1011 automount_enter_running(a);
1012 break;
1013
1014 case autofs_ptype_expire_direct:
1015 log_unit_debug(UNIT(a), "Got direct umount request on %s", a->where);
1016
1017 automount_stop_expire(a);
1018
1019 r = set_ensure_allocated(&a->expire_tokens, NULL);
1020 if (r < 0) {
1021 log_unit_error(UNIT(a), "Failed to allocate token set.");
1022 goto fail;
1023 }
1024
1025 r = set_put(a->expire_tokens, UINT_TO_PTR(packet.v5_packet.wait_queue_token));
1026 if (r < 0) {
1027 log_unit_error_errno(UNIT(a), r, "Failed to remember token: %m");
1028 goto fail;
1029 }
1030
1031 trigger = UNIT_TRIGGER(UNIT(a));
1032 if (!trigger) {
1033 log_unit_error(UNIT(a), "Unit to trigger vanished.");
1034 goto fail;
1035 }
1036
1037 r = manager_add_job(UNIT(a)->manager, JOB_STOP, trigger, JOB_REPLACE, &error, NULL);
1038 if (r < 0) {
1039 log_unit_warning(UNIT(a), "Failed to queue umount startup job: %s", bus_error_message(&error, r));
1040 goto fail;
1041 }
1042 break;
1043
1044 default:
1045 log_unit_error(UNIT(a), "Received unknown automount request %i", packet.hdr.type);
1046 break;
1047 }
1048
1049 return 0;
1050
1051 fail:
1052 automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
1053 return 0;
1054 }
1055
1056 static void automount_shutdown(Manager *m) {
1057 assert(m);
1058
1059 m->dev_autofs_fd = safe_close(m->dev_autofs_fd);
1060 }
1061
1062 static void automount_reset_failed(Unit *u) {
1063 Automount *a = AUTOMOUNT(u);
1064
1065 assert(a);
1066
1067 if (a->state == AUTOMOUNT_FAILED)
1068 automount_set_state(a, AUTOMOUNT_DEAD);
1069
1070 a->result = AUTOMOUNT_SUCCESS;
1071 }
1072
1073 static bool automount_supported(void) {
1074 static int supported = -1;
1075
1076 if (supported < 0)
1077 supported = access("/dev/autofs", F_OK) >= 0;
1078
1079 return supported;
1080 }
1081
1082 static const char* const automount_result_table[_AUTOMOUNT_RESULT_MAX] = {
1083 [AUTOMOUNT_SUCCESS] = "success",
1084 [AUTOMOUNT_FAILURE_RESOURCES] = "resources",
1085 [AUTOMOUNT_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
1086 [AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT] = "mount-start-limit-hit",
1087 };
1088
1089 DEFINE_STRING_TABLE_LOOKUP(automount_result, AutomountResult);
1090
1091 const UnitVTable automount_vtable = {
1092 .object_size = sizeof(Automount),
1093
1094 .sections =
1095 "Unit\0"
1096 "Automount\0"
1097 "Install\0",
1098
1099 .init = automount_init,
1100 .load = automount_load,
1101 .done = automount_done,
1102
1103 .coldplug = automount_coldplug,
1104
1105 .dump = automount_dump,
1106
1107 .start = automount_start,
1108 .stop = automount_stop,
1109
1110 .serialize = automount_serialize,
1111 .deserialize_item = automount_deserialize_item,
1112
1113 .active_state = automount_active_state,
1114 .sub_state_to_string = automount_sub_state_to_string,
1115
1116 .may_gc = automount_may_gc,
1117
1118 .trigger_notify = automount_trigger_notify,
1119
1120 .reset_failed = automount_reset_failed,
1121
1122 .bus_vtable = bus_automount_vtable,
1123 .bus_set_property = bus_automount_set_property,
1124
1125 .can_transient = true,
1126
1127 .shutdown = automount_shutdown,
1128 .supported = automount_supported,
1129
1130 .status_message_formats = {
1131 .finished_start_job = {
1132 [JOB_DONE] = "Set up automount %s.",
1133 [JOB_FAILED] = "Failed to set up automount %s.",
1134 },
1135 .finished_stop_job = {
1136 [JOB_DONE] = "Unset automount %s.",
1137 [JOB_FAILED] = "Failed to unset automount %s.",
1138 },
1139 },
1140 };