Home | History | Annotate | Line # | Download | only in eject
eject.c revision 1.11
      1 /*	$NetBSD: eject.c,v 1.11 1999/03/25 16:50:51 bouyer 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.11 1999/03/25 16:50:51 bouyer Exp $");
     47 #endif /* not lint */
     48 
     49 #include <ctype.h>
     50 #include <err.h>
     51 #include <fcntl.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 #include <unistd.h>
     56 #include <sys/cdio.h>
     57 #include <sys/disklabel.h>
     58 #include <sys/ioctl.h>
     59 #include <sys/param.h>
     60 #include <sys/ucred.h>
     61 #include <sys/mount.h>
     62 #include <sys/mtio.h>
     63 
     64 struct nicknames_s {
     65     char *name;			/* The name given on the command line. */
     66     char *devname;		/* The base name of the device */
     67     int type;			/* The type of device, for determining
     68 				   what ioctl to use. */
     69 #define TAPE 0x10
     70 #define DISK 0x20
     71 #define CDROM 0x40
     72     /* OR one of the above with one of the below: */
     73 #define NOTLOADABLE 0x00
     74 #define LOADABLE 0x01
     75 #define TYPEMASK ((int)~0x01)
     76 } nicknames[] = {
     77     { "diskette", "fd", DISK | NOTLOADABLE },
     78     { "floppy", "fd", DISK | NOTLOADABLE },
     79     { "fd", "fd", DISK | NOTLOADABLE },
     80     { "sd", "sd", DISK | NOTLOADABLE },
     81     { "cdrom", "cd", CDROM | LOADABLE },
     82     { "cd", "cd", CDROM | LOADABLE },
     83     { "mcd", "mcd", CDROM | LOADABLE }, /* XXX Is this true? */
     84     { "tape", "st", TAPE | NOTLOADABLE },
     85     { "st", "st", TAPE | NOTLOADABLE },
     86     { "dat", "st", TAPE | NOTLOADABLE },
     87     { "exabyte", "st", TAPE | NOTLOADABLE },
     88 };
     89 #define MAXNICKLEN 12		/* at least enough room for the
     90 				   longest nickname */
     91 #define MAXDEVLEN (MAXNICKLEN + 7) /* "/dev/r" ... "a" */
     92 
     93 struct devtypes_s {
     94     char *name;
     95     int type;
     96 } devtypes[] = {
     97     { "diskette", DISK | NOTLOADABLE },
     98     { "floppy", DISK | NOTLOADABLE },
     99     { "cdrom", CDROM | LOADABLE },
    100     { "disk", DISK | NOTLOADABLE },
    101     { "tape", TAPE | NOTLOADABLE },
    102 };
    103 
    104 int verbose_f = 0;
    105 int umount_f = 1;
    106 int load_f = 0;
    107 
    108 int main __P((int, char *[]));
    109 void usage __P((void));
    110 char *nick2dev __P((char *));
    111 char *nick2rdev __P((char *));
    112 int guess_devtype __P((char *));
    113 char *guess_nickname __P((char *));
    114 void eject_tape __P((char *));
    115 void eject_disk __P((char *, int));
    116 void unmount_dev __P((char *));
    117 
    118 int
    119 main(int argc,
    120      char *argv[])
    121 {
    122     int ch;
    123     int devtype = -1;
    124     int n, i;
    125     char *devname = NULL;
    126 
    127     while((ch = getopt(argc, argv, "d:flnt:v")) != -1) {
    128 	switch(ch) {
    129 	case 'd':
    130 	    devname = optarg;
    131 	    break;
    132 	case 'f':
    133 	    umount_f = 0;
    134 	    break;
    135 	case 'l':
    136 	    load_f = 1;
    137 	    break;
    138 	case 'n':
    139 	    for(n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]);
    140 		n++) {
    141 		struct nicknames_s *np = &nicknames[n];
    142 
    143 		printf("%s -> %s\n", np->name, nick2dev(np->name));
    144 	    }
    145 	    return(0);
    146 	case 't':
    147 	    for(i = 0; i < sizeof(devtypes) / sizeof(devtypes[0]);
    148 		i++) {
    149 		if(strcasecmp(devtypes[i].name, optarg) == 0) {
    150 		    devtype = devtypes[i].type;
    151 		    break;
    152 		}
    153 	    }
    154 	    if(devtype == -1) {
    155 		errx(1, "%s: unknown device type\n", optarg);
    156 	    }
    157 	    break;
    158 	case 'v':
    159 	    verbose_f = 1;
    160 	    break;
    161 	default:
    162 	    warnx("-%c: unknown switch", ch);
    163 	    usage();
    164 	    /* NOTREACHED */
    165 	}
    166     }
    167     argc -= optind;
    168     argv += optind;
    169 
    170     if(devname == NULL) {
    171 	if(argc == 0) {
    172 	    usage();
    173 	    /* NOTREACHED */
    174 	} else
    175 	    devname = argv[0];
    176     }
    177 
    178 
    179     if(devtype == -1) {
    180 	devtype = guess_devtype(devname);
    181     }
    182     if(devtype == -1) {
    183 	errx(1, "%s: unable to determine type of device",
    184 	     devname);
    185     }
    186     if(verbose_f) {
    187 	printf("device type == ");
    188 	if((devtype & TYPEMASK) == TAPE)
    189 	    printf("tape\n");
    190 	else
    191 	    printf("disk, floppy, or cdrom\n");
    192     }
    193 
    194     if(umount_f)
    195 	unmount_dev(devname);
    196 
    197     /* XXX Tapes, cdroms  and disks have different ioctl's: */
    198     if((devtype & TYPEMASK) == TAPE)
    199 	eject_tape(devname);
    200     else
    201 	eject_disk(devname, devtype & TYPEMASK);
    202 
    203     if(verbose_f)
    204 	printf("done.\n");
    205 
    206     return(0);
    207 }
    208 
    209 void
    210 usage(void)
    211 {
    212     errx(1, "Usage: eject [-n][-f][-v][-l][-t type][-d] device | nickname");
    213 }
    214 
    215 int
    216 guess_devtype(char *devname)
    217 {
    218     int n;
    219 
    220     /* Nickname match: */
    221     for(n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]);
    222 	n++) {
    223 	if(strncasecmp(nicknames[n].name, devname,
    224 		       strlen(nicknames[n].name)) == 0)
    225 	    return(nicknames[n].type);
    226     }
    227 
    228     /*
    229      * If we still don't know it, then try to compare vs. dev
    230      * and rdev names that we know.
    231      */
    232     /* dev first: */
    233     for(n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
    234 	char *name;
    235 	name = nick2dev(nicknames[n].name);
    236 	/*
    237 	 * Assume that the part of the name that distinguishes the
    238 	 * instance of this device begins with a 0.
    239 	 */
    240 	*(strchr(name, '0')) = '\0';
    241 	if(strncmp(name, devname, strlen(name)) == 0)
    242 	    return(nicknames[n].type);
    243     }
    244 
    245     /* Now rdev: */
    246     for(n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
    247 	char *name = nick2rdev(nicknames[n].name);
    248 	*(strchr(name, '0')) = '\0';
    249 	if(strncmp(name, devname, strlen(name)) == 0)
    250 	    return(nicknames[n].type);
    251     }
    252 
    253     /* Not found. */
    254     return(-1);
    255 }
    256 
    257 /* "floppy5" -> "/dev/fd5a".  Yep, this uses a static buffer. */
    258 char *
    259 nick2dev(char *nn)
    260 {
    261     int n;
    262     static char devname[MAXDEVLEN];
    263     int devnum = 0;
    264 
    265     for(n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
    266 	if(strncasecmp(nicknames[n].name, nn,
    267 		       strlen(nicknames[n].name)) == 0) {
    268 	    sscanf(nn, "%*[^0-9]%d", &devnum);
    269 	    sprintf(devname, "/dev/%s%d", nicknames[n].devname,
    270 		    devnum);
    271 	    if((nicknames[n].type & TYPEMASK) != TAPE)
    272 		strcat(devname, "a");
    273 	    return(devname);
    274 	}
    275     }
    276 
    277     return(NULL);
    278 }
    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] += RAW_PART;
    297 	    }
    298 	    return(devname);
    299 	}
    300     }
    301 
    302     return(NULL);
    303 }
    304 
    305 /* Unmount all filesystems attached to dev. */
    306 void
    307 unmount_dev(char *name)
    308 {
    309     struct statfs *mounts;
    310     int i, nmnts, len;
    311     char *dn;
    312 
    313     nmnts = getmntinfo(&mounts, MNT_NOWAIT);
    314     if(nmnts == 0) {
    315 	err(1, "getmntinfo");
    316     }
    317 
    318     /* Make sure we have a device name: */
    319     dn = nick2dev(name);
    320     if(dn == NULL)
    321 	dn = name;
    322 
    323     /* Set len to strip off the partition name: */
    324     len = strlen(dn);
    325     if(!isdigit(dn[len - 1]))
    326 	len--;
    327     if(!isdigit(dn[len - 1])) {
    328 	errx(1, "Can't figure out base name for dev name %s", dn);
    329     }
    330 
    331     for(i = 0; i < nmnts; i++) {
    332 	if(strncmp(mounts[i].f_mntfromname, dn, len) == 0) {
    333 	    if(verbose_f)
    334 		printf("Unmounting %s from %s...\n",
    335 		       mounts[i].f_mntfromname,
    336 		       mounts[i].f_mntonname);
    337 
    338 	    if(unmount(mounts[i].f_mntonname, 0) == -1) {
    339 		err(1, "unmount: %s", mounts[i].f_mntonname);
    340 	    }
    341 	}
    342     }
    343 
    344     return;
    345 }
    346 
    347 void
    348 eject_tape(char *name)
    349 {
    350     struct mtop m;
    351     int fd;
    352     char *dn;
    353 
    354     dn = nick2rdev(name);
    355     if(dn == NULL) {
    356 	dn = name;		/* Hope for the best. */
    357     }
    358 
    359     fd = open(dn, O_RDONLY);
    360     if(fd == -1) {
    361 	err(1, "open: %s", dn);
    362     }
    363 
    364     if(verbose_f)
    365 	printf("Ejecting %s...\n", dn);
    366 
    367     m.mt_op = MTOFFL;
    368     m.mt_count = 0;
    369     if(ioctl(fd, MTIOCTOP, &m) == -1) {
    370 	err(1, "ioctl: MTIOCTOP: %s", dn);
    371     }
    372 
    373     close(fd);
    374     return;
    375 }
    376 
    377 void
    378 eject_disk(char *name, int type)
    379 {
    380     int fd;
    381     char *dn;
    382     int arg;
    383 
    384     dn = nick2rdev(name);
    385     if(dn == NULL) {
    386 	dn = name;		/* Hope for the best. */
    387     }
    388 
    389     fd = open(dn, O_RDONLY);
    390     if(fd == -1) {
    391 	err(1, "open: %s", dn);
    392     }
    393 
    394     if(load_f) {
    395 	if(verbose_f)
    396 	    printf("Closing %s...\n", dn);
    397 
    398 	if(ioctl(fd, CDIOCCLOSE, NULL) == -1) {
    399 	    err(1, "ioctl: CDIOCCLOSE: %s", dn);
    400 	}
    401     } else {
    402 	if(verbose_f)
    403 	    printf("Ejecting %s...\n", dn);
    404 
    405 	arg = 0;
    406 	if (type == CDROM) {
    407 	    if(ioctl(fd, CDIOCEJECT, &arg) == -1)
    408 		err(1, "ioctl: CDIOCEJECT: %s", dn);
    409 	} else {
    410 	    if(ioctl(fd, DIOCLOCK, (char *)&arg) == -1)
    411 		err(1, "ioctl: DIOCLOCK: %s", dn);
    412 	    if(ioctl(fd, DIOCEJECT, &arg) == -1)
    413 		err(1, "ioctl: DIOCEJECT: %s", dn);
    414 	}
    415     }
    416 
    417     close(fd);
    418     return;
    419 }
    420