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