devfs.c revision 100206
1/*-
2 * Copyright (c) 2002 Dima Dorfman.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27/*
28 * DEVFS control.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/sbin/devfs/devfs.c 100206 2002-07-17 01:46:48Z dd $");
33
34#include <sys/param.h>
35
36#include <err.h>
37#include <fcntl.h>
38#include <paths.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <unistd.h>
43
44#include "extern.h"
45
46int mpfd;
47
48static ctbl_t ctbl_main = {
49	{ "rule",		rule_main },
50	{ "ruleset",		ruleset_main },
51	{ NULL,			NULL }
52};
53
54int
55main(int ac, char **av)
56{
57	const char *mountpt;
58	struct cmd *c;
59	char ch;
60
61	mountpt = NULL;
62	while ((ch = getopt(ac, av, "m:")) != -1)
63		switch (ch) {
64		case 'm':
65			mountpt = optarg;
66			break;
67		default:
68			usage();
69		}
70	ac -= optind;
71	av += optind;
72	if (ac < 1)
73		usage();
74
75	if (mountpt == NULL)
76		mountpt = _PATH_DEV;
77	mpfd = open(mountpt, O_RDONLY);
78	if (mpfd == -1)
79		err(1, "open: %s", mountpt);
80
81	for (c = ctbl_main; c->name != NULL; ++c)
82		if (strcmp(c->name, av[0]) == 0)
83			exit((*c->handler)(ac, av));
84	errx(1, "unknown command: %s", av[0]);
85}
86
87/*
88 * Convert an integer to a "number" (ruleset numbers and rule numbers
89 * are 16-bit).  If the conversion is successful, num contains the
90 * integer representation of s and 1 is returned; otherwise, 0 is
91 * returned and num is unchanged.
92 */
93int
94atonum(const char *s, uint16_t *num)
95{
96	unsigned long ul;
97	char *cp;
98
99	ul = strtoul(s, &cp, 10);
100	if (ul > UINT16_MAX || *cp != '\0')
101		return (0);
102	*num = (uint16_t)ul;
103	return (1);
104}
105
106/*
107 * Convert user input in ASCII to an integer.
108 */
109int
110eatoi(const char *s)
111{
112	char *cp;
113	long l;
114
115	l = strtol(s, &cp, 10);
116	if (l > INT_MAX || *cp != '\0')
117		errx(1, "error converting to integer: %s", s);
118	return ((int)l);
119}
120
121/*
122 * As atonum(), but the result of failure is death.
123 */
124uint16_t
125eatonum(const char *s)
126{
127	uint16_t num;
128
129	if (!atonum(s, &num))
130		errx(1, "error converting to number: %s", s); /* XXX clarify */
131	return (num);
132}
133
134void
135usage(void)
136{
137
138	fprintf(stderr, "usage: devfs rule|ruleset arguments\n");
139	exit(1);
140}
141