mknod.c revision 1.28 1 1.28 lukem /* $NetBSD: mknod.c,v 1.28 2003/10/27 00:12:42 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 * 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.28 lukem __RCSID("$NetBSD: mknod.c,v 1.28 2003/10/27 00:12:42 lukem 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.11 mycroft
53 1.11 mycroft #include <err.h>
54 1.11 mycroft #include <errno.h>
55 1.11 mycroft #include <limits.h>
56 1.1 cgd #include <stdio.h>
57 1.6 cgd #include <stdlib.h>
58 1.6 cgd #include <unistd.h>
59 1.26 dsl #include <pwd.h>
60 1.26 dsl #include <grp.h>
61 1.19 matt #include <string.h>
62 1.26 dsl #include <ctype.h>
63 1.8 jtc
64 1.24 lukem #include "pack_dev.h"
65 1.11 mycroft
66 1.26 dsl static int gid_name(const char *, gid_t *);
67 1.26 dsl
68 1.22 lukem int main(int, char *[]);
69 1.22 lukem static void usage(void);
70 1.11 mycroft
71 1.23 lukem #define MAXARGS 3 /* 3 for bsdos, 2 for rest */
72 1.11 mycroft
73 1.22 lukem int
74 1.22 lukem main(int argc, char **argv)
75 1.14 mycroft {
76 1.22 lukem char *name, *p;
77 1.22 lukem mode_t mode;
78 1.27 christos portdev_t dev;
79 1.11 mycroft pack_t *pack;
80 1.22 lukem u_long numbers[MAXARGS];
81 1.22 lukem int n, ch, fifo, hasformat;
82 1.26 dsl int r_flag = 0; /* force: delete existing entry */
83 1.26 dsl void *modes = 0;
84 1.26 dsl uid_t uid = -1;
85 1.26 dsl gid_t gid = -1;
86 1.26 dsl int rval;
87 1.1 cgd
88 1.22 lukem dev = 0;
89 1.22 lukem fifo = hasformat = 0;
90 1.11 mycroft pack = pack_native;
91 1.11 mycroft
92 1.26 dsl while ((ch = getopt(argc, argv, "rRF:g:m:u:")) != -1) {
93 1.11 mycroft switch (ch) {
94 1.26 dsl
95 1.26 dsl case 'r':
96 1.26 dsl r_flag = 1;
97 1.26 dsl break;
98 1.26 dsl
99 1.26 dsl case 'R':
100 1.26 dsl r_flag = 2;
101 1.26 dsl break;
102 1.26 dsl
103 1.11 mycroft case 'F':
104 1.24 lukem pack = pack_find(optarg);
105 1.22 lukem if (pack == NULL)
106 1.11 mycroft errx(1, "invalid format: %s", optarg);
107 1.22 lukem hasformat++;
108 1.11 mycroft break;
109 1.11 mycroft
110 1.26 dsl case 'g':
111 1.26 dsl if (optarg[0] == '#') {
112 1.26 dsl gid = strtol(optarg + 1, &p, 10);
113 1.26 dsl if (*p == 0)
114 1.26 dsl break;
115 1.26 dsl }
116 1.26 dsl if (gid_name(optarg, &gid) == 0)
117 1.26 dsl break;
118 1.26 dsl gid = strtol(optarg, &p, 10);
119 1.26 dsl if (*p == 0)
120 1.26 dsl break;
121 1.26 dsl errx(1, "%s: invalid group name", optarg);
122 1.26 dsl
123 1.26 dsl case 'm':
124 1.26 dsl modes = setmode(optarg);
125 1.26 dsl if (modes == NULL)
126 1.26 dsl errx(1, "invalid mode: %s", optarg);
127 1.26 dsl break;
128 1.26 dsl
129 1.26 dsl case 'u':
130 1.26 dsl if (optarg[0] == '#') {
131 1.26 dsl uid = strtol(optarg + 1, &p, 10);
132 1.26 dsl if (*p == 0)
133 1.26 dsl break;
134 1.26 dsl }
135 1.26 dsl if (uid_from_user(optarg, &uid) == 0)
136 1.26 dsl break;
137 1.26 dsl uid = strtol(optarg, &p, 10);
138 1.26 dsl if (*p == 0)
139 1.26 dsl break;
140 1.26 dsl errx(1, "%s: invalid user name", optarg);
141 1.26 dsl
142 1.11 mycroft default:
143 1.11 mycroft case '?':
144 1.11 mycroft usage();
145 1.11 mycroft }
146 1.11 mycroft }
147 1.11 mycroft argc -= optind;
148 1.11 mycroft argv += optind;
149 1.11 mycroft
150 1.18 christos if (argc < 2 || argc > 10)
151 1.8 jtc usage();
152 1.1 cgd
153 1.14 mycroft name = *argv;
154 1.14 mycroft argc--;
155 1.14 mycroft argv++;
156 1.14 mycroft
157 1.26 dsl umask(mode = umask(0));
158 1.26 dsl mode = (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mode;
159 1.26 dsl
160 1.22 lukem if (argv[0][1] != '\0')
161 1.22 lukem goto badtype;
162 1.18 christos switch (*argv[0]) {
163 1.18 christos case 'c':
164 1.1 cgd mode |= S_IFCHR;
165 1.18 christos break;
166 1.18 christos
167 1.18 christos case 'b':
168 1.1 cgd mode |= S_IFBLK;
169 1.18 christos break;
170 1.18 christos
171 1.18 christos case 'p':
172 1.22 lukem if (hasformat)
173 1.18 christos errx(1, "format is meaningless for fifos");
174 1.26 dsl mode |= S_IFIFO;
175 1.18 christos fifo = 1;
176 1.18 christos break;
177 1.18 christos
178 1.18 christos default:
179 1.22 lukem badtype:
180 1.18 christos errx(1, "node type must be 'b', 'c' or 'p'.");
181 1.18 christos }
182 1.14 mycroft argc--;
183 1.14 mycroft argv++;
184 1.14 mycroft
185 1.22 lukem if (fifo) {
186 1.22 lukem if (argc != 0)
187 1.22 lukem usage();
188 1.22 lukem } else {
189 1.23 lukem if (argc < 1 || argc > MAXARGS)
190 1.22 lukem usage();
191 1.22 lukem }
192 1.18 christos
193 1.14 mycroft for (n = 0; n < argc; n++) {
194 1.25 lukem errno = 0;
195 1.14 mycroft numbers[n] = strtoul(argv[n], &p, 0);
196 1.26 dsl if (*p == 0 && errno == 0)
197 1.26 dsl continue;
198 1.26 dsl errx(1, "invalid number: %s", argv[n]);
199 1.14 mycroft }
200 1.11 mycroft
201 1.18 christos switch (argc) {
202 1.18 christos case 0:
203 1.18 christos dev = 0;
204 1.18 christos break;
205 1.18 christos
206 1.18 christos case 1:
207 1.14 mycroft dev = numbers[0];
208 1.18 christos break;
209 1.18 christos
210 1.18 christos default:
211 1.14 mycroft dev = (*pack)(argc, numbers);
212 1.18 christos break;
213 1.18 christos }
214 1.1 cgd
215 1.26 dsl if (modes != NULL)
216 1.26 dsl mode = getmode(modes, mode);
217 1.14 mycroft #if 0
218 1.26 dsl printf("name: %s\nmode: %05o\ndev: %08x\nuid: %5d\ngid: %5d\n",
219 1.26 dsl name, mode, dev, uid, gid);
220 1.26 dsl #endif
221 1.26 dsl umask(0);
222 1.26 dsl rval = fifo ? mkfifo(name, mode) : mknod(name, mode, dev);
223 1.26 dsl if (rval < 0 && errno == EEXIST && r_flag) {
224 1.26 dsl struct stat sb;
225 1.26 dsl if (lstat(name, &sb) != 0 || (!fifo && sb.st_rdev != dev))
226 1.26 dsl sb.st_mode = 0;
227 1.26 dsl
228 1.26 dsl if ((sb.st_mode & S_IFMT) == (mode & S_IFMT)) {
229 1.26 dsl if (r_flag == 1)
230 1.26 dsl /* Ignore permissions and user/group */
231 1.26 dsl return 0;
232 1.26 dsl if (sb.st_mode != mode)
233 1.26 dsl rval = chmod(name, mode);
234 1.26 dsl else
235 1.26 dsl rval = 0;
236 1.26 dsl } else {
237 1.26 dsl unlink(name);
238 1.26 dsl rval = fifo ? mkfifo(name, mode)
239 1.26 dsl : mknod(name, mode, dev);
240 1.26 dsl }
241 1.26 dsl }
242 1.26 dsl if (rval < 0)
243 1.17 tron err(1, "%s", name);
244 1.26 dsl if ((uid != -1 || gid != -1) && chown(name, uid, gid) == -1)
245 1.26 dsl /* XXX Should we unlink the files here? */
246 1.26 dsl warn("%s: uid/gid not changed", name);
247 1.8 jtc
248 1.26 dsl return 0;
249 1.8 jtc }
250 1.8 jtc
251 1.18 christos static void
252 1.22 lukem usage(void)
253 1.8 jtc {
254 1.21 cgd const char *progname = getprogname();
255 1.21 cgd
256 1.18 christos (void)fprintf(stderr,
257 1.26 dsl "Usage: %s [-rR] [-F format] [-m mode] [-u user] [-g group]\n",
258 1.26 dsl progname);
259 1.18 christos (void)fprintf(stderr,
260 1.26 dsl " [ name [b | c] major minor\n"
261 1.26 dsl " | name [b | c] major unit subunit\n"
262 1.26 dsl " | name [b | c] number\n"
263 1.26 dsl " | name p ]\n");
264 1.8 jtc exit(1);
265 1.26 dsl }
266 1.26 dsl
267 1.26 dsl static int
268 1.26 dsl gid_name(const char *name, gid_t *gid)
269 1.26 dsl {
270 1.26 dsl struct group *g;
271 1.26 dsl
272 1.26 dsl g = getgrnam(name);
273 1.26 dsl if (!g)
274 1.26 dsl return -1;
275 1.26 dsl *gid = g->gr_gid;
276 1.26 dsl return 0;
277 1.1 cgd }
278