Home | History | Annotate | Line # | Download | only in vnconfig
vnconfig.c revision 1.7
      1 /*
      2  * Copyright (c) 1993 University of Utah.
      3  * Copyright (c) 1990, 1993
      4  *	The Regents of the University of California.  All rights reserved.
      5  *
      6  * This code is derived from software contributed to Berkeley by
      7  * the Systems Programming Group of the University of Utah Computer
      8  * Science Department.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  * from: Utah $Hdr: vnconfig.c 1.1 93/12/15$
     39  *
     40  *	@(#)vnconfig.c	8.1 (Berkeley) 12/15/93
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/ioctl.h>
     45 #include <sys/mount.h>
     46 #include <sys/stat.h>
     47 
     48 #include <dev/vndioctl.h>
     49 
     50 #include <err.h>
     51 #include <errno.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 
     56 #define VND_CONFIG	1
     57 #define VND_UNCONFIG	2
     58 
     59 int verbose = 0;
     60 
     61 char *rawdevice __P((char *));
     62 void usage __P((void));
     63 
     64 main(argc, argv)
     65 	int argc;
     66 	char **argv;
     67 {
     68 	int ch, rv, action = VND_CONFIG;
     69 
     70 	while ((ch = getopt(argc, argv, "cuv")) != -1) {
     71 		switch (ch) {
     72 		case 'c':
     73 			action = VND_CONFIG;
     74 			break;
     75 		case 'u':
     76 			action = VND_UNCONFIG;
     77 			break;
     78 		case 'v':
     79 			verbose = 1;
     80 			break;
     81 		default:
     82 		case '?':
     83 			usage();
     84 			/* NOTREACHED */
     85 		}
     86 	}
     87 	argc -= optind;
     88 	argv += optind;
     89 
     90 	if (action == VND_CONFIG && argc == 2)
     91 		rv = config(argv[0], argv[1], action);
     92 	else if (action == VND_UNCONFIG && argc == 1)
     93 		rv = config(argv[0], NULL, action);
     94 	else
     95 		usage();
     96 	exit(rv);
     97 }
     98 
     99 config(dev, file, action)
    100 	char *dev;
    101 	char *file;
    102 	int action;
    103 {
    104 	struct vnd_ioctl vndio;
    105 	FILE *f;
    106 	char *rdev;
    107 	int rv;
    108 
    109 	rdev = rawdevice(dev);
    110 	f = fopen(rdev, "rw");
    111 	if (f == NULL) {
    112 		warn(rdev);
    113 		return (1);
    114 	}
    115 	vndio.vnd_file = file;
    116 
    117 	/*
    118 	 * Clear (un-configure) the device
    119 	 */
    120 	if (action == VND_UNCONFIG) {
    121 		rv = ioctl(fileno(f), VNDIOCCLR, &vndio);
    122 		if (rv)
    123 			warn("VNDIOCCLR");
    124 		else if (verbose)
    125 			printf("%s: cleared\n", dev);
    126 	}
    127 	/*
    128 	 * Configure the device
    129 	 */
    130 	if (action == VND_CONFIG) {
    131 		rv = ioctl(fileno(f), VNDIOCSET, &vndio);
    132 		if (rv)
    133 			warn("VNDIOCSET");
    134 		else if (verbose)
    135 			printf("%s: %d bytes on %s\n", dev, vndio.vnd_size,
    136 			    file);
    137 	}
    138 done:
    139 	fclose(f);
    140 	fflush(stdout);
    141 	return (rv < 0);
    142 }
    143 
    144 char *
    145 rawdevice(dev)
    146 	char *dev;
    147 {
    148 	register char *rawbuf, *dp, *ep;
    149 	struct stat sb;
    150 	int len;
    151 
    152 	len = strlen(dev);
    153 	rawbuf = malloc(len + 2);
    154 	strcpy(rawbuf, dev);
    155 	if (stat(rawbuf, &sb) != 0 || !S_ISCHR(sb.st_mode)) {
    156 		dp = rindex(rawbuf, '/');
    157 		if (dp) {
    158 			for (ep = &rawbuf[len]; ep > dp; --ep)
    159 				*(ep+1) = *ep;
    160 			*++ep = 'r';
    161 		}
    162 	}
    163 	return (rawbuf);
    164 }
    165 
    166 void
    167 usage()
    168 {
    169 
    170 	(void)fprintf(stderr, "%s%s",
    171 	    "usage: vnconfig -c [-v] special-file regular-file\n",
    172 	    "       vnconfig -u [-v] special-file\n");
    173 	exit(1);
    174 }
    175