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