Home | History | Annotate | Line # | Download | only in mount_tmpfs
mount_tmpfs.c revision 1.24.24.1
      1  1.24.24.1       tls /*	$NetBSD: mount_tmpfs.c,v 1.24.24.1 2013/06/23 06:28:52 tls 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  *
     20        1.1      jmmv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21        1.1      jmmv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22        1.1      jmmv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23        1.1      jmmv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24        1.1      jmmv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25        1.1      jmmv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26        1.1      jmmv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27        1.1      jmmv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28        1.1      jmmv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29        1.1      jmmv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30        1.1      jmmv  * POSSIBILITY OF SUCH DAMAGE.
     31        1.1      jmmv  */
     32        1.1      jmmv 
     33        1.1      jmmv #include <sys/cdefs.h>
     34        1.1      jmmv #ifndef lint
     35  1.24.24.1       tls __RCSID("$NetBSD: mount_tmpfs.c,v 1.24.24.1 2013/06/23 06:28:52 tls Exp $");
     36        1.1      jmmv #endif /* not lint */
     37        1.1      jmmv 
     38        1.1      jmmv #include <sys/param.h>
     39        1.1      jmmv #include <sys/mount.h>
     40        1.4      jmmv #include <sys/stat.h>
     41        1.1      jmmv 
     42       1.23     pooka #include <fs/tmpfs/tmpfs_args.h>
     43        1.1      jmmv 
     44        1.1      jmmv #include <ctype.h>
     45        1.1      jmmv #include <err.h>
     46        1.1      jmmv #include <errno.h>
     47        1.1      jmmv #include <grp.h>
     48        1.1      jmmv #include <mntopts.h>
     49        1.1      jmmv #include <pwd.h>
     50        1.1      jmmv #include <stdio.h>
     51        1.1      jmmv #include <stdlib.h>
     52        1.1      jmmv #include <string.h>
     53        1.1      jmmv #include <unistd.h>
     54       1.24     pooka 
     55       1.24     pooka #include "mountprog.h"
     56       1.24     pooka #include "mount_tmpfs.h"
     57        1.1      jmmv 
     58        1.1      jmmv /* --------------------------------------------------------------------- */
     59        1.1      jmmv 
     60        1.1      jmmv static const struct mntopt mopts[] = {
     61        1.1      jmmv 	MOPT_STDOPTS,
     62        1.8      jmmv 	MOPT_GETARGS,
     63       1.15  christos 	MOPT_NULL,
     64        1.1      jmmv };
     65        1.1      jmmv 
     66        1.1      jmmv /* --------------------------------------------------------------------- */
     67        1.1      jmmv 
     68       1.20     perry static void	usage(void) __dead;
     69        1.1      jmmv 
     70        1.1      jmmv /* --------------------------------------------------------------------- */
     71        1.1      jmmv 
     72       1.24     pooka void
     73       1.24     pooka mount_tmpfs_parseargs(int argc, char *argv[],
     74       1.24     pooka 	struct tmpfs_args *args, int *mntflags,
     75       1.24     pooka 	char *canon_dev, char *canon_dir)
     76        1.1      jmmv {
     77       1.10      jmmv 	int gidset, modeset, uidset; /* Ought to be 'bool'. */
     78       1.24     pooka 	int ch;
     79        1.4      jmmv 	gid_t gid;
     80        1.4      jmmv 	uid_t uid;
     81        1.4      jmmv 	mode_t mode;
     82       1.19  christos 	int64_t tmpnumber;
     83       1.12  christos 	mntoptparse_t mp;
     84        1.4      jmmv 	struct stat sb;
     85        1.1      jmmv 
     86        1.1      jmmv 	/* Set default values for mount point arguments. */
     87       1.24     pooka 	memset(args, 0, sizeof(*args));
     88       1.24     pooka 	args->ta_version = TMPFS_ARGS_VERSION;
     89       1.24     pooka 	args->ta_size_max = 0;
     90       1.24     pooka 	args->ta_nodes_max = 0;
     91       1.24     pooka 	*mntflags = 0;
     92        1.1      jmmv 
     93       1.10      jmmv 	gidset = 0; gid = 0;
     94       1.10      jmmv 	uidset = 0; uid = 0;
     95       1.10      jmmv 	modeset = 0; mode = 0;
     96        1.4      jmmv 
     97        1.1      jmmv 	optind = optreset = 1;
     98        1.1      jmmv 	while ((ch = getopt(argc, argv, "g:m:n:o:s:u:")) != -1 ) {
     99        1.1      jmmv 		switch (ch) {
    100        1.1      jmmv 		case 'g':
    101       1.19  christos 			gid = a_gid(optarg);
    102       1.10      jmmv 			gidset = 1;
    103        1.1      jmmv 			break;
    104        1.1      jmmv 
    105        1.1      jmmv 		case 'm':
    106       1.19  christos 			mode = a_mask(optarg);
    107       1.10      jmmv 			modeset = 1;
    108        1.1      jmmv 			break;
    109        1.1      jmmv 
    110        1.1      jmmv 		case 'n':
    111       1.19  christos 			if (dehumanize_number(optarg, &tmpnumber) == -1)
    112       1.19  christos 				err(EXIT_FAILURE, "failed to parse nodes `%s'",
    113       1.19  christos 				    optarg);
    114       1.24     pooka 			args->ta_nodes_max = tmpnumber;
    115        1.1      jmmv 			break;
    116        1.1      jmmv 
    117        1.1      jmmv 		case 'o':
    118       1.24     pooka 			mp = getmntopts(optarg, mopts, mntflags, 0);
    119       1.12  christos 			if (mp == NULL)
    120       1.19  christos 				err(EXIT_FAILURE, "getmntopts");
    121       1.12  christos 			freemntopts(mp);
    122        1.1      jmmv 			break;
    123        1.1      jmmv 
    124        1.1      jmmv 		case 's':
    125       1.19  christos 			if (dehumanize_number(optarg, &tmpnumber) == -1)
    126       1.19  christos 				err(EXIT_FAILURE, "failed to parse size `%s'",
    127       1.19  christos 				    optarg);
    128       1.24     pooka 			args->ta_size_max = tmpnumber;
    129        1.1      jmmv 			break;
    130        1.1      jmmv 
    131        1.1      jmmv 		case 'u':
    132       1.19  christos 			uid = a_uid(optarg);
    133       1.10      jmmv 			uidset = 1;
    134        1.1      jmmv 			break;
    135        1.1      jmmv 
    136        1.1      jmmv 		case '?':
    137        1.1      jmmv 		default:
    138        1.1      jmmv 			usage();
    139        1.1      jmmv 		}
    140        1.1      jmmv 	}
    141        1.1      jmmv 	argc -= optind;
    142        1.1      jmmv 	argv += optind;
    143        1.1      jmmv 
    144       1.19  christos 	if (argc != 2)
    145        1.1      jmmv 		usage();
    146        1.1      jmmv 
    147       1.24     pooka 	strlcpy(canon_dev, argv[0], MAXPATHLEN);
    148       1.24     pooka 	pathadj(argv[1], canon_dir);
    149        1.1      jmmv 
    150       1.19  christos 	if (stat(canon_dir, &sb) == -1)
    151       1.19  christos 		err(EXIT_FAILURE, "cannot stat `%s'", canon_dir);
    152       1.19  christos 
    153       1.24     pooka 	args->ta_root_uid = uidset ? uid : sb.st_uid;
    154       1.24     pooka 	args->ta_root_gid = gidset ? gid : sb.st_gid;
    155       1.24     pooka 	args->ta_root_mode = modeset ? mode : sb.st_mode;
    156       1.24     pooka }
    157       1.24     pooka 
    158       1.24     pooka /* --------------------------------------------------------------------- */
    159       1.24     pooka 
    160       1.24     pooka static void
    161       1.24     pooka usage(void)
    162       1.24     pooka {
    163       1.24     pooka 	(void)fprintf(stderr,
    164  1.24.24.1       tls 	    "usage: %s [-g group] [-m mode] [-n nodes] [-o options] [-s size]\n"
    165  1.24.24.1       tls 	    "           [-u user] tmpfs mount_point\n", getprogname());
    166       1.24     pooka 	exit(1);
    167       1.24     pooka }
    168       1.24     pooka 
    169       1.24     pooka /* --------------------------------------------------------------------- */
    170       1.24     pooka 
    171       1.24     pooka int
    172       1.24     pooka mount_tmpfs(int argc, char *argv[])
    173       1.24     pooka {
    174       1.24     pooka 	struct tmpfs_args args;
    175       1.24     pooka 	char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
    176       1.24     pooka 	int mntflags;
    177       1.24     pooka 
    178       1.24     pooka 	mount_tmpfs_parseargs(argc, argv, &args, &mntflags,
    179       1.24     pooka 	    canon_dev, canon_dir);
    180        1.4      jmmv 
    181       1.19  christos 	if (mount(MOUNT_TMPFS, canon_dir, mntflags, &args, sizeof args) == -1)
    182        1.1      jmmv 		err(EXIT_FAILURE, "tmpfs on %s", canon_dir);
    183       1.19  christos 
    184        1.8      jmmv 	if (mntflags & MNT_GETARGS) {
    185        1.8      jmmv 		struct passwd *pw;
    186        1.8      jmmv 		struct group *gr;
    187        1.8      jmmv 
    188       1.18     pooka 		(void)printf("version=%d, ", args.ta_version);
    189       1.18     pooka 		(void)printf("size_max=%" PRIuMAX ", ",
    190        1.8      jmmv 		    (uintmax_t)args.ta_size_max);
    191       1.18     pooka 		(void)printf("nodes_max=%" PRIuMAX ", ",
    192        1.8      jmmv 		    (uintmax_t)args.ta_nodes_max);
    193        1.8      jmmv 
    194        1.8      jmmv 		pw = getpwuid(args.ta_root_uid);
    195        1.8      jmmv 		if (pw == NULL)
    196       1.18     pooka 			(void)printf("root_uid=%" PRIuMAX ", ",
    197        1.8      jmmv 			    (uintmax_t)args.ta_root_uid);
    198        1.8      jmmv 		else
    199       1.18     pooka 			(void)printf("root_uid=%s, ", pw->pw_name);
    200        1.8      jmmv 
    201        1.8      jmmv 		gr = getgrgid(args.ta_root_gid);
    202        1.8      jmmv 		if (gr == NULL)
    203       1.18     pooka 			(void)printf("root_gid=%" PRIuMAX ", ",
    204        1.8      jmmv 			    (uintmax_t)args.ta_root_gid);
    205        1.8      jmmv 		else
    206       1.18     pooka 			(void)printf("root_gid=%s, ", gr->gr_name);
    207        1.8      jmmv 
    208        1.8      jmmv 		(void)printf("root_mode=%o\n", args.ta_root_mode);
    209        1.8      jmmv 	}
    210        1.1      jmmv 
    211        1.1      jmmv 	return EXIT_SUCCESS;
    212        1.1      jmmv }
    213        1.1      jmmv 
    214        1.1      jmmv #ifndef MOUNT_NOMAIN
    215        1.1      jmmv int
    216        1.1      jmmv main(int argc, char *argv[])
    217        1.1      jmmv {
    218        1.1      jmmv 
    219       1.24     pooka 	setprogname(argv[0]);
    220        1.1      jmmv 	return mount_tmpfs(argc, argv);
    221        1.1      jmmv }
    222        1.1      jmmv #endif
    223