Home | History | Annotate | Line # | Download | only in mount_tmpfs
mount_tmpfs.c revision 1.27
      1  1.26    martin 
      2  1.27    martin /*	$NetBSD: mount_tmpfs.c,v 1.27 2013/12/04 15:10:11 martin 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.27    martin __RCSID("$NetBSD: mount_tmpfs.c,v 1.27 2013/12/04 15:10:11 martin 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.1      jmmv 	MOPT_STDOPTS,
     64   1.8      jmmv 	MOPT_GETARGS,
     65  1.15  christos 	MOPT_NULL,
     66   1.1      jmmv };
     67   1.1      jmmv 
     68   1.1      jmmv /* --------------------------------------------------------------------- */
     69   1.1      jmmv 
     70  1.20     perry static void	usage(void) __dead;
     71  1.26    martin static int64_t	ram_fract(const char *arg);
     72  1.26    martin static int64_t	ram_percent(const char *arg);
     73  1.26    martin static int64_t	ram_factor(float f);
     74  1.26    martin 
     75  1.26    martin /* --------------------------------------------------------------------- */
     76  1.26    martin 
     77  1.26    martin /* return f * available system ram */
     78  1.26    martin static int64_t
     79  1.26    martin ram_factor(float f)
     80  1.26    martin {
     81  1.26    martin 	uint64_t ram;
     82  1.26    martin 	size_t len;
     83  1.26    martin 
     84  1.26    martin 	len = sizeof(ram);
     85  1.26    martin 	if (sysctlbyname("hw.physmem64", &ram, &len, NULL, 0))
     86  1.27    martin 		err(EXIT_FAILURE, "can't get \"hw.physmem64\"");
     87  1.26    martin 
     88  1.26    martin 	return (int64_t)((float)ram * f);
     89  1.26    martin }
     90  1.26    martin 
     91  1.26    martin /* return fraction of available ram given by arg */
     92  1.26    martin static int64_t
     93  1.26    martin ram_fract(const char *arg)
     94  1.26    martin {
     95  1.26    martin 	char *endp;
     96  1.26    martin 	float f;
     97  1.26    martin 
     98  1.26    martin 	f = strtof(arg, &endp);
     99  1.26    martin 	if (endp && *endp != 0)
    100  1.27    martin 		errx(EXIT_FAILURE, "syntax error in ram fraction: ram/%s"
    101  1.26    martin 		    " at %s", arg, endp);
    102  1.26    martin 	if (f <= 0.0f)
    103  1.27    martin 		errx(EXIT_FAILURE, "ram fraction must be a positive number:"
    104  1.26    martin 		     " ram/%s", arg);
    105  1.26    martin 
    106  1.26    martin 	return ram_factor(1.0f/f);
    107  1.26    martin }
    108  1.26    martin 
    109  1.26    martin /* --------------------------------------------------------------------- */
    110  1.26    martin 
    111  1.26    martin /* return percentage of available ram given by arg */
    112  1.26    martin static int64_t
    113  1.26    martin ram_percent(const char *arg)
    114  1.26    martin {
    115  1.26    martin 	char *endp;
    116  1.26    martin 	float f;
    117  1.26    martin 
    118  1.26    martin 	f = strtof(arg, &endp);
    119  1.26    martin 	if (endp && *endp != 0)
    120  1.27    martin 		errx(EXIT_FAILURE, "syntax error in ram percentage: ram%%%s"
    121  1.26    martin 		    " at %s", arg, endp);
    122  1.26    martin 	if (f <= 0.0f || f >= 100.0f)
    123  1.27    martin 		errx(EXIT_FAILURE, "ram percentage must be a between 0 and 100"
    124  1.26    martin 		     " ram%%%s", arg);
    125  1.26    martin 
    126  1.26    martin 	return ram_factor(f/100.0f);
    127  1.26    martin }
    128   1.1      jmmv 
    129   1.1      jmmv /* --------------------------------------------------------------------- */
    130   1.1      jmmv 
    131  1.24     pooka void
    132  1.24     pooka mount_tmpfs_parseargs(int argc, char *argv[],
    133  1.24     pooka 	struct tmpfs_args *args, int *mntflags,
    134  1.24     pooka 	char *canon_dev, char *canon_dir)
    135   1.1      jmmv {
    136  1.10      jmmv 	int gidset, modeset, uidset; /* Ought to be 'bool'. */
    137  1.24     pooka 	int ch;
    138   1.4      jmmv 	gid_t gid;
    139   1.4      jmmv 	uid_t uid;
    140   1.4      jmmv 	mode_t mode;
    141  1.19  christos 	int64_t tmpnumber;
    142  1.12  christos 	mntoptparse_t mp;
    143   1.4      jmmv 	struct stat sb;
    144   1.1      jmmv 
    145   1.1      jmmv 	/* Set default values for mount point arguments. */
    146  1.24     pooka 	memset(args, 0, sizeof(*args));
    147  1.24     pooka 	args->ta_version = TMPFS_ARGS_VERSION;
    148  1.24     pooka 	args->ta_size_max = 0;
    149  1.24     pooka 	args->ta_nodes_max = 0;
    150  1.24     pooka 	*mntflags = 0;
    151   1.1      jmmv 
    152  1.10      jmmv 	gidset = 0; gid = 0;
    153  1.10      jmmv 	uidset = 0; uid = 0;
    154  1.10      jmmv 	modeset = 0; mode = 0;
    155   1.4      jmmv 
    156   1.1      jmmv 	optind = optreset = 1;
    157   1.1      jmmv 	while ((ch = getopt(argc, argv, "g:m:n:o:s:u:")) != -1 ) {
    158   1.1      jmmv 		switch (ch) {
    159   1.1      jmmv 		case 'g':
    160  1.19  christos 			gid = a_gid(optarg);
    161  1.10      jmmv 			gidset = 1;
    162   1.1      jmmv 			break;
    163   1.1      jmmv 
    164   1.1      jmmv 		case 'm':
    165  1.19  christos 			mode = a_mask(optarg);
    166  1.10      jmmv 			modeset = 1;
    167   1.1      jmmv 			break;
    168   1.1      jmmv 
    169   1.1      jmmv 		case 'n':
    170  1.19  christos 			if (dehumanize_number(optarg, &tmpnumber) == -1)
    171  1.19  christos 				err(EXIT_FAILURE, "failed to parse nodes `%s'",
    172  1.19  christos 				    optarg);
    173  1.24     pooka 			args->ta_nodes_max = tmpnumber;
    174   1.1      jmmv 			break;
    175   1.1      jmmv 
    176   1.1      jmmv 		case 'o':
    177  1.24     pooka 			mp = getmntopts(optarg, mopts, mntflags, 0);
    178  1.12  christos 			if (mp == NULL)
    179  1.19  christos 				err(EXIT_FAILURE, "getmntopts");
    180  1.12  christos 			freemntopts(mp);
    181   1.1      jmmv 			break;
    182   1.1      jmmv 
    183   1.1      jmmv 		case 's':
    184  1.26    martin 			if (strncmp(optarg, "ram/", 4) == 0)
    185  1.26    martin 				tmpnumber = ram_fract(optarg+4);
    186  1.26    martin 			else if (strncmp(optarg, "ram%", 4) == 0)
    187  1.26    martin 				tmpnumber = ram_percent(optarg+4);
    188  1.26    martin 			else if (dehumanize_number(optarg, &tmpnumber) == -1)
    189  1.19  christos 				err(EXIT_FAILURE, "failed to parse size `%s'",
    190  1.19  christos 				    optarg);
    191  1.24     pooka 			args->ta_size_max = tmpnumber;
    192   1.1      jmmv 			break;
    193   1.1      jmmv 
    194   1.1      jmmv 		case 'u':
    195  1.19  christos 			uid = a_uid(optarg);
    196  1.10      jmmv 			uidset = 1;
    197   1.1      jmmv 			break;
    198   1.1      jmmv 
    199   1.1      jmmv 		case '?':
    200   1.1      jmmv 		default:
    201   1.1      jmmv 			usage();
    202   1.1      jmmv 		}
    203   1.1      jmmv 	}
    204   1.1      jmmv 	argc -= optind;
    205   1.1      jmmv 	argv += optind;
    206   1.1      jmmv 
    207  1.19  christos 	if (argc != 2)
    208   1.1      jmmv 		usage();
    209   1.1      jmmv 
    210  1.24     pooka 	strlcpy(canon_dev, argv[0], MAXPATHLEN);
    211  1.24     pooka 	pathadj(argv[1], canon_dir);
    212   1.1      jmmv 
    213  1.19  christos 	if (stat(canon_dir, &sb) == -1)
    214  1.19  christos 		err(EXIT_FAILURE, "cannot stat `%s'", canon_dir);
    215  1.19  christos 
    216  1.24     pooka 	args->ta_root_uid = uidset ? uid : sb.st_uid;
    217  1.24     pooka 	args->ta_root_gid = gidset ? gid : sb.st_gid;
    218  1.24     pooka 	args->ta_root_mode = modeset ? mode : sb.st_mode;
    219  1.24     pooka }
    220  1.24     pooka 
    221  1.24     pooka /* --------------------------------------------------------------------- */
    222  1.24     pooka 
    223  1.24     pooka static void
    224  1.24     pooka usage(void)
    225  1.24     pooka {
    226  1.24     pooka 	(void)fprintf(stderr,
    227  1.25       wiz 	    "usage: %s [-g group] [-m mode] [-n nodes] [-o options] [-s size]\n"
    228  1.25       wiz 	    "           [-u user] tmpfs mount_point\n", getprogname());
    229  1.24     pooka 	exit(1);
    230  1.24     pooka }
    231  1.24     pooka 
    232  1.24     pooka /* --------------------------------------------------------------------- */
    233  1.24     pooka 
    234  1.24     pooka int
    235  1.24     pooka mount_tmpfs(int argc, char *argv[])
    236  1.24     pooka {
    237  1.24     pooka 	struct tmpfs_args args;
    238  1.24     pooka 	char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
    239  1.24     pooka 	int mntflags;
    240  1.24     pooka 
    241  1.24     pooka 	mount_tmpfs_parseargs(argc, argv, &args, &mntflags,
    242  1.24     pooka 	    canon_dev, canon_dir);
    243   1.4      jmmv 
    244  1.19  christos 	if (mount(MOUNT_TMPFS, canon_dir, mntflags, &args, sizeof args) == -1)
    245   1.1      jmmv 		err(EXIT_FAILURE, "tmpfs on %s", canon_dir);
    246  1.19  christos 
    247   1.8      jmmv 	if (mntflags & MNT_GETARGS) {
    248   1.8      jmmv 		struct passwd *pw;
    249   1.8      jmmv 		struct group *gr;
    250   1.8      jmmv 
    251  1.18     pooka 		(void)printf("version=%d, ", args.ta_version);
    252  1.18     pooka 		(void)printf("size_max=%" PRIuMAX ", ",
    253   1.8      jmmv 		    (uintmax_t)args.ta_size_max);
    254  1.18     pooka 		(void)printf("nodes_max=%" PRIuMAX ", ",
    255   1.8      jmmv 		    (uintmax_t)args.ta_nodes_max);
    256   1.8      jmmv 
    257   1.8      jmmv 		pw = getpwuid(args.ta_root_uid);
    258   1.8      jmmv 		if (pw == NULL)
    259  1.18     pooka 			(void)printf("root_uid=%" PRIuMAX ", ",
    260   1.8      jmmv 			    (uintmax_t)args.ta_root_uid);
    261   1.8      jmmv 		else
    262  1.18     pooka 			(void)printf("root_uid=%s, ", pw->pw_name);
    263   1.8      jmmv 
    264   1.8      jmmv 		gr = getgrgid(args.ta_root_gid);
    265   1.8      jmmv 		if (gr == NULL)
    266  1.18     pooka 			(void)printf("root_gid=%" PRIuMAX ", ",
    267   1.8      jmmv 			    (uintmax_t)args.ta_root_gid);
    268   1.8      jmmv 		else
    269  1.18     pooka 			(void)printf("root_gid=%s, ", gr->gr_name);
    270   1.8      jmmv 
    271   1.8      jmmv 		(void)printf("root_mode=%o\n", args.ta_root_mode);
    272   1.8      jmmv 	}
    273   1.1      jmmv 
    274   1.1      jmmv 	return EXIT_SUCCESS;
    275   1.1      jmmv }
    276   1.1      jmmv 
    277   1.1      jmmv #ifndef MOUNT_NOMAIN
    278   1.1      jmmv int
    279   1.1      jmmv main(int argc, char *argv[])
    280   1.1      jmmv {
    281   1.1      jmmv 
    282  1.24     pooka 	setprogname(argv[0]);
    283   1.1      jmmv 	return mount_tmpfs(argc, argv);
    284   1.1      jmmv }
    285   1.1      jmmv #endif
    286