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