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