1 /* $NetBSD: mount_tmpfs.c,v 1.23 2008/07/28 12:42:12 pooka Exp $ */
4 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: mount_tmpfs.c,v 1.23 2008/07/28 12:42:12 pooka Exp $");
38 #include <sys/param.h>
39 #include <sys/mount.h>
42 #include <fs/tmpfs/tmpfs_args.h>
55 #include "mountprog.h"
56 #include "mount_tmpfs.h"
58 /* --------------------------------------------------------------------- */
60 static const struct mntopt mopts
[] = {
66 /* --------------------------------------------------------------------- */
68 static void usage(void) __dead
;
70 /* --------------------------------------------------------------------- */
73 mount_tmpfs_parseargs(int argc
, char *argv
[],
74 struct tmpfs_args
*args
, int *mntflags
,
75 char *canon_dev
, char *canon_dir
)
77 int gidset
, modeset
, uidset
; /* Ought to be 'bool'. */
86 /* Set default values for mount point arguments. */
87 memset(args
, 0, sizeof(*args
));
88 args
->ta_version
= TMPFS_ARGS_VERSION
;
89 args
->ta_size_max
= 0;
90 args
->ta_nodes_max
= 0;
95 modeset
= 0; mode
= 0;
97 optind
= optreset
= 1;
98 while ((ch
= getopt(argc
, argv
, "g:m:n:o:s:u:")) != -1 ) {
106 mode
= a_mask(optarg
);
111 if (dehumanize_number(optarg
, &tmpnumber
) == -1)
112 err(EXIT_FAILURE
, "failed to parse nodes `%s'",
114 args
->ta_nodes_max
= tmpnumber
;
118 mp
= getmntopts(optarg
, mopts
, mntflags
, 0);
120 err(EXIT_FAILURE
, "getmntopts");
125 if (dehumanize_number(optarg
, &tmpnumber
) == -1)
126 err(EXIT_FAILURE
, "failed to parse size `%s'",
128 args
->ta_size_max
= tmpnumber
;
147 strlcpy(canon_dev
, argv
[0], MAXPATHLEN
);
148 pathadj(argv
[1], canon_dir
);
150 if (stat(canon_dir
, &sb
) == -1)
151 err(EXIT_FAILURE
, "cannot stat `%s'", canon_dir
);
153 args
->ta_root_uid
= uidset
? uid
: sb
.st_uid
;
154 args
->ta_root_gid
= gidset
? gid
: sb
.st_gid
;
155 args
->ta_root_mode
= modeset
? mode
: sb
.st_mode
;
158 /* --------------------------------------------------------------------- */
163 (void)fprintf(stderr
,
164 "Usage: %s [-g group] [-m mode] [-n nodes] [-o options] [-s size]\n"
165 " [-u user] tmpfs mountpoint\n", getprogname());
169 /* --------------------------------------------------------------------- */
172 mount_tmpfs(int argc
, char *argv
[])
174 struct tmpfs_args args
;
175 char canon_dev
[MAXPATHLEN
], canon_dir
[MAXPATHLEN
];
178 mount_tmpfs_parseargs(argc
, argv
, &args
, &mntflags
,
179 canon_dev
, canon_dir
);
181 if (mount(MOUNT_TMPFS
, canon_dir
, mntflags
, &args
, sizeof args
) == -1)
182 err(EXIT_FAILURE
, "tmpfs on %s", canon_dir
);
184 if (mntflags
& MNT_GETARGS
) {
188 (void)printf("version=%d, ", args
.ta_version
);
189 (void)printf("size_max=%" PRIuMAX
", ",
190 (uintmax_t)args
.ta_size_max
);
191 (void)printf("nodes_max=%" PRIuMAX
", ",
192 (uintmax_t)args
.ta_nodes_max
);
194 pw
= getpwuid(args
.ta_root_uid
);
196 (void)printf("root_uid=%" PRIuMAX
", ",
197 (uintmax_t)args
.ta_root_uid
);
199 (void)printf("root_uid=%s, ", pw
->pw_name
);
201 gr
= getgrgid(args
.ta_root_gid
);
203 (void)printf("root_gid=%" PRIuMAX
", ",
204 (uintmax_t)args
.ta_root_gid
);
206 (void)printf("root_gid=%s, ", gr
->gr_name
);
208 (void)printf("root_mode=%o\n", args
.ta_root_mode
);
216 main(int argc
, char *argv
[])
219 setprogname(argv
[0]);
220 return mount_tmpfs(argc
, argv
);