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