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