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