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