Home | History | Annotate | Line # | Download | only in eject
eject.c revision 1.22
      1 /*	$NetBSD: eject.c,v 1.22 2008/07/21 14:19:22 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Chris Jones.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1999\
     35  The NetBSD Foundation, Inc.  All rights reserved.");
     36 #endif				/* not lint */
     37 
     38 #ifndef lint
     39 __RCSID("$NetBSD: eject.c,v 1.22 2008/07/21 14:19:22 lukem Exp $");
     40 #endif				/* not lint */
     41 
     42 #include <sys/types.h>
     43 #include <sys/cdio.h>
     44 #include <sys/disklabel.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/param.h>
     47 #include <sys/ucred.h>
     48 #include <sys/mount.h>
     49 #include <sys/mtio.h>
     50 
     51 #include <ctype.h>
     52 #include <err.h>
     53 #include <fcntl.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 #include <unistd.h>
     58 #include <util.h>
     59 
     60 struct nicknames_s {
     61 	char *name;		/* The name given on the command line. */
     62 	char *devname;		/* The base name of the device */
     63 	int type;		/* The type of device, for determining what
     64 				 * ioctl to use. */
     65 #define TAPE 0x10
     66 #define DISK 0x20
     67 	/* OR one of the above with one of the below: */
     68 #define NOTLOADABLE 0x00
     69 #define LOADABLE 0x01
     70 #define FLOPPY 0x2
     71 #define TYPEMASK ((int)~0x01)
     72 }           nicknames[] = {
     73 	{ "diskette", "fd", DISK | FLOPPY | NOTLOADABLE },
     74 	{ "floppy", "fd", DISK | FLOPPY | NOTLOADABLE },
     75 	{ "fd", "fd", DISK | FLOPPY | NOTLOADABLE },
     76 	{ "sd", "sd", DISK | NOTLOADABLE },
     77 	{ "cdrom", "cd", DISK | LOADABLE },
     78 	{ "cd", "cd", DISK | LOADABLE },
     79 	{ "cdr", "cd", DISK | LOADABLE },
     80 	{ "cdrw", "cd", DISK | LOADABLE },
     81 	{ "dvdrom", "cd", DISK | LOADABLE },
     82 	{ "dvd", "cd", DISK | LOADABLE },
     83 	{ "dvdr", "cd", DISK | LOADABLE },
     84 	{ "dvdrw", "cd", DISK | LOADABLE },
     85 	{ "mcd", "mcd", DISK | LOADABLE },	/* XXX Is this true? */
     86 	{ "tape", "st", TAPE | NOTLOADABLE },
     87 	{ "st", "st", TAPE | NOTLOADABLE },
     88 	{ "dat", "st", TAPE | NOTLOADABLE },
     89 	{ "exabyte", "st", TAPE | NOTLOADABLE },
     90 };
     91 #define MAXNICKLEN 12		/* at least enough room for the longest
     92 				 * nickname */
     93 #define MAXDEVLEN (MAXNICKLEN + 7)	/* "/dev/r" ... "a" */
     94 
     95 struct devtypes_s {
     96 	char *name;
     97 	int type;
     98 }          devtypes[] = {
     99 	{ "diskette", DISK | NOTLOADABLE },
    100 	{ "floppy", DISK | NOTLOADABLE },
    101 	{ "cdrom", DISK | LOADABLE },
    102 	{ "disk", DISK | NOTLOADABLE },
    103 	{ "tape", TAPE | NOTLOADABLE },
    104 };
    105 
    106 enum eject_op {
    107 	OP_EJECT, OP_LOAD, OP_LOCK, OP_UNLOCK
    108 };
    109 
    110 int verbose_f = 0;
    111 int umount_f = 1;
    112 
    113 int main(int, char *[]);
    114 void usage(void);
    115 char *nick2dev(char *);
    116 char *nick2rdev(char *);
    117 int guess_devtype(char *);
    118 char *guess_nickname(char *);
    119 void eject_tape(char *, enum eject_op);
    120 void eject_disk(char *, enum eject_op);
    121 void unmount_dev(char *);
    122 
    123 int
    124 main(int argc, char *argv[])
    125 {
    126 	int ch;
    127 	int devtype = -1;
    128 	int n, i;
    129 	char *devname = NULL;
    130 	enum eject_op op = OP_EJECT;
    131 
    132 	while ((ch = getopt(argc, argv, "d:flLnt:Uv")) != -1) {
    133 		switch (ch) {
    134 		case 'd':
    135 			devname = optarg;
    136 			break;
    137 		case 'f':
    138 			umount_f = 0;
    139 			break;
    140 		case 'l':
    141 			if (op != OP_EJECT)
    142 				usage();
    143 			op = OP_LOAD;
    144 			break;
    145 		case 'L':
    146 			if (op != OP_EJECT)
    147 				usage();
    148 			op = OP_LOCK;
    149 			break;
    150 		case 'n':
    151 			for (n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]);
    152 			    n++) {
    153 				struct nicknames_s *np = &nicknames[n];
    154 
    155 				printf("%s -> %s\n", np->name, nick2dev(np->name));
    156 			}
    157 			return (0);
    158 		case 't':
    159 			for (i = 0; i < sizeof(devtypes) / sizeof(devtypes[0]);
    160 			    i++) {
    161 				if (strcasecmp(devtypes[i].name, optarg) == 0) {
    162 					devtype = devtypes[i].type;
    163 					break;
    164 				}
    165 			}
    166 			if (devtype == -1)
    167 				errx(1, "%s: unknown device type", optarg);
    168 			break;
    169 		case 'U':
    170 			if (op != OP_EJECT)
    171 				usage();
    172 			op = OP_UNLOCK;
    173 			break;
    174 		case 'v':
    175 			verbose_f = 1;
    176 			break;
    177 		default:
    178 			usage();
    179 			/* NOTREACHED */
    180 		}
    181 	}
    182 	argc -= optind;
    183 	argv += optind;
    184 
    185 	if (devname == NULL) {
    186 		if (argc == 0) {
    187 			usage();
    188 			/* NOTREACHED */
    189 		} else
    190 			devname = argv[0];
    191 	}
    192 	if (devtype == -1)
    193 		devtype = guess_devtype(devname);
    194 	if (devtype == -1)
    195 		errx(1, "%s: unable to determine type of device",
    196 		    devname);
    197 	if (verbose_f) {
    198 		printf("device type == ");
    199 		if ((devtype & TYPEMASK) == TAPE)
    200 			printf("tape\n");
    201 		else
    202 			printf("disk, floppy, or cdrom\n");
    203 	}
    204 	if (umount_f)
    205 		unmount_dev(devname);
    206 
    207 	/* XXX Tapes and disks have different ioctl's: */
    208 	if ((devtype & TYPEMASK) == TAPE)
    209 		eject_tape(devname, op);
    210 	else
    211 		eject_disk(devname, op);
    212 
    213 	if (verbose_f)
    214 		printf("done.\n");
    215 
    216 	return (0);
    217 }
    218 
    219 void
    220 usage(void)
    221 {
    222 
    223 	fprintf(stderr, "usage: eject [-fv] [-l | -L | -U] "
    224 	    "[-t device-type] [-d] device\n");
    225 	fprintf(stderr, "       eject -n\n");
    226 	exit(1);
    227 }
    228 
    229 int
    230 guess_devtype(char *devname)
    231 {
    232 	int n;
    233 
    234 	/* Nickname match: */
    235 	for (n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]);
    236 	    n++) {
    237 		if (strncasecmp(nicknames[n].name, devname,
    238 			strlen(nicknames[n].name)) == 0)
    239 			return (nicknames[n].type);
    240 	}
    241 
    242 	/*
    243          * If we still don't know it, then try to compare vs. dev
    244          * and rdev names that we know.
    245          */
    246 	/* dev first: */
    247 	for (n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
    248 		char *name;
    249 		name = nick2dev(nicknames[n].name);
    250 		/*
    251 		 * Assume that the part of the name that distinguishes the
    252 		 * instance of this device begins with a 0.
    253 		 */
    254 		*(strchr(name, '0')) = '\0';
    255 		if (strncmp(name, devname, strlen(name)) == 0)
    256 			return (nicknames[n].type);
    257 	}
    258 
    259 	/* Now rdev: */
    260 	for (n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
    261 		char *name = nick2rdev(nicknames[n].name);
    262 		*(strchr(name, '0')) = '\0';
    263 		if (strncmp(name, devname, strlen(name)) == 0)
    264 			return (nicknames[n].type);
    265 	}
    266 
    267 	/* Not found. */
    268 	return (-1);
    269 }
    270 /* "floppy5" -> "/dev/fd5a".  Yep, this uses a static buffer. */
    271 char *
    272 nick2dev(char *nn)
    273 {
    274 	int n;
    275 	static char devname[MAXDEVLEN];
    276 	int devnum = 0;
    277 
    278 	for (n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
    279 		if (strncasecmp(nicknames[n].name, nn,
    280 			strlen(nicknames[n].name)) == 0) {
    281 			sscanf(nn, "%*[^0-9]%d", &devnum);
    282 			sprintf(devname, "/dev/%s%d", nicknames[n].devname,
    283 			    devnum);
    284 			if ((nicknames[n].type & TYPEMASK) != TAPE)
    285 				strcat(devname, "a");
    286 			return (devname);
    287 		}
    288 	}
    289 
    290 	return (NULL);
    291 }
    292 /* "floppy5" -> "/dev/rfd5c".  Static buffer. */
    293 char *
    294 nick2rdev(char *nn)
    295 {
    296 	int n;
    297 	static char devname[MAXDEVLEN];
    298 	int devnum = 0;
    299 
    300 	for (n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
    301 		if (strncasecmp(nicknames[n].name, nn,
    302 			strlen(nicknames[n].name)) == 0) {
    303 			sscanf(nn, "%*[^0-9]%d", &devnum);
    304 			sprintf(devname, "/dev/r%s%d", nicknames[n].devname,
    305 			    devnum);
    306 			if ((nicknames[n].type & TYPEMASK) != TAPE) {
    307 				strcat(devname, "a");
    308 				if ((nicknames[n].type & FLOPPY) != FLOPPY)
    309 					devname[strlen(devname) - 1] += getrawpartition();
    310 			}
    311 			return (devname);
    312 		}
    313 	}
    314 
    315 	return (NULL);
    316 }
    317 /* Unmount all filesystems attached to dev. */
    318 void
    319 unmount_dev(char *name)
    320 {
    321 	struct statvfs *mounts;
    322 	int i, nmnts, len;
    323 	char *dn;
    324 
    325 	nmnts = getmntinfo(&mounts, MNT_NOWAIT);
    326 	if (nmnts == 0) {
    327 		err(1, "getmntinfo");
    328 	}
    329 	/* Make sure we have a device name: */
    330 	dn = nick2dev(name);
    331 	if (dn == NULL)
    332 		dn = name;
    333 
    334 	/* Set len to strip off the partition name: */
    335 	len = strlen(dn);
    336 	if (!isdigit((unsigned char)dn[len - 1]))
    337 		len--;
    338 	if (!isdigit((unsigned char)dn[len - 1])) {
    339 		errx(1, "Can't figure out base name for dev name %s", dn);
    340 	}
    341 	for (i = 0; i < nmnts; i++) {
    342 		if (strncmp(mounts[i].f_mntfromname, dn, len) == 0) {
    343 			if (verbose_f)
    344 				printf("Unmounting %s from %s...\n",
    345 				    mounts[i].f_mntfromname,
    346 				    mounts[i].f_mntonname);
    347 
    348 			if (unmount(mounts[i].f_mntonname, 0) == -1) {
    349 				err(1, "unmount: %s", mounts[i].f_mntonname);
    350 			}
    351 		}
    352 	}
    353 
    354 	return;
    355 }
    356 
    357 void
    358 eject_tape(char *name, enum eject_op op)
    359 {
    360 	struct mtop m;
    361 	int fd;
    362 	char *dn;
    363 
    364 	dn = nick2rdev(name);
    365 	if (dn == NULL)
    366 		dn = name;	/* Hope for the best. */
    367 	fd = open(dn, O_RDONLY);
    368 	if (fd == -1)
    369 		err(1, "open: %s", dn);
    370 	switch (op) {
    371 	case OP_EJECT:
    372 		if (verbose_f)
    373 			printf("Ejecting %s...\n", dn);
    374 
    375 		m.mt_op = MTOFFL;
    376 		m.mt_count = 0;
    377 		if (ioctl(fd, MTIOCTOP, &m) == -1)
    378 			err(1, "ioctl: MTIOCTOP: %s", dn);
    379 		break;
    380 	case OP_LOAD:
    381 		errx(1, "cannot load tapes");
    382 		/* NOTREACHED */
    383 	case OP_LOCK:
    384 		errx(1, "cannot lock tapes");
    385 		/* NOTREACHED */
    386 	case OP_UNLOCK:
    387 		errx(1, "cannot unlock tapes");
    388 		/* NOTREACHED */
    389 	}
    390 	close(fd);
    391 	return;
    392 }
    393 
    394 void
    395 eject_disk(char *name, enum eject_op op)
    396 {
    397 	int fd;
    398 	char *dn;
    399 	int arg;
    400 
    401 	dn = nick2rdev(name);
    402 	if (dn == NULL)
    403 		dn = name;	/* Hope for the best. */
    404 	fd = open(dn, O_RDONLY);
    405 	if (fd == -1)
    406 		err(1, "open: %s", dn);
    407 	switch (op) {
    408 	case OP_LOAD:
    409 		if (verbose_f)
    410 			printf("Closing %s...\n", dn);
    411 
    412 		if (ioctl(fd, CDIOCCLOSE, NULL) == -1)
    413 			err(1, "ioctl: CDIOCCLOSE: %s", dn);
    414 		break;
    415 	case OP_EJECT:
    416 		if (verbose_f)
    417 			printf("Ejecting %s...\n", dn);
    418 
    419 		arg = 0;
    420 		if (umount_f == 0) {
    421 			/* force eject, unlock the device first */
    422 			if (ioctl(fd, DIOCLOCK, &arg) == -1)
    423 				err(1, "ioctl: DIOCLOCK: %s", dn);
    424 			arg = 1;
    425 		}
    426 		if (ioctl(fd, DIOCEJECT, &arg) == -1)
    427 			err(1, "ioctl: DIOCEJECT: %s", dn);
    428 		break;
    429 	case OP_LOCK:
    430 		if (verbose_f)
    431 			printf("Locking %s...\n", dn);
    432 
    433 		arg = 1;
    434 		if (ioctl(fd, DIOCLOCK, &arg) == -1)
    435 			err(1, "ioctl: DIOCLOCK: %s", dn);
    436 		break;
    437 	case OP_UNLOCK:
    438 		if (verbose_f)
    439 			printf("Unlocking %s...\n", dn);
    440 
    441 		arg = 0;
    442 		if (ioctl(fd, DIOCLOCK, &arg) == -1)
    443 			err(1, "ioctl: DIOCLOCK: %s", dn);
    444 		break;
    445 	}
    446 
    447 	close(fd);
    448 	return;
    449 }
    450