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