mount_tmpfs.c revision 1.19 1 1.19 christos /* $NetBSD: mount_tmpfs.c,v 1.19 2007/12/14 17:37:22 christos Exp $ */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.13 jmmv * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5 1.1 jmmv * All rights reserved.
6 1.1 jmmv *
7 1.1 jmmv * This code is derived from software contributed to The NetBSD Foundation
8 1.3 jmmv * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 1.3 jmmv * 2005 program.
10 1.1 jmmv *
11 1.1 jmmv * Redistribution and use in source and binary forms, with or without
12 1.1 jmmv * modification, are permitted provided that the following conditions
13 1.1 jmmv * are met:
14 1.1 jmmv * 1. Redistributions of source code must retain the above copyright
15 1.1 jmmv * notice, this list of conditions and the following disclaimer.
16 1.1 jmmv * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 jmmv * notice, this list of conditions and the following disclaimer in the
18 1.1 jmmv * documentation and/or other materials provided with the distribution.
19 1.1 jmmv * 3. All advertising materials mentioning features or use of this software
20 1.1 jmmv * must display the following acknowledgement:
21 1.1 jmmv * This product includes software developed by the NetBSD
22 1.1 jmmv * Foundation, Inc. and its contributors.
23 1.1 jmmv * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 jmmv * contributors may be used to endorse or promote products derived
25 1.1 jmmv * from this software without specific prior written permission.
26 1.1 jmmv *
27 1.1 jmmv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 jmmv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 jmmv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 jmmv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 jmmv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 jmmv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 jmmv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 jmmv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 jmmv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 jmmv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 jmmv * POSSIBILITY OF SUCH DAMAGE.
38 1.1 jmmv */
39 1.1 jmmv
40 1.1 jmmv #include <sys/cdefs.h>
41 1.1 jmmv #ifndef lint
42 1.19 christos __RCSID("$NetBSD: mount_tmpfs.c,v 1.19 2007/12/14 17:37:22 christos Exp $");
43 1.1 jmmv #endif /* not lint */
44 1.1 jmmv
45 1.1 jmmv #include <sys/param.h>
46 1.1 jmmv #include <sys/mount.h>
47 1.4 jmmv #include <sys/stat.h>
48 1.1 jmmv
49 1.1 jmmv #include <fs/tmpfs/tmpfs.h>
50 1.1 jmmv
51 1.1 jmmv #include <ctype.h>
52 1.1 jmmv #include <err.h>
53 1.1 jmmv #include <errno.h>
54 1.1 jmmv #include <grp.h>
55 1.1 jmmv #include <mntopts.h>
56 1.1 jmmv #include <pwd.h>
57 1.1 jmmv #include <stdio.h>
58 1.1 jmmv #include <stdlib.h>
59 1.1 jmmv #include <string.h>
60 1.1 jmmv #include <unistd.h>
61 1.19 christos #include "fattr.h"
62 1.1 jmmv
63 1.1 jmmv /* --------------------------------------------------------------------- */
64 1.1 jmmv
65 1.1 jmmv static const struct mntopt mopts[] = {
66 1.1 jmmv MOPT_STDOPTS,
67 1.8 jmmv MOPT_GETARGS,
68 1.15 christos MOPT_NULL,
69 1.1 jmmv };
70 1.1 jmmv
71 1.1 jmmv /* --------------------------------------------------------------------- */
72 1.1 jmmv
73 1.1 jmmv static int mount_tmpfs(int argc, char **argv);
74 1.19 christos static void usage(void) __attribute__((__noreturn__));
75 1.1 jmmv
76 1.1 jmmv /* --------------------------------------------------------------------- */
77 1.1 jmmv
78 1.1 jmmv int
79 1.1 jmmv mount_tmpfs(int argc, char *argv[])
80 1.1 jmmv {
81 1.1 jmmv char canon_dir[MAXPATHLEN];
82 1.10 jmmv int gidset, modeset, uidset; /* Ought to be 'bool'. */
83 1.1 jmmv int ch, mntflags;
84 1.4 jmmv gid_t gid;
85 1.4 jmmv uid_t uid;
86 1.4 jmmv mode_t mode;
87 1.19 christos int64_t tmpnumber;
88 1.12 christos mntoptparse_t mp;
89 1.1 jmmv struct tmpfs_args args;
90 1.4 jmmv struct stat sb;
91 1.1 jmmv
92 1.1 jmmv setprogname(argv[0]);
93 1.1 jmmv
94 1.1 jmmv /* Set default values for mount point arguments. */
95 1.1 jmmv args.ta_version = TMPFS_ARGS_VERSION;
96 1.1 jmmv args.ta_size_max = 0;
97 1.1 jmmv args.ta_nodes_max = 0;
98 1.1 jmmv mntflags = 0;
99 1.1 jmmv
100 1.10 jmmv gidset = 0; gid = 0;
101 1.10 jmmv uidset = 0; uid = 0;
102 1.10 jmmv modeset = 0; mode = 0;
103 1.4 jmmv
104 1.1 jmmv optind = optreset = 1;
105 1.1 jmmv while ((ch = getopt(argc, argv, "g:m:n:o:s:u:")) != -1 ) {
106 1.1 jmmv switch (ch) {
107 1.1 jmmv case 'g':
108 1.19 christos gid = a_gid(optarg);
109 1.10 jmmv gidset = 1;
110 1.1 jmmv break;
111 1.1 jmmv
112 1.1 jmmv case 'm':
113 1.19 christos mode = a_mask(optarg);
114 1.10 jmmv modeset = 1;
115 1.1 jmmv break;
116 1.1 jmmv
117 1.1 jmmv case 'n':
118 1.19 christos if (dehumanize_number(optarg, &tmpnumber) == -1)
119 1.19 christos err(EXIT_FAILURE, "failed to parse nodes `%s'",
120 1.19 christos optarg);
121 1.19 christos args.ta_nodes_max = tmpnumber;
122 1.1 jmmv break;
123 1.1 jmmv
124 1.1 jmmv case 'o':
125 1.12 christos mp = getmntopts(optarg, mopts, &mntflags, 0);
126 1.12 christos if (mp == NULL)
127 1.19 christos err(EXIT_FAILURE, "getmntopts");
128 1.12 christos freemntopts(mp);
129 1.1 jmmv break;
130 1.1 jmmv
131 1.1 jmmv case 's':
132 1.19 christos if (dehumanize_number(optarg, &tmpnumber) == -1)
133 1.19 christos err(EXIT_FAILURE, "failed to parse size `%s'",
134 1.19 christos optarg);
135 1.19 christos args.ta_size_max = tmpnumber;
136 1.1 jmmv break;
137 1.1 jmmv
138 1.1 jmmv case 'u':
139 1.19 christos uid = a_uid(optarg);
140 1.10 jmmv uidset = 1;
141 1.1 jmmv break;
142 1.1 jmmv
143 1.1 jmmv case '?':
144 1.1 jmmv default:
145 1.1 jmmv usage();
146 1.1 jmmv }
147 1.1 jmmv }
148 1.1 jmmv argc -= optind;
149 1.1 jmmv argv += optind;
150 1.1 jmmv
151 1.19 christos if (argc != 2)
152 1.1 jmmv usage();
153 1.1 jmmv
154 1.19 christos if (realpath(argv[1], canon_dir) == NULL)
155 1.1 jmmv err(EXIT_FAILURE, "realpath %s", argv[0]);
156 1.1 jmmv
157 1.1 jmmv if (strncmp(argv[1], canon_dir, MAXPATHLEN) != 0) {
158 1.1 jmmv warnx("\"%s\" is a relative path", argv[0]);
159 1.1 jmmv warnx("using \"%s\" instead", canon_dir);
160 1.1 jmmv }
161 1.1 jmmv
162 1.19 christos if (stat(canon_dir, &sb) == -1)
163 1.19 christos err(EXIT_FAILURE, "cannot stat `%s'", canon_dir);
164 1.19 christos
165 1.4 jmmv args.ta_root_uid = uidset ? uid : sb.st_uid;
166 1.4 jmmv args.ta_root_gid = gidset ? gid : sb.st_gid;
167 1.4 jmmv args.ta_root_mode = modeset ? mode : sb.st_mode;
168 1.4 jmmv
169 1.19 christos if (mount(MOUNT_TMPFS, canon_dir, mntflags, &args, sizeof args) == -1)
170 1.1 jmmv err(EXIT_FAILURE, "tmpfs on %s", canon_dir);
171 1.19 christos
172 1.8 jmmv if (mntflags & MNT_GETARGS) {
173 1.8 jmmv struct passwd *pw;
174 1.8 jmmv struct group *gr;
175 1.8 jmmv
176 1.18 pooka (void)printf("version=%d, ", args.ta_version);
177 1.18 pooka (void)printf("size_max=%" PRIuMAX ", ",
178 1.8 jmmv (uintmax_t)args.ta_size_max);
179 1.18 pooka (void)printf("nodes_max=%" PRIuMAX ", ",
180 1.8 jmmv (uintmax_t)args.ta_nodes_max);
181 1.8 jmmv
182 1.8 jmmv pw = getpwuid(args.ta_root_uid);
183 1.8 jmmv if (pw == NULL)
184 1.18 pooka (void)printf("root_uid=%" PRIuMAX ", ",
185 1.8 jmmv (uintmax_t)args.ta_root_uid);
186 1.8 jmmv else
187 1.18 pooka (void)printf("root_uid=%s, ", pw->pw_name);
188 1.8 jmmv
189 1.8 jmmv gr = getgrgid(args.ta_root_gid);
190 1.8 jmmv if (gr == NULL)
191 1.18 pooka (void)printf("root_gid=%" PRIuMAX ", ",
192 1.8 jmmv (uintmax_t)args.ta_root_gid);
193 1.8 jmmv else
194 1.18 pooka (void)printf("root_gid=%s, ", gr->gr_name);
195 1.8 jmmv
196 1.8 jmmv (void)printf("root_mode=%o\n", args.ta_root_mode);
197 1.8 jmmv }
198 1.1 jmmv
199 1.1 jmmv return EXIT_SUCCESS;
200 1.1 jmmv }
201 1.1 jmmv
202 1.1 jmmv /* --------------------------------------------------------------------- */
203 1.1 jmmv
204 1.1 jmmv static void
205 1.1 jmmv usage(void)
206 1.1 jmmv {
207 1.1 jmmv (void)fprintf(stderr,
208 1.1 jmmv "Usage: %s [-g group] [-m mode] [-n nodes] [-o options] [-s size]\n"
209 1.1 jmmv " [-u user] tmpfs mountpoint\n", getprogname());
210 1.1 jmmv exit(1);
211 1.1 jmmv }
212 1.1 jmmv
213 1.1 jmmv /* --------------------------------------------------------------------- */
214 1.1 jmmv
215 1.1 jmmv #ifndef MOUNT_NOMAIN
216 1.1 jmmv int
217 1.1 jmmv main(int argc, char *argv[])
218 1.1 jmmv {
219 1.1 jmmv
220 1.1 jmmv return mount_tmpfs(argc, argv);
221 1.1 jmmv }
222 1.1 jmmv #endif
223