]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - usr.sbin/rmt/rmt.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / usr.sbin / rmt / rmt.c
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  * 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.
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 int     tape = -1;
58
59 char    *record;
60 int     maxrecsize = -1;
61
62 #define SSIZE   64
63 char    device[SSIZE];
64 char    count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
65
66 char    resp[BUFSIZ];
67
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)
72
73 char    *checkbuf(char *, int);
74 void     error(int);
75 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 == 0)
88                         exit(1);
89                 (void)setbuf(debug, (char *)0);
90         }
91 top:
92         errno = 0;
93         rval = 0;
94         if (read(STDIN_FILENO, &c, 1) != 1)
95                 exit(0);
96         switch (c) {
97
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);
104                 /*
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
108                  */
109                 tape = open(device, atoi(mode), 0666);
110                 if (tape < 0)
111                         goto ioerror;
112                 goto respond;
113
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;
121
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;
130
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);
141                         }
142                 }
143                 rval = write(tape, record, n);
144                 if (rval < 0)
145                         goto ioerror;
146                 goto respond;
147
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;
160
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;
171                 }
172                 goto respond;
173
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;
186                 }
187
188         case 'V':               /* version */
189                 getstring(op);
190                 DEBUG1("rmtd: V %s\n", op);
191                 rval = 2;
192                 goto respond;
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(char *bp)
210 {
211         int i;
212         char *cp = bp;
213
214         for (i = 0; i < SSIZE; i++) {
215                 if (read(STDIN_FILENO, cp+i, 1) != 1)
216                         exit(0);
217                 if (cp[i] == '\n')
218                         break;
219         }
220         cp[i] = '\0';
221 }
222
223 char *
224 checkbuf(char *rec, int size)
225 {
226
227         if (size <= maxrecsize)
228                 return (rec);
229         if (rec != 0)
230                 free(rec);
231         rec = malloc(size);
232         if (rec == 0) {
233                 DEBUG("rmtd: cannot allocate buffer space\n");
234                 exit(4);
235         }
236         maxrecsize = size;
237         while (size > 1024 &&
238                setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
239                 size -= 1024;
240         return (rec);
241 }
242
243 void
244 error(int num)
245 {
246
247         DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
248         (void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num));
249         (void)write(STDOUT_FILENO, resp, strlen(resp));
250 }