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