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