]>
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, | |
43 | [AUTOMOUNT_MAINTAINANCE] = UNIT_INACTIVE, | |
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 | ||
267 | automount_set_state(a, a->failure ? AUTOMOUNT_MAINTAINANCE : AUTOMOUNT_DEAD); | |
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) { |
ca9a0317 LP |
279 | |
280 | if (errno == ENOENT || errno == ENODEV) { | |
281 | log_error("Your kernel apparently lacks built-in autofs4 support. Please fix that. " | |
282 | "We'll now try to work around this by calling 'modprobe autofs4'..."); | |
283 | system("/sbin/modprobe -q -- autofs4"); | |
284 | m->dev_autofs_fd = open("/dev/autofs", O_CLOEXEC|O_RDONLY); | |
285 | } | |
286 | ||
287 | if (m->dev_autofs_fd < 0) { | |
288 | log_error("Failed to open /dev/autofs: %s", strerror(errno)); | |
289 | return -errno; | |
290 | } | |
8d567588 LP |
291 | } |
292 | ||
293 | init_autofs_dev_ioctl(¶m); | |
294 | if (ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, ¶m) < 0) { | |
295 | close_nointr_nofail(m->dev_autofs_fd); | |
296 | m->dev_autofs_fd = -1; | |
297 | return -errno; | |
298 | } | |
299 | ||
300 | log_debug("Autofs kernel version %i.%i", param.ver_major, param.ver_minor); | |
301 | ||
302 | return m->dev_autofs_fd; | |
303 | } | |
304 | ||
305 | static int open_ioctl_fd(int dev_autofs_fd, const char *where, dev_t devid) { | |
306 | struct autofs_dev_ioctl *param; | |
307 | size_t l; | |
308 | int r; | |
309 | ||
310 | assert(dev_autofs_fd >= 0); | |
311 | assert(where); | |
312 | ||
313 | l = sizeof(struct autofs_dev_ioctl) + strlen(where) + 1; | |
314 | ||
315 | if (!(param = malloc(l))) | |
316 | return -ENOMEM; | |
317 | ||
318 | init_autofs_dev_ioctl(param); | |
319 | param->size = l; | |
320 | param->ioctlfd = -1; | |
321 | param->openmount.devid = devid; | |
322 | strcpy(param->path, where); | |
323 | ||
324 | if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_OPENMOUNT, param) < 0) { | |
325 | r = -errno; | |
326 | goto finish; | |
327 | } | |
328 | ||
329 | if (param->ioctlfd < 0) { | |
330 | r = -EIO; | |
331 | goto finish; | |
332 | } | |
333 | ||
a16e1123 | 334 | fd_cloexec(param->ioctlfd, true); |
8d567588 LP |
335 | r = param->ioctlfd; |
336 | ||
337 | finish: | |
338 | free(param); | |
339 | return r; | |
340 | } | |
341 | ||
342 | static int autofs_protocol(int dev_autofs_fd, int ioctl_fd) { | |
343 | uint32_t major, minor; | |
344 | struct autofs_dev_ioctl param; | |
345 | ||
346 | assert(dev_autofs_fd >= 0); | |
347 | assert(ioctl_fd >= 0); | |
348 | ||
349 | init_autofs_dev_ioctl(¶m); | |
350 | param.ioctlfd = ioctl_fd; | |
351 | ||
352 | if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOVER, ¶m) < 0) | |
353 | return -errno; | |
354 | ||
355 | major = param.protover.version; | |
356 | ||
357 | init_autofs_dev_ioctl(¶m); | |
358 | param.ioctlfd = ioctl_fd; | |
359 | ||
360 | if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOSUBVER, ¶m) < 0) | |
361 | return -errno; | |
362 | ||
363 | minor = param.protosubver.sub_version; | |
364 | ||
365 | log_debug("Autofs protocol version %i.%i", major, minor); | |
366 | return 0; | |
367 | } | |
368 | ||
369 | static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, time_t sec) { | |
370 | struct autofs_dev_ioctl param; | |
371 | ||
372 | assert(dev_autofs_fd >= 0); | |
373 | assert(ioctl_fd >= 0); | |
374 | ||
375 | init_autofs_dev_ioctl(¶m); | |
376 | param.ioctlfd = ioctl_fd; | |
377 | param.timeout.timeout = sec; | |
378 | ||
379 | if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, ¶m) < 0) | |
380 | return -errno; | |
381 | ||
382 | return 0; | |
383 | } | |
384 | ||
385 | static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, int status) { | |
386 | struct autofs_dev_ioctl param; | |
387 | ||
388 | assert(dev_autofs_fd >= 0); | |
389 | assert(ioctl_fd >= 0); | |
390 | ||
391 | init_autofs_dev_ioctl(¶m); | |
392 | param.ioctlfd = ioctl_fd; | |
393 | ||
394 | if (status) { | |
395 | param.fail.token = token; | |
396 | param.fail.status = status; | |
397 | } else | |
398 | param.ready.token = token; | |
399 | ||
400 | if (ioctl(dev_autofs_fd, status ? AUTOFS_DEV_IOCTL_FAIL : AUTOFS_DEV_IOCTL_READY, ¶m) < 0) | |
401 | return -errno; | |
402 | ||
403 | return 0; | |
404 | } | |
405 | ||
406 | int automount_send_ready(Automount *a, int status) { | |
407 | int ioctl_fd, r; | |
408 | unsigned token; | |
409 | ||
410 | assert(a); | |
411 | assert(status <= 0); | |
412 | ||
413 | if (set_isempty(a->tokens)) | |
414 | return 0; | |
415 | ||
416 | if ((ioctl_fd = open_ioctl_fd(UNIT(a)->meta.manager->dev_autofs_fd, a->where, a->dev_id)) < 0) { | |
417 | r = ioctl_fd; | |
418 | goto fail; | |
419 | } | |
420 | ||
421 | if (status) | |
422 | log_debug("Sending failure: %s", strerror(-status)); | |
423 | else | |
424 | log_debug("Sending success."); | |
425 | ||
426 | /* Autofs thankfully does not hand out 0 as a token */ | |
427 | while ((token = PTR_TO_UINT(set_steal_first(a->tokens)))) { | |
428 | int k; | |
429 | ||
430 | /* Autofs fun fact II: | |
431 | * | |
432 | * if you pass a positive status code here, the kernel will | |
433 | * freeze! Yay! */ | |
434 | ||
435 | if ((k = autofs_send_ready(UNIT(a)->meta.manager->dev_autofs_fd, | |
436 | ioctl_fd, | |
437 | token, | |
438 | status)) < 0) | |
439 | r = k; | |
440 | } | |
441 | ||
442 | r = 0; | |
443 | ||
444 | fail: | |
445 | if (ioctl_fd >= 0) | |
446 | close_nointr_nofail(ioctl_fd); | |
447 | ||
448 | return r; | |
449 | } | |
450 | ||
451 | static void automount_enter_waiting(Automount *a) { | |
452 | int p[2] = { -1, -1 }; | |
453 | char name[32], options[128]; | |
454 | bool mounted = false; | |
455 | int r, ioctl_fd = -1, dev_autofs_fd; | |
456 | struct stat st; | |
457 | ||
458 | assert(a); | |
459 | assert(a->pipe_fd < 0); | |
460 | assert(a->where); | |
461 | ||
462 | if (a->tokens) | |
463 | set_clear(a->tokens); | |
8d567588 LP |
464 | |
465 | if ((dev_autofs_fd = open_dev_autofs(UNIT(a)->meta.manager)) < 0) { | |
466 | r = dev_autofs_fd; | |
467 | goto fail; | |
468 | } | |
469 | ||
470 | /* We knowingly ignore the results of this call */ | |
471 | mkdir_p(a->where, 0555); | |
472 | ||
a16e1123 | 473 | if (pipe2(p, O_NONBLOCK|O_CLOEXEC) < 0) { |
8d567588 LP |
474 | r = -errno; |
475 | goto fail; | |
476 | } | |
477 | ||
478 | snprintf(options, sizeof(options), "fd=%i,pgrp=%u,minproto=5,maxproto=5,direct", p[1], (unsigned) getpgrp()); | |
479 | char_array_0(options); | |
480 | ||
481 | snprintf(name, sizeof(name), "systemd-%u", (unsigned) getpid()); | |
482 | char_array_0(name); | |
483 | ||
484 | if (mount(name, a->where, "autofs", 0, options) < 0) { | |
485 | r = -errno; | |
486 | goto fail; | |
487 | } | |
488 | ||
489 | mounted = true; | |
490 | ||
491 | close_nointr_nofail(p[1]); | |
492 | p[1] = -1; | |
493 | ||
494 | if (stat(a->where, &st) < 0) { | |
495 | r = -errno; | |
496 | goto fail; | |
497 | } | |
498 | ||
499 | if ((ioctl_fd = open_ioctl_fd(dev_autofs_fd, a->where, st.st_dev)) < 0) { | |
500 | r = ioctl_fd; | |
501 | goto fail; | |
502 | } | |
503 | ||
504 | if ((r = autofs_protocol(dev_autofs_fd, ioctl_fd)) < 0) | |
505 | goto fail; | |
506 | ||
507 | if ((r = autofs_set_timeout(dev_autofs_fd, ioctl_fd, 300)) < 0) | |
508 | goto fail; | |
509 | ||
510 | /* Autofs fun fact: | |
511 | * | |
512 | * Unless we close the ioctl fd here, for some weird reason | |
513 | * the direct mount will not receive events from the | |
514 | * kernel. */ | |
515 | ||
516 | close_nointr_nofail(ioctl_fd); | |
517 | ioctl_fd = -1; | |
518 | ||
519 | if ((r = unit_watch_fd(UNIT(a), p[0], EPOLLIN, &a->pipe_watch)) < 0) | |
520 | goto fail; | |
521 | ||
522 | a->pipe_fd = p[0]; | |
523 | a->dev_id = st.st_dev; | |
524 | ||
525 | automount_set_state(a, AUTOMOUNT_WAITING); | |
526 | ||
527 | return; | |
528 | ||
529 | fail: | |
530 | assert_se(close_pipe(p) == 0); | |
531 | ||
532 | if (ioctl_fd >= 0) | |
533 | close_nointr_nofail(ioctl_fd); | |
534 | ||
535 | if (mounted) | |
536 | repeat_unmout(a->where); | |
537 | ||
538 | log_error("Failed to initialize automounter: %s", strerror(-r)); | |
539 | automount_enter_dead(a, false); | |
540 | } | |
541 | ||
542 | static void automount_enter_runnning(Automount *a) { | |
543 | int r; | |
544 | struct stat st; | |
545 | ||
546 | assert(a); | |
547 | assert(a->mount); | |
548 | ||
549 | /* Before we do anything, let's see if somebody is playing games with us? */ | |
550 | ||
551 | if (stat(a->where, &st) < 0) { | |
552 | log_warning("%s failed stat automount point: %m", a->meta.id); | |
553 | goto fail; | |
554 | } | |
555 | ||
556 | if (!S_ISDIR(st.st_mode) || st.st_dev != a->dev_id) | |
557 | log_info("%s's automount point already active?", a->meta.id); | |
558 | else if ((r = manager_add_job(UNIT(a)->meta.manager, JOB_START, UNIT(a->mount), JOB_REPLACE, true, NULL)) < 0) { | |
559 | log_warning("%s failed to queue mount startup job: %s", a->meta.id, strerror(-r)); | |
560 | goto fail; | |
561 | } | |
562 | ||
563 | automount_set_state(a, AUTOMOUNT_RUNNING); | |
564 | return; | |
565 | ||
566 | fail: | |
567 | automount_enter_dead(a, false); | |
568 | } | |
569 | ||
570 | static int automount_start(Unit *u) { | |
571 | Automount *a = AUTOMOUNT(u); | |
572 | ||
573 | assert(a); | |
574 | ||
575 | if (path_is_mount_point(a->where)) { | |
576 | log_error("Path %s is already a mount point, refusing start for %s", a->where, u->meta.id); | |
577 | return -EEXIST; | |
578 | } | |
579 | ||
580 | assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_MAINTAINANCE); | |
581 | ||
582 | a->failure = false; | |
583 | automount_enter_waiting(a); | |
584 | return 0; | |
585 | } | |
586 | ||
587 | static int automount_stop(Unit *u) { | |
588 | Automount *a = AUTOMOUNT(u); | |
589 | ||
590 | assert(a); | |
591 | ||
592 | assert(a->state == AUTOMOUNT_WAITING || a->state == AUTOMOUNT_RUNNING); | |
593 | ||
594 | automount_enter_dead(a, true); | |
595 | return 0; | |
596 | } | |
597 | ||
a16e1123 LP |
598 | static int automount_serialize(Unit *u, FILE *f, FDSet *fds) { |
599 | Automount *a = AUTOMOUNT(u); | |
600 | void *p; | |
601 | Iterator i; | |
602 | ||
603 | assert(a); | |
604 | assert(f); | |
605 | assert(fds); | |
606 | ||
607 | unit_serialize_item(u, f, "state", automount_state_to_string(a->state)); | |
608 | unit_serialize_item(u, f, "failure", yes_no(a->failure)); | |
609 | unit_serialize_item_format(u, f, "dev-id", "%u", (unsigned) a->dev_id); | |
610 | ||
611 | SET_FOREACH(p, a->tokens, i) | |
612 | unit_serialize_item_format(u, f, "token", "%u", PTR_TO_UINT(p)); | |
613 | ||
614 | if (a->pipe_fd >= 0) { | |
615 | int copy; | |
616 | ||
617 | if ((copy = fdset_put_dup(fds, a->pipe_fd)) < 0) | |
618 | return copy; | |
619 | ||
620 | unit_serialize_item_format(u, f, "pipe-fd", "%i", copy); | |
621 | } | |
622 | ||
623 | return 0; | |
624 | } | |
625 | ||
626 | static int automount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
627 | Automount *a = AUTOMOUNT(u); | |
628 | int r; | |
629 | ||
630 | assert(a); | |
631 | assert(fds); | |
632 | ||
633 | if (streq(key, "state")) { | |
634 | AutomountState state; | |
635 | ||
636 | if ((state = automount_state_from_string(value)) < 0) | |
637 | log_debug("Failed to parse state value %s", value); | |
638 | else | |
639 | a->deserialized_state = state; | |
640 | } else if (streq(key, "failure")) { | |
641 | int b; | |
642 | ||
643 | if ((b = parse_boolean(value)) < 0) | |
644 | log_debug("Failed to parse failure value %s", value); | |
645 | else | |
646 | a->failure = b || a->failure; | |
647 | } else if (streq(key, "dev-id")) { | |
648 | unsigned d; | |
649 | ||
650 | if (safe_atou(value, &d) < 0) | |
651 | log_debug("Failed to parse dev-id value %s", value); | |
652 | else | |
653 | a->dev_id = (unsigned) d; | |
654 | } else if (streq(key, "token")) { | |
655 | unsigned token; | |
656 | ||
657 | if (safe_atou(value, &token) < 0) | |
658 | log_debug("Failed to parse token value %s", value); | |
659 | else { | |
660 | if (!a->tokens) | |
661 | if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func))) | |
662 | return -ENOMEM; | |
663 | ||
664 | if ((r = set_put(a->tokens, UINT_TO_PTR(token))) < 0) | |
665 | return r; | |
666 | } | |
667 | } else if (streq(key, "pipe-fd")) { | |
668 | int fd; | |
669 | ||
670 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
671 | log_debug("Failed to parse pipe-fd value %s", value); | |
672 | else { | |
673 | if (a->pipe_fd >= 0) | |
674 | close_nointr_nofail(a->pipe_fd); | |
675 | ||
676 | a->pipe_fd = fdset_remove(fds, fd); | |
677 | } | |
678 | } else | |
679 | log_debug("Unknown serialization key '%s'", key); | |
680 | ||
681 | return 0; | |
682 | } | |
683 | ||
87f0e418 | 684 | static UnitActiveState automount_active_state(Unit *u) { |
a16e1123 | 685 | assert(u); |
87f0e418 | 686 | |
e537352b | 687 | return state_translation_table[AUTOMOUNT(u)->state]; |
5cb5a6ff LP |
688 | } |
689 | ||
10a94420 LP |
690 | static const char *automount_sub_state_to_string(Unit *u) { |
691 | assert(u); | |
692 | ||
a16e1123 | 693 | return automount_state_to_string(AUTOMOUNT(u)->state); |
10a94420 LP |
694 | } |
695 | ||
701cc384 LP |
696 | static bool automount_check_gc(Unit *u) { |
697 | Automount *a = AUTOMOUNT(u); | |
698 | ||
699 | assert(a); | |
700 | ||
701 | return UNIT_VTABLE(UNIT(a->mount))->check_gc(UNIT(a->mount)); | |
702 | } | |
703 | ||
8d567588 | 704 | static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) { |
701cc384 | 705 | Automount *a = AUTOMOUNT(u); |
8d567588 LP |
706 | union autofs_v5_packet_union packet; |
707 | ssize_t l; | |
708 | int r; | |
709 | ||
8d567588 LP |
710 | assert(a); |
711 | assert(fd == a->pipe_fd); | |
712 | ||
713 | if (events != EPOLLIN) { | |
714 | log_error("Got invalid poll event on pipe."); | |
715 | goto fail; | |
716 | } | |
717 | ||
718 | if ((l = loop_read(a->pipe_fd, &packet, sizeof(packet))) != sizeof(packet)) { | |
719 | log_error("Invalid read from pipe: %s", l < 0 ? strerror(-l) : "short read"); | |
720 | goto fail; | |
721 | } | |
722 | ||
723 | switch (packet.hdr.type) { | |
724 | ||
725 | case autofs_ptype_missing_direct: | |
726 | log_debug("Got direct mount request for %s", packet.v5_packet.name); | |
727 | ||
a16e1123 LP |
728 | if (!a->tokens) |
729 | if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func))) { | |
730 | log_error("Failed to allocate token set."); | |
731 | goto fail; | |
732 | } | |
733 | ||
8d567588 LP |
734 | if ((r = set_put(a->tokens, UINT_TO_PTR(packet.v5_packet.wait_queue_token))) < 0) { |
735 | log_error("Failed to remember token: %s", strerror(-r)); | |
736 | goto fail; | |
737 | } | |
738 | ||
739 | automount_enter_runnning(a); | |
740 | break; | |
741 | ||
742 | default: | |
743 | log_error("Received unknown automount request %i", packet.hdr.type); | |
744 | break; | |
745 | } | |
746 | ||
747 | return; | |
748 | ||
749 | fail: | |
750 | automount_enter_dead(a, false); | |
751 | } | |
752 | ||
753 | static void automount_shutdown(Manager *m) { | |
754 | assert(m); | |
755 | ||
756 | if (m->dev_autofs_fd >= 0) | |
757 | close_nointr_nofail(m->dev_autofs_fd); | |
758 | } | |
759 | ||
a16e1123 LP |
760 | static const char* const automount_state_table[_AUTOMOUNT_STATE_MAX] = { |
761 | [AUTOMOUNT_DEAD] = "dead", | |
762 | [AUTOMOUNT_WAITING] = "waiting", | |
763 | [AUTOMOUNT_RUNNING] = "running", | |
764 | [AUTOMOUNT_MAINTAINANCE] = "maintainance" | |
765 | }; | |
766 | ||
767 | DEFINE_STRING_TABLE_LOOKUP(automount_state, AutomountState); | |
768 | ||
87f0e418 | 769 | const UnitVTable automount_vtable = { |
8d567588 | 770 | .suffix = ".automount", |
5cb5a6ff | 771 | |
e537352b | 772 | .no_alias = true, |
8d567588 | 773 | .no_instances = true, |
e537352b | 774 | |
034c6ed7 | 775 | .init = automount_init, |
e537352b | 776 | .load = automount_load, |
034c6ed7 | 777 | .done = automount_done, |
5cb5a6ff | 778 | |
a16e1123 LP |
779 | .coldplug = automount_coldplug, |
780 | ||
034c6ed7 | 781 | .dump = automount_dump, |
5cb5a6ff | 782 | |
8d567588 LP |
783 | .start = automount_start, |
784 | .stop = automount_stop, | |
785 | ||
a16e1123 LP |
786 | .serialize = automount_serialize, |
787 | .deserialize_item = automount_deserialize_item, | |
788 | ||
10a94420 | 789 | .active_state = automount_active_state, |
8d567588 LP |
790 | .sub_state_to_string = automount_sub_state_to_string, |
791 | ||
701cc384 LP |
792 | .check_gc = automount_check_gc, |
793 | ||
8d567588 LP |
794 | .fd_event = automount_fd_event, |
795 | ||
4139c1b2 LP |
796 | .bus_message_handler = bus_automount_message_handler, |
797 | ||
8d567588 | 798 | .shutdown = automount_shutdown |
5cb5a6ff | 799 | }; |