Home | History | Annotate | Line # | Download | only in xinstall
xinstall.c revision 1.11
      1  1.11      fvdl /*	$NetBSD: xinstall.c,v 1.11 1997/03/05 23:48:47 fvdl Exp $	*/
      2   1.5       jtc 
      3   1.1       cgd /*
      4   1.5       jtc  * Copyright (c) 1987, 1993
      5   1.5       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36   1.1       cgd #ifndef lint
     37   1.5       jtc static char copyright[] =
     38   1.5       jtc "@(#) Copyright (c) 1987, 1993\n\
     39   1.5       jtc 	The Regents of the University of California.  All rights reserved.\n";
     40   1.1       cgd #endif /* not lint */
     41   1.1       cgd 
     42   1.1       cgd #ifndef lint
     43   1.5       jtc #if 0
     44   1.5       jtc static char sccsid[] = "@(#)xinstall.c	8.1 (Berkeley) 7/21/93";
     45   1.5       jtc #endif
     46  1.11      fvdl static char rcsid[] = "$NetBSD: xinstall.c,v 1.11 1997/03/05 23:48:47 fvdl Exp $";
     47   1.1       cgd #endif /* not lint */
     48   1.1       cgd 
     49   1.1       cgd #include <sys/param.h>
     50   1.5       jtc #include <sys/wait.h>
     51   1.5       jtc #include <sys/mman.h>
     52   1.1       cgd #include <sys/stat.h>
     53   1.5       jtc 
     54   1.5       jtc #include <ctype.h>
     55   1.5       jtc #include <errno.h>
     56   1.5       jtc #include <fcntl.h>
     57   1.1       cgd #include <grp.h>
     58   1.5       jtc #include <paths.h>
     59   1.1       cgd #include <pwd.h>
     60   1.1       cgd #include <stdio.h>
     61   1.5       jtc #include <stdlib.h>
     62   1.5       jtc #include <string.h>
     63   1.5       jtc #include <unistd.h>
     64   1.5       jtc #include <err.h>
     65   1.5       jtc 
     66   1.1       cgd #include "pathnames.h"
     67   1.1       cgd 
     68   1.5       jtc struct passwd *pp;
     69   1.5       jtc struct group *gp;
     70   1.5       jtc int docopy, dodir, dostrip;
     71   1.5       jtc int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
     72   1.8   ghudson char pathbuf[MAXPATHLEN];
     73   1.8   ghudson uid_t uid;
     74   1.8   ghudson gid_t gid;
     75   1.5       jtc 
     76   1.5       jtc #define	DIRECTORY	0x01		/* Tell install it's a directory. */
     77   1.5       jtc #define	SETFLAGS	0x02		/* Tell install to set flags. */
     78   1.5       jtc 
     79   1.5       jtc void	copy __P((int, char *, int, char *, off_t));
     80   1.5       jtc void	install __P((char *, char *, u_long, u_int));
     81   1.7       jtc void	install_dir __P((char *));
     82   1.5       jtc u_long	string_to_flags __P((char **, u_long *, u_long *));
     83   1.5       jtc void	strip __P((char *));
     84   1.5       jtc void	usage __P((void));
     85   1.1       cgd 
     86   1.5       jtc int
     87   1.1       cgd main(argc, argv)
     88   1.1       cgd 	int argc;
     89   1.5       jtc 	char *argv[];
     90   1.1       cgd {
     91   1.1       cgd 	struct stat from_sb, to_sb;
     92   1.5       jtc 	mode_t *set;
     93   1.5       jtc 	u_long fset;
     94   1.5       jtc 	u_int iflags;
     95   1.1       cgd 	int ch, no_target;
     96   1.8   ghudson 	char *flags, *to_name, *group = NULL, *owner = NULL;
     97   1.1       cgd 
     98   1.5       jtc 	iflags = 0;
     99   1.5       jtc 	while ((ch = getopt(argc, argv, "cf:g:m:o:sd")) != EOF)
    100   1.1       cgd 		switch((char)ch) {
    101   1.1       cgd 		case 'c':
    102   1.1       cgd 			docopy = 1;
    103   1.1       cgd 			break;
    104   1.5       jtc 		case 'f':
    105   1.5       jtc 			flags = optarg;
    106   1.5       jtc 			if (string_to_flags(&flags, &fset, NULL))
    107   1.5       jtc 				errx(1, "%s: invalid flag", flags);
    108   1.5       jtc 			iflags |= SETFLAGS;
    109   1.5       jtc 			break;
    110   1.1       cgd 		case 'g':
    111   1.1       cgd 			group = optarg;
    112   1.1       cgd 			break;
    113   1.1       cgd 		case 'm':
    114   1.5       jtc 			if (!(set = setmode(optarg)))
    115   1.5       jtc 				errx(1, "%s: invalid file mode", optarg);
    116   1.1       cgd 			mode = getmode(set, 0);
    117   1.1       cgd 			break;
    118   1.1       cgd 		case 'o':
    119   1.1       cgd 			owner = optarg;
    120   1.1       cgd 			break;
    121   1.1       cgd 		case 's':
    122   1.1       cgd 			dostrip = 1;
    123   1.1       cgd 			break;
    124   1.2       jtc 		case 'd':
    125   1.2       jtc 			dodir = 1;
    126   1.2       jtc 			break;
    127   1.1       cgd 		case '?':
    128   1.1       cgd 		default:
    129   1.1       cgd 			usage();
    130   1.1       cgd 		}
    131   1.1       cgd 	argc -= optind;
    132   1.1       cgd 	argv += optind;
    133   1.2       jtc 
    134   1.2       jtc 	/* copy and strip options make no sense when creating directories */
    135   1.2       jtc 	if ((docopy || dostrip) && dodir)
    136   1.2       jtc 		usage();
    137   1.2       jtc 
    138   1.2       jtc 	/* must have at least two arguments, except when creating directories */
    139   1.2       jtc 	if (argc < 2 && !dodir)
    140   1.1       cgd 		usage();
    141   1.1       cgd 
    142   1.1       cgd 	/* get group and owner id's */
    143   1.8   ghudson 	if (group && !(gp = getgrnam(group)) && !isdigit(*group))
    144   1.5       jtc 		errx(1, "unknown group %s", group);
    145   1.8   ghudson 	gid = (group) ? ((gp) ? gp->gr_gid : atoi(group)) : -1;
    146   1.8   ghudson 	if (owner && !(pp = getpwnam(owner)) && !isdigit(*owner))
    147   1.5       jtc 		errx(1, "unknown user %s", owner);
    148   1.8   ghudson 	uid = (owner) ? ((pp) ? pp->pw_uid : atoi(owner)) : -1;
    149   1.1       cgd 
    150   1.2       jtc 	if (dodir) {
    151   1.2       jtc 		for (; *argv != NULL; ++argv)
    152   1.5       jtc 			install_dir(*argv);
    153   1.2       jtc 		exit (0);
    154   1.2       jtc 		/* NOTREACHED */
    155   1.5       jtc 	}
    156   1.2       jtc 
    157   1.1       cgd 	no_target = stat(to_name = argv[argc - 1], &to_sb);
    158   1.2       jtc 	if (!no_target && S_ISDIR(to_sb.st_mode)) {
    159   1.1       cgd 		for (; *argv != to_name; ++argv)
    160   1.5       jtc 			install(*argv, to_name, fset, iflags | DIRECTORY);
    161   1.1       cgd 		exit(0);
    162   1.1       cgd 	}
    163   1.1       cgd 
    164   1.1       cgd 	/* can't do file1 file2 directory/file */
    165   1.1       cgd 	if (argc != 2)
    166   1.1       cgd 		usage();
    167   1.1       cgd 
    168   1.1       cgd 	if (!no_target) {
    169   1.5       jtc 		if (stat(*argv, &from_sb))
    170   1.5       jtc 			err(1, "%s", *argv);
    171   1.5       jtc 		if (!S_ISREG(to_sb.st_mode))
    172   1.5       jtc 			errx(1, "%s: %s", to_name, strerror(EFTYPE));
    173   1.5       jtc 		if (to_sb.st_dev == from_sb.st_dev &&
    174   1.5       jtc 		    to_sb.st_ino == from_sb.st_ino)
    175   1.5       jtc 			errx(1, "%s and %s are the same file", *argv, to_name);
    176   1.5       jtc 		/*
    177   1.5       jtc 		 * Unlink now... avoid ETXTBSY errors later.  Try and turn
    178   1.5       jtc 		 * off the append/immutable bits -- if we fail, go ahead,
    179   1.5       jtc 		 * it might work.
    180   1.5       jtc 		 */
    181   1.5       jtc #define	NOCHANGEBITS	(UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
    182   1.5       jtc 		if (to_sb.st_flags & NOCHANGEBITS)
    183   1.5       jtc 			(void)chflags(to_name,
    184   1.5       jtc 			    to_sb.st_flags & ~(NOCHANGEBITS));
    185   1.1       cgd 		(void)unlink(to_name);
    186   1.1       cgd 	}
    187   1.5       jtc 	install(*argv, to_name, fset, iflags);
    188   1.1       cgd 	exit(0);
    189   1.1       cgd }
    190   1.1       cgd 
    191   1.1       cgd /*
    192   1.1       cgd  * install --
    193   1.1       cgd  *	build a path name and install the file
    194   1.1       cgd  */
    195   1.5       jtc void
    196   1.5       jtc install(from_name, to_name, fset, flags)
    197   1.1       cgd 	char *from_name, *to_name;
    198   1.5       jtc 	u_long fset;
    199   1.5       jtc 	u_int flags;
    200   1.1       cgd {
    201   1.5       jtc 	struct stat from_sb, to_sb;
    202   1.5       jtc 	int devnull, from_fd, to_fd, serrno;
    203   1.5       jtc 	char *p;
    204   1.5       jtc 
    205   1.5       jtc 	/* If try to install NULL file to a directory, fails. */
    206   1.5       jtc 	if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
    207   1.5       jtc 		if (stat(from_name, &from_sb))
    208   1.5       jtc 			err(1, "%s", from_name);
    209   1.5       jtc 		if (!S_ISREG(from_sb.st_mode))
    210   1.5       jtc 			errx(1, "%s: %s", from_name, strerror(EFTYPE));
    211   1.5       jtc 		/* Build the target path. */
    212   1.5       jtc 		if (flags & DIRECTORY) {
    213   1.5       jtc 			(void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
    214   1.5       jtc 			    to_name,
    215   1.5       jtc 			    (p = rindex(from_name, '/')) ? ++p : from_name);
    216   1.1       cgd 			to_name = pathbuf;
    217   1.1       cgd 		}
    218   1.1       cgd 		devnull = 0;
    219   1.5       jtc 	} else {
    220   1.5       jtc 		from_sb.st_flags = 0;	/* XXX */
    221   1.1       cgd 		devnull = 1;
    222   1.5       jtc 	}
    223   1.1       cgd 
    224   1.5       jtc 	/*
    225   1.5       jtc 	 * Unlink now... avoid ETXTBSY errors later.  Try and turn
    226   1.5       jtc 	 * off the append/immutable bits -- if we fail, go ahead,
    227   1.5       jtc 	 * it might work.
    228   1.5       jtc 	 */
    229   1.5       jtc 	if (stat(to_name, &to_sb) == 0 &&
    230   1.5       jtc 	    to_sb.st_flags & (NOCHANGEBITS))
    231   1.5       jtc 		(void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
    232   1.1       cgd 	(void)unlink(to_name);
    233   1.1       cgd 
    234   1.5       jtc 	/* Create target. */
    235   1.5       jtc 	if ((to_fd = open(to_name,
    236   1.5       jtc 	    O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
    237   1.5       jtc 		err(1, "%s", to_name);
    238   1.1       cgd 	if (!devnull) {
    239   1.1       cgd 		if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
    240   1.1       cgd 			(void)unlink(to_name);
    241   1.5       jtc 			err(1, "%s", from_name);
    242   1.1       cgd 		}
    243   1.5       jtc 		copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
    244   1.1       cgd 		(void)close(from_fd);
    245   1.1       cgd 	}
    246   1.9  jonathan 
    247   1.9  jonathan 	if (dostrip) {
    248   1.1       cgd 		strip(to_name);
    249   1.9  jonathan 
    250   1.9  jonathan 		/*
    251   1.9  jonathan 		 * Re-open our fd on the target, in case we used a strip
    252   1.9  jonathan 		 *  that does not work in-place -- like gnu binutils strip.
    253   1.9  jonathan 		 */
    254   1.9  jonathan 		close(to_fd);
    255   1.9  jonathan 		if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
    256   1.9  jonathan 		  err(1, "stripping %s", to_name);
    257   1.9  jonathan 	}
    258   1.9  jonathan 
    259   1.1       cgd 	/*
    260   1.5       jtc 	 * Set owner, group, mode for target; do the chown first,
    261   1.1       cgd 	 * chown may lose the setuid bits.
    262   1.1       cgd 	 */
    263   1.8   ghudson 	if ((gid != -1 || uid != -1) && fchown(to_fd, uid, gid)) {
    264   1.5       jtc 		serrno = errno;
    265   1.5       jtc 		(void)unlink(to_name);
    266   1.5       jtc 		errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
    267   1.1       cgd 	}
    268   1.5       jtc 	if (fchmod(to_fd, mode)) {
    269   1.5       jtc 		serrno = errno;
    270   1.5       jtc 		(void)unlink(to_name);
    271   1.5       jtc 		errx(1, "%s: chmod: %s", to_name, strerror(serrno));
    272   1.5       jtc 	}
    273   1.5       jtc 
    274   1.5       jtc 	/*
    275   1.5       jtc 	 * If provided a set of flags, set them, otherwise, preserve the
    276   1.5       jtc 	 * flags, except for the dump flag.
    277   1.5       jtc 	 */
    278   1.5       jtc 	if (fchflags(to_fd,
    279   1.5       jtc 	    flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
    280  1.11      fvdl 		if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
    281  1.11      fvdl 			warnx(1, "%s: chflags: %s", to_name, strerror(errno));
    282   1.5       jtc 	}
    283   1.5       jtc 
    284   1.1       cgd 	(void)close(to_fd);
    285   1.5       jtc 	if (!docopy && !devnull && unlink(from_name))
    286   1.5       jtc 		err(1, "%s", from_name);
    287   1.1       cgd }
    288   1.1       cgd 
    289   1.1       cgd /*
    290   1.1       cgd  * copy --
    291   1.1       cgd  *	copy from one file to another
    292   1.1       cgd  */
    293   1.5       jtc void
    294   1.5       jtc copy(from_fd, from_name, to_fd, to_name, size)
    295   1.1       cgd 	register int from_fd, to_fd;
    296   1.1       cgd 	char *from_name, *to_name;
    297   1.5       jtc 	off_t size;
    298   1.1       cgd {
    299   1.5       jtc 	register int nr, nw;
    300   1.5       jtc 	int serrno;
    301  1.10   thorpej #ifndef XXX_BROKEN_GCC
    302   1.5       jtc 	char *p, buf[MAXBSIZE];
    303  1.10   thorpej #else
    304  1.10   thorpej 	char *p, *buf;
    305  1.10   thorpej 
    306  1.10   thorpej 	if ((buf = (char *)malloc(MAXBSIZE)) == NULL)
    307  1.10   thorpej 		err(1, "can't allocate memory for copy");
    308  1.10   thorpej #endif
    309   1.1       cgd 
    310   1.5       jtc 	/*
    311   1.5       jtc 	 * Mmap and write if less than 8M (the limit is so we don't totally
    312   1.5       jtc 	 * trash memory on big files.  This is really a minor hack, but it
    313   1.5       jtc 	 * wins some CPU back.
    314   1.5       jtc 	 */
    315   1.5       jtc 	if (size <= 8 * 1048576) {
    316   1.5       jtc 		if ((p = mmap(NULL, (size_t)size, PROT_READ,
    317   1.5       jtc 		    0, from_fd, (off_t)0)) == (char *)-1)
    318   1.5       jtc 			err(1, "%s", from_name);
    319   1.5       jtc 		if (write(to_fd, p, size) != size)
    320   1.5       jtc 			err(1, "%s", to_name);
    321   1.5       jtc 	} else {
    322   1.5       jtc 		while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
    323   1.5       jtc 			if ((nw = write(to_fd, buf, nr)) != nr) {
    324   1.5       jtc 				serrno = errno;
    325   1.5       jtc 				(void)unlink(to_name);
    326   1.5       jtc 				errx(1, "%s: %s",
    327   1.5       jtc 				    to_name, strerror(nw > 0 ? EIO : serrno));
    328   1.5       jtc 			}
    329   1.5       jtc 		if (nr != 0) {
    330   1.5       jtc 			serrno = errno;
    331   1.5       jtc 			(void)unlink(to_name);
    332   1.5       jtc 			errx(1, "%s: %s", from_name, strerror(serrno));
    333   1.1       cgd 		}
    334   1.1       cgd 	}
    335  1.10   thorpej #ifdef XXX_BROKEN_GCC
    336  1.10   thorpej 	free(buf);
    337  1.10   thorpej #endif
    338   1.1       cgd }
    339   1.1       cgd 
    340   1.1       cgd /*
    341   1.1       cgd  * strip --
    342   1.1       cgd  *	use strip(1) to strip the target file
    343   1.1       cgd  */
    344   1.5       jtc void
    345   1.1       cgd strip(to_name)
    346   1.1       cgd 	char *to_name;
    347   1.1       cgd {
    348   1.5       jtc 	int serrno, status;
    349   1.1       cgd 
    350   1.1       cgd 	switch (vfork()) {
    351   1.1       cgd 	case -1:
    352   1.5       jtc 		serrno = errno;
    353   1.5       jtc 		(void)unlink(to_name);
    354   1.5       jtc 		errx(1, "forks: %s", strerror(errno));
    355   1.1       cgd 	case 0:
    356   1.5       jtc 		execl(_PATH_STRIP, "strip", to_name, NULL);
    357   1.5       jtc 		err(1, "%s", _PATH_STRIP);
    358   1.1       cgd 	default:
    359   1.1       cgd 		if (wait(&status) == -1 || status)
    360   1.5       jtc 			(void)unlink(to_name);
    361   1.1       cgd 	}
    362   1.1       cgd }
    363   1.1       cgd 
    364   1.1       cgd /*
    365   1.5       jtc  * install_dir --
    366   1.2       jtc  *	build directory heirarchy
    367   1.2       jtc  */
    368   1.7       jtc void
    369   1.5       jtc install_dir(path)
    370   1.2       jtc         char *path;
    371   1.2       jtc {
    372   1.2       jtc         register char *p;
    373   1.2       jtc         struct stat sb;
    374   1.4       cgd         int ch;
    375   1.2       jtc 
    376   1.4       cgd         for (p = path;; ++p)
    377   1.4       cgd                 if (!*p || (p != path && *p  == '/')) {
    378   1.2       jtc                         ch = *p;
    379   1.2       jtc                         *p = '\0';
    380   1.2       jtc                         if (stat(path, &sb)) {
    381   1.2       jtc                                 if (errno != ENOENT || mkdir(path, 0777) < 0) {
    382   1.7       jtc 					err(1, "%s", path);
    383   1.7       jtc 					/* NOTREACHED */
    384   1.2       jtc                                 }
    385   1.2       jtc                         }
    386   1.2       jtc                         if (!(*p = ch))
    387   1.4       cgd 				break;
    388   1.2       jtc                 }
    389   1.2       jtc 
    390   1.8   ghudson 	if (((gid != -1 || uid != -1) && chown(path, uid, gid)) ||
    391   1.2       jtc             chmod(path, mode)) {
    392   1.5       jtc                 warn("%s", path);
    393   1.2       jtc         }
    394   1.2       jtc }
    395   1.2       jtc 
    396   1.2       jtc /*
    397   1.1       cgd  * usage --
    398   1.1       cgd  *	print a usage message and die
    399   1.1       cgd  */
    400   1.5       jtc void
    401   1.1       cgd usage()
    402   1.1       cgd {
    403   1.2       jtc 	(void)fprintf(stderr, "\
    404   1.5       jtc usage: install [-cs] [-f flags] [-g group] [-m mode] [-o owner] file1 file2\n\
    405   1.5       jtc        install [-cs] [-f flags] [-g group] [-m mode] [-o owner] file1 ... fileN directory\n\
    406   1.2       jtc        install  -d   [-g group] [-m mode] [-o owner] directory ...\n");
    407   1.6       jtc 	exit(1);
    408   1.1       cgd }
    409