Home | History | Annotate | Line # | Download | only in eject
      1 /*	$NetBSD: eject.c,v 1.28 2016/09/05 00:40:28 sevan 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.28 2016/09/05 00:40:28 sevan 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 #ifdef AMD_SUPPORT
     61 # include "am_glue.h"
     62 #endif
     63 
     64 struct nicknames_s {
     65 	const char *name;	/* The name given on the command line. */
     66 	const char *devname;	/* The base name of the device */
     67 	int type;		/* The type of device, for determining what
     68 				 * ioctl to use. */
     69 # define TAPE 0x10
     70 # define DISK 0x20
     71 	/* OR one of the above with one of the below: */
     72 # define NOTLOADABLE 0x00
     73 # define LOADABLE 0x01
     74 # define FLOPPY 0x2
     75 # define TYPEMASK ((int)~0x01)
     76 } nicknames[] = {
     77 	{ "diskette", "fd",   DISK | FLOPPY | NOTLOADABLE },
     78 	{ "floppy",   "fd",   DISK | FLOPPY | NOTLOADABLE },
     79 	{ "fd",       "fd",   DISK | FLOPPY | NOTLOADABLE },
     80 	{ "sd",       "sd",   DISK | NOTLOADABLE },
     81 	{ "cdrom",    "cd",   DISK | LOADABLE },
     82 	{ "cd",       "cd",   DISK | LOADABLE },
     83 	{ "racd",     "racd", DISK | LOADABLE },
     84 	{ "cdr",      "cd",   DISK | LOADABLE },
     85 	{ "cdrw",     "cd",   DISK | LOADABLE },
     86 	{ "dvdrom",   "cd",   DISK | LOADABLE },
     87 	{ "dvd",      "cd",   DISK | LOADABLE },
     88 	{ "dvdr",     "cd",   DISK | LOADABLE },
     89 	{ "dvdrw",    "cd",   DISK | LOADABLE },
     90 	{ "mcd",      "mcd",  DISK | LOADABLE },	/* XXX Is this true? */
     91 	{ "tape",     "st",   TAPE | NOTLOADABLE },
     92 	{ "st",       "st",   TAPE | NOTLOADABLE },
     93 	{ "dat",      "st",   TAPE | NOTLOADABLE },
     94 	{ "exabyte",  "st",   TAPE | NOTLOADABLE }
     95 };
     96 #define MAXNICKLEN 12		/* at least enough room for the longest
     97 				 * nickname */
     98 #define MAXDEVLEN (MAXNICKLEN + 7)	/* "/dev/r" ... "a" */
     99 
    100 struct devtypes_s {
    101 	const char *name;
    102 	int type;
    103 } devtypes[] = {
    104 	{ "diskette", DISK | NOTLOADABLE },
    105 	{ "floppy",   DISK | NOTLOADABLE },
    106 	{ "cdrom",    DISK | LOADABLE },
    107 	{ "disk",     DISK | NOTLOADABLE },
    108 	{ "tape",     TAPE | NOTLOADABLE }
    109 };
    110 
    111 enum eject_op {
    112 	OP_EJECT, OP_LOAD, OP_LOCK, OP_UNLOCK
    113 };
    114 
    115 int verbose_f = 0;
    116 int umount_f = 1;
    117 
    118 __dead static void usage(void);
    119 static char *nick2dev(const char *);
    120 static char *nick2rdev(const char *);
    121 static int guess_devtype(const char *);
    122 static void eject_disk(const char *, enum eject_op);
    123 static void eject_tape(const char *, enum eject_op);
    124 static void unmount_dev(const char *);
    125 
    126 int
    127 main(int argc, char *argv[])
    128 {
    129 	int ch;
    130 	int devtype;
    131 	size_t n;
    132 	char *dev_name;		/* XXX - devname is declared in stdlib.h */
    133 	enum eject_op op;
    134 
    135 	devtype = -1;
    136 	dev_name = NULL;
    137 	op = OP_EJECT;
    138 
    139 	while ((ch = getopt(argc, argv, "d:flLnt:Uv")) != -1) {
    140 		switch (ch) {
    141 		case 'd':
    142 			dev_name = optarg;
    143 			break;
    144 		case 'f':
    145 			umount_f = 0;
    146 			break;
    147 		case 'l':
    148 			if (op != OP_EJECT)
    149 				usage();
    150 			op = OP_LOAD;
    151 			break;
    152 		case 'L':
    153 			if (op != OP_EJECT)
    154 				usage();
    155 			op = OP_LOCK;
    156 			break;
    157 		case 'n':
    158 			for (n = 0; n < __arraycount(nicknames); n++) {
    159 				struct nicknames_s *np = &nicknames[n];
    160 
    161 				(void)printf("%s -> %s\n",
    162 				    np->name, nick2dev(np->name));
    163 			}
    164 			return 0;
    165 		case 't':
    166 			for (n = 0; n < __arraycount(devtypes); n++) {
    167 				if (strcasecmp(devtypes[n].name, optarg)
    168 				    == 0) {
    169 					devtype = devtypes[n].type;
    170 					break;
    171 				}
    172 			}
    173 			if (devtype == -1)
    174 				errx(1, "%s: unknown device type", optarg);
    175 			break;
    176 		case 'U':
    177 			if (op != OP_EJECT)
    178 				usage();
    179 			op = OP_UNLOCK;
    180 			break;
    181 		case 'v':
    182 			verbose_f = 1;
    183 			break;
    184 		default:
    185 			usage();
    186 			/* NOTREACHED */
    187 		}
    188 	}
    189 	argc -= optind;
    190 	argv += optind;
    191 
    192 	if (dev_name == NULL) {
    193 		if (argc == 0)
    194 			usage();
    195 		else
    196 			dev_name = argv[0];
    197 	}
    198 	if (devtype == -1)
    199 		devtype = guess_devtype(dev_name);
    200 	if (devtype == -1)
    201 		errx(1, "%s: unable to determine type of device", dev_name);
    202 	if (verbose_f) {
    203 		(void)printf("device type == ");
    204 		if ((devtype & TYPEMASK) == TAPE)
    205 			(void)printf("tape\n");
    206 		else
    207 			(void)printf("disk, floppy, or cdrom\n");
    208 	}
    209 	if (umount_f)
    210 		unmount_dev(dev_name);
    211 
    212 	/* XXX Tapes and disks have different ioctl's: */
    213 	if ((devtype & TYPEMASK) == TAPE)
    214 		eject_tape(dev_name, op);
    215 	else
    216 		eject_disk(dev_name, op);
    217 
    218 	if (verbose_f)
    219 		(void)printf("done.\n");
    220 
    221 	return 0;
    222 }
    223 
    224 __dead
    225 static void
    226 usage(void)
    227 {
    228 
    229 	(void)fprintf(stderr, "usage: eject [-fv] [-l | -L | -U] "
    230 	    "[-t device-type] [-d] device\n");
    231 	(void)fprintf(stderr, "       eject -n\n");
    232 	exit(1);
    233 }
    234 
    235 static int
    236 guess_devtype(const char *dev_name)
    237 {
    238 	size_t n;
    239 
    240 	/* Nickname match: */
    241 	for (n = 0; n < __arraycount(nicknames); n++) {
    242 		if (strncasecmp(nicknames[n].name, dev_name,
    243 			strlen(nicknames[n].name)) == 0)
    244 			return nicknames[n].type;
    245 	}
    246 
    247 	/*
    248          * If we still don't know it, then try to compare vs. dev and
    249          * rdev names that we know.
    250          */
    251 	/* dev first: */
    252 	for (n = 0; n < __arraycount(nicknames); n++) {
    253 		char *name;
    254 		name = nick2dev(nicknames[n].name);
    255 		/*
    256 		 * Assume that the part of the name that distinguishes
    257 		 * the instance of this device begins with a 0.
    258 		 */
    259 		*(strchr(name, '0')) = '\0';
    260 		if (strncmp(name, dev_name, strlen(name)) == 0)
    261 			return nicknames[n].type;
    262 	}
    263 
    264 	/* Now rdev: */
    265 	for (n = 0; n < __arraycount(nicknames); n++) {
    266 		char *name = nick2rdev(nicknames[n].name);
    267 		*(strchr(name, '0')) = '\0';
    268 		if (strncmp(name, dev_name, strlen(name)) == 0)
    269 			return nicknames[n].type;
    270 	}
    271 
    272 	/* Not found. */
    273 	return -1;
    274 }
    275 
    276 /* "floppy5" -> "/dev/fd5a".  Yep, this uses a static buffer. */
    277 static char *
    278 nick2dev(const char *nn)
    279 {
    280 	static char dev_name[MAXDEVLEN];
    281 	size_t n;
    282 	int devnum;
    283 
    284 	devnum = 0;
    285 	for (n = 0; n < __arraycount(nicknames); n++) {
    286 		if (strncasecmp(nicknames[n].name, nn,
    287 			strlen(nicknames[n].name)) == 0) {
    288 			(void)sscanf(nn, "%*[^0-9]%d", &devnum);
    289 			(void)sprintf(dev_name, "/dev/%s%d",
    290 			    nicknames[n].devname, devnum);
    291 			if ((nicknames[n].type & TYPEMASK) != TAPE)
    292 				(void)strcat(dev_name, "a");
    293 			return dev_name;
    294 		}
    295 	}
    296 	return NULL;
    297 }
    298 
    299 /* "floppy5" -> "/dev/rfd5c".  Static buffer. */
    300 static char *
    301 nick2rdev(const char *nn)
    302 {
    303 	static char dev_name[MAXDEVLEN];
    304 	size_t n;
    305 	int devnum;
    306 
    307 	devnum = 0;
    308 	for (n = 0; n < __arraycount(nicknames); n++) {
    309 		if (strncasecmp(nicknames[n].name, nn,
    310 			strlen(nicknames[n].name)) == 0) {
    311 			(void)sscanf(nn, "%*[^0-9]%d", &devnum);
    312 			(void)sprintf(dev_name, "/dev/r%s%d",
    313 			    nicknames[n].devname, devnum);
    314 			if ((nicknames[n].type & TYPEMASK) != TAPE) {
    315 				(void)strcat(dev_name, "a");
    316 				if ((nicknames[n].type & FLOPPY) != FLOPPY)
    317 					dev_name[strlen(dev_name) - 1]
    318 					    += getrawpartition();
    319 			}
    320 			return dev_name;
    321 		}
    322 	}
    323 	return NULL;
    324 }
    325 
    326 /* Unmount all filesystems attached to dev. */
    327 static void
    328 unmount_dev(const char *name)
    329 {
    330 	struct statvfs *mounts;
    331 	size_t len;
    332 	int i, nmnts;
    333 	const char *dn;
    334 
    335 	nmnts = getmntinfo(&mounts, MNT_NOWAIT);
    336 	if (nmnts == 0)
    337 		err(1, "getmntinfo");
    338 
    339 	/* Make sure we have a device name: */
    340 	dn = nick2dev(name);
    341 	if (dn == NULL)
    342 		dn = name;
    343 
    344 	/* Set len to strip off the partition name: */
    345 	len = strlen(dn);
    346 	if (!isdigit((unsigned char)dn[len - 1]))
    347 		len--;
    348 	if (!isdigit((unsigned char)dn[len - 1]))
    349 		errx(1, "Can't figure out base name for dev name %s", dn);
    350 
    351 	for (i = 0; i < nmnts; i++) {
    352 		if (strncmp(mounts[i].f_mntfromname, dn, len) == 0) {
    353 			if (verbose_f)
    354 				(void)printf("Unmounting %s from %s...\n",
    355 				    mounts[i].f_mntfromname,
    356 				    mounts[i].f_mntonname);
    357 
    358 			if (
    359 #ifdef AMD_SUPPORT
    360 			    am_unmount(mounts[i].f_mntonname) != 0 &&
    361 #endif
    362 			    unmount(mounts[i].f_mntonname, 0) == -1)
    363 				err(1, "unmount: %s", mounts[i].f_mntonname);
    364 		}
    365 	}
    366 	return;
    367 }
    368 
    369 static void
    370 eject_tape(const char *name, enum eject_op op)
    371 {
    372 	struct mtop m;
    373 	int fd;
    374 	const char *dn;
    375 
    376 	dn = nick2rdev(name);
    377 	if (dn == NULL)
    378 		dn = name;	/* Hope for the best. */
    379 	fd = open(dn, O_RDONLY);
    380 	if (fd == -1)
    381 		err(1, "open: %s", dn);
    382 	switch (op) {
    383 	case OP_EJECT:
    384 		if (verbose_f)
    385 			(void)printf("Ejecting %s...\n", dn);
    386 
    387 		m.mt_op = MTOFFL;
    388 		m.mt_count = 0;
    389 		if (ioctl(fd, MTIOCTOP, &m) == -1)
    390 			err(1, "ioctl: MTIOCTOP: %s", dn);
    391 		break;
    392 	case OP_LOAD:
    393 		errx(1, "cannot load tapes");
    394 		/* NOTREACHED */
    395 	case OP_LOCK:
    396 		errx(1, "cannot lock tapes");
    397 		/* NOTREACHED */
    398 	case OP_UNLOCK:
    399 		errx(1, "cannot unlock tapes");
    400 		/* NOTREACHED */
    401 	}
    402 	(void)close(fd);
    403 	return;
    404 }
    405 
    406 static void
    407 eject_disk(const char *name, enum eject_op op)
    408 {
    409 	int fd;
    410 	const char *dn;
    411 	int arg;
    412 
    413 	dn = nick2rdev(name);
    414 	if (dn == NULL)
    415 		dn = name;	/* Hope for the best. */
    416 	fd = open(dn, O_RDONLY);
    417 	if (fd == -1)
    418 		err(1, "open: %s", dn);
    419 	switch (op) {
    420 	case OP_LOAD:
    421 		if (verbose_f)
    422 			(void)printf("Closing %s...\n", dn);
    423 
    424 		if (ioctl(fd, CDIOCCLOSE, NULL) == -1)
    425 			err(1, "ioctl: CDIOCCLOSE: %s", dn);
    426 		break;
    427 	case OP_EJECT:
    428 		if (verbose_f)
    429 			(void)printf("Ejecting %s...\n", dn);
    430 
    431 		arg = 0;
    432 		if (umount_f == 0) {
    433 			/* force eject, unlock the device first */
    434 			if (ioctl(fd, DIOCLOCK, &arg) == -1)
    435 				err(1, "ioctl: DIOCLOCK: %s", dn);
    436 			arg = 1;
    437 		}
    438 		if (ioctl(fd, DIOCEJECT, &arg) == -1)
    439 			err(1, "ioctl: DIOCEJECT: %s", dn);
    440 		break;
    441 	case OP_LOCK:
    442 		if (verbose_f)
    443 			(void)printf("Locking %s...\n", dn);
    444 
    445 		arg = 1;
    446 		if (ioctl(fd, DIOCLOCK, &arg) == -1)
    447 			err(1, "ioctl: DIOCLOCK: %s", dn);
    448 		break;
    449 	case OP_UNLOCK:
    450 		if (verbose_f)
    451 			(void)printf("Unlocking %s...\n", dn);
    452 
    453 		arg = 0;
    454 		if (ioctl(fd, DIOCLOCK, &arg) == -1)
    455 			err(1, "ioctl: DIOCLOCK: %s", dn);
    456 		break;
    457 	}
    458 
    459 	(void)close(fd);
    460 	return;
    461 }
    462