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