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