Home | History | Annotate | Line # | Download | only in mknod
mknod.c revision 1.32
      1  1.32       dsl /*	$NetBSD: mknod.c,v 1.32 2004/06/17 21:09:01 dsl 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.32       dsl __RCSID("$NetBSD: mknod.c,v 1.32 2004/06/17 21:09:01 dsl 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.32       dsl #include <sys/sysctl.h>
     53  1.11   mycroft 
     54  1.11   mycroft #include <err.h>
     55  1.11   mycroft #include <errno.h>
     56  1.11   mycroft #include <limits.h>
     57   1.1       cgd #include <stdio.h>
     58   1.6       cgd #include <stdlib.h>
     59   1.6       cgd #include <unistd.h>
     60  1.26       dsl #include <pwd.h>
     61  1.26       dsl #include <grp.h>
     62  1.19      matt #include <string.h>
     63  1.26       dsl #include <ctype.h>
     64   1.8       jtc 
     65  1.24     lukem #include "pack_dev.h"
     66  1.11   mycroft 
     67  1.26       dsl static int gid_name(const char *, gid_t *);
     68  1.30      ross static portdev_t callPack(pack_t *, int, u_long *);
     69  1.26       dsl 
     70  1.22     lukem 	int	main(int, char *[]);
     71  1.22     lukem static	void	usage(void);
     72  1.11   mycroft 
     73  1.32       dsl #ifdef KERN_DRIVERS
     74  1.32       dsl static struct kinfo_drivers *kern_drivers;
     75  1.32       dsl static int num_drivers;
     76  1.32       dsl 
     77  1.32       dsl static void get_device_info(void);
     78  1.32       dsl static void print_device_info(char **);
     79  1.32       dsl static int major_from_name(const char *, mode_t);
     80  1.32       dsl #endif
     81  1.32       dsl 
     82  1.23     lukem #define	MAXARGS	3		/* 3 for bsdos, 2 for rest */
     83  1.11   mycroft 
     84  1.22     lukem int
     85  1.22     lukem main(int argc, char **argv)
     86  1.14   mycroft {
     87  1.22     lukem 	char	*name, *p;
     88  1.22     lukem 	mode_t	 mode;
     89  1.27  christos 	portdev_t	 dev;
     90  1.11   mycroft 	pack_t	*pack;
     91  1.22     lukem 	u_long	 numbers[MAXARGS];
     92  1.22     lukem 	int	 n, ch, fifo, hasformat;
     93  1.26       dsl 	int	 r_flag = 0;		/* force: delete existing entry */
     94  1.32       dsl #ifdef KERN_DRIVERS
     95  1.32       dsl 	int	 l_flag = 0;		/* list device names and numbers */
     96  1.32       dsl 	int	 major;
     97  1.32       dsl #endif
     98  1.26       dsl 	void	*modes = 0;
     99  1.26       dsl 	uid_t	 uid = -1;
    100  1.26       dsl 	gid_t	 gid = -1;
    101  1.26       dsl 	int	 rval;
    102   1.1       cgd 
    103  1.22     lukem 	dev = 0;
    104  1.22     lukem 	fifo = hasformat = 0;
    105  1.11   mycroft 	pack = pack_native;
    106  1.11   mycroft 
    107  1.32       dsl #ifdef KERN_DRIVERS
    108  1.32       dsl 	while ((ch = getopt(argc, argv, "lrRF:g:m:u:")) != -1) {
    109  1.32       dsl #else
    110  1.26       dsl 	while ((ch = getopt(argc, argv, "rRF:g:m:u:")) != -1) {
    111  1.32       dsl #endif
    112  1.11   mycroft 		switch (ch) {
    113  1.26       dsl 
    114  1.32       dsl #ifdef KERN_DRIVERS
    115  1.32       dsl 		case 'l':
    116  1.32       dsl 			l_flag = 1;
    117  1.32       dsl 			break;
    118  1.32       dsl #endif
    119  1.32       dsl 
    120  1.26       dsl 		case 'r':
    121  1.26       dsl 			r_flag = 1;
    122  1.26       dsl 			break;
    123  1.26       dsl 
    124  1.26       dsl 		case 'R':
    125  1.26       dsl 			r_flag = 2;
    126  1.26       dsl 			break;
    127  1.26       dsl 
    128  1.11   mycroft 		case 'F':
    129  1.24     lukem 			pack = pack_find(optarg);
    130  1.22     lukem 			if (pack == NULL)
    131  1.11   mycroft 				errx(1, "invalid format: %s", optarg);
    132  1.22     lukem 			hasformat++;
    133  1.11   mycroft 			break;
    134  1.11   mycroft 
    135  1.26       dsl 		case 'g':
    136  1.26       dsl 			if (optarg[0] == '#') {
    137  1.26       dsl 				gid = strtol(optarg + 1, &p, 10);
    138  1.26       dsl 				if (*p == 0)
    139  1.26       dsl 					break;
    140  1.26       dsl 			}
    141  1.26       dsl 			if (gid_name(optarg, &gid) == 0)
    142  1.26       dsl 				break;
    143  1.26       dsl 			gid = strtol(optarg, &p, 10);
    144  1.26       dsl 			if (*p == 0)
    145  1.26       dsl 				break;
    146  1.26       dsl 			errx(1, "%s: invalid group name", optarg);
    147  1.26       dsl 
    148  1.26       dsl 		case 'm':
    149  1.26       dsl 			modes = setmode(optarg);
    150  1.26       dsl 			if (modes == NULL)
    151  1.26       dsl 				errx(1, "invalid mode: %s", optarg);
    152  1.26       dsl 			break;
    153  1.26       dsl 
    154  1.26       dsl 		case 'u':
    155  1.26       dsl 			if (optarg[0] == '#') {
    156  1.26       dsl 				uid = strtol(optarg + 1, &p, 10);
    157  1.26       dsl 				if (*p == 0)
    158  1.26       dsl 					break;
    159  1.26       dsl 			}
    160  1.26       dsl 			if (uid_from_user(optarg, &uid) == 0)
    161  1.26       dsl 				break;
    162  1.26       dsl 			uid = strtol(optarg, &p, 10);
    163  1.26       dsl 			if (*p == 0)
    164  1.26       dsl 				break;
    165  1.26       dsl 			errx(1, "%s: invalid user name", optarg);
    166  1.26       dsl 
    167  1.11   mycroft 		default:
    168  1.11   mycroft 		case '?':
    169  1.11   mycroft 			usage();
    170  1.11   mycroft 		}
    171  1.11   mycroft 	}
    172  1.11   mycroft 	argc -= optind;
    173  1.11   mycroft 	argv += optind;
    174  1.11   mycroft 
    175  1.32       dsl #ifdef KERN_DRIVERS
    176  1.32       dsl 	if (l_flag) {
    177  1.32       dsl 		print_device_info(argv);
    178  1.32       dsl 		return 0;
    179  1.32       dsl 	}
    180  1.32       dsl #endif
    181  1.32       dsl 
    182  1.18  christos 	if (argc < 2 || argc > 10)
    183   1.8       jtc 		usage();
    184   1.1       cgd 
    185  1.14   mycroft 	name = *argv;
    186  1.14   mycroft 	argc--;
    187  1.14   mycroft 	argv++;
    188  1.14   mycroft 
    189  1.26       dsl 	umask(mode = umask(0));
    190  1.26       dsl 	mode = (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mode;
    191  1.26       dsl 
    192  1.22     lukem 	if (argv[0][1] != '\0')
    193  1.22     lukem 		goto badtype;
    194  1.18  christos 	switch (*argv[0]) {
    195  1.18  christos 	case 'c':
    196   1.1       cgd 		mode |= S_IFCHR;
    197  1.18  christos 		break;
    198  1.18  christos 
    199  1.18  christos 	case 'b':
    200   1.1       cgd 		mode |= S_IFBLK;
    201  1.18  christos 		break;
    202  1.18  christos 
    203  1.18  christos 	case 'p':
    204  1.22     lukem 		if (hasformat)
    205  1.18  christos 			errx(1, "format is meaningless for fifos");
    206  1.26       dsl 		mode |= S_IFIFO;
    207  1.18  christos 		fifo = 1;
    208  1.18  christos 		break;
    209  1.18  christos 
    210  1.18  christos 	default:
    211  1.22     lukem  badtype:
    212  1.18  christos 		errx(1, "node type must be 'b', 'c' or 'p'.");
    213  1.18  christos 	}
    214  1.14   mycroft 	argc--;
    215  1.14   mycroft 	argv++;
    216  1.14   mycroft 
    217  1.22     lukem 	if (fifo) {
    218  1.22     lukem 		if (argc != 0)
    219  1.22     lukem 			usage();
    220  1.22     lukem 	} else {
    221  1.23     lukem 		if (argc < 1 || argc > MAXARGS)
    222  1.22     lukem 			usage();
    223  1.22     lukem 	}
    224  1.18  christos 
    225  1.14   mycroft 	for (n = 0; n < argc; n++) {
    226  1.25     lukem 		errno = 0;
    227  1.14   mycroft 		numbers[n] = strtoul(argv[n], &p, 0);
    228  1.26       dsl 		if (*p == 0 && errno == 0)
    229  1.26       dsl 			continue;
    230  1.32       dsl #ifdef KERN_DRIVERS
    231  1.32       dsl 		if (n == 0) {
    232  1.32       dsl 			major = major_from_name(argv[0], mode);
    233  1.32       dsl 			if (major != -1) {
    234  1.32       dsl 				numbers[0] = major;
    235  1.32       dsl 				continue;
    236  1.32       dsl 			}
    237  1.32       dsl 			if (!isdigit(*(unsigned char *)argv[0]))
    238  1.32       dsl 				errx(1, "unknown driver: %s", argv[0]);
    239  1.32       dsl 		}
    240  1.32       dsl #endif
    241  1.26       dsl 		errx(1, "invalid number: %s", argv[n]);
    242  1.14   mycroft 	}
    243  1.11   mycroft 
    244  1.18  christos 	switch (argc) {
    245  1.18  christos 	case 0:
    246  1.18  christos 		dev = 0;
    247  1.18  christos 		break;
    248  1.18  christos 
    249  1.18  christos 	case 1:
    250  1.14   mycroft 		dev = numbers[0];
    251  1.18  christos 		break;
    252  1.18  christos 
    253  1.18  christos 	default:
    254  1.30      ross 		dev = callPack(pack, argc, numbers);
    255  1.18  christos 		break;
    256  1.18  christos 	}
    257   1.1       cgd 
    258  1.26       dsl 	if (modes != NULL)
    259  1.26       dsl 		mode = getmode(modes, mode);
    260  1.26       dsl 	umask(0);
    261  1.26       dsl 	rval = fifo ? mkfifo(name, mode) : mknod(name, mode, dev);
    262  1.26       dsl 	if (rval < 0 && errno == EEXIST && r_flag) {
    263  1.26       dsl 		struct stat sb;
    264  1.26       dsl 		if (lstat(name, &sb) != 0 || (!fifo && sb.st_rdev != dev))
    265  1.26       dsl 			sb.st_mode = 0;
    266  1.26       dsl 
    267  1.26       dsl 		if ((sb.st_mode & S_IFMT) == (mode & S_IFMT)) {
    268  1.26       dsl 			if (r_flag == 1)
    269  1.26       dsl 				/* Ignore permissions and user/group */
    270  1.26       dsl 				return 0;
    271  1.26       dsl 			if (sb.st_mode != mode)
    272  1.26       dsl 				rval = chmod(name, mode);
    273  1.26       dsl 			else
    274  1.26       dsl 				rval = 0;
    275  1.26       dsl 		} else {
    276  1.26       dsl 			unlink(name);
    277  1.26       dsl 			rval = fifo ? mkfifo(name, mode)
    278  1.26       dsl 				    : mknod(name, mode, dev);
    279  1.26       dsl 		}
    280  1.26       dsl 	}
    281  1.26       dsl 	if (rval < 0)
    282  1.17      tron 		err(1, "%s", name);
    283  1.26       dsl 	if ((uid != -1 || gid != -1) && chown(name, uid, gid) == -1)
    284  1.26       dsl 		/* XXX Should we unlink the files here? */
    285  1.26       dsl 		warn("%s: uid/gid not changed", name);
    286   1.8       jtc 
    287  1.26       dsl 	return 0;
    288   1.8       jtc }
    289   1.8       jtc 
    290  1.18  christos static void
    291  1.22     lukem usage(void)
    292   1.8       jtc {
    293  1.21       cgd 	const char *progname = getprogname();
    294  1.21       cgd 
    295  1.18  christos 	(void)fprintf(stderr,
    296  1.29      jmmv 	    "usage: %s [-rR] [-F format] [-m mode] [-u user] [-g group]\n",
    297  1.26       dsl 	    progname);
    298  1.18  christos 	(void)fprintf(stderr,
    299  1.32       dsl #ifdef KERN_DRIVERS
    300  1.32       dsl 	    "                   [ name [b | c] [major | driver] minor\n"
    301  1.32       dsl #else
    302  1.26       dsl 	    "                   [ name [b | c] major minor\n"
    303  1.32       dsl #endif
    304  1.26       dsl 	    "                   | name [b | c] major unit subunit\n"
    305  1.26       dsl 	    "                   | name [b | c] number\n"
    306  1.26       dsl 	    "                   | name p ]\n");
    307  1.32       dsl #ifdef KERN_DRIVERS
    308  1.32       dsl 	(void)fprintf(stderr, "       %s -l [driver] ...\n", progname);
    309  1.32       dsl #endif
    310   1.8       jtc 	exit(1);
    311  1.26       dsl }
    312  1.26       dsl 
    313  1.26       dsl static int
    314  1.26       dsl gid_name(const char *name, gid_t *gid)
    315  1.26       dsl {
    316  1.26       dsl 	struct group *g;
    317  1.26       dsl 
    318  1.26       dsl 	g = getgrnam(name);
    319  1.26       dsl 	if (!g)
    320  1.26       dsl 		return -1;
    321  1.26       dsl 	*gid = g->gr_gid;
    322  1.26       dsl 	return 0;
    323   1.1       cgd }
    324  1.30      ross 
    325  1.30      ross static portdev_t
    326  1.30      ross callPack(pack_t *f, int n, u_long *numbers)
    327  1.30      ross {
    328  1.30      ross 	portdev_t d;
    329  1.31  christos 	const char *error = NULL;
    330  1.30      ross 
    331  1.30      ross 	d = (*f)(n, numbers, &error);
    332  1.30      ross 	if (error != NULL)
    333  1.30      ross 		errx(1, "%s", error);
    334  1.30      ross 	return d;
    335  1.30      ross }
    336  1.32       dsl 
    337  1.32       dsl #ifdef KERN_DRIVERS
    338  1.32       dsl static void
    339  1.32       dsl get_device_info(void)
    340  1.32       dsl {
    341  1.32       dsl 	static int mib[2] = {CTL_KERN, KERN_DRIVERS};
    342  1.32       dsl 	size_t len;
    343  1.32       dsl 
    344  1.32       dsl 	if (sysctl(mib, 2, NULL, &len, NULL, 0) != 0)
    345  1.32       dsl 		err(1, "kern.drivers" );
    346  1.32       dsl 	kern_drivers = malloc(len);
    347  1.32       dsl 	if (kern_drivers == NULL)
    348  1.32       dsl 		err(1, "malloc");
    349  1.32       dsl 	if (sysctl(mib, 2, kern_drivers, &len, NULL, 0) != 0)
    350  1.32       dsl 		err(1, "kern.drivers" );
    351  1.32       dsl 
    352  1.32       dsl 	num_drivers = len / sizeof *kern_drivers;
    353  1.32       dsl }
    354  1.32       dsl 
    355  1.32       dsl static void
    356  1.32       dsl print_device_info(char **names)
    357  1.32       dsl {
    358  1.32       dsl 	int i;
    359  1.32       dsl 	struct kinfo_drivers *kd;
    360  1.32       dsl 
    361  1.32       dsl 	if (kern_drivers == NULL)
    362  1.32       dsl 		get_device_info();
    363  1.32       dsl 
    364  1.32       dsl 	do {
    365  1.32       dsl 		kd = kern_drivers;
    366  1.32       dsl 		for (i = 0; i < num_drivers; kd++, i++) {
    367  1.32       dsl 			if (*names && strcmp(*names, kd->d_name))
    368  1.32       dsl 				continue;
    369  1.32       dsl 			printf("%s", kd->d_name);
    370  1.32       dsl 			if (kd->d_cmajor != -1)
    371  1.32       dsl 				printf(" character major %d", kd->d_cmajor);
    372  1.32       dsl 			if (kd->d_bmajor != -1)
    373  1.32       dsl 				printf(" block major %d", kd->d_bmajor);
    374  1.32       dsl 			printf("\n");
    375  1.32       dsl 		}
    376  1.32       dsl 	} while (*names && *++names);
    377  1.32       dsl }
    378  1.32       dsl 
    379  1.32       dsl static int
    380  1.32       dsl major_from_name(const char *name, mode_t mode)
    381  1.32       dsl {
    382  1.32       dsl 	int i;
    383  1.32       dsl 	struct kinfo_drivers *kd;
    384  1.32       dsl 
    385  1.32       dsl 	if (kern_drivers == NULL)
    386  1.32       dsl 		get_device_info();
    387  1.32       dsl 
    388  1.32       dsl 	kd = kern_drivers;
    389  1.32       dsl 	for (i = 0; i < num_drivers; kd++, i++) {
    390  1.32       dsl 		if (strcmp(name, kd->d_name))
    391  1.32       dsl 			continue;
    392  1.32       dsl 		if (mode & S_IFCHR)
    393  1.32       dsl 			return kd->d_cmajor;
    394  1.32       dsl 		return kd->d_bmajor;
    395  1.32       dsl 	}
    396  1.32       dsl 	return -1;
    397  1.32       dsl }
    398  1.32       dsl #endif
    399