2006-12-06 17:25:41 -06:00
|
|
|
/*
|
|
|
|
* raw.c: User mode tool to bind and query raw character devices.
|
|
|
|
*
|
2006-12-06 17:25:43 -06:00
|
|
|
* Stephen Tweedie, 1999, 2000
|
2006-12-06 17:25:41 -06:00
|
|
|
*
|
|
|
|
* This file may be redistributed under the terms of the GNU General
|
|
|
|
* Public License, version 2.
|
|
|
|
*
|
2006-12-06 17:25:43 -06:00
|
|
|
* Copyright Red Hat Software, 1999, 2000
|
2006-12-06 17:25:41 -06:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2008-07-24 08:08:16 -05:00
|
|
|
#include <fcntl.h>
|
2006-12-06 17:25:41 -06:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/sysmacros.h>
|
2006-12-06 17:25:43 -06:00
|
|
|
#include <linux/raw.h>
|
|
|
|
#include <linux/major.h>
|
2008-10-03 01:46:36 -05:00
|
|
|
#include "nls.h"
|
2006-12-06 17:25:41 -06:00
|
|
|
|
2008-12-25 18:09:34 -06:00
|
|
|
|
2006-12-06 17:25:43 -06:00
|
|
|
#define RAWDEVDIR "/dev/raw/"
|
2008-12-25 18:09:34 -06:00
|
|
|
#define RAWDEVCTL RAWDEVDIR "rawctl"
|
|
|
|
/* deprecated */
|
|
|
|
#define RAWDEVCTL_OLD "/dev/rawctl"
|
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
|
2007-01-26 12:55:41 -06:00
|
|
|
#define RAW_NR_MINORS 8192
|
2006-12-06 17:25:41 -06:00
|
|
|
|
|
|
|
char * progname;
|
|
|
|
int do_query = 0;
|
|
|
|
int do_query_all = 0;
|
|
|
|
|
|
|
|
int master_fd;
|
|
|
|
int raw_minor;
|
|
|
|
|
|
|
|
void open_raw_ctl(void);
|
2011-08-29 11:59:35 -05:00
|
|
|
static int query(int minor_raw, const char *raw_name, int quiet);
|
|
|
|
static int bind(int minor_raw, int block_major, int block_minor);
|
2006-12-06 17:25:41 -06:00
|
|
|
|
|
|
|
|
2009-06-29 16:05:36 -05:00
|
|
|
static void usage(int err)
|
2006-12-06 17:25:41 -06:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2008-10-03 01:46:36 -05:00
|
|
|
_("Usage:\n"
|
2011-02-27 09:27:43 -06:00
|
|
|
" %1$s %2$srawN <major> <minor>\n"
|
|
|
|
" %1$s %2$srawN /dev/<blockdevice>\n"
|
|
|
|
" %1$s -q %2$srawN\n"
|
|
|
|
" %1$s -qa\n"),
|
|
|
|
progname, RAWDEVDIR);
|
2006-12-06 17:25:41 -06:00
|
|
|
exit(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2006-12-06 17:25:48 -06:00
|
|
|
int c;
|
2006-12-06 17:25:41 -06:00
|
|
|
char * raw_name;
|
|
|
|
char * block_name;
|
|
|
|
int err;
|
2008-10-03 01:46:36 -05:00
|
|
|
int block_major, block_minor;
|
2007-01-26 12:55:41 -06:00
|
|
|
int i, rc;
|
2006-12-06 17:25:41 -06:00
|
|
|
|
|
|
|
struct stat statbuf;
|
2008-10-03 01:46:36 -05:00
|
|
|
|
|
|
|
setlocale(LC_MESSAGES, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
progname = argv[0];
|
2008-10-03 01:46:36 -05:00
|
|
|
|
2006-12-06 17:25:53 -06:00
|
|
|
while ((c = getopt(argc, argv, "ahq")) != -1) {
|
2006-12-06 17:25:41 -06:00
|
|
|
switch (c) {
|
|
|
|
case 'a':
|
|
|
|
do_query_all = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage(0);
|
|
|
|
case 'q':
|
|
|
|
do_query = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(1);
|
|
|
|
}
|
|
|
|
}
|
2008-10-03 01:46:36 -05:00
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
/*
|
|
|
|
* Check for, and open, the master raw device, /dev/raw
|
|
|
|
*/
|
2008-10-03 01:46:36 -05:00
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
open_raw_ctl();
|
2008-10-03 01:46:36 -05:00
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
if (do_query_all) {
|
|
|
|
if (optind < argc)
|
|
|
|
usage(1);
|
2007-01-26 12:55:41 -06:00
|
|
|
for (i = 1; i < RAW_NR_MINORS; i++)
|
2009-06-29 16:05:36 -05:00
|
|
|
query(i, NULL, 1);
|
2006-12-06 17:25:41 -06:00
|
|
|
exit(0);
|
|
|
|
}
|
2008-10-03 01:46:36 -05:00
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
/*
|
|
|
|
* It's a bind or a single query. Either way we need a raw device.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (optind >= argc)
|
|
|
|
usage(1);
|
|
|
|
raw_name = argv[optind++];
|
|
|
|
|
2007-01-26 12:55:41 -06:00
|
|
|
/*
|
|
|
|
* try to check the device name before stat(), because on systems with
|
|
|
|
* udev the raw0 causes a create udev event for char 162/0, which
|
|
|
|
* causes udev to *remove* /dev/rawctl
|
|
|
|
*/
|
|
|
|
rc = sscanf(raw_name, RAWDEVDIR "raw%d", &raw_minor);
|
2009-06-29 16:05:36 -05:00
|
|
|
if (rc != 1)
|
|
|
|
usage(1);
|
|
|
|
|
|
|
|
if (raw_minor == 0) {
|
2008-10-03 01:46:36 -05:00
|
|
|
fprintf (stderr,
|
2011-02-27 09:27:43 -06:00
|
|
|
_("Device '%s' is the control raw device "
|
2008-10-03 01:46:36 -05:00
|
|
|
"(use raw<N> where <N> is greater than zero)\n"),
|
|
|
|
raw_name);
|
2007-01-26 12:55:41 -06:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
if (do_query)
|
2009-06-29 16:05:36 -05:00
|
|
|
return query(raw_minor, raw_name, 0);
|
2007-01-26 12:55:41 -06:00
|
|
|
|
2008-10-03 01:46:36 -05:00
|
|
|
/*
|
2006-12-06 17:25:41 -06:00
|
|
|
* It's not a query, so we still have some parsing to do. Have
|
2008-10-03 01:46:36 -05:00
|
|
|
* we been given a block device filename or a major/minor pair?
|
2006-12-06 17:25:41 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
switch (argc - optind) {
|
|
|
|
case 1:
|
|
|
|
block_name = argv[optind];
|
|
|
|
err = stat(block_name, &statbuf);
|
|
|
|
if (err) {
|
|
|
|
fprintf (stderr,
|
2008-10-03 01:46:36 -05:00
|
|
|
_("Cannot locate block device '%s' (%s)\n"),
|
2006-12-06 17:25:41 -06:00
|
|
|
block_name, strerror(errno));
|
|
|
|
exit(2);
|
|
|
|
}
|
2008-10-03 01:46:36 -05:00
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
if (!S_ISBLK(statbuf.st_mode)) {
|
2011-02-27 09:27:43 -06:00
|
|
|
fprintf (stderr, _("Device '%s' is not a block device\n"),
|
2006-12-06 17:25:41 -06:00
|
|
|
block_name);
|
|
|
|
exit(2);
|
|
|
|
}
|
2008-10-03 01:46:36 -05:00
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
block_major = major(statbuf.st_rdev);
|
|
|
|
block_minor = minor(statbuf.st_rdev);
|
|
|
|
break;
|
2008-10-03 01:46:36 -05:00
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
case 2:
|
|
|
|
block_major = strtol(argv[optind], 0, 0);
|
|
|
|
block_minor = strtol(argv[optind+1], 0, 0);
|
|
|
|
break;
|
2008-10-03 01:46:36 -05:00
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
default:
|
2006-12-06 17:25:44 -06:00
|
|
|
block_major = block_minor = 0; /* just to keep gcc happy */
|
2006-12-06 17:25:41 -06:00
|
|
|
usage(1);
|
|
|
|
}
|
2008-10-03 01:46:36 -05:00
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
return bind(raw_minor, block_major, block_minor);
|
|
|
|
return 0;
|
2008-10-03 01:46:36 -05:00
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void open_raw_ctl(void)
|
|
|
|
{
|
2006-12-06 17:25:53 -06:00
|
|
|
int errsv;
|
|
|
|
|
2008-12-25 18:09:34 -06:00
|
|
|
master_fd = open(RAWDEVCTL, O_RDWR, 0);
|
2006-12-06 17:25:41 -06:00
|
|
|
if (master_fd < 0) {
|
2006-12-06 17:25:53 -06:00
|
|
|
errsv = errno;
|
2008-12-25 18:09:34 -06:00
|
|
|
master_fd = open(RAWDEVCTL_OLD, O_RDWR, 0);
|
2006-12-06 17:25:53 -06:00
|
|
|
if (master_fd < 0) {
|
2008-12-25 18:09:34 -06:00
|
|
|
fprintf (stderr,
|
2011-02-27 09:27:43 -06:00
|
|
|
_("Cannot open master raw device '%s' (%s)\n"),
|
|
|
|
RAWDEVCTL, strerror(errsv));
|
2006-12-06 17:25:53 -06:00
|
|
|
exit(2);
|
|
|
|
}
|
2006-12-06 17:25:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-29 11:59:35 -05:00
|
|
|
static int query(int minor_raw, const char *raw_name, int quiet)
|
2006-12-06 17:25:41 -06:00
|
|
|
{
|
|
|
|
struct raw_config_request rq;
|
2007-01-26 12:55:41 -06:00
|
|
|
static int has_worked = 0;
|
2006-12-06 17:25:41 -06:00
|
|
|
int err;
|
2007-01-26 12:55:41 -06:00
|
|
|
|
2009-06-29 16:05:36 -05:00
|
|
|
if (raw_name) {
|
|
|
|
struct stat statbuf;
|
|
|
|
|
|
|
|
err = stat(raw_name, &statbuf);
|
|
|
|
if (err) {
|
|
|
|
fprintf (stderr, _("Cannot locate raw device '%s' (%s)\n"),
|
|
|
|
raw_name, strerror(errno));
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISCHR(statbuf.st_mode)) {
|
|
|
|
fprintf (stderr, _("Raw device '%s' is not a character dev\n"),
|
|
|
|
raw_name);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
if (major(statbuf.st_rdev) != RAW_MAJOR) {
|
|
|
|
fprintf (stderr, _("Device '%s' is not a raw dev\n"),
|
|
|
|
raw_name);
|
|
|
|
exit(2);
|
|
|
|
}
|
2011-08-29 11:59:35 -05:00
|
|
|
minor_raw = minor(statbuf.st_rdev);
|
2009-06-29 16:05:36 -05:00
|
|
|
}
|
|
|
|
|
2011-08-29 11:59:35 -05:00
|
|
|
rq.raw_minor = minor_raw;
|
2006-12-06 17:25:41 -06:00
|
|
|
err = ioctl(master_fd, RAW_GETBIND, &rq);
|
|
|
|
if (err < 0) {
|
|
|
|
if (quiet && errno == ENODEV)
|
|
|
|
return 3;
|
2007-01-26 12:55:41 -06:00
|
|
|
if (has_worked && errno == EINVAL)
|
|
|
|
return 0;
|
|
|
|
fprintf (stderr,
|
2008-10-03 01:46:36 -05:00
|
|
|
_("Error querying raw device (%s)\n"),
|
2006-12-06 17:25:41 -06:00
|
|
|
strerror(errno));
|
|
|
|
exit(3);
|
|
|
|
}
|
2007-01-26 12:55:41 -06:00
|
|
|
/* If one query has worked, mark that fact so that we don't
|
|
|
|
* report spurious fatal errors if raw(8) has been built to
|
|
|
|
* support more raw minor numbers than the kernel has. */
|
|
|
|
has_worked = 1;
|
2006-12-06 17:25:41 -06:00
|
|
|
if (quiet && !rq.block_major && !rq.block_minor)
|
|
|
|
return 0;
|
2011-02-27 09:27:43 -06:00
|
|
|
printf (_("%sraw%d: bound to major %d, minor %d\n"),
|
2011-08-29 11:59:35 -05:00
|
|
|
RAWDEVDIR, minor_raw, (int) rq.block_major, (int) rq.block_minor);
|
2006-12-06 17:25:41 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-29 11:59:35 -05:00
|
|
|
static int bind(int minor_raw, int block_major, int block_minor)
|
2006-12-06 17:25:41 -06:00
|
|
|
{
|
|
|
|
struct raw_config_request rq;
|
|
|
|
int err;
|
2008-10-03 01:46:36 -05:00
|
|
|
|
2011-08-29 11:59:35 -05:00
|
|
|
rq.raw_minor = minor_raw;
|
2006-12-06 17:25:41 -06:00
|
|
|
rq.block_major = block_major;
|
|
|
|
rq.block_minor = block_minor;
|
|
|
|
err = ioctl(master_fd, RAW_SETBIND, &rq);
|
|
|
|
if (err < 0) {
|
2008-10-03 01:46:36 -05:00
|
|
|
fprintf (stderr,
|
|
|
|
_("Error setting raw device (%s)\n"),
|
2006-12-06 17:25:41 -06:00
|
|
|
strerror(errno));
|
|
|
|
exit(3);
|
|
|
|
}
|
2011-02-27 09:27:43 -06:00
|
|
|
printf (_("%sraw%d: bound to major %d, minor %d\n"),
|
|
|
|
RAWDEVDIR, raw_minor, (int) rq.block_major, (int) rq.block_minor);
|
2006-12-06 17:25:41 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|