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