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