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