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