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