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