| @@ -1,394 +1,393 @@ | | | @@ -1,394 +1,393 @@ |
1 | /* $NetBSD: mknod.c,v 1.39 2009/02/13 01:37:23 lukem Exp $ */ | | 1 | /* $NetBSD: mknod.c,v 1.40 2011/08/27 18:37:41 joerg Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Charles M. Hannum. | | 8 | * by Charles M. Hannum. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright | | 15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the | | 16 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. | | 17 | * documentation and/or other materials provided with the distribution. |
18 | * | | 18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | | 19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. | | 29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | | | 31 | |
32 | #if HAVE_NBTOOL_CONFIG_H | | 32 | #if HAVE_NBTOOL_CONFIG_H |
33 | #include "nbtool_config.h" | | 33 | #include "nbtool_config.h" |
34 | #endif | | 34 | #endif |
35 | | | 35 | |
36 | #include <sys/cdefs.h> | | 36 | #include <sys/cdefs.h> |
37 | #ifndef lint | | 37 | #ifndef lint |
38 | __COPYRIGHT("@(#) Copyright (c) 1998\ | | 38 | __COPYRIGHT("@(#) Copyright (c) 1998\ |
39 | The NetBSD Foundation, Inc. All rights reserved."); | | 39 | The NetBSD Foundation, Inc. All rights reserved."); |
40 | __RCSID("$NetBSD: mknod.c,v 1.39 2009/02/13 01:37:23 lukem Exp $"); | | 40 | __RCSID("$NetBSD: mknod.c,v 1.40 2011/08/27 18:37:41 joerg Exp $"); |
41 | #endif /* not lint */ | | 41 | #endif /* not lint */ |
42 | | | 42 | |
43 | #include <sys/types.h> | | 43 | #include <sys/types.h> |
44 | #include <sys/stat.h> | | 44 | #include <sys/stat.h> |
45 | #include <sys/param.h> | | 45 | #include <sys/param.h> |
46 | #if !HAVE_NBTOOL_CONFIG_H | | 46 | #if !HAVE_NBTOOL_CONFIG_H |
47 | #include <sys/sysctl.h> | | 47 | #include <sys/sysctl.h> |
48 | #endif | | 48 | #endif |
49 | | | 49 | |
50 | #include <err.h> | | 50 | #include <err.h> |
51 | #include <errno.h> | | 51 | #include <errno.h> |
52 | #include <limits.h> | | 52 | #include <limits.h> |
53 | #include <stdio.h> | | 53 | #include <stdio.h> |
54 | #include <stdlib.h> | | 54 | #include <stdlib.h> |
55 | #include <unistd.h> | | 55 | #include <unistd.h> |
56 | #include <pwd.h> | | 56 | #include <pwd.h> |
57 | #include <grp.h> | | 57 | #include <grp.h> |
58 | #include <string.h> | | 58 | #include <string.h> |
59 | #include <ctype.h> | | 59 | #include <ctype.h> |
60 | | | 60 | |
61 | #include "pack_dev.h" | | 61 | #include "pack_dev.h" |
62 | | | 62 | |
63 | static int gid_name(const char *, gid_t *); | | 63 | static int gid_name(const char *, gid_t *); |
64 | static portdev_t callPack(pack_t *, int, u_long *); | | 64 | static portdev_t callPack(pack_t *, int, u_long *); |
65 | | | 65 | |
66 | int main(int, char *[]); | | 66 | __dead static void usage(void); |
67 | static void usage(void); | | | |
68 | | | 67 | |
69 | #ifdef KERN_DRIVERS | | 68 | #ifdef KERN_DRIVERS |
70 | static struct kinfo_drivers *kern_drivers; | | 69 | static struct kinfo_drivers *kern_drivers; |
71 | static int num_drivers; | | 70 | static int num_drivers; |
72 | | | 71 | |
73 | static void get_device_info(void); | | 72 | static void get_device_info(void); |
74 | static void print_device_info(char **); | | 73 | static void print_device_info(char **); |
75 | static int major_from_name(const char *, mode_t); | | 74 | static int major_from_name(const char *, mode_t); |
76 | #endif | | 75 | #endif |
77 | | | 76 | |
78 | #define MAXARGS 3 /* 3 for bsdos, 2 for rest */ | | 77 | #define MAXARGS 3 /* 3 for bsdos, 2 for rest */ |
79 | | | 78 | |
80 | int | | 79 | int |
81 | main(int argc, char **argv) | | 80 | main(int argc, char **argv) |
82 | { | | 81 | { |
83 | char *name, *p; | | 82 | char *name, *p; |
84 | mode_t mode; | | 83 | mode_t mode; |
85 | portdev_t dev; | | 84 | portdev_t dev; |
86 | pack_t *pack; | | 85 | pack_t *pack; |
87 | u_long numbers[MAXARGS]; | | 86 | u_long numbers[MAXARGS]; |
88 | int n, ch, fifo, hasformat; | | 87 | int n, ch, fifo, hasformat; |
89 | int r_flag = 0; /* force: delete existing entry */ | | 88 | int r_flag = 0; /* force: delete existing entry */ |
90 | #ifdef KERN_DRIVERS | | 89 | #ifdef KERN_DRIVERS |
91 | int l_flag = 0; /* list device names and numbers */ | | 90 | int l_flag = 0; /* list device names and numbers */ |
92 | int major; | | 91 | int major; |
93 | #endif | | 92 | #endif |
94 | void *modes = 0; | | 93 | void *modes = 0; |
95 | uid_t uid = -1; | | 94 | uid_t uid = -1; |
96 | gid_t gid = -1; | | 95 | gid_t gid = -1; |
97 | int rval; | | 96 | int rval; |
98 | | | 97 | |
99 | dev = 0; | | 98 | dev = 0; |
100 | fifo = hasformat = 0; | | 99 | fifo = hasformat = 0; |
101 | pack = pack_native; | | 100 | pack = pack_native; |
102 | | | 101 | |
103 | #ifdef KERN_DRIVERS | | 102 | #ifdef KERN_DRIVERS |
104 | while ((ch = getopt(argc, argv, "lrRF:g:m:u:")) != -1) { | | 103 | while ((ch = getopt(argc, argv, "lrRF:g:m:u:")) != -1) { |
105 | #else | | 104 | #else |
106 | while ((ch = getopt(argc, argv, "rRF:g:m:u:")) != -1) { | | 105 | while ((ch = getopt(argc, argv, "rRF:g:m:u:")) != -1) { |
107 | #endif | | 106 | #endif |
108 | switch (ch) { | | 107 | switch (ch) { |
109 | | | 108 | |
110 | #ifdef KERN_DRIVERS | | 109 | #ifdef KERN_DRIVERS |
111 | case 'l': | | 110 | case 'l': |
112 | l_flag = 1; | | 111 | l_flag = 1; |
113 | break; | | 112 | break; |
114 | #endif | | 113 | #endif |
115 | | | 114 | |
116 | case 'r': | | 115 | case 'r': |
117 | r_flag = 1; | | 116 | r_flag = 1; |
118 | break; | | 117 | break; |
119 | | | 118 | |
120 | case 'R': | | 119 | case 'R': |
121 | r_flag = 2; | | 120 | r_flag = 2; |
122 | break; | | 121 | break; |
123 | | | 122 | |
124 | case 'F': | | 123 | case 'F': |
125 | pack = pack_find(optarg); | | 124 | pack = pack_find(optarg); |
126 | if (pack == NULL) | | 125 | if (pack == NULL) |
127 | errx(1, "invalid format: %s", optarg); | | 126 | errx(1, "invalid format: %s", optarg); |
128 | hasformat++; | | 127 | hasformat++; |
129 | break; | | 128 | break; |
130 | | | 129 | |
131 | case 'g': | | 130 | case 'g': |
132 | if (optarg[0] == '#') { | | 131 | if (optarg[0] == '#') { |
133 | gid = strtol(optarg + 1, &p, 10); | | 132 | gid = strtol(optarg + 1, &p, 10); |
134 | if (*p == 0) | | 133 | if (*p == 0) |
135 | break; | | 134 | break; |
136 | } | | 135 | } |
137 | if (gid_name(optarg, &gid) == 0) | | 136 | if (gid_name(optarg, &gid) == 0) |
138 | break; | | 137 | break; |
139 | gid = strtol(optarg, &p, 10); | | 138 | gid = strtol(optarg, &p, 10); |
140 | if (*p == 0) | | 139 | if (*p == 0) |
141 | break; | | 140 | break; |
142 | errx(1, "%s: invalid group name", optarg); | | 141 | errx(1, "%s: invalid group name", optarg); |
143 | | | 142 | |
144 | case 'm': | | 143 | case 'm': |
145 | modes = setmode(optarg); | | 144 | modes = setmode(optarg); |
146 | if (modes == NULL) | | 145 | if (modes == NULL) |
147 | err(1, "Cannot set file mode `%s'", optarg); | | 146 | err(1, "Cannot set file mode `%s'", optarg); |
148 | break; | | 147 | break; |
149 | | | 148 | |
150 | case 'u': | | 149 | case 'u': |
151 | if (optarg[0] == '#') { | | 150 | if (optarg[0] == '#') { |
152 | uid = strtol(optarg + 1, &p, 10); | | 151 | uid = strtol(optarg + 1, &p, 10); |
153 | if (*p == 0) | | 152 | if (*p == 0) |
154 | break; | | 153 | break; |
155 | } | | 154 | } |
156 | if (uid_from_user(optarg, &uid) == 0) | | 155 | if (uid_from_user(optarg, &uid) == 0) |
157 | break; | | 156 | break; |
158 | uid = strtol(optarg, &p, 10); | | 157 | uid = strtol(optarg, &p, 10); |
159 | if (*p == 0) | | 158 | if (*p == 0) |
160 | break; | | 159 | break; |
161 | errx(1, "%s: invalid user name", optarg); | | 160 | errx(1, "%s: invalid user name", optarg); |
162 | | | 161 | |
163 | default: | | 162 | default: |
164 | case '?': | | 163 | case '?': |
165 | usage(); | | 164 | usage(); |
166 | } | | 165 | } |
167 | } | | 166 | } |
168 | argc -= optind; | | 167 | argc -= optind; |
169 | argv += optind; | | 168 | argv += optind; |
170 | | | 169 | |
171 | #ifdef KERN_DRIVERS | | 170 | #ifdef KERN_DRIVERS |
172 | if (l_flag) { | | 171 | if (l_flag) { |
173 | print_device_info(argv); | | 172 | print_device_info(argv); |
174 | return 0; | | 173 | return 0; |
175 | } | | 174 | } |
176 | #endif | | 175 | #endif |
177 | | | 176 | |
178 | if (argc < 2 || argc > 10) | | 177 | if (argc < 2 || argc > 10) |
179 | usage(); | | 178 | usage(); |
180 | | | 179 | |
181 | name = *argv; | | 180 | name = *argv; |
182 | argc--; | | 181 | argc--; |
183 | argv++; | | 182 | argv++; |
184 | | | 183 | |
185 | umask(mode = umask(0)); | | 184 | umask(mode = umask(0)); |
186 | mode = (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mode; | | 185 | mode = (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mode; |
187 | | | 186 | |
188 | if (argv[0][1] != '\0') | | 187 | if (argv[0][1] != '\0') |
189 | goto badtype; | | 188 | goto badtype; |
190 | switch (*argv[0]) { | | 189 | switch (*argv[0]) { |
191 | case 'c': | | 190 | case 'c': |
192 | mode |= S_IFCHR; | | 191 | mode |= S_IFCHR; |
193 | break; | | 192 | break; |
194 | | | 193 | |
195 | case 'b': | | 194 | case 'b': |
196 | mode |= S_IFBLK; | | 195 | mode |= S_IFBLK; |
197 | break; | | 196 | break; |
198 | | | 197 | |
199 | case 'p': | | 198 | case 'p': |
200 | if (hasformat) | | 199 | if (hasformat) |
201 | errx(1, "format is meaningless for fifos"); | | 200 | errx(1, "format is meaningless for fifos"); |
202 | mode |= S_IFIFO; | | 201 | mode |= S_IFIFO; |
203 | fifo = 1; | | 202 | fifo = 1; |
204 | break; | | 203 | break; |
205 | | | 204 | |
206 | default: | | 205 | default: |
207 | badtype: | | 206 | badtype: |
208 | errx(1, "node type must be 'b', 'c' or 'p'."); | | 207 | errx(1, "node type must be 'b', 'c' or 'p'."); |
209 | } | | 208 | } |
210 | argc--; | | 209 | argc--; |
211 | argv++; | | 210 | argv++; |
212 | | | 211 | |
213 | if (fifo) { | | 212 | if (fifo) { |
214 | if (argc != 0) | | 213 | if (argc != 0) |
215 | usage(); | | 214 | usage(); |
216 | } else { | | 215 | } else { |
217 | if (argc < 1 || argc > MAXARGS) | | 216 | if (argc < 1 || argc > MAXARGS) |
218 | usage(); | | 217 | usage(); |
219 | } | | 218 | } |
220 | | | 219 | |
221 | for (n = 0; n < argc; n++) { | | 220 | for (n = 0; n < argc; n++) { |
222 | errno = 0; | | 221 | errno = 0; |
223 | numbers[n] = strtoul(argv[n], &p, 0); | | 222 | numbers[n] = strtoul(argv[n], &p, 0); |
224 | if (*p == 0 && errno == 0) | | 223 | if (*p == 0 && errno == 0) |
225 | continue; | | 224 | continue; |
226 | #ifdef KERN_DRIVERS | | 225 | #ifdef KERN_DRIVERS |
227 | if (n == 0) { | | 226 | if (n == 0) { |
228 | major = major_from_name(argv[0], mode); | | 227 | major = major_from_name(argv[0], mode); |
229 | if (major != -1) { | | 228 | if (major != -1) { |
230 | numbers[0] = major; | | 229 | numbers[0] = major; |
231 | continue; | | 230 | continue; |
232 | } | | 231 | } |
233 | if (!isdigit(*(unsigned char *)argv[0])) | | 232 | if (!isdigit(*(unsigned char *)argv[0])) |
234 | errx(1, "unknown driver: %s", argv[0]); | | 233 | errx(1, "unknown driver: %s", argv[0]); |
235 | } | | 234 | } |
236 | #endif | | 235 | #endif |
237 | errx(1, "invalid number: %s", argv[n]); | | 236 | errx(1, "invalid number: %s", argv[n]); |
238 | } | | 237 | } |
239 | | | 238 | |
240 | switch (argc) { | | 239 | switch (argc) { |
241 | case 0: | | 240 | case 0: |
242 | dev = 0; | | 241 | dev = 0; |
243 | break; | | 242 | break; |
244 | | | 243 | |
245 | case 1: | | 244 | case 1: |
246 | dev = numbers[0]; | | 245 | dev = numbers[0]; |
247 | break; | | 246 | break; |
248 | | | 247 | |
249 | default: | | 248 | default: |
250 | dev = callPack(pack, argc, numbers); | | 249 | dev = callPack(pack, argc, numbers); |
251 | break; | | 250 | break; |
252 | } | | 251 | } |
253 | | | 252 | |
254 | if (modes != NULL) | | 253 | if (modes != NULL) |
255 | mode = getmode(modes, mode); | | 254 | mode = getmode(modes, mode); |
256 | umask(0); | | 255 | umask(0); |
257 | rval = fifo ? mkfifo(name, mode) : mknod(name, mode, dev); | | 256 | rval = fifo ? mkfifo(name, mode) : mknod(name, mode, dev); |
258 | if (rval < 0 && errno == EEXIST && r_flag) { | | 257 | if (rval < 0 && errno == EEXIST && r_flag) { |
259 | struct stat sb; | | 258 | struct stat sb; |
260 | if (lstat(name, &sb) != 0 || (!fifo && sb.st_rdev != dev)) | | 259 | if (lstat(name, &sb) != 0 || (!fifo && sb.st_rdev != dev)) |
261 | sb.st_mode = 0; | | 260 | sb.st_mode = 0; |
262 | | | 261 | |
263 | if ((sb.st_mode & S_IFMT) == (mode & S_IFMT)) { | | 262 | if ((sb.st_mode & S_IFMT) == (mode & S_IFMT)) { |
264 | if (r_flag == 1) | | 263 | if (r_flag == 1) |
265 | /* Ignore permissions and user/group */ | | 264 | /* Ignore permissions and user/group */ |
266 | return 0; | | 265 | return 0; |
267 | if (sb.st_mode != mode) | | 266 | if (sb.st_mode != mode) |
268 | rval = chmod(name, mode); | | 267 | rval = chmod(name, mode); |
269 | else | | 268 | else |
270 | rval = 0; | | 269 | rval = 0; |
271 | } else { | | 270 | } else { |
272 | unlink(name); | | 271 | unlink(name); |
273 | rval = fifo ? mkfifo(name, mode) | | 272 | rval = fifo ? mkfifo(name, mode) |
274 | : mknod(name, mode, dev); | | 273 | : mknod(name, mode, dev); |
275 | } | | 274 | } |
276 | } | | 275 | } |
277 | if (rval < 0) | | 276 | if (rval < 0) |
278 | err(1, "%s", name); | | 277 | err(1, "%s", name); |
279 | if ((uid != (uid_t)-1 || gid != (uid_t)-1) && chown(name, uid, gid) == -1) | | 278 | if ((uid != (uid_t)-1 || gid != (uid_t)-1) && chown(name, uid, gid) == -1) |
280 | /* XXX Should we unlink the files here? */ | | 279 | /* XXX Should we unlink the files here? */ |
281 | warn("%s: uid/gid not changed", name); | | 280 | warn("%s: uid/gid not changed", name); |
282 | | | 281 | |
283 | return 0; | | 282 | return 0; |
284 | } | | 283 | } |
285 | | | 284 | |
286 | static void | | 285 | static void |
287 | usage(void) | | 286 | usage(void) |
288 | { | | 287 | { |
289 | const char *progname = getprogname(); | | 288 | const char *progname = getprogname(); |
290 | | | 289 | |
291 | (void)fprintf(stderr, | | 290 | (void)fprintf(stderr, |
292 | "usage: %s [-rR] [-F format] [-m mode] [-u user] [-g group]\n", | | 291 | "usage: %s [-rR] [-F format] [-m mode] [-u user] [-g group]\n", |
293 | progname); | | 292 | progname); |
294 | (void)fprintf(stderr, | | 293 | (void)fprintf(stderr, |
295 | #ifdef KERN_DRIVERS | | 294 | #ifdef KERN_DRIVERS |
296 | " [ name [b | c] [major | driver] minor\n" | | 295 | " [ name [b | c] [major | driver] minor\n" |
297 | #else | | 296 | #else |
298 | " [ name [b | c] major minor\n" | | 297 | " [ name [b | c] major minor\n" |
299 | #endif | | 298 | #endif |
300 | " | name [b | c] major unit subunit\n" | | 299 | " | name [b | c] major unit subunit\n" |
301 | " | name [b | c] number\n" | | 300 | " | name [b | c] number\n" |
302 | " | name p ]\n"); | | 301 | " | name p ]\n"); |
303 | #ifdef KERN_DRIVERS | | 302 | #ifdef KERN_DRIVERS |
304 | (void)fprintf(stderr, " %s -l [driver] ...\n", progname); | | 303 | (void)fprintf(stderr, " %s -l [driver] ...\n", progname); |
305 | #endif | | 304 | #endif |
306 | exit(1); | | 305 | exit(1); |
307 | } | | 306 | } |
308 | | | 307 | |
309 | static int | | 308 | static int |
310 | gid_name(const char *name, gid_t *gid) | | 309 | gid_name(const char *name, gid_t *gid) |
311 | { | | 310 | { |
312 | struct group *g; | | 311 | struct group *g; |
313 | | | 312 | |
314 | g = getgrnam(name); | | 313 | g = getgrnam(name); |
315 | if (!g) | | 314 | if (!g) |
316 | return -1; | | 315 | return -1; |
317 | *gid = g->gr_gid; | | 316 | *gid = g->gr_gid; |
318 | return 0; | | 317 | return 0; |
319 | } | | 318 | } |
320 | | | 319 | |
321 | static portdev_t | | 320 | static portdev_t |
322 | callPack(pack_t *f, int n, u_long *numbers) | | 321 | callPack(pack_t *f, int n, u_long *numbers) |
323 | { | | 322 | { |
324 | portdev_t d; | | 323 | portdev_t d; |
325 | const char *error = NULL; | | 324 | const char *error = NULL; |
326 | | | 325 | |
327 | d = (*f)(n, numbers, &error); | | 326 | d = (*f)(n, numbers, &error); |
328 | if (error != NULL) | | 327 | if (error != NULL) |
329 | errx(1, "%s", error); | | 328 | errx(1, "%s", error); |
330 | return d; | | 329 | return d; |
331 | } | | 330 | } |
332 | | | 331 | |
333 | #ifdef KERN_DRIVERS | | 332 | #ifdef KERN_DRIVERS |
334 | static void | | 333 | static void |
335 | get_device_info(void) | | 334 | get_device_info(void) |
336 | { | | 335 | { |
337 | static int mib[2] = {CTL_KERN, KERN_DRIVERS}; | | 336 | static int mib[2] = {CTL_KERN, KERN_DRIVERS}; |
338 | size_t len; | | 337 | size_t len; |
339 | | | 338 | |
340 | if (sysctl(mib, 2, NULL, &len, NULL, 0) != 0) | | 339 | if (sysctl(mib, 2, NULL, &len, NULL, 0) != 0) |
341 | err(1, "kern.drivers" ); | | 340 | err(1, "kern.drivers" ); |
342 | kern_drivers = malloc(len); | | 341 | kern_drivers = malloc(len); |
343 | if (kern_drivers == NULL) | | 342 | if (kern_drivers == NULL) |
344 | err(1, "malloc"); | | 343 | err(1, "malloc"); |
345 | if (sysctl(mib, 2, kern_drivers, &len, NULL, 0) != 0) | | 344 | if (sysctl(mib, 2, kern_drivers, &len, NULL, 0) != 0) |
346 | err(1, "kern.drivers" ); | | 345 | err(1, "kern.drivers" ); |
347 | | | 346 | |
348 | num_drivers = len / sizeof *kern_drivers; | | 347 | num_drivers = len / sizeof *kern_drivers; |
349 | } | | 348 | } |
350 | | | 349 | |
351 | static void | | 350 | static void |
352 | print_device_info(char **names) | | 351 | print_device_info(char **names) |
353 | { | | 352 | { |
354 | int i; | | 353 | int i; |
355 | struct kinfo_drivers *kd; | | 354 | struct kinfo_drivers *kd; |
356 | | | 355 | |
357 | if (kern_drivers == NULL) | | 356 | if (kern_drivers == NULL) |
358 | get_device_info(); | | 357 | get_device_info(); |
359 | | | 358 | |
360 | do { | | 359 | do { |
361 | kd = kern_drivers; | | 360 | kd = kern_drivers; |
362 | for (i = 0; i < num_drivers; kd++, i++) { | | 361 | for (i = 0; i < num_drivers; kd++, i++) { |
363 | if (*names && strcmp(*names, kd->d_name)) | | 362 | if (*names && strcmp(*names, kd->d_name)) |
364 | continue; | | 363 | continue; |
365 | printf("%s", kd->d_name); | | 364 | printf("%s", kd->d_name); |
366 | if (kd->d_cmajor != -1) | | 365 | if (kd->d_cmajor != -1) |
367 | printf(" character major %d", kd->d_cmajor); | | 366 | printf(" character major %d", kd->d_cmajor); |
368 | if (kd->d_bmajor != -1) | | 367 | if (kd->d_bmajor != -1) |
369 | printf(" block major %d", kd->d_bmajor); | | 368 | printf(" block major %d", kd->d_bmajor); |
370 | printf("\n"); | | 369 | printf("\n"); |
371 | } | | 370 | } |
372 | } while (*names && *++names); | | 371 | } while (*names && *++names); |
373 | } | | 372 | } |
374 | | | 373 | |
375 | static int | | 374 | static int |
376 | major_from_name(const char *name, mode_t mode) | | 375 | major_from_name(const char *name, mode_t mode) |
377 | { | | 376 | { |
378 | int i; | | 377 | int i; |
379 | struct kinfo_drivers *kd; | | 378 | struct kinfo_drivers *kd; |
380 | | | 379 | |
381 | if (kern_drivers == NULL) | | 380 | if (kern_drivers == NULL) |
382 | get_device_info(); | | 381 | get_device_info(); |
383 | | | 382 | |
384 | kd = kern_drivers; | | 383 | kd = kern_drivers; |
385 | for (i = 0; i < num_drivers; kd++, i++) { | | 384 | for (i = 0; i < num_drivers; kd++, i++) { |
386 | if (strcmp(name, kd->d_name)) | | 385 | if (strcmp(name, kd->d_name)) |
387 | continue; | | 386 | continue; |
388 | if (S_ISCHR(mode)) | | 387 | if (S_ISCHR(mode)) |
389 | return kd->d_cmajor; | | 388 | return kd->d_cmajor; |
390 | return kd->d_bmajor; | | 389 | return kd->d_bmajor; |
391 | } | | 390 | } |
392 | return -1; | | 391 | return -1; |
393 | } | | 392 | } |
394 | #endif | | 393 | #endif |