xinstall.c revision 245312
1/*
2 * Copyright (c) 1987, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifndef lint
31static const char copyright[] =
32"@(#) Copyright (c) 1987, 1993\n\
33	The Regents of the University of California.  All rights reserved.\n";
34#endif /* not lint */
35
36#if 0
37#ifndef lint
38static char sccsid[] = "@(#)xinstall.c	8.1 (Berkeley) 7/21/93";
39#endif /* not lint */
40#endif
41
42#include <sys/cdefs.h>
43__FBSDID("$FreeBSD: head/usr.bin/xinstall/xinstall.c 245312 2013-01-11 20:53:28Z brooks $");
44
45#include <sys/param.h>
46#include <sys/mman.h>
47#include <sys/mount.h>
48#include <sys/stat.h>
49#include <sys/time.h>
50#include <sys/wait.h>
51
52#include <err.h>
53#include <errno.h>
54#include <fcntl.h>
55#include <grp.h>
56#include <paths.h>
57#include <pwd.h>
58#include <stdint.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62#include <sysexits.h>
63#include <unistd.h>
64
65#include "mtree.h"
66
67/* Bootstrap aid - this doesn't exist in most older releases */
68#ifndef MAP_FAILED
69#define MAP_FAILED ((void *)-1)	/* from <sys/mman.h> */
70#endif
71
72#define MAX_CMP_SIZE	(16 * 1024 * 1024)
73
74#define	DIRECTORY	0x01		/* Tell install it's a directory. */
75#define	SETFLAGS	0x02		/* Tell install to set flags. */
76#define	NOCHANGEBITS	(UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
77#define	BACKUP_SUFFIX	".old"
78
79static gid_t gid;
80static uid_t uid;
81static int dobackup, docompare, dodir, dopreserve, dostrip, nommap, safecopy,
82    verbose;
83static mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
84static const char *suffix = BACKUP_SUFFIX;
85
86static int	compare(int, const char *, size_t, int, const char *, size_t);
87static void	copy(int, const char *, int, const char *, off_t);
88static int	create_newfile(const char *, int, struct stat *);
89static int	create_tempfile(const char *, char *, size_t);
90static void	install(const char *, const char *, u_long, u_int);
91static void	install_dir(char *);
92static int	parseid(const char *, id_t *);
93static void	strip(const char *);
94static int	trymmap(int);
95static void	usage(void);
96
97int
98main(int argc, char *argv[])
99{
100	struct stat from_sb, to_sb;
101	mode_t *set;
102	u_long fset;
103	int ch, no_target;
104	u_int iflags;
105	char *flags;
106	const char *group, *owner, *to_name;
107
108	iflags = 0;
109	group = owner = NULL;
110	while ((ch = getopt(argc, argv, "B:bCcdf:g:Mm:N:o:pSsv")) != -1)
111		switch((char)ch) {
112		case 'B':
113			suffix = optarg;
114			/* FALLTHROUGH */
115		case 'b':
116			dobackup = 1;
117			break;
118		case 'C':
119			docompare = 1;
120			break;
121		case 'c':
122			/* For backwards compatibility. */
123			break;
124		case 'd':
125			dodir = 1;
126			break;
127		case 'f':
128			flags = optarg;
129			if (strtofflags(&flags, &fset, NULL))
130				errx(EX_USAGE, "%s: invalid flag", flags);
131			iflags |= SETFLAGS;
132			break;
133		case 'g':
134			group = optarg;
135			break;
136		case 'M':
137			nommap = 1;
138			break;
139		case 'm':
140			if (!(set = setmode(optarg)))
141				errx(EX_USAGE, "invalid file mode: %s",
142				     optarg);
143			mode = getmode(set, 0);
144			free(set);
145			break;
146		case 'N':
147			if (!setup_getid(optarg))
148				err(1, "Unable to use user and group "
149				    "databases in `%s'", optarg);
150			break;
151		case 'o':
152			owner = optarg;
153			break;
154		case 'p':
155			docompare = dopreserve = 1;
156			break;
157		case 'S':
158			safecopy = 1;
159			break;
160		case 's':
161			dostrip = 1;
162			break;
163		case 'v':
164			verbose = 1;
165			break;
166		case '?':
167		default:
168			usage();
169		}
170	argc -= optind;
171	argv += optind;
172
173	/* some options make no sense when creating directories */
174	if (dostrip && dodir) {
175		warnx("-d and -s may not be specified together");
176		usage();
177	}
178
179	if (getenv("DONTSTRIP") != NULL) {
180		warnx("DONTSTRIP set - will not strip installed binaries");
181		dostrip = 0;
182	}
183
184	/* must have at least two arguments, except when creating directories */
185	if (argc == 0 || (argc == 1 && !dodir))
186		usage();
187
188	/* need to make a temp copy so we can compare stripped version */
189	if (docompare && dostrip)
190		safecopy = 1;
191
192	/* get group and owner id's */
193	if (group != NULL) {
194		if (gid_from_group(group, &gid) == -1) {
195			id_t id;
196			if (!parseid(group, &id))
197				errx(1, "unknown group %s", group);
198			gid = id;
199		}
200	} else
201		gid = (gid_t)-1;
202
203	if (owner != NULL) {
204		if (uid_from_user(owner, &uid) == -1) {
205			id_t id;
206			if (!parseid(owner, &id))
207				errx(1, "unknown user %s", owner);
208			uid = id;
209		}
210	} else
211		uid = (uid_t)-1;
212
213	if (dodir) {
214		for (; *argv != NULL; ++argv)
215			install_dir(*argv);
216		exit(EX_OK);
217		/* NOTREACHED */
218	}
219
220	no_target = stat(to_name = argv[argc - 1], &to_sb);
221	if (!no_target && S_ISDIR(to_sb.st_mode)) {
222		for (; *argv != to_name; ++argv)
223			install(*argv, to_name, fset, iflags | DIRECTORY);
224		exit(EX_OK);
225		/* NOTREACHED */
226	}
227
228	/* can't do file1 file2 directory/file */
229	if (argc != 2) {
230		if (no_target)
231			warnx("target directory `%s' does not exist",
232			    argv[argc - 1]);
233		else
234			warnx("target `%s' is not a directory",
235			    argv[argc - 1]);
236		usage();
237	}
238
239	if (!no_target) {
240		if (stat(*argv, &from_sb))
241			err(EX_OSERR, "%s", *argv);
242		if (!S_ISREG(to_sb.st_mode)) {
243			errno = EFTYPE;
244			err(EX_OSERR, "%s", to_name);
245		}
246		if (to_sb.st_dev == from_sb.st_dev &&
247		    to_sb.st_ino == from_sb.st_ino)
248			errx(EX_USAGE,
249			    "%s and %s are the same file", *argv, to_name);
250	}
251	install(*argv, to_name, fset, iflags);
252	exit(EX_OK);
253	/* NOTREACHED */
254}
255
256/*
257 * parseid --
258 *	parse uid or gid from arg into id, returning non-zero if successful
259 */
260static int
261parseid(const char *name, id_t *id)
262{
263	char	*ep;
264	errno = 0;
265	*id = (id_t)strtoul(name, &ep, 10);
266	if (errno || *ep != '\0')
267		return (0);
268	return (1);
269}
270
271/*
272 * install --
273 *	build a path name and install the file
274 */
275static void
276install(const char *from_name, const char *to_name, u_long fset, u_int flags)
277{
278	struct stat from_sb, temp_sb, to_sb;
279	struct timeval tvb[2];
280	int devnull, files_match, from_fd, serrno, target;
281	int tempcopy, temp_fd, to_fd;
282	char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
283
284	files_match = 0;
285	from_fd = -1;
286	to_fd = -1;
287
288	/* If try to install NULL file to a directory, fails. */
289	if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
290		if (stat(from_name, &from_sb))
291			err(EX_OSERR, "%s", from_name);
292		if (!S_ISREG(from_sb.st_mode)) {
293			errno = EFTYPE;
294			err(EX_OSERR, "%s", from_name);
295		}
296		/* Build the target path. */
297		if (flags & DIRECTORY) {
298			(void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
299			    to_name,
300			    (p = strrchr(from_name, '/')) ? ++p : from_name);
301			to_name = pathbuf;
302		}
303		devnull = 0;
304	} else {
305		devnull = 1;
306	}
307
308	target = stat(to_name, &to_sb) == 0;
309
310	/* Only install to regular files. */
311	if (target && !S_ISREG(to_sb.st_mode)) {
312		errno = EFTYPE;
313		warn("%s", to_name);
314		return;
315	}
316
317	/* Only copy safe if the target exists. */
318	tempcopy = safecopy && target;
319
320	if (!devnull && (from_fd = open(from_name, O_RDONLY, 0)) < 0)
321		err(EX_OSERR, "%s", from_name);
322
323	/* If we don't strip, we can compare first. */
324	if (docompare && !dostrip && target) {
325		if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
326			err(EX_OSERR, "%s", to_name);
327		if (devnull)
328			files_match = to_sb.st_size == 0;
329		else
330			files_match = !(compare(from_fd, from_name,
331			    (size_t)from_sb.st_size, to_fd,
332			    to_name, (size_t)to_sb.st_size));
333
334		/* Close "to" file unless we match. */
335		if (!files_match)
336			(void)close(to_fd);
337	}
338
339	if (!files_match) {
340		if (tempcopy) {
341			to_fd = create_tempfile(to_name, tempfile,
342			    sizeof(tempfile));
343			if (to_fd < 0)
344				err(EX_OSERR, "%s", tempfile);
345		} else {
346			if ((to_fd = create_newfile(to_name, target,
347			    &to_sb)) < 0)
348				err(EX_OSERR, "%s", to_name);
349			if (verbose)
350				(void)printf("install: %s -> %s\n",
351				    from_name, to_name);
352		}
353		if (!devnull)
354			copy(from_fd, from_name, to_fd,
355			     tempcopy ? tempfile : to_name, from_sb.st_size);
356	}
357
358	if (dostrip) {
359		strip(tempcopy ? tempfile : to_name);
360
361		/*
362		 * Re-open our fd on the target, in case we used a strip
363		 * that does not work in-place -- like GNU binutils strip.
364		 */
365		close(to_fd);
366		to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY, 0);
367		if (to_fd < 0)
368			err(EX_OSERR, "stripping %s", to_name);
369	}
370
371	/*
372	 * Compare the stripped temp file with the target.
373	 */
374	if (docompare && dostrip && target) {
375		temp_fd = to_fd;
376
377		/* Re-open to_fd using the real target name. */
378		if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
379			err(EX_OSERR, "%s", to_name);
380
381		if (fstat(temp_fd, &temp_sb)) {
382			serrno = errno;
383			(void)unlink(tempfile);
384			errno = serrno;
385			err(EX_OSERR, "%s", tempfile);
386		}
387
388		if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
389			    to_name, (size_t)to_sb.st_size) == 0) {
390			/*
391			 * If target has more than one link we need to
392			 * replace it in order to snap the extra links.
393			 * Need to preserve target file times, though.
394			 */
395			if (to_sb.st_nlink != 1) {
396				tvb[0].tv_sec = to_sb.st_atime;
397				tvb[0].tv_usec = 0;
398				tvb[1].tv_sec = to_sb.st_mtime;
399				tvb[1].tv_usec = 0;
400				(void)utimes(tempfile, tvb);
401			} else {
402				files_match = 1;
403				(void)unlink(tempfile);
404			}
405			(void) close(temp_fd);
406		}
407	}
408
409	/*
410	 * Move the new file into place if doing a safe copy
411	 * and the files are different (or just not compared).
412	 */
413	if (tempcopy && !files_match) {
414		/* Try to turn off the immutable bits. */
415		if (to_sb.st_flags & NOCHANGEBITS)
416			(void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
417		if (dobackup) {
418			if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
419			    suffix) != strlen(to_name) + strlen(suffix)) {
420				unlink(tempfile);
421				errx(EX_OSERR, "%s: backup filename too long",
422				    to_name);
423			}
424			if (verbose)
425				(void)printf("install: %s -> %s\n", to_name, backup);
426			if (rename(to_name, backup) < 0) {
427				serrno = errno;
428				unlink(tempfile);
429				errno = serrno;
430				err(EX_OSERR, "rename: %s to %s", to_name,
431				     backup);
432			}
433		}
434		if (verbose)
435			(void)printf("install: %s -> %s\n", from_name, to_name);
436		if (rename(tempfile, to_name) < 0) {
437			serrno = errno;
438			unlink(tempfile);
439			errno = serrno;
440			err(EX_OSERR, "rename: %s to %s",
441			    tempfile, to_name);
442		}
443
444		/* Re-open to_fd so we aren't hosed by the rename(2). */
445		(void) close(to_fd);
446		if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
447			err(EX_OSERR, "%s", to_name);
448	}
449
450	/*
451	 * Preserve the timestamp of the source file if necessary.
452	 */
453	if (dopreserve && !files_match && !devnull) {
454		tvb[0].tv_sec = from_sb.st_atime;
455		tvb[0].tv_usec = 0;
456		tvb[1].tv_sec = from_sb.st_mtime;
457		tvb[1].tv_usec = 0;
458		(void)utimes(to_name, tvb);
459	}
460
461	if (fstat(to_fd, &to_sb) == -1) {
462		serrno = errno;
463		(void)unlink(to_name);
464		errno = serrno;
465		err(EX_OSERR, "%s", to_name);
466	}
467
468	/*
469	 * Set owner, group, mode for target; do the chown first,
470	 * chown may lose the setuid bits.
471	 */
472	if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
473	    (uid != (uid_t)-1 && uid != to_sb.st_uid) ||
474	    (mode != (to_sb.st_mode & ALLPERMS))) {
475		/* Try to turn off the immutable bits. */
476		if (to_sb.st_flags & NOCHANGEBITS)
477			(void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
478	}
479
480	if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
481	    (uid != (uid_t)-1 && uid != to_sb.st_uid))
482		if (fchown(to_fd, uid, gid) == -1) {
483			serrno = errno;
484			(void)unlink(to_name);
485			errno = serrno;
486			err(EX_OSERR,"%s: chown/chgrp", to_name);
487		}
488
489	if (mode != (to_sb.st_mode & ALLPERMS))
490		if (fchmod(to_fd, mode)) {
491			serrno = errno;
492			(void)unlink(to_name);
493			errno = serrno;
494			err(EX_OSERR, "%s: chmod", to_name);
495		}
496
497	/*
498	 * If provided a set of flags, set them, otherwise, preserve the
499	 * flags, except for the dump flag.
500	 * NFS does not support flags.  Ignore EOPNOTSUPP flags if we're just
501	 * trying to turn off UF_NODUMP.  If we're trying to set real flags,
502	 * then warn if the fs doesn't support it, otherwise fail.
503	 */
504	if (!devnull && (flags & SETFLAGS ||
505	    (from_sb.st_flags & ~UF_NODUMP) != to_sb.st_flags) &&
506	    fchflags(to_fd,
507	    flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
508		if (flags & SETFLAGS) {
509			if (errno == EOPNOTSUPP)
510				warn("%s: chflags", to_name);
511			else {
512				serrno = errno;
513				(void)unlink(to_name);
514				errno = serrno;
515				err(EX_OSERR, "%s: chflags", to_name);
516			}
517		}
518	}
519
520	(void)close(to_fd);
521	if (!devnull)
522		(void)close(from_fd);
523}
524
525/*
526 * compare --
527 *	compare two files; non-zero means files differ
528 */
529static int
530compare(int from_fd, const char *from_name __unused, size_t from_len,
531	int to_fd, const char *to_name __unused, size_t to_len)
532{
533	char *p, *q;
534	int rv;
535	int done_compare;
536
537	rv = 0;
538	if (from_len != to_len)
539		return 1;
540
541	if (from_len <= MAX_CMP_SIZE) {
542		done_compare = 0;
543		if (trymmap(from_fd) && trymmap(to_fd)) {
544			p = mmap(NULL, from_len, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
545			if (p == (char *)MAP_FAILED)
546				goto out;
547			q = mmap(NULL, from_len, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
548			if (q == (char *)MAP_FAILED) {
549				munmap(p, from_len);
550				goto out;
551			}
552
553			rv = memcmp(p, q, from_len);
554			munmap(p, from_len);
555			munmap(q, from_len);
556			done_compare = 1;
557		}
558	out:
559		if (!done_compare) {
560			char buf1[MAXBSIZE];
561			char buf2[MAXBSIZE];
562			int n1, n2;
563
564			rv = 0;
565			lseek(from_fd, 0, SEEK_SET);
566			lseek(to_fd, 0, SEEK_SET);
567			while (rv == 0) {
568				n1 = read(from_fd, buf1, sizeof(buf1));
569				if (n1 == 0)
570					break;		/* EOF */
571				else if (n1 > 0) {
572					n2 = read(to_fd, buf2, n1);
573					if (n2 == n1)
574						rv = memcmp(buf1, buf2, n1);
575					else
576						rv = 1;	/* out of sync */
577				} else
578					rv = 1;		/* read failure */
579			}
580			lseek(from_fd, 0, SEEK_SET);
581			lseek(to_fd, 0, SEEK_SET);
582		}
583	} else
584		rv = 1;	/* don't bother in this case */
585
586	return rv;
587}
588
589/*
590 * create_tempfile --
591 *	create a temporary file based on path and open it
592 */
593static int
594create_tempfile(const char *path, char *temp, size_t tsize)
595{
596	char *p;
597
598	(void)strncpy(temp, path, tsize);
599	temp[tsize - 1] = '\0';
600	if ((p = strrchr(temp, '/')) != NULL)
601		p++;
602	else
603		p = temp;
604	(void)strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
605	temp[tsize - 1] = '\0';
606	return (mkstemp(temp));
607}
608
609/*
610 * create_newfile --
611 *	create a new file, overwriting an existing one if necessary
612 */
613static int
614create_newfile(const char *path, int target, struct stat *sbp)
615{
616	char backup[MAXPATHLEN];
617	int saved_errno = 0;
618	int newfd;
619
620	if (target) {
621		/*
622		 * Unlink now... avoid ETXTBSY errors later.  Try to turn
623		 * off the append/immutable bits -- if we fail, go ahead,
624		 * it might work.
625		 */
626		if (sbp->st_flags & NOCHANGEBITS)
627			(void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
628
629		if (dobackup) {
630			if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
631			    path, suffix) != strlen(path) + strlen(suffix))
632				errx(EX_OSERR, "%s: backup filename too long",
633				    path);
634			(void)snprintf(backup, MAXPATHLEN, "%s%s",
635			    path, suffix);
636			if (verbose)
637				(void)printf("install: %s -> %s\n",
638				    path, backup);
639			if (rename(path, backup) < 0)
640				err(EX_OSERR, "rename: %s to %s", path, backup);
641		} else
642			if (unlink(path) < 0)
643				saved_errno = errno;
644	}
645
646	newfd = open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
647	if (newfd < 0 && saved_errno != 0)
648		errno = saved_errno;
649	return newfd;
650}
651
652/*
653 * copy --
654 *	copy from one file to another
655 */
656static void
657copy(int from_fd, const char *from_name, int to_fd, const char *to_name,
658    off_t size)
659{
660	int nr, nw;
661	int serrno;
662	char *p, buf[MAXBSIZE];
663	int done_copy;
664
665	/* Rewind file descriptors. */
666	if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
667		err(EX_OSERR, "lseek: %s", from_name);
668	if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
669		err(EX_OSERR, "lseek: %s", to_name);
670
671	/*
672	 * Mmap and write if less than 8M (the limit is so we don't totally
673	 * trash memory on big files.  This is really a minor hack, but it
674	 * wins some CPU back.
675	 */
676	done_copy = 0;
677	if (size <= 8 * 1048576 && trymmap(from_fd) &&
678	    (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
679		    from_fd, (off_t)0)) != (char *)MAP_FAILED) {
680		nw = write(to_fd, p, size);
681		if (nw != size) {
682			serrno = errno;
683			(void)unlink(to_name);
684			if (nw >= 0) {
685				errx(EX_OSERR,
686     "short write to %s: %jd bytes written, %jd bytes asked to write",
687				    to_name, (uintmax_t)nw, (uintmax_t)size);
688			} else {
689				errno = serrno;
690				err(EX_OSERR, "%s", to_name);
691			}
692		}
693		done_copy = 1;
694	}
695	if (!done_copy) {
696		while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
697			if ((nw = write(to_fd, buf, nr)) != nr) {
698				serrno = errno;
699				(void)unlink(to_name);
700				if (nw >= 0) {
701					errx(EX_OSERR,
702     "short write to %s: %jd bytes written, %jd bytes asked to write",
703					    to_name, (uintmax_t)nw,
704					    (uintmax_t)size);
705				} else {
706					errno = serrno;
707					err(EX_OSERR, "%s", to_name);
708				}
709			}
710		if (nr != 0) {
711			serrno = errno;
712			(void)unlink(to_name);
713			errno = serrno;
714			err(EX_OSERR, "%s", from_name);
715		}
716	}
717}
718
719/*
720 * strip --
721 *	use strip(1) to strip the target file
722 */
723static void
724strip(const char *to_name)
725{
726	const char *stripbin;
727	int serrno, status;
728
729	switch (fork()) {
730	case -1:
731		serrno = errno;
732		(void)unlink(to_name);
733		errno = serrno;
734		err(EX_TEMPFAIL, "fork");
735	case 0:
736		stripbin = getenv("STRIPBIN");
737		if (stripbin == NULL)
738			stripbin = "strip";
739		execlp(stripbin, stripbin, to_name, (char *)NULL);
740		err(EX_OSERR, "exec(%s)", stripbin);
741	default:
742		if (wait(&status) == -1 || status) {
743			serrno = errno;
744			(void)unlink(to_name);
745			errc(EX_SOFTWARE, serrno, "wait");
746			/* NOTREACHED */
747		}
748	}
749}
750
751/*
752 * install_dir --
753 *	build directory hierarchy
754 */
755static void
756install_dir(char *path)
757{
758	char *p;
759	struct stat sb;
760	int ch;
761
762	for (p = path;; ++p)
763		if (!*p || (p != path && *p  == '/')) {
764			ch = *p;
765			*p = '\0';
766			if (stat(path, &sb)) {
767				if (errno != ENOENT || mkdir(path, 0755) < 0) {
768					err(EX_OSERR, "mkdir %s", path);
769					/* NOTREACHED */
770				} else if (verbose)
771					(void)printf("install: mkdir %s\n",
772						     path);
773			} else if (!S_ISDIR(sb.st_mode))
774				errx(EX_OSERR, "%s exists but is not a directory", path);
775			if (!(*p = ch))
776				break;
777 		}
778
779	if ((gid != (gid_t)-1 || uid != (uid_t)-1) && chown(path, uid, gid))
780		warn("chown %u:%u %s", uid, gid, path);
781	if (chmod(path, mode))
782		warn("chmod %o %s", mode, path);
783}
784
785/*
786 * usage --
787 *	print a usage message and die
788 */
789static void
790usage(void)
791{
792	(void)fprintf(stderr,
793"usage: install [-bCcMpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n"
794"               [-N dbdir] [-o owner] file1 file2\n"
795"       install [-bCcMpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n"
796"               [-N dbdir] [-o owner] file1 ... fileN directory\n"
797"       install -d [-v] [-g group] [-m mode] [-N dbdir] [-o owner]\n"
798"               directory ...\n");
799	exit(EX_USAGE);
800	/* NOTREACHED */
801}
802
803/*
804 * trymmap --
805 *	return true (1) if mmap should be tried, false (0) if not.
806 */
807static int
808trymmap(int fd)
809{
810/*
811 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
812 * pre-Lite2-merge systems.
813 */
814#ifdef MFSNAMELEN
815	struct statfs stfs;
816
817	if (nommap || fstatfs(fd, &stfs) != 0)
818		return (0);
819	if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
820	    strcmp(stfs.f_fstypename, "cd9660") == 0)
821		return (1);
822#endif
823	return (0);
824}
825