xref: /dragonfly/usr.sbin/autofs/automount.c (revision 7d3e9a5b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2016 The DragonFly Project
5  * Copyright (c) 2014 The FreeBSD Foundation
6  * All rights reserved.
7  *
8  * This software was developed by Edward Tomasz Napierala under sponsorship
9  * from the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  */
33 
34 #include <sys/types.h>
35 #include <sys/mount.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <vfs/autofs/autofs_mount.h>
41 
42 #include "common.h"
43 
44 static int
45 unmount_by_statfs(const struct statfs *sb, bool force)
46 {
47 	int error, flags;
48 
49 	log_debugx("unmounting %s", sb->f_mntonname);
50 
51 	flags = 0;
52 	if (force)
53 		flags |= MNT_FORCE;
54 	error = unmount(sb->f_mntonname, flags);
55 	if (error != 0)
56 		log_warn("cannot unmount %s", sb->f_mntonname);
57 	else
58 		rpc_umntall();
59 
60 	return (error);
61 }
62 
63 static const struct statfs *
64 find_statfs(const struct statfs *mntbuf, int nitems, const char *mountpoint)
65 {
66 	int i;
67 
68 	for (i = 0; i < nitems; i++) {
69 		if (strcmp(mntbuf[i].f_mntonname, mountpoint) == 0)
70 			return (mntbuf + i);
71 	}
72 
73 	return (NULL);
74 }
75 
76 static void
77 mount_autofs(const char *from, const char *fspath, const char *options,
78     const char *prefix)
79 {
80 	struct autofs_mount_info info;
81 	int error;
82 
83 	create_directory(fspath);
84 
85 	log_debugx("mounting %s on %s, prefix \"%s\", options \"%s\"",
86 	    from, fspath, prefix, options);
87 
88 	memset(&info, 0, sizeof(info));
89 	info.from = from;
90 	info.master_options = options;
91 	info.master_prefix = prefix;
92 
93 	error = mount("autofs", fspath, 0, &info);
94 	if (error != 0)
95 		log_err(1, "cannot mount %s on %s", from, fspath);
96 }
97 
98 static void
99 mount_if_not_already(const struct node *n, const char *map, const char *options,
100     const char *prefix, const struct statfs *mntbuf, int nitems)
101 {
102 	const struct statfs *sb;
103 	char *mountpoint;
104 	char *from;
105 	int ret;
106 
107 	ret = asprintf(&from, "map %s", map);
108 	if (ret < 0)
109 		log_err(1, "asprintf");
110 
111 	mountpoint = node_path(n);
112 	sb = find_statfs(mntbuf, nitems, mountpoint);
113 	if (sb != NULL) {
114 		if (strcmp(sb->f_fstypename, "autofs") != 0) {
115 			log_debugx("unknown filesystem mounted "
116 			    "on %s; mounting", mountpoint);
117 			/*
118 			 * XXX: Compare options and 'from',
119 			 *	and update the mount if necessary.
120 			 */
121 		} else {
122 			log_debugx("autofs already mounted "
123 			    "on %s", mountpoint);
124 			free(from);
125 			free(mountpoint);
126 			return;
127 		}
128 	} else {
129 		log_debugx("nothing mounted on %s; mounting",
130 		    mountpoint);
131 	}
132 
133 	mount_autofs(from, mountpoint, options, prefix);
134 	free(from);
135 	free(mountpoint);
136 }
137 
138 static void
139 mount_unmount(struct node *root)
140 {
141 	struct statfs *mntbuf;
142 	struct node *n, *n2;
143 	int i, nitems;
144 
145 	nitems = getmntinfo(&mntbuf, MNT_WAIT);
146 	if (nitems <= 0)
147 		log_err(1, "getmntinfo");
148 
149 	log_debugx("unmounting stale autofs mounts");
150 
151 	for (i = 0; i < nitems; i++) {
152 		if (strcmp(mntbuf[i].f_fstypename, "autofs") != 0) {
153 			log_debugx("skipping %s, filesystem type is not autofs",
154 			    mntbuf[i].f_mntonname);
155 			continue;
156 		}
157 
158 		n = node_find(root, mntbuf[i].f_mntonname);
159 		if (n != NULL) {
160 			log_debugx("leaving autofs mounted on %s",
161 			    mntbuf[i].f_mntonname);
162 			continue;
163 		}
164 
165 		log_debugx("autofs mounted on %s not found "
166 		    "in new configuration; unmounting", mntbuf[i].f_mntonname);
167 		unmount_by_statfs(&(mntbuf[i]), false);
168 	}
169 
170 	log_debugx("mounting new autofs mounts");
171 
172 	TAILQ_FOREACH(n, &root->n_children, n_next) {
173 		if (!node_is_direct_map(n)) {
174 			mount_if_not_already(n, n->n_map, n->n_options,
175 			    n->n_key, mntbuf, nitems);
176 			continue;
177 		}
178 
179 		TAILQ_FOREACH(n2, &n->n_children, n_next) {
180 			mount_if_not_already(n2, n->n_map, n->n_options,
181 			    "/", mntbuf, nitems);
182 		}
183 	}
184 }
185 
186 static void
187 flush_autofs(const char *fspath)
188 {
189 	int error;
190 
191 	log_debugx("flushing %s", fspath);
192 
193 	error = mount("autofs", fspath, MNT_UPDATE, NULL);
194 	if (error != 0)
195 		log_err(1, "cannot flush %s", fspath);
196 }
197 
198 static void
199 flush_caches(void)
200 {
201 	struct statfs *mntbuf;
202 	int i, nitems;
203 
204 	nitems = getmntinfo(&mntbuf, MNT_WAIT);
205 	if (nitems <= 0)
206 		log_err(1, "getmntinfo");
207 
208 	log_debugx("flushing autofs caches");
209 
210 	for (i = 0; i < nitems; i++) {
211 		if (strcmp(mntbuf[i].f_fstypename, "autofs") != 0) {
212 			log_debugx("skipping %s, filesystem type is not autofs",
213 			    mntbuf[i].f_mntonname);
214 			continue;
215 		}
216 
217 		flush_autofs(mntbuf[i].f_mntonname);
218 	}
219 }
220 
221 static void
222 unmount_automounted(bool force)
223 {
224 	struct statfs *mntbuf;
225 	int i, nitems;
226 
227 	nitems = getmntinfo(&mntbuf, MNT_WAIT);
228 	if (nitems <= 0)
229 		log_err(1, "getmntinfo");
230 
231 	log_debugx("unmounting automounted filesystems");
232 
233 	for (i = 0; i < nitems; i++) {
234 		if (strcmp(mntbuf[i].f_fstypename, "autofs") == 0) {
235 			log_debugx("skipping %s, filesystem type is autofs",
236 			    mntbuf[i].f_mntonname);
237 			continue;
238 		}
239 
240 		if ((mntbuf[i].f_flags & MNT_AUTOMOUNTED) == 0) {
241 			log_debugx("skipping %s, not automounted",
242 			    mntbuf[i].f_mntonname);
243 			continue;
244 		}
245 
246 		unmount_by_statfs(&(mntbuf[i]), force);
247 	}
248 }
249 
250 static void
251 usage_automount(void)
252 {
253 
254 	fprintf(stderr, "usage: automount [-D name=value][-o opts][-Lcfuv]\n");
255 	exit(1);
256 }
257 
258 int
259 main_automount(int argc, char **argv)
260 {
261 	struct node *root;
262 	int ch, debug = 0, show_maps = 0;
263 	char *options = NULL;
264 	bool do_unmount = false, force_unmount = false, flush = false;
265 
266 	/*
267 	 * Note that in automount(8), the only purpose of variable
268 	 * handling is to aid in debugging maps (automount -L).
269 	 */
270 	defined_init();
271 
272 	while ((ch = getopt(argc, argv, "D:Lfco:uv")) != -1) {
273 		switch (ch) {
274 		case 'D':
275 			defined_parse_and_add(optarg);
276 			break;
277 		case 'L':
278 			show_maps++;
279 			break;
280 		case 'c':
281 			flush = true;
282 			break;
283 		case 'f':
284 			force_unmount = true;
285 			break;
286 		case 'o':
287 			options = concat(options, ',', optarg);
288 			break;
289 		case 'u':
290 			do_unmount = true;
291 			break;
292 		case 'v':
293 			debug++;
294 			break;
295 		case '?':
296 		default:
297 			usage_automount();
298 		}
299 	}
300 	argc -= optind;
301 	if (argc != 0)
302 		usage_automount();
303 
304 	if (force_unmount && !do_unmount)
305 		usage_automount();
306 
307 	log_init(debug);
308 
309 	if (flush) {
310 		flush_caches();
311 		return (0);
312 	}
313 
314 	if (do_unmount) {
315 		unmount_automounted(force_unmount);
316 		return (0);
317 	}
318 
319 	root = node_new_root();
320 	parse_master(root, AUTO_MASTER_PATH);
321 
322 	if (show_maps) {
323 		if (show_maps > 1) {
324 			node_expand_indirect_maps(root);
325 			node_expand_ampersand(root, NULL);
326 		}
327 		node_expand_defined(root);
328 		node_print(root, options);
329 		return (0);
330 	}
331 
332 	mount_unmount(root);
333 
334 	return (0);
335 }
336