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