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>
63 unmount_by_statfs(const struct statfs *sb, bool force)
66 int error, ret, flags;
68 ret = asprintf(&fsid_str, "FSID:%d:%d",
69 sb->f_fsid.val[0], sb->f_fsid.val[1]);
71 log_err(1, "asprintf");
73 log_debugx("unmounting %s using %s", sb->f_mntonname, fsid_str);
78 error = unmount(fsid_str, flags);
81 log_warn("cannot unmount %s", sb->f_mntonname);
86 static const struct statfs *
87 find_statfs(const struct statfs *mntbuf, int nitems, const char *mountpoint)
91 for (i = 0; i < nitems; i++) {
92 if (strcmp(mntbuf[i].f_mntonname, mountpoint) == 0)
100 mount_autofs(const char *from, const char *fspath, const char *options,
103 struct iovec *iov = NULL;
105 int error, iovlen = 0;
107 create_directory(fspath);
109 log_debugx("mounting %s on %s, prefix \"%s\", options \"%s\"",
110 from, fspath, prefix, options);
111 memset(errmsg, 0, sizeof(errmsg));
113 build_iovec(&iov, &iovlen, "fstype",
114 __DECONST(void *, "autofs"), (size_t)-1);
115 build_iovec(&iov, &iovlen, "fspath",
116 __DECONST(void *, fspath), (size_t)-1);
117 build_iovec(&iov, &iovlen, "from",
118 __DECONST(void *, from), (size_t)-1);
119 build_iovec(&iov, &iovlen, "errmsg",
120 errmsg, sizeof(errmsg));
123 * Append the options and mountpoint defined in auto_master(5);
124 * this way automountd(8) does not need to parse it.
126 build_iovec(&iov, &iovlen, "master_options",
127 __DECONST(void *, options), (size_t)-1);
128 build_iovec(&iov, &iovlen, "master_prefix",
129 __DECONST(void *, prefix), (size_t)-1);
131 error = nmount(iov, iovlen, 0);
133 if (*errmsg != '\0') {
134 log_err(1, "cannot mount %s on %s: %s",
135 from, fspath, errmsg);
137 log_err(1, "cannot mount %s on %s", from, fspath);
143 mount_if_not_already(const struct node *n, const char *map, const char *options,
144 const char *prefix, const struct statfs *mntbuf, int nitems)
146 const struct statfs *sb;
151 ret = asprintf(&from, "map %s", map);
153 log_err(1, "asprintf");
155 mountpoint = node_path(n);
156 sb = find_statfs(mntbuf, nitems, mountpoint);
158 if (strcmp(sb->f_fstypename, "autofs") != 0) {
159 log_debugx("unknown filesystem mounted "
160 "on %s; mounting", mountpoint);
162 * XXX: Compare options and 'from',
163 * and update the mount if necessary.
166 log_debugx("autofs already mounted "
167 "on %s", mountpoint);
173 log_debugx("nothing mounted on %s; mounting",
177 mount_autofs(from, mountpoint, options, prefix);
183 mount_unmount(struct node *root)
185 struct statfs *mntbuf;
189 nitems = getmntinfo(&mntbuf, MNT_WAIT);
191 log_err(1, "getmntinfo");
193 log_debugx("unmounting stale autofs mounts");
195 for (i = 0; i < nitems; i++) {
196 if (strcmp(mntbuf[i].f_fstypename, "autofs") != 0) {
197 log_debugx("skipping %s, filesystem type is not autofs",
198 mntbuf[i].f_mntonname);
202 n = node_find(root, mntbuf[i].f_mntonname);
204 log_debugx("leaving autofs mounted on %s",
205 mntbuf[i].f_mntonname);
209 log_debugx("autofs mounted on %s not found "
210 "in new configuration; unmounting", mntbuf[i].f_mntonname);
211 unmount_by_statfs(&(mntbuf[i]), false);
214 log_debugx("mounting new autofs mounts");
216 TAILQ_FOREACH(n, &root->n_children, n_next) {
217 if (!node_is_direct_map(n)) {
218 mount_if_not_already(n, n->n_map, n->n_options,
219 n->n_key, mntbuf, nitems);
223 TAILQ_FOREACH(n2, &n->n_children, n_next) {
224 mount_if_not_already(n2, n->n_map, n->n_options,
225 "/", mntbuf, nitems);
231 flush_autofs(const char *fspath)
233 struct iovec *iov = NULL;
235 int error, iovlen = 0;
237 log_debugx("flushing %s", fspath);
238 memset(errmsg, 0, sizeof(errmsg));
240 build_iovec(&iov, &iovlen, "fstype",
241 __DECONST(void *, "autofs"), (size_t)-1);
242 build_iovec(&iov, &iovlen, "fspath",
243 __DECONST(void *, fspath), (size_t)-1);
244 build_iovec(&iov, &iovlen, "errmsg",
245 errmsg, sizeof(errmsg));
247 error = nmount(iov, iovlen, MNT_UPDATE);
249 if (*errmsg != '\0') {
250 log_err(1, "cannot flush %s: %s",
253 log_err(1, "cannot flush %s", fspath);
261 struct statfs *mntbuf;
264 nitems = getmntinfo(&mntbuf, MNT_WAIT);
266 log_err(1, "getmntinfo");
268 log_debugx("flushing autofs caches");
270 for (i = 0; i < nitems; i++) {
271 if (strcmp(mntbuf[i].f_fstypename, "autofs") != 0) {
272 log_debugx("skipping %s, filesystem type is not autofs",
273 mntbuf[i].f_mntonname);
277 flush_autofs(mntbuf[i].f_mntonname);
282 unmount_automounted(bool force)
284 struct statfs *mntbuf;
287 nitems = getmntinfo(&mntbuf, MNT_WAIT);
289 log_err(1, "getmntinfo");
291 log_debugx("unmounting automounted filesystems");
293 for (i = 0; i < nitems; i++) {
294 if (strcmp(mntbuf[i].f_fstypename, "autofs") == 0) {
295 log_debugx("skipping %s, filesystem type is autofs",
296 mntbuf[i].f_mntonname);
300 if ((mntbuf[i].f_flags & MNT_AUTOMOUNTED) == 0) {
301 log_debugx("skipping %s, not automounted",
302 mntbuf[i].f_mntonname);
306 unmount_by_statfs(&(mntbuf[i]), force);
311 usage_automount(void)
314 fprintf(stderr, "usage: automount [-D name=value][-o opts][-Lcfuv]\n");
319 main_automount(int argc, char **argv)
322 int ch, debug = 0, show_maps = 0;
323 char *options = NULL;
324 bool do_unmount = false, force_unmount = false, flush = false;
327 * Note that in automount(8), the only purpose of variable
328 * handling is to aid in debugging maps (automount -L).
332 while ((ch = getopt(argc, argv, "D:Lfco:uv")) != -1) {
335 defined_parse_and_add(optarg);
344 force_unmount = true;
347 options = concat(options, ',', optarg);
364 if (force_unmount && !do_unmount)
375 unmount_automounted(force_unmount);
379 root = node_new_root();
380 parse_master(root, AUTO_MASTER_PATH);
384 node_expand_indirect_maps(root);
385 node_expand_ampersand(root, NULL);
387 node_expand_defined(root);
388 node_print(root, options);