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. 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. 16 * 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. 28 */ 29 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 */ 36 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$"); 43 44 /* 45 * rmt 46 */ 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> 56 57 static int tape = -1; 58 59 static char *record; 60 static int maxrecsize = -1; 61 62 #define SSIZE 64 63 static char device[SSIZE]; 64 static char count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE]; 65 66 static char resp[BUFSIZ]; 67 68 static 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) 72 73 static char *checkbuf(char *, int); 74 static void error(int); 75 static void getstring(char *); 76 77 int 78 main(int argc, char **argv) 79 { 80 int rval; 81 char c; 82 int n, i, cc; 83 84 argc--, argv++; 85 if (argc > 0) { 86 debug = fopen(*argv, "w"); 87 if (debug == NULL) { 88 DEBUG1("rmtd: error to open %s\n", *argv); 89 exit(1); 90 } 91 (void)setbuf(debug, (char *)0); 92 } 93 top: 94 errno = 0; 95 rval = 0; 96 if (read(STDIN_FILENO, &c, 1) != 1) 97 exit(0); 98 switch (c) { 99 100 case 'O': 101 if (tape >= 0) 102 (void) close(tape); 103 getstring(device); 104 getstring(mode); 105 DEBUG2("rmtd: O %s %s\n", device, mode); 106 /* 107 * XXX the rmt protocol does not provide a means to 108 * specify the permission bits; allow rw for everyone, 109 * as modified by the users umask 110 */ 111 tape = open(device, atoi(mode), 0666); 112 if (tape < 0) 113 goto ioerror; 114 goto respond; 115 116 case 'C': 117 DEBUG("rmtd: C\n"); 118 getstring(device); /* discard */ 119 if (close(tape) < 0) 120 goto ioerror; 121 tape = -1; 122 goto respond; 123 124 case 'L': 125 getstring(count); 126 getstring(pos); 127 DEBUG2("rmtd: L %s %s\n", count, pos); 128 rval = lseek(tape, (off_t)strtoll(count, NULL, 10), atoi(pos)); 129 if (rval < 0) 130 goto ioerror; 131 goto respond; 132 133 case 'W': 134 getstring(count); 135 n = atoi(count); 136 DEBUG1("rmtd: W %s\n", count); 137 record = checkbuf(record, n); 138 for (i = 0; i < n; i += cc) { 139 cc = read(STDIN_FILENO, &record[i], n - i); 140 if (cc <= 0) { 141 DEBUG("rmtd: premature eof\n"); 142 exit(2); 143 } 144 } 145 rval = write(tape, record, n); 146 if (rval < 0) 147 goto ioerror; 148 goto respond; 149 150 case 'R': 151 getstring(count); 152 DEBUG1("rmtd: R %s\n", count); 153 n = atoi(count); 154 record = checkbuf(record, n); 155 rval = read(tape, record, n); 156 if (rval < 0) 157 goto ioerror; 158 (void)sprintf(resp, "A%d\n", rval); 159 (void)write(STDOUT_FILENO, resp, strlen(resp)); 160 (void)write(STDOUT_FILENO, record, rval); 161 goto top; 162 163 case 'I': 164 getstring(op); 165 getstring(count); 166 DEBUG2("rmtd: I %s %s\n", op, count); 167 { struct mtop mtop; 168 mtop.mt_op = atoi(op); 169 mtop.mt_count = atoi(count); 170 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0) 171 goto ioerror; 172 rval = mtop.mt_count; 173 } 174 goto respond; 175 176 case 'S': /* status */ 177 DEBUG("rmtd: S\n"); 178 { struct mtget mtget; 179 if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0) 180 goto ioerror; 181 rval = sizeof (mtget); 182 if (rval > 24) /* original mtget structure size */ 183 rval = 24; 184 (void)sprintf(resp, "A%d\n", rval); 185 (void)write(STDOUT_FILENO, resp, strlen(resp)); 186 (void)write(STDOUT_FILENO, (char *)&mtget, rval); 187 goto top; 188 } 189 190 case 'V': /* version */ 191 getstring(op); 192 DEBUG1("rmtd: V %s\n", op); 193 rval = 2; 194 goto respond; 195 196 default: 197 DEBUG1("rmtd: garbage command %c\n", c); 198 exit(3); 199 } 200 respond: 201 DEBUG1("rmtd: A %d\n", rval); 202 (void)sprintf(resp, "A%d\n", rval); 203 (void)write(STDOUT_FILENO, resp, strlen(resp)); 204 goto top; 205 ioerror: 206 error(errno); 207 goto top; 208 } 209 210 void 211 getstring(char *bp) 212 { 213 int i; 214 char *cp = bp; 215 216 for (i = 0; i < SSIZE; i++) { 217 if (read(STDIN_FILENO, cp+i, 1) != 1) 218 exit(0); 219 if (cp[i] == '\n') 220 break; 221 } 222 cp[i] = '\0'; 223 } 224 225 static char * 226 checkbuf(char *rec, int size) 227 { 228 229 if (size <= maxrecsize) 230 return (rec); 231 if (rec != NULL) 232 free(rec); 233 rec = malloc(size); 234 if (rec == NULL) { 235 DEBUG("rmtd: cannot allocate buffer space\n"); 236 exit(4); 237 } 238 maxrecsize = size; 239 while (size > 1024 && 240 setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0) 241 size -= 1024; 242 return (rec); 243 } 244 245 static void 246 error(int num) 247 { 248 249 DEBUG2("rmtd: E %d (%s)\n", num, strerror(num)); 250 (void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num)); 251 (void)write(STDOUT_FILENO, resp, strlen(resp)); 252 } 253