Ignore machine-check MSRs
[freebsd-src/fkvm-freebsd.git] / usr.sbin / rmt / rmt.c
blobaf4f954e07b9334ba40b7c16ce6d6d9ee28a333a
1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
30 #if 0
31 #ifndef lint
32 static const char copyright[] =
33 "@(#) Copyright (c) 1983, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
35 #endif /* not lint */
37 #ifndef lint
38 static char sccsid[] = "@(#)rmt.c 8.1 (Berkeley) 6/6/93";
39 #endif /* not lint */
40 #endif
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
45 * rmt
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <sys/mtio.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
57 int tape = -1;
59 char *record;
60 int maxrecsize = -1;
62 #define SSIZE 64
63 char device[SSIZE];
64 char count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
66 char resp[BUFSIZ];
68 FILE *debug;
69 #define DEBUG(f) if (debug) fprintf(debug, f)
70 #define DEBUG1(f,a) if (debug) fprintf(debug, f, a)
71 #define DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
73 char *checkbuf(char *, int);
74 void error(int);
75 void getstring(char *);
77 int
78 main(int argc, char **argv)
80 int rval;
81 char c;
82 int n, i, cc;
84 argc--, argv++;
85 if (argc > 0) {
86 debug = fopen(*argv, "w");
87 if (debug == 0)
88 exit(1);
89 (void)setbuf(debug, (char *)0);
91 top:
92 errno = 0;
93 rval = 0;
94 if (read(STDIN_FILENO, &c, 1) != 1)
95 exit(0);
96 switch (c) {
98 case 'O':
99 if (tape >= 0)
100 (void) close(tape);
101 getstring(device);
102 getstring(mode);
103 DEBUG2("rmtd: O %s %s\n", device, mode);
105 * XXX the rmt protocol does not provide a means to
106 * specify the permission bits; allow rw for everyone,
107 * as modified by the users umask
109 tape = open(device, atoi(mode), 0666);
110 if (tape < 0)
111 goto ioerror;
112 goto respond;
114 case 'C':
115 DEBUG("rmtd: C\n");
116 getstring(device); /* discard */
117 if (close(tape) < 0)
118 goto ioerror;
119 tape = -1;
120 goto respond;
122 case 'L':
123 getstring(count);
124 getstring(pos);
125 DEBUG2("rmtd: L %s %s\n", count, pos);
126 rval = lseek(tape, (off_t)strtoll(count, NULL, 10), atoi(pos));
127 if (rval < 0)
128 goto ioerror;
129 goto respond;
131 case 'W':
132 getstring(count);
133 n = atoi(count);
134 DEBUG1("rmtd: W %s\n", count);
135 record = checkbuf(record, n);
136 for (i = 0; i < n; i += cc) {
137 cc = read(STDIN_FILENO, &record[i], n - i);
138 if (cc <= 0) {
139 DEBUG("rmtd: premature eof\n");
140 exit(2);
143 rval = write(tape, record, n);
144 if (rval < 0)
145 goto ioerror;
146 goto respond;
148 case 'R':
149 getstring(count);
150 DEBUG1("rmtd: R %s\n", count);
151 n = atoi(count);
152 record = checkbuf(record, n);
153 rval = read(tape, record, n);
154 if (rval < 0)
155 goto ioerror;
156 (void)sprintf(resp, "A%d\n", rval);
157 (void)write(STDOUT_FILENO, resp, strlen(resp));
158 (void)write(STDOUT_FILENO, record, rval);
159 goto top;
161 case 'I':
162 getstring(op);
163 getstring(count);
164 DEBUG2("rmtd: I %s %s\n", op, count);
165 { struct mtop mtop;
166 mtop.mt_op = atoi(op);
167 mtop.mt_count = atoi(count);
168 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
169 goto ioerror;
170 rval = mtop.mt_count;
172 goto respond;
174 case 'S': /* status */
175 DEBUG("rmtd: S\n");
176 { struct mtget mtget;
177 if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
178 goto ioerror;
179 rval = sizeof (mtget);
180 if (rval > 24) /* original mtget structure size */
181 rval = 24;
182 (void)sprintf(resp, "A%d\n", rval);
183 (void)write(STDOUT_FILENO, resp, strlen(resp));
184 (void)write(STDOUT_FILENO, (char *)&mtget, rval);
185 goto top;
188 case 'V': /* version */
189 getstring(op);
190 DEBUG1("rmtd: V %s\n", op);
191 rval = 2;
192 goto respond;
194 default:
195 DEBUG1("rmtd: garbage command %c\n", c);
196 exit(3);
198 respond:
199 DEBUG1("rmtd: A %d\n", rval);
200 (void)sprintf(resp, "A%d\n", rval);
201 (void)write(STDOUT_FILENO, resp, strlen(resp));
202 goto top;
203 ioerror:
204 error(errno);
205 goto top;
208 void
209 getstring(bp)
210 char *bp;
212 int i;
213 char *cp = bp;
215 for (i = 0; i < SSIZE; i++) {
216 if (read(STDIN_FILENO, cp+i, 1) != 1)
217 exit(0);
218 if (cp[i] == '\n')
219 break;
221 cp[i] = '\0';
224 char *
225 checkbuf(rec, size)
226 char *rec;
227 int size;
230 if (size <= maxrecsize)
231 return (rec);
232 if (rec != 0)
233 free(rec);
234 rec = malloc(size);
235 if (rec == 0) {
236 DEBUG("rmtd: cannot allocate buffer space\n");
237 exit(4);
239 maxrecsize = size;
240 while (size > 1024 &&
241 setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
242 size -= 1024;
243 return (rec);
246 void
247 error(num)
248 int num;
251 DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
252 (void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num));
253 (void)write(STDOUT_FILENO, resp, strlen(resp));