vnconfig.c revision 1.6 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 1
57 #define VN_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 = VN_CONFIG;
69
70 while ((ch = getopt(argc, argv, "cuv")) != -1) {
71 switch (ch) {
72 case 'c':
73 action = VN_CONFIG;
74 break;
75 case 'u':
76 action = VN_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 == VN_CONFIG && argc == 2)
91 rv = config(argv[0], argv[1], action);
92 else if (action == VN_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 vn_ioctl vnio;
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 vnio.vn_file = file;
116
117 /*
118 * Clear (un-configure) the device
119 */
120 if (action == VN_UNCONFIG) {
121 rv = ioctl(fileno(f), VNIOCCLR, &vnio);
122 if (rv)
123 warn("VNIOCCLR");
124 else if (verbose)
125 printf("%s: cleared\n", dev);
126 }
127 /*
128 * Configure the device
129 */
130 if (action == VN_CONFIG) {
131 rv = ioctl(fileno(f), VNIOCSET, &vnio);
132 if (rv)
133 warn("VNIOCSET");
134 else if (verbose)
135 printf("%s: %d bytes on %s\n", dev, vnio.vn_size, file);
136 }
137 done:
138 fclose(f);
139 fflush(stdout);
140 return (rv < 0);
141 }
142
143 char *
144 rawdevice(dev)
145 char *dev;
146 {
147 register char *rawbuf, *dp, *ep;
148 struct stat sb;
149 int len;
150
151 len = strlen(dev);
152 rawbuf = malloc(len + 2);
153 strcpy(rawbuf, dev);
154 if (stat(rawbuf, &sb) != 0 || !S_ISCHR(sb.st_mode)) {
155 dp = rindex(rawbuf, '/');
156 if (dp) {
157 for (ep = &rawbuf[len]; ep > dp; --ep)
158 *(ep+1) = *ep;
159 *++ep = 'r';
160 }
161 }
162 return (rawbuf);
163 }
164
165 void
166 usage()
167 {
168
169 (void)fprintf(stderr, "usage: vnconfig -c [-v] special-file regular-file\n");
170 (void)fprintf(stderr, " vnconfig -u [-v] special-file\n");
171 exit(1);
172 }
173