]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/autofs/automount.c
Merge compiler-rt trunk r321017 to contrib/compiler-rt.
[FreeBSD/FreeBSD.git] / usr.sbin / autofs / automount.c
1 /*-
2  * Copyright (c) 2014 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Edward Tomasz Napierala under sponsorship
6  * from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/types.h>
35 #include <sys/time.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>
41 #include <sys/stat.h>
42 #include <sys/wait.h>
43 #include <sys/utsname.h>
44 #include <assert.h>
45 #include <ctype.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <libgen.h>
49 #include <libutil.h>
50 #include <netdb.h>
51 #include <signal.h>
52 #include <stdbool.h>
53 #include <stdint.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 #include "common.h"
60 #include "mntopts.h"
61
62 static int
63 unmount_by_statfs(const struct statfs *sb, bool force)
64 {
65         char *fsid_str;
66         int error, ret, flags;
67
68         ret = asprintf(&fsid_str, "FSID:%d:%d",
69             sb->f_fsid.val[0], sb->f_fsid.val[1]);
70         if (ret < 0)
71                 log_err(1, "asprintf");
72
73         log_debugx("unmounting %s using %s", sb->f_mntonname, fsid_str);
74
75         flags = MNT_BYFSID;
76         if (force)
77                 flags |= MNT_FORCE;
78         error = unmount(fsid_str, flags);
79         free(fsid_str);
80         if (error != 0)
81                 log_warn("cannot unmount %s", sb->f_mntonname);
82
83         return (error);
84 }
85
86 static const struct statfs *
87 find_statfs(const struct statfs *mntbuf, int nitems, const char *mountpoint)
88 {
89         int i;
90
91         for (i = 0; i < nitems; i++) {
92                 if (strcmp(mntbuf[i].f_mntonname, mountpoint) == 0)
93                         return (mntbuf + i);
94         }
95
96         return (NULL);
97 }
98
99 static void
100 mount_autofs(const char *from, const char *fspath, const char *options,
101     const char *prefix)
102 {
103         struct iovec *iov = NULL;
104         char errmsg[255];
105         int error, iovlen = 0;
106
107         create_directory(fspath);
108
109         log_debugx("mounting %s on %s, prefix \"%s\", options \"%s\"",
110             from, fspath, prefix, options);
111         memset(errmsg, 0, sizeof(errmsg));
112
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));
121
122         /*
123          * Append the options and mountpoint defined in auto_master(5);
124          * this way automountd(8) does not need to parse it.
125          */
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);
130
131         error = nmount(iov, iovlen, 0);
132         if (error != 0) {
133                 if (*errmsg != '\0') {
134                         log_err(1, "cannot mount %s on %s: %s",
135                             from, fspath, errmsg);
136                 } else {
137                         log_err(1, "cannot mount %s on %s", from, fspath);
138                 }
139         }
140 }
141
142 static void
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)
145 {
146         const struct statfs *sb;
147         char *mountpoint;
148         char *from;
149         int ret;
150
151         ret = asprintf(&from, "map %s", map);
152         if (ret < 0)
153                 log_err(1, "asprintf");
154
155         mountpoint = node_path(n);
156         sb = find_statfs(mntbuf, nitems, mountpoint);
157         if (sb != NULL) {
158                 if (strcmp(sb->f_fstypename, "autofs") != 0) {
159                         log_debugx("unknown filesystem mounted "
160                             "on %s; mounting", mountpoint);
161                         /*
162                          * XXX: Compare options and 'from',
163                          *      and update the mount if necessary.
164                          */
165                 } else {
166                         log_debugx("autofs already mounted "
167                             "on %s", mountpoint);
168                         free(from);
169                         free(mountpoint);
170                         return;
171                 }
172         } else {
173                 log_debugx("nothing mounted on %s; mounting",
174                     mountpoint);
175         }
176
177         mount_autofs(from, mountpoint, options, prefix);
178         free(from);
179         free(mountpoint);
180 }
181
182 static void
183 mount_unmount(struct node *root)
184 {
185         struct statfs *mntbuf;
186         struct node *n, *n2;
187         int i, nitems;
188
189         nitems = getmntinfo(&mntbuf, MNT_WAIT);
190         if (nitems <= 0)
191                 log_err(1, "getmntinfo");
192
193         log_debugx("unmounting stale autofs mounts");
194
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);
199                         continue;
200                 }
201
202                 n = node_find(root, mntbuf[i].f_mntonname);
203                 if (n != NULL) {
204                         log_debugx("leaving autofs mounted on %s",
205                             mntbuf[i].f_mntonname);
206                         continue;
207                 }
208
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);
212         }
213
214         log_debugx("mounting new autofs mounts");
215
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);
220                         continue;
221                 }
222
223                 TAILQ_FOREACH(n2, &n->n_children, n_next) {
224                         mount_if_not_already(n2, n->n_map, n->n_options,
225                             "/", mntbuf, nitems);
226                 }
227         }
228 }
229
230 static void
231 flush_autofs(const char *fspath)
232 {
233         struct iovec *iov = NULL;
234         char errmsg[255];
235         int error, iovlen = 0;
236
237         log_debugx("flushing %s", fspath);
238         memset(errmsg, 0, sizeof(errmsg));
239
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));
246
247         error = nmount(iov, iovlen, MNT_UPDATE);
248         if (error != 0) {
249                 if (*errmsg != '\0') {
250                         log_err(1, "cannot flush %s: %s",
251                             fspath, errmsg);
252                 } else {
253                         log_err(1, "cannot flush %s", fspath);
254                 }
255         }
256 }
257
258 static void
259 flush_caches(void)
260 {
261         struct statfs *mntbuf;
262         int i, nitems;
263
264         nitems = getmntinfo(&mntbuf, MNT_WAIT);
265         if (nitems <= 0)
266                 log_err(1, "getmntinfo");
267
268         log_debugx("flushing autofs caches");
269
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);
274                         continue;
275                 }
276
277                 flush_autofs(mntbuf[i].f_mntonname);
278         }
279 }
280
281 static void
282 unmount_automounted(bool force)
283 {
284         struct statfs *mntbuf;
285         int i, nitems;
286
287         nitems = getmntinfo(&mntbuf, MNT_WAIT);
288         if (nitems <= 0)
289                 log_err(1, "getmntinfo");
290
291         log_debugx("unmounting automounted filesystems");
292
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);
297                         continue;
298                 }
299
300                 if ((mntbuf[i].f_flags & MNT_AUTOMOUNTED) == 0) {
301                         log_debugx("skipping %s, not automounted",
302                             mntbuf[i].f_mntonname);
303                         continue;
304                 }
305
306                 unmount_by_statfs(&(mntbuf[i]), force);
307         }
308 }
309
310 static void
311 usage_automount(void)
312 {
313
314         fprintf(stderr, "usage: automount [-D name=value][-o opts][-Lcfuv]\n");
315         exit(1);
316 }
317
318 int
319 main_automount(int argc, char **argv)
320 {
321         struct node *root;
322         int ch, debug = 0, show_maps = 0;
323         char *options = NULL;
324         bool do_unmount = false, force_unmount = false, flush = false;
325
326         /*
327          * Note that in automount(8), the only purpose of variable
328          * handling is to aid in debugging maps (automount -L).
329          */
330         defined_init();
331
332         while ((ch = getopt(argc, argv, "D:Lfco:uv")) != -1) {
333                 switch (ch) {
334                 case 'D':
335                         defined_parse_and_add(optarg);
336                         break;
337                 case 'L':
338                         show_maps++;
339                         break;
340                 case 'c':
341                         flush = true;
342                         break;
343                 case 'f':
344                         force_unmount = true;
345                         break;
346                 case 'o':
347                         options = concat(options, ',', optarg);
348                         break;
349                 case 'u':
350                         do_unmount = true;
351                         break;
352                 case 'v':
353                         debug++;
354                         break;
355                 case '?':
356                 default:
357                         usage_automount();
358                 }
359         }
360         argc -= optind;
361         if (argc != 0)
362                 usage_automount();
363
364         if (force_unmount && !do_unmount)
365                 usage_automount();
366
367         log_init(debug);
368
369         if (flush) {
370                 flush_caches();
371                 return (0);
372         }
373
374         if (do_unmount) {
375                 unmount_automounted(force_unmount);
376                 return (0);
377         }
378
379         root = node_new_root();
380         parse_master(root, AUTO_MASTER_PATH);
381
382         if (show_maps) {
383                 if (show_maps > 1) {
384                         node_expand_indirect_maps(root);
385                         node_expand_ampersand(root, NULL);
386                 }
387                 node_expand_defined(root);
388                 node_print(root, options);
389                 return (0);
390         }
391
392         mount_unmount(root);
393
394         return (0);
395 }