Example #1
0
int main(int argc, char *argv[])
{
	int  c;
	char * raw_name;
	char * block_name;
	int  err;
	int  block_major, block_minor;	
	int  i;

	struct stat statbuf;
	
	progname = argv[0];
	
	while ((c = getopt(argc, argv, "ahq")) != -1) {
		switch (c) {
		case 'a':
			do_query_all = 1;
			break;
		case 'h':
			usage(0);
		case 'q':
			do_query = 1;
			break;
		default:
			usage(1);
		}
	}
	
	/*
	 * Check for, and open, the master raw device, /dev/raw
	 */
	
	open_raw_ctl();
	
	if (do_query_all) {
		if (optind < argc)
			usage(1);
		for (i=1; i<255; i++)
			query(i, 1);
		exit(0);
	}
	
	/*
	 * It's a bind or a single query.  Either way we need a raw device.
	 */

	if (optind >= argc)
		usage(1);
	raw_name = argv[optind++];

	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);
	}

	raw_minor = minor(statbuf.st_rdev);

	if (do_query)
		return query(raw_minor, 0);
	
	/* 
	 * It's not a query, so we still have some parsing to do.  Have
	 * we been given a block device filename or a major/minor pair? 
	 */

	switch (argc - optind) {
	case 1:
		block_name = argv[optind];
		err = stat(block_name, &statbuf);
		if (err) {
			fprintf (stderr,
				 "Cannot locate block device '%s' (%s)\n",
				 block_name, strerror(errno));
			exit(2);
		}
		
		if (!S_ISBLK(statbuf.st_mode)) {
			fprintf (stderr, "Device '%s' is not a block dev\n",
				 block_name);
			exit(2);
		}
		
		block_major = major(statbuf.st_rdev);
		block_minor = minor(statbuf.st_rdev);
		break;
				
	case 2:
		block_major = strtol(argv[optind], 0, 0);
		block_minor = strtol(argv[optind+1], 0, 0);
		break;
		
	default:
		block_major = block_minor = 0; /* just to keep gcc happy */
		usage(1);
	}
	
	return bind(raw_minor, block_major, block_minor);
	return 0;
	
}
Example #2
0
int main(int argc, char *argv[])
{
	int c;
	char *raw_name;
	char *block_name;
	int retval;
	int block_major, block_minor;
	int i, rc;

	struct stat statbuf;

	static const struct option longopts[] = {
		{"query", no_argument, 0, 'q'},
		{"all", no_argument, 0, 'a'},
		{"version", no_argument, 0, 'V'},
		{"help", no_argument, 0, 'h'},
		{NULL, no_argument, 0, '0'},
	};

	setlocale(LC_MESSAGES, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	atexit(close_stdout);

	while ((c = getopt_long(argc, argv, "qaVh", longopts, NULL)) != -1)
		switch (c) {
		case 'q':
			do_query = 1;
			break;
		case 'a':
			do_query_all = 1;
			break;
		case 'V':
			printf(UTIL_LINUX_VERSION);
			return EXIT_SUCCESS;
		case 'h':
			usage(EXIT_SUCCESS);
		default:
			usage(EXIT_FAILURE);
		}

	/*
	 * Check for, and open, the master raw device, /dev/raw
	 */
	open_raw_ctl();

	if (do_query_all) {
		if (optind < argc)
			usage(EXIT_FAILURE);
		for (i = 1; i < RAW_NR_MINORS; i++)
			query(i, NULL, 1);
		exit(EXIT_SUCCESS);
	}

	/*
	 * It's a bind or a single query.  Either way we need a raw device.
	 */

	if (optind >= argc)
		usage(EXIT_FAILURE);
	raw_name = argv[optind++];

	/*
	 * 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, _PATH_RAWDEVDIR "raw%d", &raw_minor);
	if (rc != 1)
		usage(EXIT_FAILURE);

	if (raw_minor == 0)
		errx(EXIT_RAW_ACCESS,
		     _("Device '%s' is the control raw device "
		       "(use raw<N> where <N> is greater than zero)"),
		     raw_name);

	if (do_query)
		return query(raw_minor, raw_name, 0);

	/*
	 * It's not a query, so we still have some parsing to do.  Have we been
	 * given a block device filename or a major/minor pair?
	 */
	switch (argc - optind) {
	case 1:
		block_name = argv[optind];
		retval = stat(block_name, &statbuf);
		if (retval)
			err(EXIT_RAW_ACCESS,
			    _("Cannot locate block device '%s'"), block_name);
		if (!S_ISBLK(statbuf.st_mode))
			errx(EXIT_RAW_ACCESS,
			     _("Device '%s' is not a block device"),
			     block_name);
		block_major = major(statbuf.st_rdev);
		block_minor = minor(statbuf.st_rdev);
		break;

	case 2:
		block_major =
		    strtol_octal_or_err(argv[optind],
					_("failed to parse argument"));
		block_minor =
		    strtol_octal_or_err(argv[optind + 1],
					_("failed to parse argument"));
		break;

	default:
		usage(EXIT_FAILURE);
	}

	return bind(raw_minor, block_major, block_minor);
}
Example #3
0
int main(int argc, char *argv[])
{
	int  c;
	char * raw_name;
	char * block_name;
	int  err;
	int  block_major, block_minor;
	int  i, rc;

	struct stat statbuf;

	setlocale(LC_MESSAGES, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);

	progname = argv[0];

	while ((c = getopt(argc, argv, "ahq")) != -1) {
		switch (c) {
		case 'a':
			do_query_all = 1;
			break;
		case 'h':
			usage(0);
		case 'q':
			do_query = 1;
			break;
		default:
			usage(1);
		}
	}

	/*
	 * Check for, and open, the master raw device, /dev/raw
	 */

	open_raw_ctl();

	if (do_query_all) {
		if (optind < argc)
			usage(1);
		for (i = 1; i < RAW_NR_MINORS; i++)
			query(i, NULL, 1);
		exit(0);
	}

	/*
	 * It's a bind or a single query.  Either way we need a raw device.
	 */

	if (optind >= argc)
		usage(1);
	raw_name = argv[optind++];

	/*
	 * 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);
	if (rc != 1)
		usage(1);

	if (raw_minor == 0) {
		fprintf (stderr,
			_("Device '%s' is the control raw device "
			"(use raw<N> where <N> is greater than zero)\n"),
			raw_name);
		exit(2);
	}

	if (do_query)
		return query(raw_minor, raw_name, 0);

	/*
	 * It's not a query, so we still have some parsing to do.  Have
	 * we been given a block device filename or a major/minor pair?
	 */

	switch (argc - optind) {
	case 1:
		block_name = argv[optind];
		err = stat(block_name, &statbuf);
		if (err) {
			fprintf (stderr,
				 _("Cannot locate block device '%s' (%s)\n"),
				 block_name, strerror(errno));
			exit(2);
		}

		if (!S_ISBLK(statbuf.st_mode)) {
			fprintf (stderr, _("Device '%s' is not a block device\n"),
				 block_name);
			exit(2);
		}

		block_major = major(statbuf.st_rdev);
		block_minor = minor(statbuf.st_rdev);
		break;

	case 2:
		block_major = strtol(argv[optind], 0, 0);
		block_minor = strtol(argv[optind+1], 0, 0);
		break;

	default:
		block_major = block_minor = 0; /* just to keep gcc happy */
		usage(1);
	}

	return bind(raw_minor, block_major, block_minor);
	return 0;

}