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