Home | History | Annotate | Line # | Download | only in mt
mt.c revision 1.15
      1 /*	$NetBSD: mt.c,v 1.15 1996/05/26 04:00:49 mrg 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.15 1996/05/26 04:00:49 mrg 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 struct commands {
     70 	char *c_name;
     71 	int c_code;
     72 	int c_ronly;
     73 } com[] = {
     74 	{ "blocksize",	MTSETBSIZ, 1 },
     75 	{ "bsf",	MTBSF,	1 },
     76 	{ "bsr",	MTBSR,	1 },
     77 	{ "density",	MTSETDNSTY, 1 },
     78 	{ "eof",	MTWEOF,	0 },
     79 	{ "eom",	MTEOM,	1 },
     80 	{ "erase",	MTERASE, 0 },
     81 	{ "fsf",	MTFSF,	1 },
     82 	{ "fsr",	MTFSR,	1 },
     83 	{ "offline",	MTOFFL,	1 },
     84 	{ "rewind",	MTREW,	1 },
     85 	{ "rewoffl",	MTOFFL,	1 },
     86 	{ "status",	MTNOP,	1 },
     87 	{ "retension",	MTRETEN, 1 },
     88 	{ "weof",	MTWEOF,	0 },
     89 	{ NULL }
     90 };
     91 
     92 void printreg __P((char *, u_int, char *));
     93 void status __P((struct mtget *));
     94 void usage __P((void));
     95 
     96 char	*host = NULL;	/* remote host (if any) */
     97 uid_t	uid;		/* read uid */
     98 uid_t	euid;		/* effective uid */
     99 
    100 int
    101 main(argc, argv)
    102 	int argc;
    103 	char *argv[];
    104 {
    105 	register struct commands *comp;
    106 	struct mtget mt_status;
    107 	struct mtop mt_com;
    108 	int ch, len, mtfd, flags;
    109 	char *p, *tape;
    110 
    111 	uid = getuid();
    112 	euid = geteuid();
    113 	(void) seteuid(uid);
    114 
    115 	if ((tape = getenv("TAPE")) == NULL)
    116 		tape = DEFTAPE;
    117 
    118 	while ((ch = getopt(argc, argv, "f:t:")) != -1)
    119 		switch (ch) {
    120 		case 'f':
    121 		case 't':
    122 			tape = optarg;
    123 			break;
    124 		case '?':
    125 		default:
    126 			usage();
    127 		}
    128 	argc -= optind;
    129 	argv += optind;
    130 
    131 	if (argc < 1 || argc > 2)
    132 		usage();
    133 
    134 	if (strchr(tape, ':')) {
    135 		host = tape;
    136 		tape = strchr(host, ':');
    137 		*tape++ = '\0';
    138 		if (rmthost(host) == 0)
    139 			exit(X_ABORT);
    140 	}
    141 	(void) setuid(uid); /* rmthost() is the only reason to be setuid */
    142 
    143 	len = strlen(p = *argv++);
    144 	for (comp = com;; comp++) {
    145 		if (comp->c_name == NULL)
    146 			errx(1, "%s: unknown command", p);
    147 		if (strncmp(p, comp->c_name, len) == 0)
    148 			break;
    149 	}
    150 
    151 	flags = comp->c_ronly ? O_RDONLY : O_WRONLY | O_CREAT;
    152 	if ((mtfd = host ? rmtopen(tape, flags) : open(tape, flags,
    153 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) < 0)
    154 		err(2, "%s", tape);
    155 	if (comp->c_code != MTNOP) {
    156 		mt_com.mt_op = comp->c_code;
    157 		if (*argv) {
    158 			mt_com.mt_count = strtol(*argv, &p, 10);
    159 			if (mt_com.mt_count <= 0 || *p)
    160 				errx(2, "%s: illegal count", *argv);
    161 		}
    162 		else
    163 			mt_com.mt_count = 1;
    164 		if ((host ? rmtioctl(mt_com.mt_op, mt_com.mt_count) :
    165 		    ioctl(mtfd, MTIOCTOP, &mt_com)) < 0)
    166 			err(2, "%s: %s", tape, comp->c_name);
    167 	} else {
    168 		if (host)
    169 			status(rmtstatus());
    170 		else {
    171 			if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
    172 				err(2, "ioctl MTIOCGET");
    173 			status(&mt_status);
    174 		}
    175 	}
    176 
    177 	if (host)
    178 		rmtclose();
    179 
    180 	exit(X_FINOK);
    181 	/* NOTREACHED */
    182 }
    183 
    184 #ifdef sun
    185 #include <sundev/tmreg.h>
    186 #include <sundev/arreg.h>
    187 #endif
    188 
    189 #ifdef tahoe
    190 #include <tahoe/vba/cyreg.h>
    191 #endif
    192 
    193 struct tape_desc {
    194 	short	t_type;		/* type of magtape device */
    195 	char	*t_name;	/* printing name */
    196 	char	*t_dsbits;	/* "drive status" register */
    197 	char	*t_erbits;	/* "error" register */
    198 } tapes[] = {
    199 #ifdef sun
    200 	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
    201 	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
    202 #endif
    203 #ifdef tahoe
    204 	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
    205 #endif
    206 	{ 0x7,		"SCSI",		"76543210",	"76543210" },
    207 	{ 0 }
    208 };
    209 
    210 /*
    211  * Interpret the status buffer returned
    212  */
    213 void
    214 status(bp)
    215 	register struct mtget *bp;
    216 {
    217 	register struct tape_desc *mt;
    218 
    219 	for (mt = tapes;; mt++) {
    220 		if (mt->t_type == 0) {
    221 			(void)printf("%d: unknown tape drive type\n",
    222 			    bp->mt_type);
    223 			return;
    224 		}
    225 		if (mt->t_type == bp->mt_type)
    226 			break;
    227 	}
    228 	(void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
    229 	printreg("ds", bp->mt_dsreg, mt->t_dsbits);
    230 	printreg("\ner", bp->mt_erreg, mt->t_erbits);
    231 	(void)putchar('\n');
    232 	(void)printf("blocksize: %ld (%ld, %ld, %ld, %ld)\n",
    233 		bp->mt_blksiz, bp->mt_mblksiz[0], bp->mt_mblksiz[1],
    234 		bp->mt_mblksiz[2], bp->mt_mblksiz[3]);
    235 	(void)printf("density: %ld (%ld, %ld, %ld, %ld)\n",
    236 		bp->mt_density, bp->mt_mdensity[0], bp->mt_mdensity[1],
    237 		bp->mt_mdensity[2], bp->mt_mdensity[3]);
    238 }
    239 
    240 /*
    241  * Print a register a la the %b format of the kernel's printf.
    242  */
    243 void
    244 printreg(s, v, bits)
    245 	char *s;
    246 	register u_int v;
    247 	register char *bits;
    248 {
    249 	register int i, any = 0;
    250 	register char c;
    251 
    252 	if (bits && *bits == 8)
    253 		printf("%s=%o", s, v);
    254 	else
    255 		printf("%s=%x", s, v);
    256 	bits++;
    257 	if (v && bits) {
    258 		putchar('<');
    259 		while ((i = *bits++)) {
    260 			if (v & (1 << (i-1))) {
    261 				if (any)
    262 					putchar(',');
    263 				any = 1;
    264 				for (; (c = *bits) > 32; bits++)
    265 					putchar(c);
    266 			} else
    267 				for (; *bits > 32; bits++)
    268 					;
    269 		}
    270 		putchar('>');
    271 	}
    272 }
    273 
    274 void
    275 usage()
    276 {
    277 	(void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n");
    278 	exit(X_USAGE);
    279 }
    280