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