1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
/*-
* Copyright (c) 2001, 2002 Dima Dorfman.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* DEVFS control.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/queue.h>
#include <assert.h>
#include <err.h>
#include <fcntl.h>
#include <paths.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "extern.h"
int mpfd;
static ctbl_t ctbl_main = {
{ "rule", rule_main },
{ "ruleset", ruleset_main },
{ NULL, NULL }
};
int
main(int ac, char **av)
{
const char *mountpt;
struct cmd *c;
int ch;
mountpt = NULL;
while ((ch = getopt(ac, av, "m:")) != -1)
switch (ch) {
case 'm':
mountpt = optarg;
break;
default:
usage();
}
ac -= optind;
av += optind;
if (ac < 1)
usage();
if (mountpt == NULL)
mountpt = _PATH_DEV;
mpfd = open(mountpt, O_RDONLY);
if (mpfd == -1)
err(1, "open: %s", mountpt);
for (c = ctbl_main; c->name != NULL; ++c)
if (strcmp(c->name, av[0]) == 0)
exit((*c->handler)(ac, av));
errx(1, "unknown command: %s", av[0]);
}
/*
* Convert an integer to a "number" (ruleset numbers and rule numbers
* are 16-bit). If the conversion is successful, num contains the
* integer representation of s and 1 is returned; otherwise, 0 is
* returned and num is unchanged.
*/
int
atonum(const char *s, uint16_t *num)
{
unsigned long ul;
char *cp;
ul = strtoul(s, &cp, 10);
if (ul > UINT16_MAX || *cp != '\0')
return (0);
*num = (uint16_t)ul;
return (1);
}
/*
* Convert user input in ASCII to an integer.
*/
int
eatoi(const char *s)
{
char *cp;
long l;
l = strtol(s, &cp, 10);
if (l > INT_MAX || *cp != '\0')
errx(1, "error converting to integer: %s", s);
return ((int)l);
}
/*
* As atonum(), but the result of failure is death.
*/
uint16_t
eatonum(const char *s)
{
uint16_t num;
if (!atonum(s, &num))
errx(1, "error converting to number: %s", s); /* XXX clarify */
return (num);
}
/*
* Read a line from a /FILE/. If the return value isn't 0, it is the
* length of the line, a pointer to which exists in /line/. It is the
* caller's responsibility to free(3) it. If the return value is 0,
* there was an error or we reached EOF, and /line/ is undefined (so,
* obviously, the caller shouldn't try to free(3) it).
*/
size_t
efgetln(FILE *fp, char **line)
{
size_t rv;
char *cp;
cp = fgetln(fp, &rv);
if (cp == NULL) {
*line = NULL;
return (rv);
}
if (cp[rv - 1] == '\n') {
cp[rv - 1] = '\0';
*line = strdup(cp);
if (*line == NULL)
errx(1, "cannot allocate memory");
--rv;
} else {
*line = malloc(rv + 1);
if (*line == NULL)
errx(1, "cannot allocate memory");
memcpy(*line, cp, rv);
*line[rv] = '\0';
}
assert(rv == strlen(*line));
return (rv);
}
struct ptrstq {
STAILQ_ENTRY(ptrstq) tq;
void *ptr;
};
/*
* Create an argument vector from /line/. The caller must free(3)
* /avp/, and /avp[0]/ when the argument vector is no longer
* needed unless /acp/ is 0, in which case /avp/ is undefined.
* /avp/ is NULL-terminated, so it is actually one longer than /acp/.
*/
void
tokenize(const char *line, int *acp, char ***avp)
{
static const char *delims = " \t\n";
struct ptrstq *pt;
STAILQ_HEAD(, ptrstq) plist;
char **ap, *cp, *wline, *xcp;
line += strspn(line, delims);
wline = strdup(line);
if (wline == NULL)
errx(1, "cannot allocate memory");
STAILQ_INIT(&plist);
for (xcp = wline, *acp = 0;
(cp = strsep(&xcp, delims)) != NULL;)
if (*cp != '\0') {
pt = calloc(1, sizeof(*pt));
if (pt == NULL)
errx(1, "cannot allocate memory");
pt->ptr = cp;
STAILQ_INSERT_TAIL(&plist, pt, tq);
++*acp;
}
if (*acp == 0)
return;
assert(STAILQ_FIRST(&plist)->ptr == wline);
*avp = malloc(sizeof(**avp) * (*acp + 1));
if (*avp == NULL)
errx(1, "cannot allocate memory");
for (ap = *avp; !STAILQ_EMPTY(&plist);) {
pt = STAILQ_FIRST(&plist);
*ap = pt->ptr;
++ap;
assert(ap <= *avp + (*acp));
STAILQ_REMOVE_HEAD(&plist, tq);
free(pt);
}
*ap = NULL;
}
void
usage(void)
{
fprintf(stderr, "usage: devfs rule|ruleset arguments\n");
exit(1);
}
|