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