1 /*- 2 * Copyright (c) 2018 The NetBSD Foundation, Inc. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to The NetBSD Foundation 6 * by Christos Zoulas. 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/param.h> 31 #include <sys/mount.h> 32 #include <sys/cdefs.h> 33 34 #include <err.h> 35 #include <mntopts.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <sysexits.h> 40 #include <unistd.h> 41 42 #include <vfs/autofs/autofs_mount.h> 43 44 #include "mount_autofs.h" 45 46 static const struct mntopt mopts[] = { 47 MOPT_STDOPTS, 48 MOPT_NULL, 49 }; 50 51 static void usage(void) __dead2; 52 53 int 54 main(int argc, char **argv) 55 { 56 return mount_autofs(argc, argv); 57 } 58 59 void 60 mount_autofs_parseargs(int argc, char *argv[], void *v, int *mntflags, 61 char *canon_dev, char *canon_dir) 62 { 63 int ch; 64 struct autofs_mount_info *am = v; 65 66 *mntflags = 0; 67 while ((ch = getopt(argc, argv, "f:o:O:p:")) != -1) 68 switch (ch) { 69 case 'f': 70 strlcpy(__DECONST(char*, am->from), optarg, MAXPATHLEN); 71 break; 72 case 'o': 73 getmntopts(optarg, mopts, mntflags, 0); 74 break; 75 case 'O': 76 strlcpy(__DECONST(char*, am->master_options), optarg, 77 MAXPATHLEN); 78 break; 79 case 'p': 80 strlcpy(__DECONST(char*, am->master_prefix), optarg, 81 MAXPATHLEN); 82 break; 83 case '?': 84 default: 85 usage(); 86 } 87 argc -= optind; 88 argv += optind; 89 90 if (argc != 2) 91 usage(); 92 93 strlcpy(canon_dev, argv[0], MAXPATHLEN); 94 if (realpath(argv[1], canon_dir) == NULL) /* Check mounton path */ 95 err(EXIT_FAILURE, "realpath %s", canon_dir); 96 if (strncmp(argv[1], canon_dir, MAXPATHLEN)) { 97 warnx("\"%s\" is a relative path.", argv[1]); 98 warnx("using \"%s\" instead.", canon_dir); 99 } 100 } 101 102 int 103 mount_autofs(int argc, char *argv[]) 104 { 105 char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN]; 106 char from[MAXPATHLEN], master_options[MAXPATHLEN]; 107 char master_prefix[MAXPATHLEN]; 108 int mntflags, error; 109 struct vfsconf vfc; 110 struct autofs_mount_info am = { 111 .from = from, 112 .master_options = master_options, 113 .master_prefix = master_prefix, 114 }; 115 116 mount_autofs_parseargs(argc, argv, &am, &mntflags, 117 canon_dev, canon_dir); 118 119 error = getvfsbyname("autofs", &vfc); 120 if (error && vfsisloadable("autofs")) { 121 if(vfsload("autofs")) 122 err(EX_OSERR, "vfsload(%s)", "autofs"); 123 endvfsent(); 124 error = getvfsbyname("autofs", &vfc); 125 } 126 if (error) 127 errx(EX_OSERR, "%s filesystem not available", "autofs"); 128 129 if (mount(vfc.vfc_name, canon_dir, mntflags, &am) == -1) 130 err(EXIT_FAILURE, "autofs on %s", canon_dir); 131 132 return EXIT_SUCCESS; 133 } 134 135 static void 136 usage(void) 137 { 138 (void)fprintf(stderr, 139 "Usage: %s [-o options] [-O master_options] [-p master_prefix] " 140 "[-f <from>] autofs mount_point\n", getprogname()); 141 exit(EXIT_FAILURE); 142 } 143