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