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