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