mount_tmpfs.c revision 1.28 1 1.26 martin
2 1.28 christos /* $NetBSD: mount_tmpfs.c,v 1.28 2014/04/30 01:33:11 christos Exp $ */
3 1.1 jmmv
4 1.1 jmmv /*
5 1.13 jmmv * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
6 1.1 jmmv * All rights reserved.
7 1.1 jmmv *
8 1.1 jmmv * This code is derived from software contributed to The NetBSD Foundation
9 1.3 jmmv * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
10 1.3 jmmv * 2005 program.
11 1.1 jmmv *
12 1.1 jmmv * Redistribution and use in source and binary forms, with or without
13 1.1 jmmv * modification, are permitted provided that the following conditions
14 1.1 jmmv * are met:
15 1.1 jmmv * 1. Redistributions of source code must retain the above copyright
16 1.1 jmmv * notice, this list of conditions and the following disclaimer.
17 1.1 jmmv * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 jmmv * notice, this list of conditions and the following disclaimer in the
19 1.1 jmmv * documentation and/or other materials provided with the distribution.
20 1.1 jmmv *
21 1.1 jmmv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 1.1 jmmv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 1.1 jmmv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.1 jmmv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 1.1 jmmv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1 jmmv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1 jmmv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 jmmv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 jmmv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 jmmv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1 jmmv * POSSIBILITY OF SUCH DAMAGE.
32 1.1 jmmv */
33 1.1 jmmv
34 1.1 jmmv #include <sys/cdefs.h>
35 1.1 jmmv #ifndef lint
36 1.28 christos __RCSID("$NetBSD: mount_tmpfs.c,v 1.28 2014/04/30 01:33:11 christos Exp $");
37 1.1 jmmv #endif /* not lint */
38 1.1 jmmv
39 1.1 jmmv #include <sys/param.h>
40 1.1 jmmv #include <sys/mount.h>
41 1.4 jmmv #include <sys/stat.h>
42 1.26 martin #include <sys/sysctl.h>
43 1.1 jmmv
44 1.23 pooka #include <fs/tmpfs/tmpfs_args.h>
45 1.1 jmmv
46 1.1 jmmv #include <ctype.h>
47 1.1 jmmv #include <err.h>
48 1.1 jmmv #include <errno.h>
49 1.1 jmmv #include <grp.h>
50 1.1 jmmv #include <mntopts.h>
51 1.1 jmmv #include <pwd.h>
52 1.1 jmmv #include <stdio.h>
53 1.1 jmmv #include <stdlib.h>
54 1.1 jmmv #include <string.h>
55 1.1 jmmv #include <unistd.h>
56 1.24 pooka
57 1.24 pooka #include "mountprog.h"
58 1.24 pooka #include "mount_tmpfs.h"
59 1.1 jmmv
60 1.1 jmmv /* --------------------------------------------------------------------- */
61 1.1 jmmv
62 1.1 jmmv static const struct mntopt mopts[] = {
63 1.28 christos MOPT_UPDATE,
64 1.1 jmmv MOPT_STDOPTS,
65 1.8 jmmv MOPT_GETARGS,
66 1.15 christos MOPT_NULL,
67 1.1 jmmv };
68 1.1 jmmv
69 1.1 jmmv /* --------------------------------------------------------------------- */
70 1.1 jmmv
71 1.20 perry static void usage(void) __dead;
72 1.26 martin static int64_t ram_fract(const char *arg);
73 1.26 martin static int64_t ram_percent(const char *arg);
74 1.26 martin static int64_t ram_factor(float f);
75 1.26 martin
76 1.26 martin /* --------------------------------------------------------------------- */
77 1.26 martin
78 1.26 martin /* return f * available system ram */
79 1.26 martin static int64_t
80 1.26 martin ram_factor(float f)
81 1.26 martin {
82 1.26 martin uint64_t ram;
83 1.26 martin size_t len;
84 1.26 martin
85 1.26 martin len = sizeof(ram);
86 1.26 martin if (sysctlbyname("hw.physmem64", &ram, &len, NULL, 0))
87 1.27 martin err(EXIT_FAILURE, "can't get \"hw.physmem64\"");
88 1.26 martin
89 1.26 martin return (int64_t)((float)ram * f);
90 1.26 martin }
91 1.26 martin
92 1.26 martin /* return fraction of available ram given by arg */
93 1.26 martin static int64_t
94 1.26 martin ram_fract(const char *arg)
95 1.26 martin {
96 1.26 martin char *endp;
97 1.26 martin float f;
98 1.26 martin
99 1.26 martin f = strtof(arg, &endp);
100 1.26 martin if (endp && *endp != 0)
101 1.27 martin errx(EXIT_FAILURE, "syntax error in ram fraction: ram/%s"
102 1.26 martin " at %s", arg, endp);
103 1.26 martin if (f <= 0.0f)
104 1.27 martin errx(EXIT_FAILURE, "ram fraction must be a positive number:"
105 1.26 martin " ram/%s", arg);
106 1.26 martin
107 1.26 martin return ram_factor(1.0f/f);
108 1.26 martin }
109 1.26 martin
110 1.26 martin /* --------------------------------------------------------------------- */
111 1.26 martin
112 1.26 martin /* return percentage of available ram given by arg */
113 1.26 martin static int64_t
114 1.26 martin ram_percent(const char *arg)
115 1.26 martin {
116 1.26 martin char *endp;
117 1.26 martin float f;
118 1.26 martin
119 1.26 martin f = strtof(arg, &endp);
120 1.26 martin if (endp && *endp != 0)
121 1.27 martin errx(EXIT_FAILURE, "syntax error in ram percentage: ram%%%s"
122 1.26 martin " at %s", arg, endp);
123 1.26 martin if (f <= 0.0f || f >= 100.0f)
124 1.27 martin errx(EXIT_FAILURE, "ram percentage must be a between 0 and 100"
125 1.26 martin " ram%%%s", arg);
126 1.26 martin
127 1.26 martin return ram_factor(f/100.0f);
128 1.26 martin }
129 1.1 jmmv
130 1.1 jmmv /* --------------------------------------------------------------------- */
131 1.1 jmmv
132 1.24 pooka void
133 1.24 pooka mount_tmpfs_parseargs(int argc, char *argv[],
134 1.24 pooka struct tmpfs_args *args, int *mntflags,
135 1.24 pooka char *canon_dev, char *canon_dir)
136 1.1 jmmv {
137 1.10 jmmv int gidset, modeset, uidset; /* Ought to be 'bool'. */
138 1.24 pooka int ch;
139 1.4 jmmv gid_t gid;
140 1.4 jmmv uid_t uid;
141 1.4 jmmv mode_t mode;
142 1.19 christos int64_t tmpnumber;
143 1.12 christos mntoptparse_t mp;
144 1.4 jmmv struct stat sb;
145 1.1 jmmv
146 1.1 jmmv /* Set default values for mount point arguments. */
147 1.24 pooka memset(args, 0, sizeof(*args));
148 1.24 pooka args->ta_version = TMPFS_ARGS_VERSION;
149 1.24 pooka args->ta_size_max = 0;
150 1.24 pooka args->ta_nodes_max = 0;
151 1.24 pooka *mntflags = 0;
152 1.1 jmmv
153 1.10 jmmv gidset = 0; gid = 0;
154 1.10 jmmv uidset = 0; uid = 0;
155 1.10 jmmv modeset = 0; mode = 0;
156 1.4 jmmv
157 1.1 jmmv optind = optreset = 1;
158 1.1 jmmv while ((ch = getopt(argc, argv, "g:m:n:o:s:u:")) != -1 ) {
159 1.1 jmmv switch (ch) {
160 1.1 jmmv case 'g':
161 1.19 christos gid = a_gid(optarg);
162 1.10 jmmv gidset = 1;
163 1.1 jmmv break;
164 1.1 jmmv
165 1.1 jmmv case 'm':
166 1.19 christos mode = a_mask(optarg);
167 1.10 jmmv modeset = 1;
168 1.1 jmmv break;
169 1.1 jmmv
170 1.1 jmmv case 'n':
171 1.19 christos if (dehumanize_number(optarg, &tmpnumber) == -1)
172 1.19 christos err(EXIT_FAILURE, "failed to parse nodes `%s'",
173 1.19 christos optarg);
174 1.24 pooka args->ta_nodes_max = tmpnumber;
175 1.1 jmmv break;
176 1.1 jmmv
177 1.1 jmmv case 'o':
178 1.24 pooka mp = getmntopts(optarg, mopts, mntflags, 0);
179 1.12 christos if (mp == NULL)
180 1.19 christos err(EXIT_FAILURE, "getmntopts");
181 1.12 christos freemntopts(mp);
182 1.1 jmmv break;
183 1.1 jmmv
184 1.1 jmmv case 's':
185 1.26 martin if (strncmp(optarg, "ram/", 4) == 0)
186 1.26 martin tmpnumber = ram_fract(optarg+4);
187 1.26 martin else if (strncmp(optarg, "ram%", 4) == 0)
188 1.26 martin tmpnumber = ram_percent(optarg+4);
189 1.26 martin else if (dehumanize_number(optarg, &tmpnumber) == -1)
190 1.19 christos err(EXIT_FAILURE, "failed to parse size `%s'",
191 1.19 christos optarg);
192 1.24 pooka args->ta_size_max = tmpnumber;
193 1.1 jmmv break;
194 1.1 jmmv
195 1.1 jmmv case 'u':
196 1.19 christos uid = a_uid(optarg);
197 1.10 jmmv uidset = 1;
198 1.1 jmmv break;
199 1.1 jmmv
200 1.1 jmmv case '?':
201 1.1 jmmv default:
202 1.1 jmmv usage();
203 1.1 jmmv }
204 1.1 jmmv }
205 1.1 jmmv argc -= optind;
206 1.1 jmmv argv += optind;
207 1.1 jmmv
208 1.19 christos if (argc != 2)
209 1.1 jmmv usage();
210 1.1 jmmv
211 1.24 pooka strlcpy(canon_dev, argv[0], MAXPATHLEN);
212 1.24 pooka pathadj(argv[1], canon_dir);
213 1.1 jmmv
214 1.19 christos if (stat(canon_dir, &sb) == -1)
215 1.19 christos err(EXIT_FAILURE, "cannot stat `%s'", canon_dir);
216 1.19 christos
217 1.24 pooka args->ta_root_uid = uidset ? uid : sb.st_uid;
218 1.24 pooka args->ta_root_gid = gidset ? gid : sb.st_gid;
219 1.24 pooka args->ta_root_mode = modeset ? mode : sb.st_mode;
220 1.24 pooka }
221 1.24 pooka
222 1.24 pooka /* --------------------------------------------------------------------- */
223 1.24 pooka
224 1.24 pooka static void
225 1.24 pooka usage(void)
226 1.24 pooka {
227 1.24 pooka (void)fprintf(stderr,
228 1.25 wiz "usage: %s [-g group] [-m mode] [-n nodes] [-o options] [-s size]\n"
229 1.25 wiz " [-u user] tmpfs mount_point\n", getprogname());
230 1.24 pooka exit(1);
231 1.24 pooka }
232 1.24 pooka
233 1.24 pooka /* --------------------------------------------------------------------- */
234 1.24 pooka
235 1.24 pooka int
236 1.24 pooka mount_tmpfs(int argc, char *argv[])
237 1.24 pooka {
238 1.24 pooka struct tmpfs_args args;
239 1.24 pooka char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
240 1.24 pooka int mntflags;
241 1.24 pooka
242 1.24 pooka mount_tmpfs_parseargs(argc, argv, &args, &mntflags,
243 1.24 pooka canon_dev, canon_dir);
244 1.4 jmmv
245 1.19 christos if (mount(MOUNT_TMPFS, canon_dir, mntflags, &args, sizeof args) == -1)
246 1.1 jmmv err(EXIT_FAILURE, "tmpfs on %s", canon_dir);
247 1.19 christos
248 1.8 jmmv if (mntflags & MNT_GETARGS) {
249 1.8 jmmv struct passwd *pw;
250 1.8 jmmv struct group *gr;
251 1.8 jmmv
252 1.18 pooka (void)printf("version=%d, ", args.ta_version);
253 1.18 pooka (void)printf("size_max=%" PRIuMAX ", ",
254 1.8 jmmv (uintmax_t)args.ta_size_max);
255 1.18 pooka (void)printf("nodes_max=%" PRIuMAX ", ",
256 1.8 jmmv (uintmax_t)args.ta_nodes_max);
257 1.8 jmmv
258 1.8 jmmv pw = getpwuid(args.ta_root_uid);
259 1.8 jmmv if (pw == NULL)
260 1.18 pooka (void)printf("root_uid=%" PRIuMAX ", ",
261 1.8 jmmv (uintmax_t)args.ta_root_uid);
262 1.8 jmmv else
263 1.18 pooka (void)printf("root_uid=%s, ", pw->pw_name);
264 1.8 jmmv
265 1.8 jmmv gr = getgrgid(args.ta_root_gid);
266 1.8 jmmv if (gr == NULL)
267 1.18 pooka (void)printf("root_gid=%" PRIuMAX ", ",
268 1.8 jmmv (uintmax_t)args.ta_root_gid);
269 1.8 jmmv else
270 1.18 pooka (void)printf("root_gid=%s, ", gr->gr_name);
271 1.8 jmmv
272 1.8 jmmv (void)printf("root_mode=%o\n", args.ta_root_mode);
273 1.8 jmmv }
274 1.1 jmmv
275 1.1 jmmv return EXIT_SUCCESS;
276 1.1 jmmv }
277 1.1 jmmv
278 1.1 jmmv #ifndef MOUNT_NOMAIN
279 1.1 jmmv int
280 1.1 jmmv main(int argc, char *argv[])
281 1.1 jmmv {
282 1.1 jmmv
283 1.24 pooka setprogname(argv[0]);
284 1.1 jmmv return mount_tmpfs(argc, argv);
285 1.1 jmmv }
286 1.1 jmmv #endif
287