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