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