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