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