vnconfig.c revision 1.4 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/vnioctl.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 VN_CONFIG 0x01
57 #define VN_UNCONFIG 0x02
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 i, rv, flags = 0;
69
70 while ((i = getopt(argc, argv, "cuv")) != EOF)
71 switch (i) {
72 case 'c':
73 flags |= VN_CONFIG;
74 flags &= ~VN_UNCONFIG;
75 break;
76 case 'u':
77 flags |= VN_UNCONFIG;
78 flags &= ~VN_CONFIG;
79 break;
80 case 'v':
81 verbose++;
82 break;
83 default:
84 fprintf(stderr, "invalid option '%c'\n", optopt);
85 usage();
86 /* NOTREACHED */
87 }
88
89 if (flags == 0)
90 flags = VN_CONFIG;
91 if (argc < optind + 2)
92 usage();
93 rv = config(argv[optind], argv[optind + 1], flags);
94 exit(rv);
95 }
96
97 config(dev, file, flags)
98 char *dev;
99 char *file;
100 int flags;
101 {
102 struct vn_ioctl vnio;
103 FILE *f;
104 char *rdev;
105 int rv;
106
107 rdev = rawdevice(dev);
108 f = fopen(rdev, "rw");
109 if (f == NULL) {
110 warn("opening file");
111 return(1);
112 }
113 vnio.vn_file = file;
114
115 /*
116 * Clear (un-configure) the device
117 */
118 if (flags & VN_UNCONFIG) {
119 rv = ioctl(fileno(f), VNIOCCLR, &vnio);
120 if (rv) {
121 if (errno == ENODEV) {
122 if (verbose)
123 printf("%s: not configured\n", dev);
124 rv = 0;
125 } else
126 warn("VNIOCCLR");
127 } else if (verbose)
128 printf("%s: cleared\n", dev);
129 }
130 /*
131 * Configure the device
132 */
133 if (flags & VN_CONFIG) {
134 rv = ioctl(fileno(f), VNIOCSET, &vnio);
135 if (rv) {
136 warn("VNIOCSET");
137 } else if (verbose)
138 printf("%s: %d bytes on %s\n",
139 dev, vnio.vn_size, file);
140 }
141 done:
142 fclose(f);
143 fflush(stdout);
144 return(rv < 0);
145 }
146
147 char *
148 rawdevice(dev)
149 char *dev;
150 {
151 register char *rawbuf, *dp, *ep;
152 struct stat sb;
153 int len;
154
155 len = strlen(dev);
156 rawbuf = malloc(len + 2);
157 strcpy(rawbuf, dev);
158 if (stat(rawbuf, &sb) != 0 || !S_ISCHR(sb.st_mode)) {
159 dp = rindex(rawbuf, '/');
160 if (dp) {
161 for (ep = &rawbuf[len]; ep > dp; --ep)
162 *(ep+1) = *ep;
163 *++ep = 'r';
164 }
165 }
166 return (rawbuf);
167 }
168
169 void
170 usage()
171 {
172 fprintf(stderr, "usage: vnconfig [-cuv] [special-device file]\n");
173 exit(1);
174 }
175