raw: use libc error printing facilities

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2012-03-17 20:05:56 +01:00 committed by Karel Zak
parent 44e535353e
commit 82b7ef3671
1 changed files with 37 additions and 61 deletions

View File

@ -73,7 +73,7 @@ int main(int argc, char *argv[])
int c; int c;
char * raw_name; char * raw_name;
char * block_name; char * block_name;
int err; int retval;
int block_major, block_minor; int block_major, block_minor;
int i, rc; int i, rc;
@ -140,13 +140,11 @@ int main(int argc, char *argv[])
if (rc != 1) if (rc != 1)
usage(EXIT_FAILURE); usage(EXIT_FAILURE);
if (raw_minor == 0) { if (raw_minor == 0)
fprintf (stderr, errx(EXIT_RAW_ACCESS,
_("Device '%s' is the control raw device " _("Device '%s' is the control raw device "
"(use raw<N> where <N> is greater than zero)\n"), "(use raw<N> where <N> is greater than zero)"),
raw_name); raw_name);
exit(EXIT_RAW_ACCESS);
}
if (do_query) if (do_query)
return query(raw_minor, raw_name, 0); return query(raw_minor, raw_name, 0);
@ -159,20 +157,15 @@ int main(int argc, char *argv[])
switch (argc - optind) { switch (argc - optind) {
case 1: case 1:
block_name = argv[optind]; block_name = argv[optind];
err = stat(block_name, &statbuf); retval = stat(block_name, &statbuf);
if (err) { if (retval)
fprintf (stderr, err(EXIT_RAW_ACCESS,
_("Cannot locate block device '%s' (%m)\n"), _("Cannot locate block device '%s'"),
block_name); block_name);
exit(EXIT_RAW_ACCESS); if (!S_ISBLK(statbuf.st_mode))
} errx(EXIT_RAW_ACCESS,
_("Device '%s' is not a block device"),
if (!S_ISBLK(statbuf.st_mode)) { block_name);
fprintf (stderr, _("Device '%s' is not a block device\n"),
block_name);
exit(EXIT_RAW_ACCESS);
}
block_major = major(statbuf.st_rdev); block_major = major(statbuf.st_rdev);
block_minor = minor(statbuf.st_rdev); block_minor = minor(statbuf.st_rdev);
break; break;
@ -193,61 +186,49 @@ int main(int argc, char *argv[])
void open_raw_ctl(void) void open_raw_ctl(void)
{ {
int errsv;
master_fd = open(RAWDEVCTL, O_RDWR, 0); master_fd = open(RAWDEVCTL, O_RDWR, 0);
if (master_fd < 0) { if (master_fd < 0) {
errsv = errno;
master_fd = open(RAWDEVCTL_OLD, O_RDWR, 0); master_fd = open(RAWDEVCTL_OLD, O_RDWR, 0);
if (master_fd < 0) { if (master_fd < 0)
fprintf (stderr, err(EXIT_RAW_ACCESS,
_("Cannot open master raw device '%s' (%s)\n"), _("Cannot open master raw device '%s'"), RAWDEVCTL);
RAWDEVCTL, strerror(errsv));
exit(EXIT_RAW_ACCESS);
}
} }
} }
static int query(int minor_raw, const char *raw_name, int quiet) static int query(int minor_raw, const char *raw_name, int quiet)
{ {
struct raw_config_request rq; struct raw_config_request rq;
static int has_worked = 0; static int has_worked = 0;
int err; int retval;
if (raw_name) { if (raw_name) {
struct stat statbuf; struct stat statbuf;
err = stat(raw_name, &statbuf); retval = stat(raw_name, &statbuf);
if (err) { if (retval)
fprintf (stderr, _("Cannot locate raw device '%s' (%m)\n"), err(EXIT_RAW_ACCESS,
raw_name); _("Cannot locate raw device '%s'"), raw_name);
exit(EXIT_RAW_ACCESS); if (!S_ISCHR(statbuf.st_mode))
} errx(EXIT_RAW_ACCESS,
_("Raw device '%s' is not a character dev"),
if (!S_ISCHR(statbuf.st_mode)) { raw_name);
fprintf (stderr, _("Raw device '%s' is not a character dev\n"), if (major(statbuf.st_rdev) != RAW_MAJOR)
raw_name); errx(EXIT_RAW_ACCESS,
exit(EXIT_RAW_ACCESS); _("Device '%s' is not a raw dev"), raw_name);
}
if (major(statbuf.st_rdev) != RAW_MAJOR) {
fprintf (stderr, _("Device '%s' is not a raw dev\n"),
raw_name);
exit(EXIT_RAW_ACCESS);
}
minor_raw = minor(statbuf.st_rdev); minor_raw = minor(statbuf.st_rdev);
} }
rq.raw_minor = minor_raw; rq.raw_minor = minor_raw;
err = ioctl(master_fd, RAW_GETBIND, &rq); retval = ioctl(master_fd, RAW_GETBIND, &rq);
if (err < 0) { if (retval < 0) {
if (quiet && errno == ENODEV) if (quiet && errno == ENODEV)
return 3; return 3;
if (has_worked && errno == EINVAL) if (has_worked && errno == EINVAL)
return 0; return 0;
fprintf (stderr, err(EXIT_RAW_IOCTL, _("Error querying raw device"));
_("Error querying raw device (%m)\n"));
exit(EXIT_RAW_IOCTL);
} }
/* If one query has worked, mark that fact so that we don't /* If one query has worked, mark that fact so that we don't
* report spurious fatal errors if raw(8) has been built to * report spurious fatal errors if raw(8) has been built to
* support more raw minor numbers than the kernel has. */ * support more raw minor numbers than the kernel has. */
@ -262,17 +243,12 @@ static int query(int minor_raw, const char *raw_name, int quiet)
static int bind(int minor_raw, int block_major, int block_minor) static int bind(int minor_raw, int block_major, int block_minor)
{ {
struct raw_config_request rq; struct raw_config_request rq;
int err;
rq.raw_minor = minor_raw; rq.raw_minor = minor_raw;
rq.block_major = block_major; rq.block_major = block_major;
rq.block_minor = block_minor; rq.block_minor = block_minor;
err = ioctl(master_fd, RAW_SETBIND, &rq); if (!ioctl(master_fd, RAW_SETBIND, &rq))
if (err < 0) { err(EXIT_RAW_IOCTL, _("Error setting raw device"));
fprintf (stderr,
_("Error setting raw device (%m)\n"));
exit(EXIT_RAW_IOCTL);
}
printf (_("%sraw%d: bound to major %d, minor %d\n"), printf (_("%sraw%d: bound to major %d, minor %d\n"),
RAWDEVDIR, raw_minor, (int) rq.block_major, (int) rq.block_minor); RAWDEVDIR, raw_minor, (int) rq.block_major, (int) rq.block_minor);
return 0; return 0;