1 /* $NetBSD: mount_autofs.c,v 1.5 2020/07/26 08:20:22 mlelstv 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.5 2020/07/26 08:20:22 mlelstv 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 "mountprog.h" 50 #include "mount_autofs.h" 51 52 static const struct mntopt mopts[] = { 53 MOPT_STDOPTS, 54 MOPT_GETARGS, 55 MOPT_NULL, 56 }; 57 58 int mount_autofs(int argc, char **argv); 59 __dead static void usage(void); 60 61 #ifndef MOUNT_NOMAIN 62 int 63 main(int argc, char **argv) 64 { 65 return mount_autofs(argc, argv); 66 } 67 #endif 68 69 void 70 mount_autofs_parseargs(int argc, char *argv[], void *v, int *mntflags, 71 char *canon_dev, char *canon_dir) 72 { 73 int ch; 74 mntoptparse_t mp; 75 struct autofs_args *am = v; 76 77 *mntflags = 0; 78 while ((ch = getopt(argc, argv, "f:o:O:p:")) != -1) 79 switch (ch) { 80 case 'f': 81 strlcpy(am->from, optarg, MAXPATHLEN); 82 break; 83 case 'o': 84 mp = getmntopts(optarg, mopts, mntflags, 0); 85 if (mp == NULL) 86 err(EXIT_FAILURE, "getmntopts"); 87 freemntopts(mp); 88 break; 89 case 'O': 90 strlcpy(am->master_options, optarg, MAXPATHLEN); 91 break; 92 case 'p': 93 strlcpy(am->master_prefix, optarg, MAXPATHLEN); 94 break; 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 pathadj(argv[1], canon_dir); 107 } 108 109 int 110 mount_autofs(int argc, char *argv[]) 111 { 112 char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN]; 113 char from[MAXPATHLEN], master_options[MAXPATHLEN]; 114 char master_prefix[MAXPATHLEN]; 115 int mntflags; 116 struct autofs_args am = { 117 .from = from, 118 .master_options = master_options, 119 .master_prefix = master_prefix, 120 }; 121 122 mount_autofs_parseargs(argc, argv, &am, &mntflags, 123 canon_dev, canon_dir); 124 if (mount(MOUNT_KERNFS, canon_dir, mntflags, &am, sizeof(am)) == -1) 125 err(EXIT_FAILURE, "autofs on %s", canon_dir); 126 if (mntflags & MNT_GETARGS) { 127 printf("from=%s, master_options=%s, master_prefix=%s\n", 128 am.from, am.master_options, am.master_prefix); 129 } 130 131 return EXIT_SUCCESS; 132 } 133 134 static void 135 usage(void) 136 { 137 (void)fprintf(stderr, 138 "Usage: %s [-f from] [-O autofs_options] [-o options] [-p prefix] " 139 "autofs mount_point\n", getprogname()); 140 exit(EXIT_FAILURE); 141 } 142