eject: add struct eject_control to remove global variables

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2014-09-06 17:51:35 +01:00
parent ee5de88c51
commit 257a003528
1 changed files with 198 additions and 196 deletions

View File

@ -81,31 +81,34 @@ static const char * const hotplug_subsystems[] = {
"ccw" "ccw"
}; };
/* Global Variables */ struct eject_control {
static int a_option; /* command flags and arguments */ struct libmnt_table *mtab;
static int c_option; char *device; /* device or mount point to be ejected */
static int d_option; int fd; /* file descriptor for device */
static int f_option; unsigned int /* command flags and arguments */
static int F_option; a_option:1,
static int n_option; c_option:1,
static int q_option; d_option:1,
static int r_option; F_option:1,
static int s_option; f_option:1,
static int t_option; i_option:1,
static int T_option; M_option:1,
static int X_option; m_option:1,
static int v_option; n_option:1,
static int x_option; p_option:1,
static int p_option; q_option:1,
static int m_option; r_option:1,
static int M_option; s_option:1,
static int i_option; T_option:1,
static int a_arg; t_option:1,
static int i_arg; v_option:1,
static long int c_arg; X_option:1,
static long int x_arg; x_option:1,
a_arg:1,
struct libmnt_table *mtab; i_arg:1;
long int c_arg; /* changer slot number */
long int x_arg; /* cd speed */
};
static void vinfo(const char *fmt, va_list va) static void vinfo(const char *fmt, va_list va)
{ {
@ -114,11 +117,11 @@ static void vinfo(const char *fmt, va_list va)
fputc('\n', stdout); fputc('\n', stdout);
} }
static inline void verbose(const char *fmt, ...) static inline void verbose(const struct eject_control *ctl, const char *fmt, ...)
{ {
va_list va; va_list va;
if (!v_option) if (!ctl->v_option)
return; return;
va_start(va, fmt); va_start(va, fmt);
@ -174,7 +177,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)
/* Handle command line options. */ /* Handle command line options. */
static void parse_args(int argc, char **argv, char **device) static void parse_args(struct eject_control *ctl, int argc, char **argv)
{ {
static const struct option long_opts[] = static const struct option long_opts[] =
{ {
@ -206,75 +209,75 @@ static void parse_args(int argc, char **argv, char **device)
"a:c:i:x:dfFhnqrstTXvVpmM", long_opts, NULL)) != -1) { "a:c:i:x:dfFhnqrstTXvVpmM", long_opts, NULL)) != -1) {
switch (c) { switch (c) {
case 'a': case 'a':
a_option = 1; ctl->a_option = 1;
if (!strcmp(optarg, "0") || !strcmp(optarg, "off")) if (!strcmp(optarg, "0") || !strcmp(optarg, "off"))
a_arg = 0; ctl->a_arg = 0;
else if (!strcmp(optarg, "1") || !strcmp(optarg, "on")) else if (!strcmp(optarg, "1") || !strcmp(optarg, "on"))
a_arg = 1; ctl->a_arg = 1;
else else
errx(EXIT_FAILURE, _("invalid argument to --auto/-a option")); errx(EXIT_FAILURE, _("invalid argument to --auto/-a option"));
break; break;
case 'c': case 'c':
c_option = 1; ctl->c_option = 1;
c_arg = strtoul_or_err(optarg, _("invalid argument to --changerslot/-c option")); ctl->c_arg = strtoul_or_err(optarg, _("invalid argument to --changerslot/-c option"));
break; break;
case 'x': case 'x':
x_option = 1; ctl->x_option = 1;
x_arg = strtoul_or_err(optarg, _("invalid argument to --cdspeed/-x option")); ctl->x_arg = strtoul_or_err(optarg, _("invalid argument to --cdspeed/-x option"));
break; break;
case 'd': case 'd':
d_option = 1; ctl->d_option = 1;
break; break;
case 'f': case 'f':
f_option = 1; ctl->f_option = 1;
break; break;
case 'F': case 'F':
F_option = 1; ctl->F_option = 1;
break; break;
case 'h': case 'h':
usage(stdout); usage(stdout);
break; break;
case 'i': case 'i':
i_option = 1; ctl->i_option = 1;
if (!strcmp(optarg, "0") || !strcmp(optarg, "off")) if (!strcmp(optarg, "0") || !strcmp(optarg, "off"))
i_arg = 0; ctl->i_arg = 0;
else if (!strcmp(optarg, "1") || !strcmp(optarg, "on")) else if (!strcmp(optarg, "1") || !strcmp(optarg, "on"))
i_arg = 1; ctl->i_arg = 1;
else else
errx(EXIT_FAILURE, _("invalid argument to --manualeject/-i option")); errx(EXIT_FAILURE, _("invalid argument to --manualeject/-i option"));
break; break;
case 'm': case 'm':
m_option = 1; ctl->m_option = 1;
break; break;
case 'M': case 'M':
M_option = 1; ctl->M_option = 1;
break; break;
case 'n': case 'n':
n_option = 1; ctl->n_option = 1;
break; break;
case 'p': case 'p':
p_option = 1; ctl->p_option = 1;
break; break;
case 'q': case 'q':
q_option = 1; ctl->q_option = 1;
break; break;
case 'r': case 'r':
r_option = 1; ctl->r_option = 1;
break; break;
case 's': case 's':
s_option = 1; ctl->s_option = 1;
break; break;
case 't': case 't':
t_option = 1; ctl->t_option = 1;
break; break;
case 'T': case 'T':
T_option = 1; ctl->T_option = 1;
break; break;
case 'X': case 'X':
X_option = 1; ctl->X_option = 1;
break; break;
case 'v': case 'v':
v_option = 1; ctl->v_option = 1;
break; break;
case 'V': case 'V':
printf(UTIL_LINUX_VERSION); printf(UTIL_LINUX_VERSION);
@ -292,7 +295,7 @@ static void parse_args(int argc, char **argv, char **device)
errx(EXIT_FAILURE, _("too many arguments")); errx(EXIT_FAILURE, _("too many arguments"));
if ((argc - optind) == 1) if ((argc - optind) == 1)
*device = xstrdup(argv[optind]); ctl->device = xstrdup(argv[optind]);
} }
/* /*
@ -323,15 +326,15 @@ static char *find_device(const char *name)
} }
/* Set or clear auto-eject mode. */ /* Set or clear auto-eject mode. */
static void auto_eject(int fd, int on) static void auto_eject(const struct eject_control *ctl)
{ {
int status = -1; int status = -1;
#if defined(CDROM_SET_OPTIONS) && defined(CDROM_CLEAR_OPTIONS) #if defined(CDROM_SET_OPTIONS) && defined(CDROM_CLEAR_OPTIONS)
if (on) if (ctl->a_arg)
status = ioctl(fd, CDROM_SET_OPTIONS, CDO_AUTO_EJECT); status = ioctl(ctl->fd, CDROM_SET_OPTIONS, CDO_AUTO_EJECT);
else else
status = ioctl(fd, CDROM_CLEAR_OPTIONS, CDO_AUTO_EJECT); status = ioctl(ctl->fd, CDROM_CLEAR_OPTIONS, CDO_AUTO_EJECT);
#else #else
errno = ENOSYS; errno = ENOSYS;
#endif #endif
@ -350,12 +353,12 @@ static void auto_eject(int fd, int on)
* EBUSY Attempt to unlock when multiple users * EBUSY Attempt to unlock when multiple users
* have the drive open and not CAP_SYS_ADMIN * have the drive open and not CAP_SYS_ADMIN
*/ */
static void manual_eject(int fd, int on) static void manual_eject(const struct eject_control *ctl)
{ {
if (ioctl(fd, CDROM_LOCKDOOR, on) < 0) if (ioctl(ctl->fd, CDROM_LOCKDOOR, ctl->i_arg) < 0)
err(EXIT_FAILURE, _("CD-ROM lock door command failed")); err(EXIT_FAILURE, _("CD-ROM lock door command failed"));
if (on) if (ctl->i_arg)
info(_("CD-Drive may NOT be ejected with device button")); info(_("CD-Drive may NOT be ejected with device button"));
else else
info(_("CD-Drive may be ejected with device button")); info(_("CD-Drive may be ejected with device button"));
@ -365,14 +368,14 @@ static void manual_eject(int fd, int on)
* Changer select. CDROM_SELECT_DISC is preferred, older kernels used * Changer select. CDROM_SELECT_DISC is preferred, older kernels used
* CDROMLOADFROMSLOT. * CDROMLOADFROMSLOT.
*/ */
static void changer_select(int fd, int slot) static void changer_select(const struct eject_control *ctl)
{ {
#ifdef CDROM_SELECT_DISC #ifdef CDROM_SELECT_DISC
if (ioctl(fd, CDROM_SELECT_DISC, slot) < 0) if (ioctl(ctl->fd, CDROM_SELECT_DISC, ctl->c_arg) < 0)
err(EXIT_FAILURE, _("CD-ROM select disc command failed")); err(EXIT_FAILURE, _("CD-ROM select disc command failed"));
#elif defined CDROMLOADFROMSLOT #elif defined CDROMLOADFROMSLOT
if (ioctl(fd, CDROMLOADFROMSLOT, slot) != 0) if (ioctl(ctl->fd, CDROMLOADFROMSLOT, ctl->c_arg) != 0)
err(EXIT_FAILURE, _("CD-ROM load from slot command failed")); err(EXIT_FAILURE, _("CD-ROM load from slot command failed"));
#else #else
warnx(_("IDE/ATAPI CD-ROM changer not supported by this kernel\n") ); warnx(_("IDE/ATAPI CD-ROM changer not supported by this kernel\n") );
@ -557,18 +560,18 @@ static int read_speed(const char *devname)
/* /*
* List Speed of CD-ROM drive. * List Speed of CD-ROM drive.
*/ */
static void list_speeds(const char *name, int fd) static void list_speeds(const struct eject_control *ctl)
{ {
#ifdef CDROM_SELECT_SPEED #ifdef CDROM_SELECT_SPEED
int max_speed, curr_speed = 0, prev_speed; int max_speed, curr_speed = 0, prev_speed;
select_speed(fd, 0); select_speed(ctl->fd, 0);
max_speed = read_speed(name); max_speed = read_speed(ctl->device);
while (curr_speed < max_speed) { while (curr_speed < max_speed) {
prev_speed = curr_speed; prev_speed = curr_speed;
select_speed(fd, prev_speed + 1); select_speed(ctl->fd, prev_speed + 1);
curr_speed = read_speed(name); curr_speed = read_speed(ctl->device);
if (curr_speed > prev_speed) if (curr_speed > prev_speed)
printf("%d ", curr_speed); printf("%d ", curr_speed);
else else
@ -584,7 +587,7 @@ static void list_speeds(const char *name, int fd)
/* /*
* Eject using SCSI SG_IO commands. Return 1 if successful, 0 otherwise. * Eject using SCSI SG_IO commands. Return 1 if successful, 0 otherwise.
*/ */
static int eject_scsi(int fd) static int eject_scsi(const struct eject_control *ctl)
{ {
int status, k; int status, k;
sg_io_hdr_t io_hdr; sg_io_hdr_t io_hdr;
@ -594,8 +597,8 @@ static int eject_scsi(int fd)
unsigned char inqBuff[2]; unsigned char inqBuff[2];
unsigned char sense_buffer[32]; unsigned char sense_buffer[32];
if ((ioctl(fd, SG_GET_VERSION_NUM, &k) < 0) || (k < 30000)) { if ((ioctl(ctl->fd, SG_GET_VERSION_NUM, &k) < 0) || (k < 30000)) {
verbose(_("not an sg device, or old sg driver")); verbose(ctl, _("not an sg device, or old sg driver"));
return 0; return 0;
} }
@ -610,12 +613,12 @@ static int eject_scsi(int fd)
io_hdr.timeout = 10000; io_hdr.timeout = 10000;
io_hdr.cmdp = allowRmBlk; io_hdr.cmdp = allowRmBlk;
status = ioctl(fd, SG_IO, (void *)&io_hdr); status = ioctl(ctl->fd, SG_IO, (void *)&io_hdr);
if (status < 0 || io_hdr.host_status || io_hdr.driver_status) if (status < 0 || io_hdr.host_status || io_hdr.driver_status)
return 0; return 0;
io_hdr.cmdp = startStop1Blk; io_hdr.cmdp = startStop1Blk;
status = ioctl(fd, SG_IO, (void *)&io_hdr); status = ioctl(ctl->fd, SG_IO, (void *)&io_hdr);
if (status < 0 || io_hdr.host_status) if (status < 0 || io_hdr.host_status)
return 0; return 0;
@ -630,12 +633,12 @@ static int eject_scsi(int fd)
return 0; return 0;
io_hdr.cmdp = startStop2Blk; io_hdr.cmdp = startStop2Blk;
status = ioctl(fd, SG_IO, (void *)&io_hdr); status = ioctl(ctl->fd, SG_IO, (void *)&io_hdr);
if (status < 0 || io_hdr.host_status || io_hdr.driver_status) if (status < 0 || io_hdr.host_status || io_hdr.driver_status)
return 0; return 0;
/* force kernel to reread partition table when new disc inserted */ /* force kernel to reread partition table when new disc inserted */
ioctl(fd, BLKRRPART); ioctl(ctl->fd, BLKRRPART);
return 1; return 1;
} }
@ -660,14 +663,14 @@ static int eject_tape(int fd)
/* umount a device. */ /* umount a device. */
static void umount_one(const char *name) static void umount_one(const struct eject_control *ctl, const char *name)
{ {
int status; int status;
if (!name) if (!name)
return; return;
verbose(_("%s: unmounting"), name); verbose(ctl, _("%s: unmounting"), name);
switch (fork()) { switch (fork()) {
case 0: /* child */ case 0: /* child */
@ -677,7 +680,7 @@ static void umount_one(const char *name)
if (setuid(getuid()) < 0) if (setuid(getuid()) < 0)
err(EXIT_FAILURE, _("cannot set user id")); err(EXIT_FAILURE, _("cannot set user id"));
if (p_option) if (ctl->p_option)
execl("/bin/umount", "/bin/umount", name, "-n", NULL); execl("/bin/umount", "/bin/umount", name, "-n", NULL);
else else
execl("/bin/umount", "/bin/umount", name, NULL); execl("/bin/umount", "/bin/umount", name, NULL);
@ -716,36 +719,36 @@ static int open_device(const char *name)
* See if device has been mounted by looking in mount table. If so, set * See if device has been mounted by looking in mount table. If so, set
* device name and mount point name, and return 1, otherwise return 0. * device name and mount point name, and return 1, otherwise return 0.
*/ */
static int device_get_mountpoint(char **devname, char **mnt) static int device_get_mountpoint(struct eject_control *ctl, char **devname, char **mnt)
{ {
struct libmnt_fs *fs; struct libmnt_fs *fs;
int rc; int rc;
*mnt = NULL; *mnt = NULL;
if (!mtab) { if (!ctl->mtab) {
struct libmnt_cache *cache; struct libmnt_cache *cache;
mtab = mnt_new_table(); ctl->mtab = mnt_new_table();
if (!mtab) if (!ctl->mtab)
err(EXIT_FAILURE, _("failed to initialize libmount table")); err(EXIT_FAILURE, _("failed to initialize libmount table"));
cache = mnt_new_cache(); cache = mnt_new_cache();
mnt_table_set_cache(mtab, cache); mnt_table_set_cache(ctl->mtab, cache);
mnt_unref_cache(cache); mnt_unref_cache(cache);
if (p_option) if (ctl->p_option)
rc = mnt_table_parse_file(mtab, _PATH_PROC_MOUNTINFO); rc = mnt_table_parse_file(ctl->mtab, _PATH_PROC_MOUNTINFO);
else else
rc = mnt_table_parse_mtab(mtab, NULL); rc = mnt_table_parse_mtab(ctl->mtab, NULL);
if (rc) if (rc)
err(EXIT_FAILURE, _("failed to parse mount table")); err(EXIT_FAILURE, _("failed to parse mount table"));
} }
fs = mnt_table_find_source(mtab, *devname, MNT_ITER_BACKWARD); fs = mnt_table_find_source(ctl->mtab, *devname, MNT_ITER_BACKWARD);
if (!fs) { if (!fs) {
/* maybe 'devname' is mountpoint rather than a real device */ /* maybe 'devname' is mountpoint rather than a real device */
fs = mnt_table_find_target(mtab, *devname, MNT_ITER_BACKWARD); fs = mnt_table_find_target(ctl->mtab, *devname, MNT_ITER_BACKWARD);
if (fs) { if (fs) {
free(*devname); free(*devname);
*devname = xstrdup(mnt_fs_get_source(fs)); *devname = xstrdup(mnt_fs_get_source(fs));
@ -774,7 +777,7 @@ static char *get_disk_devname(const char *device)
return st.st_rdev == diskno ? NULL : find_device(diskname); return st.st_rdev == diskno ? NULL : find_device(diskname);
} }
static int umount_partitions(const char *disk, int checkonly) static int umount_partitions(struct eject_control *ctl)
{ {
struct sysfs_cxt cxt = UL_SYSFSCXT_EMPTY; struct sysfs_cxt cxt = UL_SYSFSCXT_EMPTY;
dev_t devno; dev_t devno;
@ -782,7 +785,7 @@ static int umount_partitions(const char *disk, int checkonly)
struct dirent *d; struct dirent *d;
int count = 0; int count = 0;
devno = sysfs_devname_to_devno(disk, NULL); devno = sysfs_devname_to_devno(ctl->device, NULL);
if (sysfs_init(&cxt, devno, NULL) != 0) if (sysfs_init(&cxt, devno, NULL) != 0)
return 0; return 0;
@ -795,14 +798,14 @@ static int umount_partitions(const char *disk, int checkonly)
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue; continue;
if (sysfs_is_partition_dirent(dir, d, disk)) { if (sysfs_is_partition_dirent(dir, d, ctl->device)) {
char *mnt = NULL; char *mnt = NULL;
char *dev = find_device(d->d_name); char *dev = find_device(d->d_name);
if (dev && device_get_mountpoint(&dev, &mnt) == 0) { if (dev && device_get_mountpoint(ctl, &dev, &mnt) == 0) {
verbose(_("%s: mounted on %s"), dev, mnt); verbose(ctl, _("%s: mounted on %s"), dev, mnt);
if (!checkonly) if (!ctl->M_option)
umount_one(mnt); umount_one(ctl, mnt);
count++; count++;
} }
free(dev); free(dev);
@ -881,7 +884,7 @@ static char *get_subsystem(char *chain, char *buf, size_t bufsz)
return NULL; return NULL;
} }
static int is_hotpluggable(const char* device) static int is_hotpluggable(const struct eject_control *ctl)
{ {
struct sysfs_cxt cxt = UL_SYSFSCXT_EMPTY; struct sysfs_cxt cxt = UL_SYSFSCXT_EMPTY;
char devchain[PATH_MAX]; char devchain[PATH_MAX];
@ -891,13 +894,13 @@ static int is_hotpluggable(const char* device)
ssize_t sz; ssize_t sz;
char *sub; char *sub;
devno = sysfs_devname_to_devno(device, NULL); devno = sysfs_devname_to_devno(ctl->device, NULL);
if (sysfs_init(&cxt, devno, NULL) != 0) if (sysfs_init(&cxt, devno, NULL) != 0)
return 0; return 0;
/* check /sys/dev/block/<maj>:<min>/removable attribute */ /* check /sys/dev/block/<maj>:<min>/removable attribute */
if (sysfs_read_int(&cxt, "removable", &rc) == 0 && rc == 1) { if (sysfs_read_int(&cxt, "removable", &rc) == 0 && rc == 1) {
verbose(_("%s: is removable device"), device); verbose(ctl, _("%s: is removable device"), ctl->device);
goto done; goto done;
} }
@ -916,8 +919,8 @@ static int is_hotpluggable(const char* device)
while ((sub = get_subsystem(devchain, subbuf, sizeof(subbuf)))) { while ((sub = get_subsystem(devchain, subbuf, sizeof(subbuf)))) {
rc = is_hotpluggable_subsystem(sub); rc = is_hotpluggable_subsystem(sub);
if (rc) { if (rc) {
verbose(_("%s: connected by hotplug subsystem: %s"), verbose(ctl, _("%s: connected by hotplug subsystem: %s"),
device, sub); ctl->device, sub);
break; break;
} }
} }
@ -929,20 +932,20 @@ done:
/* handle -x option */ /* handle -x option */
static void set_device_speed(char *name) static void set_device_speed(const struct eject_control *ctl)
{ {
int fd; int fd;
if (!x_option) if (!ctl->x_option)
return; return;
if (x_arg == 0) if (ctl->x_arg == 0)
verbose(_("setting CD-ROM speed to auto")); verbose(ctl, _("setting CD-ROM speed to auto"));
else else
verbose(_("setting CD-ROM speed to %ldX"), x_arg); verbose(ctl, _("setting CD-ROM speed to %ldX"), ctl->x_arg);
fd = open_device(name); fd = open_device(ctl->device);
select_speed(fd, x_arg); select_speed(fd, ctl->x_arg);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
@ -950,11 +953,10 @@ static void set_device_speed(char *name)
/* main program */ /* main program */
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
char *device = NULL;
char *disk = NULL; char *disk = NULL;
char *mountpoint = NULL; char *mountpoint = NULL;
int worked = 0; /* set to 1 when successfully ejected */ int worked = 0; /* set to 1 when successfully ejected */
int fd; /* file descriptor for device */ struct eject_control ctl = { 0 };
setlocale(LC_ALL,""); setlocale(LC_ALL,"");
bindtextdomain(PACKAGE, LOCALEDIR); bindtextdomain(PACKAGE, LOCALEDIR);
@ -962,118 +964,118 @@ int main(int argc, char **argv)
atexit(close_stdout); atexit(close_stdout);
/* parse the command line arguments */ /* parse the command line arguments */
parse_args(argc, argv, &device); parse_args(&ctl, argc, argv);
/* handle -d option */ /* handle -d option */
if (d_option) { if (ctl.d_option) {
info(_("default device: `%s'"), EJECT_DEFAULT_DEVICE); info(_("default device: `%s'"), EJECT_DEFAULT_DEVICE);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
if (!device) { if (!ctl.device) {
device = mnt_resolve_path(EJECT_DEFAULT_DEVICE, NULL); ctl.device = mnt_resolve_path(EJECT_DEFAULT_DEVICE, NULL);
verbose(_("using default device `%s'"), device); verbose(&ctl, _("using default device `%s'"), ctl.device);
} else { } else {
char *p; char *p;
if (device[strlen(device)-1] == '/') if (ctl.device[strlen(ctl.device) - 1] == '/')
device[strlen(device)-1] = '\0'; ctl.device[strlen(ctl.device) - 1] = '\0';
/* figure out full device or mount point name */ /* figure out full device or mount point name */
p = find_device(device); p = find_device(ctl.device);
if (p) if (p)
free(device); free(ctl.device);
else else
p = device; p = ctl.device;
device = mnt_resolve_spec(p, NULL); ctl.device = mnt_resolve_spec(p, NULL);
free(p); free(p);
} }
if (!device) if (!ctl.device)
errx(EXIT_FAILURE, _("%s: unable to find device"), device); errx(EXIT_FAILURE, _("%s: unable to find device"), ctl.device);
verbose(_("device name is `%s'"), device); verbose(&ctl, _("device name is `%s'"), ctl.device);
device_get_mountpoint(&device, &mountpoint); device_get_mountpoint(&ctl, &ctl.device, &mountpoint);
if (mountpoint) if (mountpoint)
verbose(_("%s: mounted on %s"), device, mountpoint); verbose(&ctl, _("%s: mounted on %s"), ctl.device, mountpoint);
else else
verbose(_("%s: not mounted"), device); verbose(&ctl, _("%s: not mounted"), ctl.device);
disk = get_disk_devname(device); disk = get_disk_devname(ctl.device);
if (disk) { if (disk) {
verbose(_("%s: disc device: %s (disk device will be used for eject)"), device, disk); verbose(&ctl, _("%s: disc device: %s (disk device will be used for eject)"), ctl.device, disk);
free(device); free(ctl.device);
device = disk; ctl.device = disk;
disk = NULL; disk = NULL;
} else { } else {
struct stat st; struct stat st;
if (stat(device, &st) != 0 || !S_ISBLK(st.st_mode)) if (stat(ctl.device, &st) != 0 || !S_ISBLK(st.st_mode))
errx(EXIT_FAILURE, _("%s: not found mountpoint or device " errx(EXIT_FAILURE, _("%s: not found mountpoint or device "
"with the given name"), device); "with the given name"), ctl.device);
verbose(_("%s: is whole-disk device"), device); verbose(&ctl, _("%s: is whole-disk device"), ctl.device);
} }
if (F_option == 0 && is_hotpluggable(device) == 0) if (ctl.F_option == 0 && is_hotpluggable(&ctl) == 0)
errx(EXIT_FAILURE, _("%s: is not hot-pluggable device"), device); errx(EXIT_FAILURE, _("%s: is not hot-pluggable device"), ctl.device);
/* handle -n option */ /* handle -n option */
if (n_option) { if (ctl.n_option) {
info(_("device is `%s'"), device); info(_("device is `%s'"), ctl.device);
verbose(_("exiting due to -n/--noop option")); verbose(&ctl, _("exiting due to -n/--noop option"));
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
/* handle -i option */ /* handle -i option */
if (i_option) { if (ctl.i_option) {
fd = open_device(device); ctl.fd = open_device(ctl.device);
manual_eject(fd, i_arg); manual_eject(&ctl);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
/* handle -a option */ /* handle -a option */
if (a_option) { if (ctl.a_option) {
if (a_arg) if (ctl.a_arg)
verbose(_("%s: enabling auto-eject mode"), device); verbose(&ctl, _("%s: enabling auto-eject mode"), ctl.device);
else else
verbose(_("%s: disabling auto-eject mode"), device); verbose(&ctl, _("%s: disabling auto-eject mode"), ctl.device);
fd = open_device(device); ctl.fd = open_device(ctl.device);
auto_eject(fd, a_arg); auto_eject(&ctl);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
/* handle -t option */ /* handle -t option */
if (t_option) { if (ctl.t_option) {
verbose(_("%s: closing tray"), device); verbose(&ctl, _("%s: closing tray"), ctl.device);
fd = open_device(device); ctl.fd = open_device(ctl.device);
close_tray(fd); close_tray(ctl.fd);
set_device_speed(device); set_device_speed(&ctl);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
/* handle -T option */ /* handle -T option */
if (T_option) { if (ctl.T_option) {
verbose(_("%s: toggling tray"), device); verbose(&ctl, _("%s: toggling tray"), ctl.device);
fd = open_device(device); ctl.fd = open_device(ctl.device);
toggle_tray(fd); toggle_tray(ctl.fd);
set_device_speed(device); set_device_speed(&ctl);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
/* handle -X option */ /* handle -X option */
if (X_option) { if (ctl.X_option) {
verbose(_("%s: listing CD-ROM speed"), device); verbose(&ctl, _("%s: listing CD-ROM speed"), ctl.device);
fd = open_device(device); ctl.fd = open_device(ctl.device);
list_speeds(device, fd); list_speeds(&ctl);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
/* handle -x option only */ /* handle -x option only */
if (!c_option) if (!ctl.c_option)
set_device_speed(device); set_device_speed(&ctl);
/* /*
@ -1081,62 +1083,62 @@ int main(int argc, char **argv)
* mountpoint if -M is specified, otherwise print error of another * mountpoint if -M is specified, otherwise print error of another
* partition is mounted. * partition is mounted.
*/ */
if (!m_option) { if (!ctl.m_option) {
int ct = umount_partitions(device, M_option); int ct = umount_partitions(&ctl);
if (ct == 0 && mountpoint) if (ct == 0 && mountpoint)
umount_one(mountpoint); /* probably whole-device */ umount_one(&ctl, mountpoint); /* probably whole-device */
if (M_option) { if (ctl.M_option) {
if (ct == 1 && mountpoint) if (ct == 1 && mountpoint)
umount_one(mountpoint); umount_one(&ctl, mountpoint);
else if (ct) else if (ct)
errx(EXIT_FAILURE, _("error: %s: device in use"), device); errx(EXIT_FAILURE, _("error: %s: device in use"), ctl.device);
} }
} }
/* handle -c option */ /* handle -c option */
if (c_option) { if (ctl.c_option) {
verbose(_("%s: selecting CD-ROM disc #%ld"), device, c_arg); verbose(&ctl, _("%s: selecting CD-ROM disc #%ld"), ctl.device, ctl.c_arg);
fd = open_device(device); ctl.fd = open_device(ctl.device);
changer_select(fd, c_arg); changer_select(&ctl);
set_device_speed(device); set_device_speed(&ctl);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
/* if user did not specify type of eject, try all four methods */ /* if user did not specify type of eject, try all four methods */
if (r_option + s_option + f_option + q_option == 0) if (ctl.r_option + ctl.s_option + ctl.f_option + ctl.q_option == 0)
r_option = s_option = f_option = q_option = 1; ctl.r_option = ctl.s_option = ctl.f_option = ctl.q_option = 1;
/* open device */ /* open device */
fd = open_device(device); ctl.fd = open_device(ctl.device);
/* try various methods of ejecting until it works */ /* try various methods of ejecting until it works */
if (r_option) { if (ctl.r_option) {
verbose(_("%s: trying to eject using CD-ROM eject command"), device); verbose(&ctl, _("%s: trying to eject using CD-ROM eject command"), ctl.device);
worked = eject_cdrom(fd); worked = eject_cdrom(ctl.fd);
verbose(worked ? _("CD-ROM eject command succeeded") : verbose(&ctl, worked ? _("CD-ROM eject command succeeded") :
_("CD-ROM eject command failed")); _("CD-ROM eject command failed"));
} }
if (s_option && !worked) { if (ctl.s_option && !worked) {
verbose(_("%s: trying to eject using SCSI commands"), device); verbose(&ctl, _("%s: trying to eject using SCSI commands"), ctl.device);
worked = eject_scsi(fd); worked = eject_scsi(&ctl);
verbose(worked ? _("SCSI eject succeeded") : verbose(&ctl, worked ? _("SCSI eject succeeded") :
_("SCSI eject failed")); _("SCSI eject failed"));
} }
if (f_option && !worked) { if (ctl.f_option && !worked) {
verbose(_("%s: trying to eject using floppy eject command"), device); verbose(&ctl, _("%s: trying to eject using floppy eject command"), ctl.device);
worked = eject_floppy(fd); worked = eject_floppy(ctl.fd);
verbose(worked ? _("floppy eject command succeeded") : verbose(&ctl, worked ? _("floppy eject command succeeded") :
_("floppy eject command failed")); _("floppy eject command failed"));
} }
if (q_option && !worked) { if (ctl.q_option && !worked) {
verbose(_("%s: trying to eject using tape offline command"), device); verbose(&ctl, _("%s: trying to eject using tape offline command"), ctl.device);
worked = eject_tape(fd); worked = eject_tape(ctl.fd);
verbose(worked ? _("tape offline command succeeded") : verbose(&ctl, worked ? _("tape offline command succeeded") :
_("tape offline command failed")); _("tape offline command failed"));
} }
@ -1144,11 +1146,11 @@ int main(int argc, char **argv)
errx(EXIT_FAILURE, _("unable to eject")); errx(EXIT_FAILURE, _("unable to eject"));
/* cleanup */ /* cleanup */
close(fd); close(ctl.fd);
free(device); free(ctl.device);
free(mountpoint); free(mountpoint);
mnt_unref_table(mtab); mnt_unref_table(ctl.mtab);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }