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