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