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