1 /* $NetBSD: mount_autofs.c,v 1.3 2018/01/23 15:02:03 wiz Exp $ */ 2 3 /*- 4 * Copyright (c) 2018 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 #include <sys/cdefs.h> 32 #ifndef lint 33 __RCSID("$NetBSD: mount_autofs.c,v 1.3 2018/01/23 15:02:03 wiz Exp $"); 34 #endif /* not lint */ 35 36 #include <sys/param.h> 37 #include <sys/mount.h> 38 39 #include <err.h> 40 #include <unistd.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 45 #include <mntopts.h> 46 47 #include <fs/autofs/autofs_mount.h> 48 49 #include "mount_autofs.h" 50 51 static const struct mntopt mopts[] = { 52 MOPT_STDOPTS, 53 MOPT_GETARGS, 54 MOPT_NULL, 55 }; 56 57 int mount_autofs(int argc, char **argv); 58 __dead static void usage(void); 59 60 #ifndef MOUNT_NOMAIN 61 int 62 main(int argc, char **argv) 63 { 64 return mount_autofs(argc, argv); 65 } 66 #endif 67 68 void 69 mount_autofs_parseargs(int argc, char *argv[], void *v, int *mntflags, 70 char *canon_dev, char *canon_dir) 71 { 72 int ch; 73 mntoptparse_t mp; 74 struct autofs_args *am = v; 75 76 *mntflags = 0; 77 while ((ch = getopt(argc, argv, "f:o:O:p:")) != -1) 78 switch (ch) { 79 case 'f': 80 strlcpy(am->from, optarg, MAXPATHLEN); 81 break; 82 case 'o': 83 mp = getmntopts(optarg, mopts, mntflags, 0); 84 if (mp == NULL) 85 err(EXIT_FAILURE, "getmntopts"); 86 freemntopts(mp); 87 break; 88 case 'O': 89 strlcpy(am->master_options, optarg, MAXPATHLEN); 90 break; 91 case 'p': 92 strlcpy(am->master_prefix, optarg, MAXPATHLEN); 93 break; 94 95 case '?': 96 default: 97 usage(); 98 } 99 argc -= optind; 100 argv += optind; 101 102 if (argc != 2) 103 usage(); 104 105 strlcpy(canon_dev, argv[0], MAXPATHLEN); 106 if (realpath(argv[1], canon_dir) == NULL) /* Check mounton path */ 107 err(EXIT_FAILURE, "realpath %s", canon_dir); 108 if (strncmp(argv[1], canon_dir, MAXPATHLEN)) { 109 warnx("\"%s\" is a relative path.", argv[1]); 110 warnx("using \"%s\" instead.", canon_dir); 111 } 112 } 113 114 int 115 mount_autofs(int argc, char *argv[]) 116 { 117 char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN]; 118 char from[MAXPATHLEN], master_options[MAXPATHLEN]; 119 char master_prefix[MAXPATHLEN]; 120 int mntflags; 121 struct autofs_args am = { 122 .from = from, 123 .master_options = master_options, 124 .master_prefix = master_prefix, 125 }; 126 127 mount_autofs_parseargs(argc, argv, &am, &mntflags, 128 canon_dev, canon_dir); 129 if (mount(MOUNT_KERNFS, canon_dir, mntflags, &am, sizeof(am)) == -1) 130 err(EXIT_FAILURE, "autofs on %s", canon_dir); 131 if (mntflags & MNT_GETARGS) { 132 printf("from=%s, master_options=%s, master_prefix=%s\n", 133 am.from, am.master_options, am.master_prefix); 134 } 135 136 return EXIT_SUCCESS; 137 } 138 139 static void 140 usage(void) 141 { 142 (void)fprintf(stderr, 143 "Usage: %s [-f from] [-O autofs_options] [-o options] [-p prefix] " 144 "autofs mount_point\n", getprogname()); 145 exit(EXIT_FAILURE); 146 } 147