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