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