]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
5cb5a6ff | 2 | |
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
5cb5a6ff | 22 | #include <errno.h> |
8d567588 LP |
23 | #include <limits.h> |
24 | #include <sys/mount.h> | |
25 | #include <unistd.h> | |
26 | #include <fcntl.h> | |
27 | #include <sys/epoll.h> | |
28 | #include <sys/stat.h> | |
29 | #include <linux/auto_fs4.h> | |
30 | #include <linux/auto_dev-ioctl.h> | |
5cb5a6ff | 31 | |
87f0e418 | 32 | #include "unit.h" |
5cb5a6ff LP |
33 | #include "automount.h" |
34 | #include "load-fragment.h" | |
5cb5a6ff | 35 | #include "load-dropin.h" |
8d567588 | 36 | #include "unit-name.h" |
4139c1b2 | 37 | #include "dbus-automount.h" |
398ef8ba | 38 | #include "bus-errors.h" |
4e67ddd6 | 39 | #include "special.h" |
e51bc1a2 | 40 | #include "label.h" |
5cb5a6ff | 41 | |
e537352b LP |
42 | static const UnitActiveState state_translation_table[_AUTOMOUNT_STATE_MAX] = { |
43 | [AUTOMOUNT_DEAD] = UNIT_INACTIVE, | |
44 | [AUTOMOUNT_WAITING] = UNIT_ACTIVE, | |
45 | [AUTOMOUNT_RUNNING] = UNIT_ACTIVE, | |
74ac3cbd | 46 | [AUTOMOUNT_FAILED] = UNIT_FAILED |
e537352b | 47 | }; |
5cb5a6ff | 48 | |
a16e1123 | 49 | static int open_dev_autofs(Manager *m); |
8d567588 | 50 | |
e537352b LP |
51 | static void automount_init(Unit *u) { |
52 | Automount *a = AUTOMOUNT(u); | |
5cb5a6ff | 53 | |
8d567588 LP |
54 | assert(u); |
55 | assert(u->meta.load_state == UNIT_STUB); | |
56 | ||
57 | a->pipe_watch.fd = a->pipe_fd = -1; | |
a16e1123 | 58 | a->pipe_watch.type = WATCH_INVALID; |
1cf18f27 LP |
59 | |
60 | a->directory_mode = 0755; | |
8d567588 LP |
61 | } |
62 | ||
63 | static void repeat_unmout(const char *path) { | |
64 | assert(path); | |
65 | ||
66 | for (;;) { | |
1b560190 LP |
67 | /* If there are multiple mounts on a mount point, this |
68 | * removes them all */ | |
8d567588 LP |
69 | |
70 | if (umount2(path, MNT_DETACH) >= 0) | |
71 | continue; | |
72 | ||
73 | if (errno != EINVAL) | |
74 | log_error("Failed to unmount: %m"); | |
75 | ||
76 | break; | |
77 | } | |
78 | } | |
79 | ||
80 | static void unmount_autofs(Automount *a) { | |
81 | assert(a); | |
82 | ||
83 | if (a->pipe_fd < 0) | |
84 | return; | |
85 | ||
86 | automount_send_ready(a, -EHOSTDOWN); | |
87 | ||
88 | unit_unwatch_fd(UNIT(a), &a->pipe_watch); | |
89 | close_nointr_nofail(a->pipe_fd); | |
90 | a->pipe_fd = -1; | |
91 | ||
a16e1123 LP |
92 | /* If we reload/reexecute things we keep the mount point |
93 | * around */ | |
94 | if (a->where && | |
4cd1fbcc LP |
95 | (a->meta.manager->exit_code != MANAGER_RELOAD && |
96 | a->meta.manager->exit_code != MANAGER_REEXECUTE)) | |
a16e1123 | 97 | repeat_unmout(a->where); |
8d567588 LP |
98 | } |
99 | ||
100 | static void automount_done(Unit *u) { | |
101 | Automount *a = AUTOMOUNT(u); | |
102 | ||
103 | assert(a); | |
104 | ||
105 | unmount_autofs(a); | |
e537352b | 106 | a->mount = NULL; |
8d567588 | 107 | |
a16e1123 LP |
108 | free(a->where); |
109 | a->where = NULL; | |
110 | ||
111 | set_free(a->tokens); | |
112 | a->tokens = NULL; | |
8d567588 LP |
113 | } |
114 | ||
6e2ef85b LP |
115 | int automount_add_one_mount_link(Automount *a, Mount *m) { |
116 | int r; | |
117 | ||
118 | assert(a); | |
119 | assert(m); | |
120 | ||
121 | if (a->meta.load_state != UNIT_LOADED || | |
122 | m->meta.load_state != UNIT_LOADED) | |
123 | return 0; | |
124 | ||
125 | if (!path_startswith(a->where, m->where)) | |
126 | return 0; | |
127 | ||
1b560190 LP |
128 | if (path_equal(a->where, m->where)) |
129 | return 0; | |
130 | ||
2c966c03 | 131 | if ((r = unit_add_two_dependencies(UNIT(a), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0) |
6e2ef85b LP |
132 | return r; |
133 | ||
134 | return 0; | |
135 | } | |
136 | ||
137 | static int automount_add_mount_links(Automount *a) { | |
138 | Meta *other; | |
139 | int r; | |
140 | ||
141 | assert(a); | |
142 | ||
143 | LIST_FOREACH(units_per_type, other, a->meta.manager->units_per_type[UNIT_MOUNT]) | |
144 | if ((r = automount_add_one_mount_link(a, (Mount*) other)) < 0) | |
145 | return r; | |
146 | ||
147 | return 0; | |
148 | } | |
149 | ||
2edd4434 LP |
150 | static int automount_add_default_dependencies(Automount *a) { |
151 | int r; | |
152 | ||
153 | assert(a); | |
154 | ||
155 | if (a->meta.manager->running_as == MANAGER_SYSTEM) { | |
156 | ||
2a77d31d LP |
157 | if ((r = unit_add_dependency_by_name(UNIT(a), UNIT_BEFORE, SPECIAL_BASIC_TARGET, NULL, true)) < 0) |
158 | return r; | |
159 | ||
ead8e478 | 160 | if ((r = unit_add_two_dependencies_by_name(UNIT(a), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true)) < 0) |
2edd4434 LP |
161 | return r; |
162 | } | |
163 | ||
164 | return 0; | |
165 | } | |
166 | ||
8d567588 LP |
167 | static int automount_verify(Automount *a) { |
168 | bool b; | |
169 | char *e; | |
170 | assert(a); | |
171 | ||
4cd1fbcc | 172 | if (a->meta.load_state != UNIT_LOADED) |
8d567588 LP |
173 | return 0; |
174 | ||
41e45059 LP |
175 | if (path_equal(a->where, "/")) { |
176 | log_error("Cannot have an automount unit for the root directory. Refusing."); | |
177 | return -EINVAL; | |
178 | } | |
179 | ||
a16e1123 | 180 | if (!(e = unit_name_from_path(a->where, ".automount"))) |
8d567588 LP |
181 | return -ENOMEM; |
182 | ||
183 | b = unit_has_name(UNIT(a), e); | |
184 | free(e); | |
185 | ||
186 | if (!b) { | |
4cd1fbcc | 187 | log_error("%s's Where setting doesn't match unit name. Refusing.", a->meta.id); |
8d567588 LP |
188 | return -EINVAL; |
189 | } | |
190 | ||
191 | return 0; | |
e537352b | 192 | } |
5cb5a6ff | 193 | |
e537352b LP |
194 | static int automount_load(Unit *u) { |
195 | int r; | |
196 | Automount *a = AUTOMOUNT(u); | |
23a177ef | 197 | |
e537352b LP |
198 | assert(u); |
199 | assert(u->meta.load_state == UNIT_STUB); | |
5cb5a6ff | 200 | |
e537352b LP |
201 | /* Load a .automount file */ |
202 | if ((r = unit_load_fragment_and_dropin_optional(u)) < 0) | |
203 | return r; | |
23a177ef | 204 | |
e537352b | 205 | if (u->meta.load_state == UNIT_LOADED) { |
23a177ef | 206 | |
a16e1123 LP |
207 | if (!a->where) |
208 | if (!(a->where = unit_name_to_path(u->meta.id))) | |
209 | return -ENOMEM; | |
210 | ||
211 | path_kill_slashes(a->where); | |
212 | ||
6e2ef85b LP |
213 | if ((r = automount_add_mount_links(a)) < 0) |
214 | return r; | |
215 | ||
e537352b | 216 | if ((r = unit_load_related_unit(u, ".mount", (Unit**) &a->mount)) < 0) |
23a177ef LP |
217 | return r; |
218 | ||
701cc384 | 219 | if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(a->mount), true)) < 0) |
23a177ef | 220 | return r; |
4e67ddd6 | 221 | |
2edd4434 LP |
222 | if (a->meta.default_dependencies) |
223 | if ((r = automount_add_default_dependencies(a)) < 0) | |
4e67ddd6 | 224 | return r; |
23a177ef LP |
225 | } |
226 | ||
8d567588 | 227 | return automount_verify(a); |
5cb5a6ff LP |
228 | } |
229 | ||
8d567588 LP |
230 | static void automount_set_state(Automount *a, AutomountState state) { |
231 | AutomountState old_state; | |
e537352b | 232 | assert(a); |
034c6ed7 | 233 | |
8d567588 LP |
234 | old_state = a->state; |
235 | a->state = state; | |
236 | ||
237 | if (state != AUTOMOUNT_WAITING && | |
238 | state != AUTOMOUNT_RUNNING) | |
239 | unmount_autofs(a); | |
240 | ||
241 | if (state != old_state) | |
40d50879 | 242 | log_debug("%s changed %s -> %s", |
4cd1fbcc | 243 | a->meta.id, |
a16e1123 LP |
244 | automount_state_to_string(old_state), |
245 | automount_state_to_string(state)); | |
8d567588 | 246 | |
e2f3b44c | 247 | unit_notify(UNIT(a), state_translation_table[old_state], state_translation_table[state], true); |
034c6ed7 LP |
248 | } |
249 | ||
a16e1123 LP |
250 | static int automount_coldplug(Unit *u) { |
251 | Automount *a = AUTOMOUNT(u); | |
252 | int r; | |
253 | ||
254 | assert(a); | |
255 | assert(a->state == AUTOMOUNT_DEAD); | |
256 | ||
257 | if (a->deserialized_state != a->state) { | |
258 | ||
259 | if ((r = open_dev_autofs(u->meta.manager)) < 0) | |
260 | return r; | |
261 | ||
262 | if (a->deserialized_state == AUTOMOUNT_WAITING || | |
263 | a->deserialized_state == AUTOMOUNT_RUNNING) { | |
264 | ||
265 | assert(a->pipe_fd >= 0); | |
266 | ||
267 | if ((r = unit_watch_fd(UNIT(a), a->pipe_fd, EPOLLIN, &a->pipe_watch)) < 0) | |
268 | return r; | |
269 | } | |
270 | ||
271 | automount_set_state(a, a->deserialized_state); | |
272 | } | |
273 | ||
274 | return 0; | |
275 | } | |
276 | ||
87f0e418 | 277 | static void automount_dump(Unit *u, FILE *f, const char *prefix) { |
a16e1123 | 278 | Automount *a = AUTOMOUNT(u); |
5cb5a6ff | 279 | |
a16e1123 | 280 | assert(a); |
5cb5a6ff LP |
281 | |
282 | fprintf(f, | |
a16e1123 | 283 | "%sAutomount State: %s\n" |
1cf18f27 LP |
284 | "%sWhere: %s\n" |
285 | "%sDirectoryMode: %04o\n", | |
a16e1123 | 286 | prefix, automount_state_to_string(a->state), |
1cf18f27 LP |
287 | prefix, a->where, |
288 | prefix, a->directory_mode); | |
5cb5a6ff LP |
289 | } |
290 | ||
8d567588 LP |
291 | static void automount_enter_dead(Automount *a, bool success) { |
292 | assert(a); | |
293 | ||
294 | if (!success) | |
295 | a->failure = true; | |
296 | ||
74ac3cbd | 297 | automount_set_state(a, a->failure ? AUTOMOUNT_FAILED : AUTOMOUNT_DEAD); |
8d567588 LP |
298 | } |
299 | ||
300 | static int open_dev_autofs(Manager *m) { | |
301 | struct autofs_dev_ioctl param; | |
302 | ||
303 | assert(m); | |
304 | ||
305 | if (m->dev_autofs_fd >= 0) | |
306 | return m->dev_autofs_fd; | |
307 | ||
c904f64d | 308 | label_fix("/dev/autofs", false); |
56cf987f | 309 | |
a16e1123 | 310 | if ((m->dev_autofs_fd = open("/dev/autofs", O_CLOEXEC|O_RDONLY)) < 0) { |
11c3a4ee LP |
311 | log_error("Failed to open /dev/autofs: %s", strerror(errno)); |
312 | return -errno; | |
8d567588 LP |
313 | } |
314 | ||
315 | init_autofs_dev_ioctl(¶m); | |
316 | if (ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, ¶m) < 0) { | |
317 | close_nointr_nofail(m->dev_autofs_fd); | |
318 | m->dev_autofs_fd = -1; | |
319 | return -errno; | |
320 | } | |
321 | ||
322 | log_debug("Autofs kernel version %i.%i", param.ver_major, param.ver_minor); | |
323 | ||
324 | return m->dev_autofs_fd; | |
325 | } | |
326 | ||
327 | static int open_ioctl_fd(int dev_autofs_fd, const char *where, dev_t devid) { | |
328 | struct autofs_dev_ioctl *param; | |
329 | size_t l; | |
330 | int r; | |
331 | ||
332 | assert(dev_autofs_fd >= 0); | |
333 | assert(where); | |
334 | ||
335 | l = sizeof(struct autofs_dev_ioctl) + strlen(where) + 1; | |
336 | ||
337 | if (!(param = malloc(l))) | |
338 | return -ENOMEM; | |
339 | ||
340 | init_autofs_dev_ioctl(param); | |
341 | param->size = l; | |
342 | param->ioctlfd = -1; | |
343 | param->openmount.devid = devid; | |
344 | strcpy(param->path, where); | |
345 | ||
346 | if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_OPENMOUNT, param) < 0) { | |
347 | r = -errno; | |
348 | goto finish; | |
349 | } | |
350 | ||
351 | if (param->ioctlfd < 0) { | |
352 | r = -EIO; | |
353 | goto finish; | |
354 | } | |
355 | ||
a16e1123 | 356 | fd_cloexec(param->ioctlfd, true); |
8d567588 LP |
357 | r = param->ioctlfd; |
358 | ||
359 | finish: | |
360 | free(param); | |
361 | return r; | |
362 | } | |
363 | ||
364 | static int autofs_protocol(int dev_autofs_fd, int ioctl_fd) { | |
365 | uint32_t major, minor; | |
366 | struct autofs_dev_ioctl param; | |
367 | ||
368 | assert(dev_autofs_fd >= 0); | |
369 | assert(ioctl_fd >= 0); | |
370 | ||
371 | init_autofs_dev_ioctl(¶m); | |
372 | param.ioctlfd = ioctl_fd; | |
373 | ||
374 | if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOVER, ¶m) < 0) | |
375 | return -errno; | |
376 | ||
377 | major = param.protover.version; | |
378 | ||
379 | init_autofs_dev_ioctl(¶m); | |
380 | param.ioctlfd = ioctl_fd; | |
381 | ||
382 | if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOSUBVER, ¶m) < 0) | |
383 | return -errno; | |
384 | ||
385 | minor = param.protosubver.sub_version; | |
386 | ||
387 | log_debug("Autofs protocol version %i.%i", major, minor); | |
388 | return 0; | |
389 | } | |
390 | ||
391 | static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, time_t sec) { | |
392 | struct autofs_dev_ioctl param; | |
393 | ||
394 | assert(dev_autofs_fd >= 0); | |
395 | assert(ioctl_fd >= 0); | |
396 | ||
397 | init_autofs_dev_ioctl(¶m); | |
398 | param.ioctlfd = ioctl_fd; | |
399 | param.timeout.timeout = sec; | |
400 | ||
401 | if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, ¶m) < 0) | |
402 | return -errno; | |
403 | ||
404 | return 0; | |
405 | } | |
406 | ||
407 | static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, int status) { | |
408 | struct autofs_dev_ioctl param; | |
409 | ||
410 | assert(dev_autofs_fd >= 0); | |
411 | assert(ioctl_fd >= 0); | |
412 | ||
413 | init_autofs_dev_ioctl(¶m); | |
414 | param.ioctlfd = ioctl_fd; | |
415 | ||
416 | if (status) { | |
417 | param.fail.token = token; | |
418 | param.fail.status = status; | |
419 | } else | |
420 | param.ready.token = token; | |
421 | ||
422 | if (ioctl(dev_autofs_fd, status ? AUTOFS_DEV_IOCTL_FAIL : AUTOFS_DEV_IOCTL_READY, ¶m) < 0) | |
423 | return -errno; | |
424 | ||
425 | return 0; | |
426 | } | |
427 | ||
428 | int automount_send_ready(Automount *a, int status) { | |
429 | int ioctl_fd, r; | |
430 | unsigned token; | |
431 | ||
432 | assert(a); | |
433 | assert(status <= 0); | |
434 | ||
435 | if (set_isempty(a->tokens)) | |
436 | return 0; | |
437 | ||
4cd1fbcc | 438 | if ((ioctl_fd = open_ioctl_fd(a->meta.manager->dev_autofs_fd, a->where, a->dev_id)) < 0) { |
8d567588 LP |
439 | r = ioctl_fd; |
440 | goto fail; | |
441 | } | |
442 | ||
443 | if (status) | |
444 | log_debug("Sending failure: %s", strerror(-status)); | |
445 | else | |
446 | log_debug("Sending success."); | |
447 | ||
e364ad06 LP |
448 | r = 0; |
449 | ||
8d567588 LP |
450 | /* Autofs thankfully does not hand out 0 as a token */ |
451 | while ((token = PTR_TO_UINT(set_steal_first(a->tokens)))) { | |
452 | int k; | |
453 | ||
454 | /* Autofs fun fact II: | |
455 | * | |
456 | * if you pass a positive status code here, the kernel will | |
457 | * freeze! Yay! */ | |
458 | ||
4cd1fbcc | 459 | if ((k = autofs_send_ready(a->meta.manager->dev_autofs_fd, |
8d567588 LP |
460 | ioctl_fd, |
461 | token, | |
462 | status)) < 0) | |
463 | r = k; | |
464 | } | |
465 | ||
8d567588 LP |
466 | fail: |
467 | if (ioctl_fd >= 0) | |
468 | close_nointr_nofail(ioctl_fd); | |
469 | ||
470 | return r; | |
471 | } | |
472 | ||
473 | static void automount_enter_waiting(Automount *a) { | |
474 | int p[2] = { -1, -1 }; | |
475 | char name[32], options[128]; | |
476 | bool mounted = false; | |
477 | int r, ioctl_fd = -1, dev_autofs_fd; | |
478 | struct stat st; | |
479 | ||
480 | assert(a); | |
481 | assert(a->pipe_fd < 0); | |
482 | assert(a->where); | |
483 | ||
484 | if (a->tokens) | |
485 | set_clear(a->tokens); | |
8d567588 | 486 | |
4cd1fbcc | 487 | if ((dev_autofs_fd = open_dev_autofs(a->meta.manager)) < 0) { |
8d567588 LP |
488 | r = dev_autofs_fd; |
489 | goto fail; | |
490 | } | |
491 | ||
492 | /* We knowingly ignore the results of this call */ | |
493 | mkdir_p(a->where, 0555); | |
494 | ||
a16e1123 | 495 | if (pipe2(p, O_NONBLOCK|O_CLOEXEC) < 0) { |
8d567588 LP |
496 | r = -errno; |
497 | goto fail; | |
498 | } | |
499 | ||
500 | snprintf(options, sizeof(options), "fd=%i,pgrp=%u,minproto=5,maxproto=5,direct", p[1], (unsigned) getpgrp()); | |
501 | char_array_0(options); | |
502 | ||
503 | snprintf(name, sizeof(name), "systemd-%u", (unsigned) getpid()); | |
504 | char_array_0(name); | |
505 | ||
506 | if (mount(name, a->where, "autofs", 0, options) < 0) { | |
507 | r = -errno; | |
508 | goto fail; | |
509 | } | |
510 | ||
511 | mounted = true; | |
512 | ||
513 | close_nointr_nofail(p[1]); | |
514 | p[1] = -1; | |
515 | ||
516 | if (stat(a->where, &st) < 0) { | |
517 | r = -errno; | |
518 | goto fail; | |
519 | } | |
520 | ||
521 | if ((ioctl_fd = open_ioctl_fd(dev_autofs_fd, a->where, st.st_dev)) < 0) { | |
522 | r = ioctl_fd; | |
523 | goto fail; | |
524 | } | |
525 | ||
526 | if ((r = autofs_protocol(dev_autofs_fd, ioctl_fd)) < 0) | |
527 | goto fail; | |
528 | ||
529 | if ((r = autofs_set_timeout(dev_autofs_fd, ioctl_fd, 300)) < 0) | |
530 | goto fail; | |
531 | ||
532 | /* Autofs fun fact: | |
533 | * | |
534 | * Unless we close the ioctl fd here, for some weird reason | |
535 | * the direct mount will not receive events from the | |
536 | * kernel. */ | |
537 | ||
538 | close_nointr_nofail(ioctl_fd); | |
539 | ioctl_fd = -1; | |
540 | ||
541 | if ((r = unit_watch_fd(UNIT(a), p[0], EPOLLIN, &a->pipe_watch)) < 0) | |
542 | goto fail; | |
543 | ||
544 | a->pipe_fd = p[0]; | |
545 | a->dev_id = st.st_dev; | |
546 | ||
547 | automount_set_state(a, AUTOMOUNT_WAITING); | |
548 | ||
549 | return; | |
550 | ||
551 | fail: | |
552 | assert_se(close_pipe(p) == 0); | |
553 | ||
554 | if (ioctl_fd >= 0) | |
555 | close_nointr_nofail(ioctl_fd); | |
556 | ||
557 | if (mounted) | |
558 | repeat_unmout(a->where); | |
559 | ||
560 | log_error("Failed to initialize automounter: %s", strerror(-r)); | |
561 | automount_enter_dead(a, false); | |
562 | } | |
563 | ||
564 | static void automount_enter_runnning(Automount *a) { | |
565 | int r; | |
566 | struct stat st; | |
398ef8ba | 567 | DBusError error; |
8d567588 LP |
568 | |
569 | assert(a); | |
570 | assert(a->mount); | |
571 | ||
398ef8ba LP |
572 | dbus_error_init(&error); |
573 | ||
ba3e67a7 LP |
574 | /* We don't take mount requests anymore if we are supposed to |
575 | * shut down anyway */ | |
5d909e3e LP |
576 | if (unit_pending_inactive(UNIT(a))) { |
577 | log_debug("Suppressing automount request on %s since unit stop is scheduled.", a->meta.id); | |
ba3e67a7 LP |
578 | automount_send_ready(a, -EHOSTDOWN); |
579 | return; | |
580 | } | |
581 | ||
1cf18f27 | 582 | mkdir_p(a->where, a->directory_mode); |
8d567588 | 583 | |
1cf18f27 LP |
584 | /* Before we do anything, let's see if somebody is playing games with us? */ |
585 | if (lstat(a->where, &st) < 0) { | |
8d567588 LP |
586 | log_warning("%s failed stat automount point: %m", a->meta.id); |
587 | goto fail; | |
588 | } | |
589 | ||
590 | if (!S_ISDIR(st.st_mode) || st.st_dev != a->dev_id) | |
591 | log_info("%s's automount point already active?", a->meta.id); | |
398ef8ba LP |
592 | else if ((r = manager_add_job(a->meta.manager, JOB_START, UNIT(a->mount), JOB_REPLACE, true, &error, NULL)) < 0) { |
593 | log_warning("%s failed to queue mount startup job: %s", a->meta.id, bus_error(&error, r)); | |
8d567588 LP |
594 | goto fail; |
595 | } | |
596 | ||
597 | automount_set_state(a, AUTOMOUNT_RUNNING); | |
598 | return; | |
599 | ||
600 | fail: | |
601 | automount_enter_dead(a, false); | |
398ef8ba | 602 | dbus_error_free(&error); |
8d567588 LP |
603 | } |
604 | ||
605 | static int automount_start(Unit *u) { | |
606 | Automount *a = AUTOMOUNT(u); | |
607 | ||
608 | assert(a); | |
609 | ||
74ac3cbd | 610 | assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_FAILED); |
01f78473 | 611 | |
8d567588 LP |
612 | if (path_is_mount_point(a->where)) { |
613 | log_error("Path %s is already a mount point, refusing start for %s", a->where, u->meta.id); | |
614 | return -EEXIST; | |
615 | } | |
616 | ||
01f78473 LP |
617 | if (a->mount->meta.load_state != UNIT_LOADED) |
618 | return -ENOENT; | |
8d567588 LP |
619 | |
620 | a->failure = false; | |
621 | automount_enter_waiting(a); | |
622 | return 0; | |
623 | } | |
624 | ||
625 | static int automount_stop(Unit *u) { | |
626 | Automount *a = AUTOMOUNT(u); | |
627 | ||
628 | assert(a); | |
629 | ||
630 | assert(a->state == AUTOMOUNT_WAITING || a->state == AUTOMOUNT_RUNNING); | |
631 | ||
632 | automount_enter_dead(a, true); | |
633 | return 0; | |
634 | } | |
635 | ||
a16e1123 LP |
636 | static int automount_serialize(Unit *u, FILE *f, FDSet *fds) { |
637 | Automount *a = AUTOMOUNT(u); | |
638 | void *p; | |
639 | Iterator i; | |
640 | ||
641 | assert(a); | |
642 | assert(f); | |
643 | assert(fds); | |
644 | ||
645 | unit_serialize_item(u, f, "state", automount_state_to_string(a->state)); | |
646 | unit_serialize_item(u, f, "failure", yes_no(a->failure)); | |
647 | unit_serialize_item_format(u, f, "dev-id", "%u", (unsigned) a->dev_id); | |
648 | ||
649 | SET_FOREACH(p, a->tokens, i) | |
650 | unit_serialize_item_format(u, f, "token", "%u", PTR_TO_UINT(p)); | |
651 | ||
652 | if (a->pipe_fd >= 0) { | |
653 | int copy; | |
654 | ||
655 | if ((copy = fdset_put_dup(fds, a->pipe_fd)) < 0) | |
656 | return copy; | |
657 | ||
658 | unit_serialize_item_format(u, f, "pipe-fd", "%i", copy); | |
659 | } | |
660 | ||
661 | return 0; | |
662 | } | |
663 | ||
664 | static int automount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
665 | Automount *a = AUTOMOUNT(u); | |
666 | int r; | |
667 | ||
668 | assert(a); | |
669 | assert(fds); | |
670 | ||
671 | if (streq(key, "state")) { | |
672 | AutomountState state; | |
673 | ||
674 | if ((state = automount_state_from_string(value)) < 0) | |
675 | log_debug("Failed to parse state value %s", value); | |
676 | else | |
677 | a->deserialized_state = state; | |
678 | } else if (streq(key, "failure")) { | |
679 | int b; | |
680 | ||
681 | if ((b = parse_boolean(value)) < 0) | |
682 | log_debug("Failed to parse failure value %s", value); | |
683 | else | |
684 | a->failure = b || a->failure; | |
685 | } else if (streq(key, "dev-id")) { | |
686 | unsigned d; | |
687 | ||
688 | if (safe_atou(value, &d) < 0) | |
689 | log_debug("Failed to parse dev-id value %s", value); | |
690 | else | |
691 | a->dev_id = (unsigned) d; | |
692 | } else if (streq(key, "token")) { | |
693 | unsigned token; | |
694 | ||
695 | if (safe_atou(value, &token) < 0) | |
696 | log_debug("Failed to parse token value %s", value); | |
697 | else { | |
698 | if (!a->tokens) | |
699 | if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func))) | |
700 | return -ENOMEM; | |
701 | ||
702 | if ((r = set_put(a->tokens, UINT_TO_PTR(token))) < 0) | |
703 | return r; | |
704 | } | |
705 | } else if (streq(key, "pipe-fd")) { | |
706 | int fd; | |
707 | ||
708 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
709 | log_debug("Failed to parse pipe-fd value %s", value); | |
710 | else { | |
711 | if (a->pipe_fd >= 0) | |
712 | close_nointr_nofail(a->pipe_fd); | |
713 | ||
714 | a->pipe_fd = fdset_remove(fds, fd); | |
715 | } | |
716 | } else | |
717 | log_debug("Unknown serialization key '%s'", key); | |
718 | ||
719 | return 0; | |
720 | } | |
721 | ||
87f0e418 | 722 | static UnitActiveState automount_active_state(Unit *u) { |
a16e1123 | 723 | assert(u); |
87f0e418 | 724 | |
e537352b | 725 | return state_translation_table[AUTOMOUNT(u)->state]; |
5cb5a6ff LP |
726 | } |
727 | ||
10a94420 LP |
728 | static const char *automount_sub_state_to_string(Unit *u) { |
729 | assert(u); | |
730 | ||
a16e1123 | 731 | return automount_state_to_string(AUTOMOUNT(u)->state); |
10a94420 LP |
732 | } |
733 | ||
701cc384 LP |
734 | static bool automount_check_gc(Unit *u) { |
735 | Automount *a = AUTOMOUNT(u); | |
736 | ||
737 | assert(a); | |
738 | ||
7573916f LP |
739 | if (!a->mount) |
740 | return false; | |
741 | ||
701cc384 LP |
742 | return UNIT_VTABLE(UNIT(a->mount))->check_gc(UNIT(a->mount)); |
743 | } | |
744 | ||
8d567588 | 745 | static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) { |
701cc384 | 746 | Automount *a = AUTOMOUNT(u); |
8d567588 LP |
747 | union autofs_v5_packet_union packet; |
748 | ssize_t l; | |
749 | int r; | |
750 | ||
8d567588 LP |
751 | assert(a); |
752 | assert(fd == a->pipe_fd); | |
753 | ||
754 | if (events != EPOLLIN) { | |
755 | log_error("Got invalid poll event on pipe."); | |
756 | goto fail; | |
757 | } | |
758 | ||
eb22ac37 | 759 | if ((l = loop_read(a->pipe_fd, &packet, sizeof(packet), true)) != sizeof(packet)) { |
8d567588 LP |
760 | log_error("Invalid read from pipe: %s", l < 0 ? strerror(-l) : "short read"); |
761 | goto fail; | |
762 | } | |
763 | ||
764 | switch (packet.hdr.type) { | |
765 | ||
766 | case autofs_ptype_missing_direct: | |
941a4041 LP |
767 | |
768 | if (packet.v5_packet.pid > 0) { | |
769 | char *p = NULL; | |
770 | ||
771 | get_process_name(packet.v5_packet.pid, &p); | |
772 | log_debug("Got direct mount request for %s, triggered by %lu (%s)", packet.v5_packet.name, (unsigned long) packet.v5_packet.pid, strna(p)); | |
773 | free(p); | |
774 | ||
775 | } else | |
776 | log_debug("Got direct mount request for %s", packet.v5_packet.name); | |
8d567588 | 777 | |
a16e1123 LP |
778 | if (!a->tokens) |
779 | if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func))) { | |
780 | log_error("Failed to allocate token set."); | |
781 | goto fail; | |
782 | } | |
783 | ||
8d567588 LP |
784 | if ((r = set_put(a->tokens, UINT_TO_PTR(packet.v5_packet.wait_queue_token))) < 0) { |
785 | log_error("Failed to remember token: %s", strerror(-r)); | |
786 | goto fail; | |
787 | } | |
788 | ||
789 | automount_enter_runnning(a); | |
790 | break; | |
791 | ||
792 | default: | |
793 | log_error("Received unknown automount request %i", packet.hdr.type); | |
794 | break; | |
795 | } | |
796 | ||
797 | return; | |
798 | ||
799 | fail: | |
800 | automount_enter_dead(a, false); | |
801 | } | |
802 | ||
803 | static void automount_shutdown(Manager *m) { | |
804 | assert(m); | |
805 | ||
806 | if (m->dev_autofs_fd >= 0) | |
807 | close_nointr_nofail(m->dev_autofs_fd); | |
808 | } | |
809 | ||
74ac3cbd | 810 | static void automount_reset_failed(Unit *u) { |
5632e374 LP |
811 | Automount *a = AUTOMOUNT(u); |
812 | ||
813 | assert(a); | |
814 | ||
74ac3cbd | 815 | if (a->state == AUTOMOUNT_FAILED) |
5632e374 LP |
816 | automount_set_state(a, AUTOMOUNT_DEAD); |
817 | ||
818 | a->failure = false; | |
819 | } | |
820 | ||
a16e1123 LP |
821 | static const char* const automount_state_table[_AUTOMOUNT_STATE_MAX] = { |
822 | [AUTOMOUNT_DEAD] = "dead", | |
823 | [AUTOMOUNT_WAITING] = "waiting", | |
824 | [AUTOMOUNT_RUNNING] = "running", | |
74ac3cbd | 825 | [AUTOMOUNT_FAILED] = "failed" |
a16e1123 LP |
826 | }; |
827 | ||
828 | DEFINE_STRING_TABLE_LOOKUP(automount_state, AutomountState); | |
829 | ||
87f0e418 | 830 | const UnitVTable automount_vtable = { |
8d567588 | 831 | .suffix = ".automount", |
5cb5a6ff | 832 | |
e537352b | 833 | .no_alias = true, |
8d567588 | 834 | .no_instances = true, |
e537352b | 835 | |
034c6ed7 | 836 | .init = automount_init, |
e537352b | 837 | .load = automount_load, |
034c6ed7 | 838 | .done = automount_done, |
5cb5a6ff | 839 | |
a16e1123 LP |
840 | .coldplug = automount_coldplug, |
841 | ||
034c6ed7 | 842 | .dump = automount_dump, |
5cb5a6ff | 843 | |
8d567588 LP |
844 | .start = automount_start, |
845 | .stop = automount_stop, | |
846 | ||
a16e1123 LP |
847 | .serialize = automount_serialize, |
848 | .deserialize_item = automount_deserialize_item, | |
849 | ||
10a94420 | 850 | .active_state = automount_active_state, |
8d567588 LP |
851 | .sub_state_to_string = automount_sub_state_to_string, |
852 | ||
701cc384 LP |
853 | .check_gc = automount_check_gc, |
854 | ||
8d567588 LP |
855 | .fd_event = automount_fd_event, |
856 | ||
74ac3cbd | 857 | .reset_failed = automount_reset_failed, |
5632e374 | 858 | |
c4e2ceae | 859 | .bus_interface = "org.freedesktop.systemd1.Automount", |
4139c1b2 LP |
860 | .bus_message_handler = bus_automount_message_handler, |
861 | ||
8d567588 | 862 | .shutdown = automount_shutdown |
5cb5a6ff | 863 | }; |