]> git.ipfire.org Git - people/ms/systemd.git/blame - automount.c
cgroup: if we are already in our own cgroup, then reuse it
[people/ms/systemd.git] / automount.c
CommitLineData
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
39static 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
e537352b
LP
46static const char* const state_string_table[_AUTOMOUNT_STATE_MAX] = {
47 [AUTOMOUNT_DEAD] = "dead",
48 [AUTOMOUNT_WAITING] = "waiting",
49 [AUTOMOUNT_RUNNING] = "running",
50 [AUTOMOUNT_MAINTAINANCE] = "maintainance"
51};
5cb5a6ff 52
8d567588
LP
53static char *automount_name_from_where(const char *where) {
54 assert(where);
55
56 if (streq(where, "/"))
57 return strdup("-.automount");
58
59 return unit_name_build_escape(where+1, NULL, ".automount");
60}
61
e537352b
LP
62static void automount_init(Unit *u) {
63 Automount *a = AUTOMOUNT(u);
5cb5a6ff 64
8d567588
LP
65 assert(u);
66 assert(u->meta.load_state == UNIT_STUB);
67
68 a->pipe_watch.fd = a->pipe_fd = -1;
69}
70
71static void repeat_unmout(const char *path) {
72 assert(path);
73
74 for (;;) {
75
76 if (umount2(path, MNT_DETACH) >= 0)
77 continue;
78
79 if (errno != EINVAL)
80 log_error("Failed to unmount: %m");
81
82 break;
83 }
84}
85
86static void unmount_autofs(Automount *a) {
87 assert(a);
88
89 if (a->pipe_fd < 0)
90 return;
91
92 automount_send_ready(a, -EHOSTDOWN);
93
94 unit_unwatch_fd(UNIT(a), &a->pipe_watch);
95 close_nointr_nofail(a->pipe_fd);
96 a->pipe_fd = -1;
97
98 repeat_unmout(a->where);
99}
100
101static void automount_done(Unit *u) {
102 Automount *a = AUTOMOUNT(u);
103
104 assert(a);
105
106 unmount_autofs(a);
e537352b 107 a->mount = NULL;
8d567588
LP
108
109 if (a->tokens) {
110 set_free(a->tokens);
111 a->tokens = NULL;
112 }
113}
114
115static int automount_verify(Automount *a) {
116 bool b;
117 char *e;
118 assert(a);
119
120 if (UNIT(a)->meta.load_state != UNIT_LOADED)
121 return 0;
122
123 if (!a->where) {
124 log_error("%s lacks Where setting. Refusing.", UNIT(a)->meta.id);
125 return -EINVAL;
126 }
127
128 path_kill_slashes(a->where);
129
130 if (!(e = automount_name_from_where(a->where)))
131 return -ENOMEM;
132
133 b = unit_has_name(UNIT(a), e);
134 free(e);
135
136 if (!b) {
137 log_error("%s's Where setting doesn't match unit name. Refusing.", UNIT(a)->meta.id);
138 return -EINVAL;
139 }
140
141 return 0;
e537352b 142}
5cb5a6ff 143
e537352b
LP
144static int automount_load(Unit *u) {
145 int r;
146 Automount *a = AUTOMOUNT(u);
23a177ef 147
e537352b
LP
148 assert(u);
149 assert(u->meta.load_state == UNIT_STUB);
5cb5a6ff 150
e537352b
LP
151 /* Load a .automount file */
152 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
153 return r;
23a177ef 154
e537352b 155 if (u->meta.load_state == UNIT_LOADED) {
23a177ef 156
e537352b 157 if ((r = unit_load_related_unit(u, ".mount", (Unit**) &a->mount)) < 0)
23a177ef
LP
158 return r;
159
e537352b 160 if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(a->mount))) < 0)
23a177ef
LP
161 return r;
162 }
163
8d567588 164 return automount_verify(a);
5cb5a6ff
LP
165}
166
8d567588
LP
167static void automount_set_state(Automount *a, AutomountState state) {
168 AutomountState old_state;
e537352b 169 assert(a);
034c6ed7 170
8d567588
LP
171 old_state = a->state;
172 a->state = state;
173
174 if (state != AUTOMOUNT_WAITING &&
175 state != AUTOMOUNT_RUNNING)
176 unmount_autofs(a);
177
178 if (state != old_state)
179 log_debug("%s changed %s → %s", UNIT(a)->meta.id, state_string_table[old_state], state_string_table[state]);
180
181 unit_notify(UNIT(a), state_translation_table[old_state], state_translation_table[state]);
034c6ed7
LP
182}
183
87f0e418 184static void automount_dump(Unit *u, FILE *f, const char *prefix) {
87f0e418 185 Automount *s = AUTOMOUNT(u);
5cb5a6ff
LP
186
187 assert(s);
188
189 fprintf(f,
e537352b
LP
190 "%sAutomount State: %s\n",
191 prefix, state_string_table[s->state]);
5cb5a6ff
LP
192}
193
8d567588
LP
194static void automount_enter_dead(Automount *a, bool success) {
195 assert(a);
196
197 if (!success)
198 a->failure = true;
199
200 automount_set_state(a, a->failure ? AUTOMOUNT_MAINTAINANCE : AUTOMOUNT_DEAD);
201}
202
203static int open_dev_autofs(Manager *m) {
204 struct autofs_dev_ioctl param;
205
206 assert(m);
207
208 if (m->dev_autofs_fd >= 0)
209 return m->dev_autofs_fd;
210
211 if ((m->dev_autofs_fd = open("/dev/autofs", O_RDONLY)) < 0) {
212 log_error("Failed to open /dev/autofs: %s", strerror(errno));
213 return -errno;
214 }
215
216 init_autofs_dev_ioctl(&param);
217 if (ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, &param) < 0) {
218 close_nointr_nofail(m->dev_autofs_fd);
219 m->dev_autofs_fd = -1;
220 return -errno;
221 }
222
223 log_debug("Autofs kernel version %i.%i", param.ver_major, param.ver_minor);
224
225 return m->dev_autofs_fd;
226}
227
228static int open_ioctl_fd(int dev_autofs_fd, const char *where, dev_t devid) {
229 struct autofs_dev_ioctl *param;
230 size_t l;
231 int r;
232
233 assert(dev_autofs_fd >= 0);
234 assert(where);
235
236 l = sizeof(struct autofs_dev_ioctl) + strlen(where) + 1;
237
238 if (!(param = malloc(l)))
239 return -ENOMEM;
240
241 init_autofs_dev_ioctl(param);
242 param->size = l;
243 param->ioctlfd = -1;
244 param->openmount.devid = devid;
245 strcpy(param->path, where);
246
247 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_OPENMOUNT, param) < 0) {
248 r = -errno;
249 goto finish;
250 }
251
252 if (param->ioctlfd < 0) {
253 r = -EIO;
254 goto finish;
255 }
256
257 r = param->ioctlfd;
258
259finish:
260 free(param);
261 return r;
262}
263
264static int autofs_protocol(int dev_autofs_fd, int ioctl_fd) {
265 uint32_t major, minor;
266 struct autofs_dev_ioctl param;
267
268 assert(dev_autofs_fd >= 0);
269 assert(ioctl_fd >= 0);
270
271 init_autofs_dev_ioctl(&param);
272 param.ioctlfd = ioctl_fd;
273
274 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOVER, &param) < 0)
275 return -errno;
276
277 major = param.protover.version;
278
279 init_autofs_dev_ioctl(&param);
280 param.ioctlfd = ioctl_fd;
281
282 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_PROTOSUBVER, &param) < 0)
283 return -errno;
284
285 minor = param.protosubver.sub_version;
286
287 log_debug("Autofs protocol version %i.%i", major, minor);
288 return 0;
289}
290
291static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, time_t sec) {
292 struct autofs_dev_ioctl param;
293
294 assert(dev_autofs_fd >= 0);
295 assert(ioctl_fd >= 0);
296
297 init_autofs_dev_ioctl(&param);
298 param.ioctlfd = ioctl_fd;
299 param.timeout.timeout = sec;
300
301 if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, &param) < 0)
302 return -errno;
303
304 return 0;
305}
306
307static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, int status) {
308 struct autofs_dev_ioctl param;
309
310 assert(dev_autofs_fd >= 0);
311 assert(ioctl_fd >= 0);
312
313 init_autofs_dev_ioctl(&param);
314 param.ioctlfd = ioctl_fd;
315
316 if (status) {
317 param.fail.token = token;
318 param.fail.status = status;
319 } else
320 param.ready.token = token;
321
322 if (ioctl(dev_autofs_fd, status ? AUTOFS_DEV_IOCTL_FAIL : AUTOFS_DEV_IOCTL_READY, &param) < 0)
323 return -errno;
324
325 return 0;
326}
327
328int automount_send_ready(Automount *a, int status) {
329 int ioctl_fd, r;
330 unsigned token;
331
332 assert(a);
333 assert(status <= 0);
334
335 if (set_isempty(a->tokens))
336 return 0;
337
338 if ((ioctl_fd = open_ioctl_fd(UNIT(a)->meta.manager->dev_autofs_fd, a->where, a->dev_id)) < 0) {
339 r = ioctl_fd;
340 goto fail;
341 }
342
343 if (status)
344 log_debug("Sending failure: %s", strerror(-status));
345 else
346 log_debug("Sending success.");
347
348 /* Autofs thankfully does not hand out 0 as a token */
349 while ((token = PTR_TO_UINT(set_steal_first(a->tokens)))) {
350 int k;
351
352 /* Autofs fun fact II:
353 *
354 * if you pass a positive status code here, the kernel will
355 * freeze! Yay! */
356
357 if ((k = autofs_send_ready(UNIT(a)->meta.manager->dev_autofs_fd,
358 ioctl_fd,
359 token,
360 status)) < 0)
361 r = k;
362 }
363
364 r = 0;
365
366fail:
367 if (ioctl_fd >= 0)
368 close_nointr_nofail(ioctl_fd);
369
370 return r;
371}
372
373static void automount_enter_waiting(Automount *a) {
374 int p[2] = { -1, -1 };
375 char name[32], options[128];
376 bool mounted = false;
377 int r, ioctl_fd = -1, dev_autofs_fd;
378 struct stat st;
379
380 assert(a);
381 assert(a->pipe_fd < 0);
382 assert(a->where);
383
384 if (a->tokens)
385 set_clear(a->tokens);
386 else if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func))) {
387 r = -ENOMEM;
388 goto fail;
389 }
390
391 if ((dev_autofs_fd = open_dev_autofs(UNIT(a)->meta.manager)) < 0) {
392 r = dev_autofs_fd;
393 goto fail;
394 }
395
396 /* We knowingly ignore the results of this call */
397 mkdir_p(a->where, 0555);
398
399 if (pipe2(p, O_NONBLOCK) < 0) {
400 r = -errno;
401 goto fail;
402 }
403
404 snprintf(options, sizeof(options), "fd=%i,pgrp=%u,minproto=5,maxproto=5,direct", p[1], (unsigned) getpgrp());
405 char_array_0(options);
406
407 snprintf(name, sizeof(name), "systemd-%u", (unsigned) getpid());
408 char_array_0(name);
409
410 if (mount(name, a->where, "autofs", 0, options) < 0) {
411 r = -errno;
412 goto fail;
413 }
414
415 mounted = true;
416
417 close_nointr_nofail(p[1]);
418 p[1] = -1;
419
420 if (stat(a->where, &st) < 0) {
421 r = -errno;
422 goto fail;
423 }
424
425 if ((ioctl_fd = open_ioctl_fd(dev_autofs_fd, a->where, st.st_dev)) < 0) {
426 r = ioctl_fd;
427 goto fail;
428 }
429
430 if ((r = autofs_protocol(dev_autofs_fd, ioctl_fd)) < 0)
431 goto fail;
432
433 if ((r = autofs_set_timeout(dev_autofs_fd, ioctl_fd, 300)) < 0)
434 goto fail;
435
436 /* Autofs fun fact:
437 *
438 * Unless we close the ioctl fd here, for some weird reason
439 * the direct mount will not receive events from the
440 * kernel. */
441
442 close_nointr_nofail(ioctl_fd);
443 ioctl_fd = -1;
444
445 if ((r = unit_watch_fd(UNIT(a), p[0], EPOLLIN, &a->pipe_watch)) < 0)
446 goto fail;
447
448 a->pipe_fd = p[0];
449 a->dev_id = st.st_dev;
450
451 automount_set_state(a, AUTOMOUNT_WAITING);
452
453 return;
454
455fail:
456 assert_se(close_pipe(p) == 0);
457
458 if (ioctl_fd >= 0)
459 close_nointr_nofail(ioctl_fd);
460
461 if (mounted)
462 repeat_unmout(a->where);
463
464 log_error("Failed to initialize automounter: %s", strerror(-r));
465 automount_enter_dead(a, false);
466}
467
468static void automount_enter_runnning(Automount *a) {
469 int r;
470 struct stat st;
471
472 assert(a);
473 assert(a->mount);
474
475 /* Before we do anything, let's see if somebody is playing games with us? */
476
477 if (stat(a->where, &st) < 0) {
478 log_warning("%s failed stat automount point: %m", a->meta.id);
479 goto fail;
480 }
481
482 if (!S_ISDIR(st.st_mode) || st.st_dev != a->dev_id)
483 log_info("%s's automount point already active?", a->meta.id);
484 else if ((r = manager_add_job(UNIT(a)->meta.manager, JOB_START, UNIT(a->mount), JOB_REPLACE, true, NULL)) < 0) {
485 log_warning("%s failed to queue mount startup job: %s", a->meta.id, strerror(-r));
486 goto fail;
487 }
488
489 automount_set_state(a, AUTOMOUNT_RUNNING);
490 return;
491
492fail:
493 automount_enter_dead(a, false);
494}
495
496static int automount_start(Unit *u) {
497 Automount *a = AUTOMOUNT(u);
498
499 assert(a);
500
501 if (path_is_mount_point(a->where)) {
502 log_error("Path %s is already a mount point, refusing start for %s", a->where, u->meta.id);
503 return -EEXIST;
504 }
505
506 assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_MAINTAINANCE);
507
508 a->failure = false;
509 automount_enter_waiting(a);
510 return 0;
511}
512
513static int automount_stop(Unit *u) {
514 Automount *a = AUTOMOUNT(u);
515
516 assert(a);
517
518 assert(a->state == AUTOMOUNT_WAITING || a->state == AUTOMOUNT_RUNNING);
519
520 automount_enter_dead(a, true);
521 return 0;
522}
523
87f0e418
LP
524static UnitActiveState automount_active_state(Unit *u) {
525
e537352b 526 return state_translation_table[AUTOMOUNT(u)->state];
5cb5a6ff
LP
527}
528
10a94420
LP
529static const char *automount_sub_state_to_string(Unit *u) {
530 assert(u);
531
532 return state_string_table[AUTOMOUNT(u)->state];
533}
534
8d567588
LP
535static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
536 union autofs_v5_packet_union packet;
537 ssize_t l;
538 int r;
539
540 Automount *a = AUTOMOUNT(u);
541
542 assert(a);
543 assert(fd == a->pipe_fd);
544
545 if (events != EPOLLIN) {
546 log_error("Got invalid poll event on pipe.");
547 goto fail;
548 }
549
550 if ((l = loop_read(a->pipe_fd, &packet, sizeof(packet))) != sizeof(packet)) {
551 log_error("Invalid read from pipe: %s", l < 0 ? strerror(-l) : "short read");
552 goto fail;
553 }
554
555 switch (packet.hdr.type) {
556
557 case autofs_ptype_missing_direct:
558 log_debug("Got direct mount request for %s", packet.v5_packet.name);
559
560 if ((r = set_put(a->tokens, UINT_TO_PTR(packet.v5_packet.wait_queue_token))) < 0) {
561 log_error("Failed to remember token: %s", strerror(-r));
562 goto fail;
563 }
564
565 automount_enter_runnning(a);
566 break;
567
568 default:
569 log_error("Received unknown automount request %i", packet.hdr.type);
570 break;
571 }
572
573 return;
574
575fail:
576 automount_enter_dead(a, false);
577}
578
579static void automount_shutdown(Manager *m) {
580 assert(m);
581
582 if (m->dev_autofs_fd >= 0)
583 close_nointr_nofail(m->dev_autofs_fd);
584}
585
87f0e418 586const UnitVTable automount_vtable = {
8d567588 587 .suffix = ".automount",
5cb5a6ff 588
e537352b 589 .no_alias = true,
8d567588 590 .no_instances = true,
e537352b 591
034c6ed7 592 .init = automount_init,
e537352b 593 .load = automount_load,
034c6ed7 594 .done = automount_done,
5cb5a6ff 595
034c6ed7 596 .dump = automount_dump,
5cb5a6ff 597
8d567588
LP
598 .start = automount_start,
599 .stop = automount_stop,
600
10a94420 601 .active_state = automount_active_state,
8d567588
LP
602 .sub_state_to_string = automount_sub_state_to_string,
603
604 .fd_event = automount_fd_event,
605
4139c1b2
LP
606 .bus_message_handler = bus_automount_message_handler,
607
8d567588 608 .shutdown = automount_shutdown
5cb5a6ff 609};