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