2 * Copyright (c) 2014 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 #include <sys/param.h>
38 #include <sys/linker.h>
39 #include <sys/mount.h>
40 #include <sys/socket.h>
43 #include <sys/utsname.h>
64 unmount_by_statfs(const struct statfs *sb, bool force)
67 int error, ret, flags;
69 ret = asprintf(&fsid_str, "FSID:%d:%d",
70 sb->f_fsid.val[0], sb->f_fsid.val[1]);
72 log_err(1, "asprintf");
74 log_debugx("unmounting %s using %s", sb->f_mntonname, fsid_str);
79 error = unmount(fsid_str, flags);
82 log_warn("cannot unmount %s", sb->f_mntonname);
87 static const struct statfs *
88 find_statfs(const struct statfs *mntbuf, int nitems, const char *mountpoint)
92 for (i = 0; i < nitems; i++) {
93 if (strcmp(mntbuf[i].f_mntonname, mountpoint) == 0)
101 mount_autofs(const char *from, const char *fspath, const char *options,
104 struct iovec *iov = NULL;
106 int error, iovlen = 0;
108 create_directory(fspath);
110 log_debugx("mounting %s on %s, prefix \"%s\", options \"%s\"",
111 from, fspath, prefix, options);
112 memset(errmsg, 0, sizeof(errmsg));
114 build_iovec(&iov, &iovlen, "fstype",
115 __DECONST(void *, "autofs"), (size_t)-1);
116 build_iovec(&iov, &iovlen, "fspath",
117 __DECONST(void *, fspath), (size_t)-1);
118 build_iovec(&iov, &iovlen, "from",
119 __DECONST(void *, from), (size_t)-1);
120 build_iovec(&iov, &iovlen, "errmsg",
121 errmsg, sizeof(errmsg));
124 * Append the options and mountpoint defined in auto_master(5);
125 * this way automountd(8) does not need to parse it.
127 build_iovec(&iov, &iovlen, "master_options",
128 __DECONST(void *, options), (size_t)-1);
129 build_iovec(&iov, &iovlen, "master_prefix",
130 __DECONST(void *, prefix), (size_t)-1);
132 error = nmount(iov, iovlen, 0);
134 if (*errmsg != '\0') {
135 log_err(1, "cannot mount %s on %s: %s",
136 from, fspath, errmsg);
138 log_err(1, "cannot mount %s on %s", from, fspath);
144 mount_if_not_already(const struct node *n, const char *map,
145 const struct statfs *mntbuf, int nitems)
147 const struct statfs *sb;
152 ret = asprintf(&from, "map %s", map);
154 log_err(1, "asprintf");
156 mountpoint = node_path(n);
157 sb = find_statfs(mntbuf, nitems, mountpoint);
159 if (strcmp(sb->f_fstypename, "autofs") != 0) {
160 log_debugx("unknown filesystem mounted "
161 "on %s; mounting", mountpoint);
163 * XXX: Compare options and 'from',
164 * and update the mount if necessary.
167 log_debugx("autofs already mounted "
168 "on %s", mountpoint);
174 log_debugx("nothing mounted on %s; mounting",
178 mount_autofs(from, mountpoint, n->n_options, n->n_key);
184 mount_unmount(struct node *root)
186 struct statfs *mntbuf;
187 struct node *n, *n2, *n3;
190 nitems = getmntinfo(&mntbuf, MNT_WAIT);
192 log_err(1, "getmntinfo");
194 log_debugx("unmounting stale autofs mounts");
196 for (i = 0; i < nitems; i++) {
197 if (strcmp(mntbuf[i].f_fstypename, "autofs") != 0) {
198 log_debugx("skipping %s, filesystem type is not autofs",
199 mntbuf[i].f_mntonname);
203 n = node_find(root, mntbuf[i].f_mntonname);
205 log_debugx("leaving autofs mounted on %s",
206 mntbuf[i].f_mntonname);
210 log_debugx("autofs mounted on %s not found "
211 "in new configuration; unmounting", mntbuf[i].f_mntonname);
212 unmount_by_statfs(&(mntbuf[i]), false);
215 log_debugx("mounting new autofs mounts");
217 TAILQ_FOREACH(n, &root->n_children, n_next) {
218 if (!node_is_direct_map(n)) {
219 mount_if_not_already(n, n->n_map, mntbuf, nitems);
223 TAILQ_FOREACH(n2, &n->n_children, n_next) {
224 TAILQ_FOREACH(n3, &n2->n_children, n_next) {
225 mount_if_not_already(n3, n->n_map,
233 flush_autofs(const char *fspath)
235 struct iovec *iov = NULL;
237 int error, iovlen = 0;
239 log_debugx("flushing %s", fspath);
240 memset(errmsg, 0, sizeof(errmsg));
242 build_iovec(&iov, &iovlen, "fstype",
243 __DECONST(void *, "autofs"), (size_t)-1);
244 build_iovec(&iov, &iovlen, "fspath",
245 __DECONST(void *, fspath), (size_t)-1);
246 build_iovec(&iov, &iovlen, "errmsg",
247 errmsg, sizeof(errmsg));
249 error = nmount(iov, iovlen, MNT_UPDATE);
251 if (*errmsg != '\0') {
252 log_err(1, "cannot flush %s: %s",
255 log_err(1, "cannot flush %s", fspath);
263 struct statfs *mntbuf;
266 nitems = getmntinfo(&mntbuf, MNT_WAIT);
268 log_err(1, "getmntinfo");
270 log_debugx("flushing autofs caches");
272 for (i = 0; i < nitems; i++) {
273 if (strcmp(mntbuf[i].f_fstypename, "autofs") != 0) {
274 log_debugx("skipping %s, filesystem type is not autofs",
275 mntbuf[i].f_mntonname);
279 flush_autofs(mntbuf[i].f_mntonname);
284 unmount_automounted(bool force)
286 struct statfs *mntbuf;
289 nitems = getmntinfo(&mntbuf, MNT_WAIT);
291 log_err(1, "getmntinfo");
293 log_debugx("unmounting automounted filesystems");
295 for (i = 0; i < nitems; i++) {
296 if (strcmp(mntbuf[i].f_fstypename, "autofs") == 0) {
297 log_debugx("skipping %s, filesystem type is autofs",
298 mntbuf[i].f_mntonname);
302 if ((mntbuf[i].f_flags & MNT_AUTOMOUNTED) == 0) {
303 log_debugx("skipping %s, not automounted",
304 mntbuf[i].f_mntonname);
308 unmount_by_statfs(&(mntbuf[i]), force);
313 usage_automount(void)
316 fprintf(stderr, "usage: automount [-D name=value][-o opts][-Lcfuv]\n");
321 main_automount(int argc, char **argv)
324 int ch, debug = 0, show_maps = 0;
325 char *options = NULL;
326 bool do_unmount = false, force_unmount = false, flush = false;
329 * Note that in automount(8), the only purpose of variable
330 * handling is to aid in debugging maps (automount -L).
334 while ((ch = getopt(argc, argv, "D:Lfco:uv")) != -1) {
337 defined_parse_and_add(optarg);
346 force_unmount = true;
349 if (options == NULL) {
350 options = checked_strdup(optarg);
353 separated_concat(options, optarg, ',');
371 if (force_unmount && !do_unmount)
382 unmount_automounted(force_unmount);
386 root = node_new_root();
387 parse_master(root, AUTO_MASTER_PATH);
390 if (options != NULL) {
391 root->n_options = separated_concat(options,
392 root->n_options, ',');
395 node_expand_indirect_maps(root);
396 node_expand_ampersand(root, NULL);
398 node_expand_defined(root);