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