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