Home | History | Annotate | Line # | Download | only in mount_udf
mount_udf.c revision 1.12.2.1
      1  1.12.2.1       snj /* $NetBSD: mount_udf.c,v 1.12.2.1 2009/02/18 00:54:06 snj Exp $ */
      2       1.1   reinoud 
      3       1.1   reinoud /*
      4       1.1   reinoud  * Copyright (c) 2006 Reinoud Zandijk
      5       1.1   reinoud  * All rights reserved.
      6       1.1   reinoud  *
      7       1.1   reinoud  * Redistribution and use in source and binary forms, with or without
      8       1.1   reinoud  * modification, are permitted provided that the following conditions
      9       1.1   reinoud  * are met:
     10       1.1   reinoud  * 1. Redistributions of source code must retain the above copyright
     11       1.1   reinoud  *    notice, this list of conditions and the following disclaimer.
     12       1.1   reinoud  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1   reinoud  *    notice, this list of conditions and the following disclaimer in the
     14       1.1   reinoud  *    documentation and/or other materials provided with the distribution.
     15       1.1   reinoud  * 3. All advertising materials mentioning features or use of this software
     16       1.1   reinoud  *    must display the following acknowledgement:
     17       1.1   reinoud  *          This product includes software developed for the
     18       1.1   reinoud  *          NetBSD Project.  See http://www.NetBSD.org/ for
     19       1.1   reinoud  *          information about NetBSD.
     20       1.1   reinoud  * 4. The name of the author may not be used to endorse or promote products
     21       1.1   reinoud  *    derived from this software without specific prior written permission.
     22       1.1   reinoud  *
     23       1.1   reinoud  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24       1.1   reinoud  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25       1.1   reinoud  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26       1.1   reinoud  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27       1.1   reinoud  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28       1.1   reinoud  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29       1.1   reinoud  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30       1.1   reinoud  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31       1.1   reinoud  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32       1.1   reinoud  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33       1.1   reinoud  *
     34       1.1   reinoud  */
     35       1.1   reinoud 
     36       1.1   reinoud 
     37       1.1   reinoud #include <sys/cdefs.h>
     38       1.1   reinoud #ifndef lint
     39  1.12.2.1       snj __RCSID("$NetBSD: mount_udf.c,v 1.12.2.1 2009/02/18 00:54:06 snj Exp $");
     40       1.1   reinoud #endif /* not lint */
     41       1.1   reinoud 
     42       1.1   reinoud 
     43       1.1   reinoud #include <sys/param.h>
     44       1.1   reinoud #include <sys/mount.h>
     45       1.1   reinoud #include <sys/stat.h>
     46       1.8   hubertf 
     47       1.8   hubertf #include <assert.h>
     48       1.1   reinoud #include <err.h>
     49       1.8   hubertf #include <errno.h>
     50       1.1   reinoud #include <grp.h>
     51       1.1   reinoud #include <pwd.h>
     52       1.1   reinoud #include <stdio.h>
     53       1.1   reinoud #include <stdlib.h>
     54       1.1   reinoud #include <string.h>
     55       1.1   reinoud #include <time.h>
     56       1.1   reinoud #include <unistd.h>
     57       1.1   reinoud #include <util.h>
     58       1.1   reinoud 
     59       1.1   reinoud 
     60       1.1   reinoud /* mount specific options */
     61       1.1   reinoud #include <fs/udf/udf_mount.h>
     62       1.1   reinoud #include <mntopts.h>
     63       1.1   reinoud 
     64      1.12     pooka #include "mountprog.h"
     65      1.12     pooka #include "mount_udf.h"
     66       1.1   reinoud 
     67       1.1   reinoud /* options to pass on to the `mount' call */
     68       1.1   reinoud static const struct mntopt mopts[] = {
     69       1.7   reinoud 	MOPT_STDOPTS,		/* `normal' options		*/
     70       1.7   reinoud 	MOPT_ASYNC,		/* default			*/
     71       1.7   reinoud 	MOPT_NOATIME,		/* dont update access times	*/
     72       1.7   reinoud 	MOPT_UPDATE,		/* not yet supported		*/
     73       1.7   reinoud 	MOPT_GETARGS,		/* printing			*/
     74       1.6  christos 	MOPT_NULL,
     75       1.1   reinoud };
     76       1.1   reinoud 
     77       1.1   reinoud 
     78       1.1   reinoud /* prototypes */
     79      1.11     perry static void	usage(void) __dead;
     80       1.1   reinoud 
     81       1.1   reinoud 
     82       1.1   reinoud /* code */
     83       1.1   reinoud 
     84       1.1   reinoud static void
     85       1.4  christos usage(void)
     86       1.1   reinoud {
     87       1.4  christos 	(void)fprintf(stderr, "Usage: %s [-g gid] [-o options] [-s session] "
     88       1.4  christos 	    "[-t gmtoff] [-u uid] special node\n", getprogname());
     89       1.1   reinoud 	exit(EXIT_FAILURE);
     90       1.1   reinoud }
     91       1.1   reinoud 
     92       1.1   reinoud 
     93       1.1   reinoud #ifndef MOUNT_NOMAIN
     94       1.1   reinoud int
     95       1.1   reinoud main(int argc, char **argv)
     96       1.1   reinoud {
     97      1.12     pooka 
     98      1.12     pooka 	setprogname(argv[0]);
     99       1.1   reinoud 	return mount_udf(argc, argv);
    100       1.1   reinoud }
    101       1.1   reinoud #endif
    102       1.1   reinoud 
    103       1.1   reinoud 
    104       1.1   reinoud /* main routine */
    105      1.12     pooka void
    106      1.12     pooka mount_udf_parseargs(int argc, char **argv,
    107      1.12     pooka 	struct udf_args *args, int *mntflags,
    108      1.12     pooka 	char *canon_dev, char *canon_dir)
    109       1.1   reinoud {
    110       1.1   reinoud 	struct tm *tm;
    111       1.1   reinoud 	time_t	 now;
    112       1.1   reinoud 	uid_t	 anon_uid, nobody_uid;
    113       1.1   reinoud 	gid_t	 anon_gid, nobody_gid;
    114      1.12     pooka 	int	 ch, set_gmtoff;
    115       1.1   reinoud 	uint32_t sector_size;
    116       1.5  christos 	mntoptparse_t mp;
    117       1.1   reinoud 
    118       1.1   reinoud 	/* initialise */
    119      1.12     pooka 	(void)memset(args, 0, sizeof(*args));
    120       1.1   reinoud 
    121      1.12     pooka 	set_gmtoff = *mntflags = 0;
    122       1.1   reinoud 	sector_size = 0;
    123       1.1   reinoud 
    124       1.1   reinoud 	/* get nobody */
    125       1.4  christos 	nobody_uid = anon_uid = a_uid("nobody");
    126       1.4  christos 	nobody_gid = anon_gid = a_gid("nobody");
    127       1.1   reinoud 
    128       1.1   reinoud 	/* NEVER EVER allow nobody_uid:nobody_gid to be 0:0 */
    129       1.1   reinoud 	assert(nobody_uid != 0);
    130       1.1   reinoud 	assert(nobody_gid != 0);
    131       1.1   reinoud 
    132       1.1   reinoud 	while ((ch = getopt(argc, argv, "cg:o:s:t:u:")) != -1) {
    133       1.1   reinoud 		switch (ch) {
    134       1.1   reinoud 		case 'c' :
    135      1.12     pooka 			args->udfmflags |= UDFMNT_CLOSESESSION;
    136       1.1   reinoud 			break;
    137       1.1   reinoud 		case 'g' :
    138       1.1   reinoud 			/* convert groupname or numeric equiv. */
    139       1.4  christos 			anon_gid = a_gid(optarg);
    140       1.1   reinoud 			break;
    141       1.1   reinoud 		case 'u' :
    142       1.1   reinoud 			/* convert username or numeric equiv. */
    143       1.4  christos 			anon_uid = a_uid(optarg);
    144       1.1   reinoud 			break;
    145       1.1   reinoud 		case 'o' :
    146       1.1   reinoud 			/* process generic mount options */
    147      1.12     pooka 			mp = getmntopts(optarg, mopts, mntflags, 0);
    148       1.5  christos 			if (mp == NULL)
    149       1.5  christos 				err(EXIT_FAILURE, "getmntopts");
    150       1.5  christos 			freemntopts(mp);
    151       1.1   reinoud 			break;
    152       1.1   reinoud 		case 's' :
    153      1.12     pooka 			args->sessionnr = a_num(optarg, "session number");
    154       1.1   reinoud 			break;
    155       1.1   reinoud 		case 't' :
    156      1.12     pooka 			args->gmtoff = a_num(optarg, "gmtoff");
    157       1.1   reinoud 			set_gmtoff  = 1;
    158       1.1   reinoud 			break;
    159       1.1   reinoud 		default  :
    160       1.1   reinoud 			usage();
    161       1.3   xtraeme 			/* NOTREACHED */
    162       1.3   xtraeme 		}
    163       1.3   xtraeme 	}
    164       1.1   reinoud 
    165       1.1   reinoud 	if (optind + 2 != argc)
    166       1.1   reinoud 		usage();
    167       1.1   reinoud 
    168       1.1   reinoud 	if (!set_gmtoff) {
    169       1.1   reinoud 		/* use user's time zone as default */
    170       1.4  christos 		(void)time(&now);
    171       1.1   reinoud 		tm = localtime(&now);
    172      1.12     pooka 		args->gmtoff = tm->tm_gmtoff;
    173       1.1   reinoud 	}
    174       1.1   reinoud 
    175       1.1   reinoud 	/* get device and directory specifier */
    176      1.12     pooka 	pathadj(argv[optind], canon_dev);
    177      1.12     pooka 	pathadj(argv[optind+1], canon_dir);
    178      1.12     pooka 
    179      1.12     pooka 	args->version = UDFMNT_VERSION;
    180      1.12     pooka 	args->fspec = canon_dev;
    181      1.12     pooka 	args->anon_uid    = anon_uid;
    182      1.12     pooka 	args->anon_gid    = anon_gid;
    183      1.12     pooka 	args->nobody_uid  = nobody_uid;
    184      1.12     pooka 	args->nobody_gid  = nobody_gid;
    185      1.12     pooka 	args->sector_size = sector_size;		/* invalid */
    186      1.12     pooka }
    187      1.12     pooka 
    188      1.12     pooka int
    189      1.12     pooka mount_udf(int argc, char *argv[])
    190      1.12     pooka {
    191      1.12     pooka 	struct udf_args args;
    192      1.12     pooka 	char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
    193      1.12     pooka 	int mntflags;
    194       1.4  christos 
    195      1.12     pooka 	mount_udf_parseargs(argc, argv, &args, &mntflags, canon_dev, canon_dir);
    196       1.1   reinoud 
    197       1.1   reinoud 	/* mount it! :) */
    198      1.12     pooka 	if (mount(MOUNT_UDF, canon_dir, mntflags, &args, sizeof args) == -1)
    199      1.12     pooka 		err(EXIT_FAILURE, "Cannot mount %s on %s", canon_dev,canon_dir);
    200       1.1   reinoud 
    201       1.1   reinoud 	if (mntflags & MNT_GETARGS) {
    202       1.1   reinoud 		char buf[1024];
    203       1.1   reinoud 
    204       1.4  christos 		(void)snprintb(buf, sizeof(buf), UDFMNT_BITS,
    205       1.4  christos 		    (uint64_t)args.udfmflags);
    206       1.4  christos 		(void)printf("gmtoffset=%d, sessionnr=%d, flags=%s\n",
    207       1.4  christos 		    args.gmtoff, args.sessionnr, buf);
    208       1.1   reinoud 	}
    209       1.1   reinoud 
    210       1.4  christos 	return EXIT_SUCCESS;
    211       1.1   reinoud }
    212