1 /* 2 * Copyright (c) 1983 Regents of the University of California. 3 * 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 #ifndef lint 35 char copyright[] = 36 "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 37 All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "from: @(#)rmt.c 5.6 (Berkeley) 6/1/90"; 43 #else 44 static char rcsid[] = "$NetBSD: rmt.c,v 1.7 1997/07/01 07:42:28 mikel Exp $"; 45 #endif 46 #endif /* not lint */ 47 48 /* 49 * rmt 50 */ 51 #include <sys/types.h> 52 #include <sys/ioctl.h> 53 #include <sys/mtio.h> 54 #include <sys/socket.h> 55 #include <sys/stat.h> 56 57 #include <errno.h> 58 #include <fcntl.h> 59 #include <stdio.h> 60 #include <stdlib.h> 61 #include <string.h> 62 #include <unistd.h> 63 64 int tape = -1; 65 66 char *record; 67 int maxrecsize = -1; 68 char *checkbuf(); 69 70 #define SSIZE 64 71 char device[SSIZE]; 72 char count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE]; 73 74 char resp[BUFSIZ]; 75 76 FILE *debug; 77 #define DEBUG(f) if (debug) fprintf(debug, f) 78 #define DEBUG1(f,a) if (debug) fprintf(debug, f, a) 79 #define DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2) 80 81 void getstring __P((char *)); 82 char *checkbuf __P((char *, int)); 83 void error __P((int)); 84 85 int 86 main(argc, argv) 87 int argc; 88 char **argv; 89 { 90 int rval; 91 char c; 92 int n, i, cc; 93 94 argc--, argv++; 95 if (argc > 0) { 96 debug = fopen(*argv, "w"); 97 if (debug == 0) 98 exit(1); 99 (void) setbuf(debug, (char *)0); 100 } 101 top: 102 errno = 0; 103 rval = 0; 104 if (read(STDIN_FILENO, &c, 1) != 1) 105 exit(0); 106 switch (c) { 107 108 case 'O': 109 if (tape >= 0) 110 (void) close(tape); 111 getstring(device); getstring(mode); 112 DEBUG2("rmtd: O %s %s\n", device, mode); 113 tape = open(device, atoi(mode), 114 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); 115 if (tape < 0) 116 goto ioerror; 117 goto respond; 118 119 case 'C': 120 DEBUG("rmtd: C\n"); 121 getstring(device); /* discard */ 122 if (close(tape) < 0) 123 goto ioerror; 124 tape = -1; 125 goto respond; 126 127 case 'L': 128 getstring(count); getstring(pos); 129 DEBUG2("rmtd: L %s %s\n", count, pos); 130 rval = lseek(tape, (off_t)strtoq(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); getstring(count); 167 DEBUG2("rmtd: I %s %s\n", op, count); 168 { 169 struct mtop mtop; 170 171 mtop.mt_op = atoi(op); 172 mtop.mt_count = atoi(count); 173 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0) 174 goto ioerror; 175 rval = mtop.mt_count; 176 } 177 goto respond; 178 179 case 'S': /* status */ 180 DEBUG("rmtd: S\n"); 181 { 182 struct mtget mtget; 183 184 if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0) 185 goto ioerror; 186 rval = sizeof (mtget); 187 (void) sprintf(resp, "A%d\n", rval); 188 (void) write(STDOUT_FILENO, resp, strlen(resp)); 189 (void) write(STDOUT_FILENO, (char *)&mtget, 190 sizeof (mtget)); 191 goto top; 192 } 193 194 default: 195 DEBUG1("rmtd: garbage command %c\n", c); 196 exit(3); 197 } 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; 206 } 207 208 void 209 getstring(bp) 210 char *bp; 211 { 212 int i; 213 char *cp = bp; 214 215 for (i = 0; i < SSIZE - 1; i++) { 216 if (read(STDIN_FILENO, cp+i, 1) != 1) 217 exit(0); 218 if (cp[i] == '\n') 219 break; 220 } 221 cp[i] = '\0'; 222 } 223 224 char * 225 checkbuf(record, size) 226 char *record; 227 int size; 228 { 229 230 if (size <= maxrecsize) 231 return (record); 232 if (record != 0) 233 free(record); 234 record = malloc(size); 235 if (record == 0) { 236 DEBUG("rmtd: cannot allocate buffer space\n"); 237 exit(4); 238 } 239 maxrecsize = size; 240 while (size > 1024 && 241 setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0) 242 size -= 1024; 243 return (record); 244 } 245 246 void 247 error(num) 248 int num; 249 { 250 251 DEBUG2("rmtd: E %d (%s)\n", num, strerror(num)); 252 (void) sprintf(resp, "E%d\n%s\n", num, strerror(num)); 253 (void) write(STDOUT_FILENO, resp, strlen(resp)); 254 } 255