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