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