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