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