1 /* $OpenBSD: mt.c,v 1.25 2005/05/01 18:56:36 deraadt Exp $ */
2 /* $NetBSD: mt.c,v 1.14.2.1 1996/05/27 15:12:11 mrg Exp $ */
3
4 /*
5 * Copyright (c) 1980, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #ifndef lint
34 static char copyright[] =
35 "@(#) Copyright (c) 1980, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
37 #endif /* not lint */
38
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)mt.c 8.2 (Berkeley) 6/6/93";
42 #else
43 static char rcsid[] = "$OpenBSD: mt.c,v 1.25 2005/05/01 18:56:36 deraadt Exp $";
44 #endif
45 #endif /* not lint */
46
47 /*
48 * mt --
49 * magnetic tape manipulation program
50 */
51 #include <sys/types.h>
52 #include <sys/ioctl.h>
53 #include <sys/mtio.h>
54 #include <sys/stat.h>
55 #include <sys/disklabel.h>
56
57 #include <ctype.h>
58 #include <err.h>
59 #include <errno.h>
60 #include <fcntl.h>
61 #include <paths.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66 #include <util.h>
67
68 #include "mt.h"
69
70 struct commands {
71 char *c_name;
72 int c_code;
73 int c_ronly;
74 int c_mincount;
75 } com[] = {
76 { "blocksize", MTSETBSIZ, 1, 0 },
77 { "bsf", MTBSF, 1, 1 },
78 { "bsr", MTBSR, 1, 1 },
79 { "density", MTSETDNSTY, 1, 1 },
80 { "eof", MTWEOF, 0, 1 },
81 { "eom", MTEOM, 1, 1 },
82 { "erase", MTERASE, 0, 1 },
83 { "fsf", MTFSF, 1, 1 },
84 { "fsr", MTFSR, 1, 1 },
85 { "offline", MTOFFL, 1, 1 },
86 #define COM_EJECT 9 /* element in the above array */
87 { "rewind", MTREW, 1, 1 },
88 { "rewoffl", MTOFFL, 1, 1 },
89 { "status", MTNOP, 1, 1 },
90 { "retension", MTRETEN, 1, 1 },
91 #define COM_RETEN 13 /* element in the above array */
92 { "weof", MTWEOF, 0, 1 },
93 { NULL }
94 };
95
96 void printreg(char *, u_int, char *);
97 void status(struct mtget *);
98 void usage(void);
99
100 char *host = NULL; /* remote host (if any) */
101
102 char *progname;
103 int eject = 0;
104
105 int
main(int argc,char * argv[])106 main(int argc, char *argv[])
107 {
108 struct commands *comp;
109 struct mtget mt_status;
110 struct mtop mt_com;
111 int ch, len, mtfd, flags, insert = 0;
112 char *p, *tape, *realtape, *opts;
113
114 if ((progname = strrchr(argv[0], '/')))
115 progname++;
116 else
117 progname = argv[0];
118
119 if (strcmp(progname, "eject") == 0) {
120 opts = "t";
121 eject = 1;
122 tape = NULL;
123 } else {
124 opts = "f:";
125 if ((tape = getenv("TAPE")) == NULL)
126 tape = _PATH_DEFTAPE;
127 }
128
129 while ((ch = getopt(argc, argv, opts)) != -1) {
130 switch (ch) {
131 case 't':
132 insert = 1;
133 break;
134 case 'f':
135 tape = optarg;
136 break;
137 default:
138 usage();
139 }
140 }
141 argc -= optind;
142 argv += optind;
143
144 if (eject) {
145 if (argc == 1) {
146 tape = *argv++;
147 argc--;
148 }
149 if (argc != 0)
150 usage();
151 } else if (argc < 1 || argc > 2)
152 usage();
153
154 if (tape == NULL)
155 usage();
156
157 if (strchr(tape, ':')) {
158 host = tape;
159 tape = strchr(host, ':');
160 *tape++ = '\0';
161 if (rmthost(host) == 0)
162 exit(X_ABORT);
163 }
164
165 if (eject) {
166 if (insert)
167 comp = &com[COM_RETEN];
168 else
169 comp = &com[COM_EJECT];
170 } else {
171 len = strlen(p = *argv++);
172 for (comp = com;; comp++) {
173 if (comp->c_name == NULL)
174 errx(1, "%s: unknown command", p);
175 if (strncmp(p, comp->c_name, len) == 0)
176 break;
177 }
178 }
179
180 flags = comp->c_ronly ? O_RDONLY : O_WRONLY | O_CREAT;
181 if ((mtfd = host ? rmtopen(tape, flags) : opendev(tape, flags,
182 OPENDEV_PART | OPENDEV_DRCT, &realtape)) < 0) {
183 if (errno != 0)
184 warn("%s", host ? tape : realtape);
185 exit(2);
186 }
187 if (comp->c_code != MTNOP) {
188 mt_com.mt_op = comp->c_code;
189 if (*argv) {
190 mt_com.mt_count = strtol(*argv, &p, 10);
191 if (mt_com.mt_count < comp->c_mincount || *p)
192 errx(2, "%s: illegal count", *argv);
193 }
194 else
195 mt_com.mt_count = 1;
196 if ((host ? rmtioctl(mt_com.mt_op, mt_com.mt_count) :
197 ioctl(mtfd, MTIOCTOP, &mt_com)) < 0)
198 err(2, "%s: %s", tape, comp->c_name);
199 } else {
200 if (host)
201 status(rmtstatus());
202 else {
203 if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
204 err(2, "ioctl MTIOCGET");
205 status(&mt_status);
206 }
207 }
208
209 if (host)
210 rmtclose();
211
212 exit(X_FINOK);
213 /* NOTREACHED */
214 }
215
216 #ifdef sun
217 #include <sundev/tmreg.h>
218 #include <sundev/arreg.h>
219 #endif
220
221 #ifdef tahoe
222 #include <tahoe/vba/cyreg.h>
223 #endif
224
225 struct tape_desc {
226 short t_type; /* type of magtape device */
227 char *t_name; /* printing name */
228 char *t_dsbits; /* "drive status" register */
229 char *t_erbits; /* "error" register */
230 } tapes[] = {
231 #ifdef sun
232 { MT_ISCPC, "TapeMaster", TMS_BITS, 0 },
233 { MT_ISAR, "Archive", ARCH_CTRL_BITS, ARCH_BITS },
234 #endif
235 #ifdef tahoe
236 { MT_ISCY, "cipher", CYS_BITS, CYCW_BITS },
237 #endif
238 #define SCSI_DS_BITS "\20\5WriteProtect\2Mounted"
239 { 0x7, "SCSI", SCSI_DS_BITS, "76543210" },
240 { 0 }
241 };
242
243 /*
244 * Interpret the status buffer returned
245 */
246 void
status(struct mtget * bp)247 status(struct mtget *bp)
248 {
249 struct tape_desc *mt;
250
251 for (mt = tapes;; mt++) {
252 if (mt->t_type == 0) {
253 (void)printf("%d: unknown tape drive type\n",
254 bp->mt_type);
255 return;
256 }
257 if (mt->t_type == bp->mt_type)
258 break;
259 }
260 (void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
261 printreg("ds", bp->mt_dsreg, mt->t_dsbits);
262 printreg("\ner", bp->mt_erreg, mt->t_erbits);
263 (void)putchar('\n');
264 (void)printf("blocksize: %d (%d, %d, %d, %d)\n",
265 bp->mt_blksiz, bp->mt_mblksiz[0], bp->mt_mblksiz[1],
266 bp->mt_mblksiz[2], bp->mt_mblksiz[3]);
267 (void)printf("density: %d (%d, %d, %d, %d)\n",
268 bp->mt_density, bp->mt_mdensity[0], bp->mt_mdensity[1],
269 bp->mt_mdensity[2], bp->mt_mdensity[3]);
270 }
271
272 /*
273 * Print a register a la the %b format of the kernel's printf.
274 */
275 void
printreg(char * s,u_int v,char * bits)276 printreg(char *s, u_int v, char *bits)
277 {
278 int i, any = 0;
279 char c;
280
281 if (bits && *bits == 8)
282 printf("%s=%o", s, v);
283 else
284 printf("%s=%x", s, v);
285 if (!bits)
286 return;
287 bits++;
288 if (v && *bits) {
289 putchar('<');
290 while ((i = *bits++)) {
291 if (v & (1 << (i-1))) {
292 if (any)
293 putchar(',');
294 any = 1;
295 for (; (c = *bits) > 32; bits++)
296 putchar(c);
297 } else
298 for (; *bits > 32; bits++)
299 ;
300 }
301 putchar('>');
302 }
303 }
304
305 void
usage(void)306 usage(void)
307 {
308 if (eject)
309 (void)fprintf(stderr, "usage: %s [-t] device\n", progname);
310 else
311 (void)fprintf(stderr,
312 "usage: %s [-f device] command [count]\n", progname);
313 exit(X_USAGE);
314 }
315