2006-09-24 12:42:55 +04:00
|
|
|
/* $NetBSD: eject.c,v 1.20 2006/09/24 08:42:55 xtraeme Exp $ */
|
1999-02-18 01:59:14 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1999 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Chris Jones.
|
1995-10-22 19:48:52 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
1999-02-18 01:59:14 +03:00
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
1995-10-22 19:48:52 +03:00
|
|
|
*
|
1999-02-18 01:59:14 +03:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
2001-10-06 18:29:55 +04:00
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
1999-02-18 01:59:14 +03:00
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
1995-10-22 19:48:52 +03:00
|
|
|
*/
|
|
|
|
|
1999-02-18 01:59:14 +03:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
1999-02-18 20:18:46 +03:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1999 The NetBSD Foundation, Inc.\n\
|
|
|
|
All rights reserved.\n");
|
2001-10-06 18:29:55 +04:00
|
|
|
#endif /* not lint */
|
1999-02-18 20:18:46 +03:00
|
|
|
|
|
|
|
#ifndef lint
|
2006-09-24 12:42:55 +04:00
|
|
|
__RCSID("$NetBSD: eject.c,v 1.20 2006/09/24 08:42:55 xtraeme Exp $");
|
2001-10-06 18:29:55 +04:00
|
|
|
#endif /* not lint */
|
1999-02-18 01:59:14 +03:00
|
|
|
|
2001-08-20 12:21:09 +04:00
|
|
|
#include <sys/types.h>
|
1999-02-18 01:59:14 +03:00
|
|
|
#include <sys/cdio.h>
|
|
|
|
#include <sys/disklabel.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ucred.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/mtio.h>
|
|
|
|
|
2001-08-20 12:21:09 +04:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <util.h>
|
|
|
|
|
1999-02-18 01:59:14 +03:00
|
|
|
struct nicknames_s {
|
2001-10-06 18:29:55 +04:00
|
|
|
char *name; /* The name given on the command line. */
|
|
|
|
char *devname; /* The base name of the device */
|
|
|
|
int type; /* The type of device, for determining what
|
|
|
|
* ioctl to use. */
|
1999-02-18 01:59:14 +03:00
|
|
|
#define TAPE 0x10
|
|
|
|
#define DISK 0x20
|
2001-10-06 18:29:55 +04:00
|
|
|
/* OR one of the above with one of the below: */
|
1999-02-18 01:59:14 +03:00
|
|
|
#define NOTLOADABLE 0x00
|
|
|
|
#define LOADABLE 0x01
|
2006-09-24 12:42:55 +04:00
|
|
|
#define FLOPPY 0x2
|
1999-02-18 01:59:14 +03:00
|
|
|
#define TYPEMASK ((int)~0x01)
|
2001-10-06 18:29:55 +04:00
|
|
|
} nicknames[] = {
|
2006-09-24 12:42:55 +04:00
|
|
|
{ "diskette", "fd", DISK | FLOPPY | NOTLOADABLE },
|
|
|
|
{ "floppy", "fd", DISK | FLOPPY | NOTLOADABLE },
|
|
|
|
{ "fd", "fd", DISK | FLOPPY | NOTLOADABLE },
|
2001-10-06 18:29:55 +04:00
|
|
|
{ "sd", "sd", DISK | NOTLOADABLE },
|
|
|
|
{ "cdrom", "cd", DISK | LOADABLE },
|
|
|
|
{ "cd", "cd", DISK | LOADABLE },
|
|
|
|
{ "cdr", "cd", DISK | LOADABLE },
|
|
|
|
{ "cdrw", "cd", DISK | LOADABLE },
|
|
|
|
{ "dvdrom", "cd", DISK | LOADABLE },
|
|
|
|
{ "dvd", "cd", DISK | LOADABLE },
|
|
|
|
{ "dvdr", "cd", DISK | LOADABLE },
|
|
|
|
{ "dvdrw", "cd", DISK | LOADABLE },
|
|
|
|
{ "mcd", "mcd", DISK | LOADABLE }, /* XXX Is this true? */
|
|
|
|
{ "tape", "st", TAPE | NOTLOADABLE },
|
|
|
|
{ "st", "st", TAPE | NOTLOADABLE },
|
|
|
|
{ "dat", "st", TAPE | NOTLOADABLE },
|
|
|
|
{ "exabyte", "st", TAPE | NOTLOADABLE },
|
1999-02-18 01:59:14 +03:00
|
|
|
};
|
2001-10-06 18:29:55 +04:00
|
|
|
#define MAXNICKLEN 12 /* at least enough room for the longest
|
|
|
|
* nickname */
|
|
|
|
#define MAXDEVLEN (MAXNICKLEN + 7) /* "/dev/r" ... "a" */
|
1999-02-18 01:59:14 +03:00
|
|
|
|
|
|
|
struct devtypes_s {
|
2001-10-06 18:29:55 +04:00
|
|
|
char *name;
|
|
|
|
int type;
|
|
|
|
} devtypes[] = {
|
|
|
|
{ "diskette", DISK | NOTLOADABLE },
|
|
|
|
{ "floppy", DISK | NOTLOADABLE },
|
|
|
|
{ "cdrom", DISK | LOADABLE },
|
|
|
|
{ "disk", DISK | NOTLOADABLE },
|
|
|
|
{ "tape", TAPE | NOTLOADABLE },
|
1999-02-18 01:59:14 +03:00
|
|
|
};
|
|
|
|
|
2001-10-06 19:43:33 +04:00
|
|
|
enum eject_op {
|
|
|
|
OP_EJECT, OP_LOAD, OP_LOCK, OP_UNLOCK
|
|
|
|
};
|
|
|
|
|
1999-02-18 01:59:14 +03:00
|
|
|
int verbose_f = 0;
|
|
|
|
int umount_f = 1;
|
|
|
|
|
2001-10-06 18:29:55 +04:00
|
|
|
int main(int, char *[]);
|
|
|
|
void usage(void);
|
|
|
|
char *nick2dev(char *);
|
|
|
|
char *nick2rdev(char *);
|
|
|
|
int guess_devtype(char *);
|
|
|
|
char *guess_nickname(char *);
|
2001-10-06 19:43:33 +04:00
|
|
|
void eject_tape(char *, enum eject_op);
|
|
|
|
void eject_disk(char *, enum eject_op);
|
2001-10-06 18:29:55 +04:00
|
|
|
void unmount_dev(char *);
|
1999-02-18 01:59:14 +03:00
|
|
|
|
1995-10-22 19:48:52 +03:00
|
|
|
int
|
2001-10-06 18:29:55 +04:00
|
|
|
main(int argc, char *argv[])
|
1995-10-22 19:48:52 +03:00
|
|
|
{
|
2001-10-06 18:29:55 +04:00
|
|
|
int ch;
|
|
|
|
int devtype = -1;
|
|
|
|
int n, i;
|
|
|
|
char *devname = NULL;
|
2001-10-06 19:43:33 +04:00
|
|
|
enum eject_op op = OP_EJECT;
|
2001-10-06 18:29:55 +04:00
|
|
|
|
2001-10-06 19:43:33 +04:00
|
|
|
while ((ch = getopt(argc, argv, "d:flLnt:Uv")) != -1) {
|
2001-10-06 18:29:55 +04:00
|
|
|
switch (ch) {
|
|
|
|
case 'd':
|
|
|
|
devname = optarg;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
umount_f = 0;
|
|
|
|
break;
|
|
|
|
case 'l':
|
2001-10-06 19:43:33 +04:00
|
|
|
if (op != OP_EJECT)
|
|
|
|
usage();
|
|
|
|
op = OP_LOAD;
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
if (op != OP_EJECT)
|
|
|
|
usage();
|
|
|
|
op = OP_LOCK;
|
2001-10-06 18:29:55 +04:00
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
for (n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]);
|
|
|
|
n++) {
|
|
|
|
struct nicknames_s *np = &nicknames[n];
|
|
|
|
|
|
|
|
printf("%s -> %s\n", np->name, nick2dev(np->name));
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
case 't':
|
|
|
|
for (i = 0; i < sizeof(devtypes) / sizeof(devtypes[0]);
|
|
|
|
i++) {
|
|
|
|
if (strcasecmp(devtypes[i].name, optarg) == 0) {
|
|
|
|
devtype = devtypes[i].type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (devtype == -1)
|
2001-10-06 19:43:33 +04:00
|
|
|
errx(1, "%s: unknown device type", optarg);
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
if (op != OP_EJECT)
|
|
|
|
usage();
|
|
|
|
op = OP_UNLOCK;
|
2001-10-06 18:29:55 +04:00
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose_f = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
1995-10-22 19:48:52 +03:00
|
|
|
}
|
|
|
|
}
|
2001-10-06 18:29:55 +04:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (devname == NULL) {
|
|
|
|
if (argc == 0) {
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
} else
|
|
|
|
devname = argv[0];
|
|
|
|
}
|
|
|
|
if (devtype == -1)
|
|
|
|
devtype = guess_devtype(devname);
|
|
|
|
if (devtype == -1)
|
|
|
|
errx(1, "%s: unable to determine type of device",
|
|
|
|
devname);
|
|
|
|
if (verbose_f) {
|
|
|
|
printf("device type == ");
|
|
|
|
if ((devtype & TYPEMASK) == TAPE)
|
|
|
|
printf("tape\n");
|
|
|
|
else
|
|
|
|
printf("disk, floppy, or cdrom\n");
|
|
|
|
}
|
|
|
|
if (umount_f)
|
|
|
|
unmount_dev(devname);
|
1999-02-18 01:59:14 +03:00
|
|
|
|
2001-10-06 18:29:55 +04:00
|
|
|
/* XXX Tapes and disks have different ioctl's: */
|
|
|
|
if ((devtype & TYPEMASK) == TAPE)
|
2001-10-06 19:43:33 +04:00
|
|
|
eject_tape(devname, op);
|
2001-10-06 18:29:55 +04:00
|
|
|
else
|
2001-10-06 19:43:33 +04:00
|
|
|
eject_disk(devname, op);
|
1999-02-18 01:59:14 +03:00
|
|
|
|
2001-10-06 18:29:55 +04:00
|
|
|
if (verbose_f)
|
|
|
|
printf("done.\n");
|
1999-02-18 01:59:14 +03:00
|
|
|
|
2001-10-06 18:29:55 +04:00
|
|
|
return (0);
|
1999-02-18 01:59:14 +03:00
|
|
|
}
|
1995-10-22 19:48:52 +03:00
|
|
|
|
1999-02-18 01:59:14 +03:00
|
|
|
void
|
|
|
|
usage(void)
|
|
|
|
{
|
2001-10-06 18:29:55 +04:00
|
|
|
|
2004-01-06 02:23:32 +03:00
|
|
|
fprintf(stderr, "usage: eject [-fv] [-l | -L | -U] "
|
2001-10-06 19:43:33 +04:00
|
|
|
"[-t device-type] [-d] device\n");
|
|
|
|
fprintf(stderr, " eject -n\n");
|
|
|
|
exit(1);
|
1999-02-18 01:59:14 +03:00
|
|
|
}
|
1995-10-22 19:48:52 +03:00
|
|
|
|
1999-02-18 01:59:14 +03:00
|
|
|
int
|
|
|
|
guess_devtype(char *devname)
|
|
|
|
{
|
2001-10-06 18:29:55 +04:00
|
|
|
int n;
|
|
|
|
|
|
|
|
/* Nickname match: */
|
|
|
|
for (n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]);
|
|
|
|
n++) {
|
|
|
|
if (strncasecmp(nicknames[n].name, devname,
|
|
|
|
strlen(nicknames[n].name)) == 0)
|
|
|
|
return (nicknames[n].type);
|
|
|
|
}
|
|
|
|
|
1999-02-18 01:59:14 +03:00
|
|
|
/*
|
2001-10-06 18:29:55 +04:00
|
|
|
* If we still don't know it, then try to compare vs. dev
|
|
|
|
* and rdev names that we know.
|
|
|
|
*/
|
|
|
|
/* dev first: */
|
|
|
|
for (n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
|
|
|
|
char *name;
|
|
|
|
name = nick2dev(nicknames[n].name);
|
|
|
|
/*
|
|
|
|
* Assume that the part of the name that distinguishes the
|
|
|
|
* instance of this device begins with a 0.
|
|
|
|
*/
|
|
|
|
*(strchr(name, '0')) = '\0';
|
|
|
|
if (strncmp(name, devname, strlen(name)) == 0)
|
|
|
|
return (nicknames[n].type);
|
|
|
|
}
|
1995-10-22 19:48:52 +03:00
|
|
|
|
2001-10-06 18:29:55 +04:00
|
|
|
/* Now rdev: */
|
|
|
|
for (n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
|
|
|
|
char *name = nick2rdev(nicknames[n].name);
|
|
|
|
*(strchr(name, '0')) = '\0';
|
|
|
|
if (strncmp(name, devname, strlen(name)) == 0)
|
|
|
|
return (nicknames[n].type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not found. */
|
|
|
|
return (-1);
|
|
|
|
}
|
1999-02-18 01:59:14 +03:00
|
|
|
/* "floppy5" -> "/dev/fd5a". Yep, this uses a static buffer. */
|
|
|
|
char *
|
|
|
|
nick2dev(char *nn)
|
|
|
|
{
|
2001-10-06 18:29:55 +04:00
|
|
|
int n;
|
|
|
|
static char devname[MAXDEVLEN];
|
|
|
|
int devnum = 0;
|
|
|
|
|
|
|
|
for (n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
|
|
|
|
if (strncasecmp(nicknames[n].name, nn,
|
|
|
|
strlen(nicknames[n].name)) == 0) {
|
|
|
|
sscanf(nn, "%*[^0-9]%d", &devnum);
|
|
|
|
sprintf(devname, "/dev/%s%d", nicknames[n].devname,
|
|
|
|
devnum);
|
|
|
|
if ((nicknames[n].type & TYPEMASK) != TAPE)
|
|
|
|
strcat(devname, "a");
|
|
|
|
return (devname);
|
|
|
|
}
|
1995-10-22 19:48:52 +03:00
|
|
|
}
|
|
|
|
|
2001-10-06 18:29:55 +04:00
|
|
|
return (NULL);
|
1999-02-18 01:59:14 +03:00
|
|
|
}
|
|
|
|
/* "floppy5" -> "/dev/rfd5c". Static buffer. */
|
|
|
|
char *
|
|
|
|
nick2rdev(char *nn)
|
|
|
|
{
|
2001-10-06 18:29:55 +04:00
|
|
|
int n;
|
|
|
|
static char devname[MAXDEVLEN];
|
|
|
|
int devnum = 0;
|
|
|
|
|
|
|
|
for (n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
|
|
|
|
if (strncasecmp(nicknames[n].name, nn,
|
|
|
|
strlen(nicknames[n].name)) == 0) {
|
|
|
|
sscanf(nn, "%*[^0-9]%d", &devnum);
|
|
|
|
sprintf(devname, "/dev/r%s%d", nicknames[n].devname,
|
|
|
|
devnum);
|
|
|
|
if ((nicknames[n].type & TYPEMASK) != TAPE) {
|
|
|
|
strcat(devname, "a");
|
2006-09-24 12:42:55 +04:00
|
|
|
if ((nicknames[n].type & FLOPPY) != FLOPPY)
|
|
|
|
devname[strlen(devname) - 1] += getrawpartition();
|
2001-10-06 18:29:55 +04:00
|
|
|
}
|
|
|
|
return (devname);
|
|
|
|
}
|
1999-02-08 19:35:33 +03:00
|
|
|
}
|
|
|
|
|
2001-10-06 18:29:55 +04:00
|
|
|
return (NULL);
|
1999-02-18 01:59:14 +03:00
|
|
|
}
|
|
|
|
/* Unmount all filesystems attached to dev. */
|
|
|
|
void
|
|
|
|
unmount_dev(char *name)
|
|
|
|
{
|
2004-04-21 05:05:31 +04:00
|
|
|
struct statvfs *mounts;
|
2001-10-06 18:29:55 +04:00
|
|
|
int i, nmnts, len;
|
|
|
|
char *dn;
|
|
|
|
|
|
|
|
nmnts = getmntinfo(&mounts, MNT_NOWAIT);
|
|
|
|
if (nmnts == 0) {
|
|
|
|
err(1, "getmntinfo");
|
|
|
|
}
|
|
|
|
/* Make sure we have a device name: */
|
|
|
|
dn = nick2dev(name);
|
|
|
|
if (dn == NULL)
|
|
|
|
dn = name;
|
|
|
|
|
|
|
|
/* Set len to strip off the partition name: */
|
|
|
|
len = strlen(dn);
|
2004-10-30 21:17:35 +04:00
|
|
|
if (!isdigit((unsigned char)dn[len - 1]))
|
2001-10-06 18:29:55 +04:00
|
|
|
len--;
|
2004-10-30 21:17:35 +04:00
|
|
|
if (!isdigit((unsigned char)dn[len - 1])) {
|
2001-10-06 18:29:55 +04:00
|
|
|
errx(1, "Can't figure out base name for dev name %s", dn);
|
|
|
|
}
|
|
|
|
for (i = 0; i < nmnts; i++) {
|
|
|
|
if (strncmp(mounts[i].f_mntfromname, dn, len) == 0) {
|
|
|
|
if (verbose_f)
|
|
|
|
printf("Unmounting %s from %s...\n",
|
|
|
|
mounts[i].f_mntfromname,
|
|
|
|
mounts[i].f_mntonname);
|
|
|
|
|
|
|
|
if (unmount(mounts[i].f_mntonname, 0) == -1) {
|
|
|
|
err(1, "unmount: %s", mounts[i].f_mntonname);
|
|
|
|
}
|
|
|
|
}
|
1995-10-22 19:48:52 +03:00
|
|
|
}
|
1999-02-18 01:59:14 +03:00
|
|
|
|
2001-10-06 18:29:55 +04:00
|
|
|
return;
|
1999-02-18 01:59:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-10-06 19:43:33 +04:00
|
|
|
eject_tape(char *name, enum eject_op op)
|
1999-02-18 01:59:14 +03:00
|
|
|
{
|
2001-10-06 18:29:55 +04:00
|
|
|
struct mtop m;
|
|
|
|
int fd;
|
|
|
|
char *dn;
|
|
|
|
|
|
|
|
dn = nick2rdev(name);
|
|
|
|
if (dn == NULL)
|
|
|
|
dn = name; /* Hope for the best. */
|
|
|
|
fd = open(dn, O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
err(1, "open: %s", dn);
|
2001-10-06 19:43:33 +04:00
|
|
|
switch (op) {
|
|
|
|
case OP_EJECT:
|
|
|
|
if (verbose_f)
|
|
|
|
printf("Ejecting %s...\n", dn);
|
2001-10-06 18:29:55 +04:00
|
|
|
|
2001-10-06 19:43:33 +04:00
|
|
|
m.mt_op = MTOFFL;
|
|
|
|
m.mt_count = 0;
|
|
|
|
if (ioctl(fd, MTIOCTOP, &m) == -1)
|
|
|
|
err(1, "ioctl: MTIOCTOP: %s", dn);
|
|
|
|
break;
|
|
|
|
case OP_LOAD:
|
|
|
|
errx(1, "cannot load tapes");
|
|
|
|
/* NOTREACHED */
|
|
|
|
case OP_LOCK:
|
|
|
|
errx(1, "cannot lock tapes");
|
|
|
|
/* NOTREACHED */
|
|
|
|
case OP_UNLOCK:
|
|
|
|
errx(1, "cannot unlock tapes");
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2001-10-06 18:29:55 +04:00
|
|
|
close(fd);
|
|
|
|
return;
|
1999-02-18 01:59:14 +03:00
|
|
|
}
|
1995-10-22 19:48:52 +03:00
|
|
|
|
1999-02-18 01:59:14 +03:00
|
|
|
void
|
2001-10-06 19:43:33 +04:00
|
|
|
eject_disk(char *name, enum eject_op op)
|
1999-02-18 01:59:14 +03:00
|
|
|
{
|
2001-10-06 18:29:55 +04:00
|
|
|
int fd;
|
|
|
|
char *dn;
|
|
|
|
int arg;
|
|
|
|
|
|
|
|
dn = nick2rdev(name);
|
|
|
|
if (dn == NULL)
|
|
|
|
dn = name; /* Hope for the best. */
|
|
|
|
fd = open(dn, O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
err(1, "open: %s", dn);
|
2001-10-06 19:43:33 +04:00
|
|
|
switch (op) {
|
|
|
|
case OP_LOAD:
|
2001-10-06 18:29:55 +04:00
|
|
|
if (verbose_f)
|
|
|
|
printf("Closing %s...\n", dn);
|
|
|
|
|
|
|
|
if (ioctl(fd, CDIOCCLOSE, NULL) == -1)
|
|
|
|
err(1, "ioctl: CDIOCCLOSE: %s", dn);
|
2001-10-06 19:43:33 +04:00
|
|
|
break;
|
|
|
|
case OP_EJECT:
|
2001-10-06 18:29:55 +04:00
|
|
|
if (verbose_f)
|
|
|
|
printf("Ejecting %s...\n", dn);
|
|
|
|
|
|
|
|
arg = 0;
|
|
|
|
if (umount_f == 0) {
|
|
|
|
/* force eject, unlock the device first */
|
|
|
|
if (ioctl(fd, DIOCLOCK, &arg) == -1)
|
2001-10-06 19:43:33 +04:00
|
|
|
err(1, "ioctl: DIOCLOCK: %s", dn);
|
2001-10-06 18:29:55 +04:00
|
|
|
arg = 1;
|
|
|
|
}
|
|
|
|
if (ioctl(fd, DIOCEJECT, &arg) == -1)
|
|
|
|
err(1, "ioctl: DIOCEJECT: %s", dn);
|
2001-10-06 19:43:33 +04:00
|
|
|
break;
|
|
|
|
case OP_LOCK:
|
|
|
|
if (verbose_f)
|
|
|
|
printf("Locking %s...\n", dn);
|
|
|
|
|
|
|
|
arg = 1;
|
|
|
|
if (ioctl(fd, DIOCLOCK, &arg) == -1)
|
|
|
|
err(1, "ioctl: DIOCLOCK: %s", dn);
|
|
|
|
break;
|
|
|
|
case OP_UNLOCK:
|
|
|
|
if (verbose_f)
|
|
|
|
printf("Unlocking %s...\n", dn);
|
|
|
|
|
|
|
|
arg = 0;
|
|
|
|
if (ioctl(fd, DIOCLOCK, &arg) == -1)
|
|
|
|
err(1, "ioctl: DIOCLOCK: %s", dn);
|
|
|
|
break;
|
1999-03-25 19:50:51 +03:00
|
|
|
}
|
1999-02-18 01:59:14 +03:00
|
|
|
|
2001-10-06 18:29:55 +04:00
|
|
|
close(fd);
|
|
|
|
return;
|
1995-10-22 19:48:52 +03:00
|
|
|
}
|