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