2 * Copyright (c) 1987, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 3. 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.
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
29 * @(#) Copyright (c) 1987, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)xinstall.c 8.1 (Berkeley) 7/21/93
31 * $FreeBSD: src/usr.bin/xinstall/xinstall.c,v 1.38.2.8 2002/08/07 16:29:48 ru Exp $
34 #include <sys/param.h>
36 #include <sys/mount.h>
54 /* Bootstrap aid - this doesn't exist in most older releases */
56 #define MAP_FAILED ((void *)-1) /* from <sys/mman.h> */
59 #define UF_NOHISTORY 0
62 #define MAX_CMP_SIZE (16 * 1024 * 1024)
64 #define DIRECTORY 0x01 /* Tell install it's a directory. */
65 #define SETFLAGS 0x02 /* Tell install to set flags. */
66 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
67 #define BACKUP_SUFFIX ".old"
69 static struct passwd
*pp
;
70 static struct group
*gp
;
73 static int dobackup
, docompare
, dodir
, dopreserve
, dostrip
, dounpriv
, nommap
,
75 static mode_t mode
= S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
;
76 static const char *suffix
= BACKUP_SUFFIX
;
77 static char *destdir
, *fflags
;
79 static int file_getgroup(const char *etcdir
, const char *group
, gid_t
*gidret
);
80 static int file_getowner(const char *etcdir
, const char *owner
, uid_t
*uidret
);
82 static int compare(int, const char *, size_t, int, const char *, size_t);
83 static void copy(int, const char *, int, const char *, off_t
);
84 static int create_newfile(const char *, int, struct stat
*);
85 static int create_tempfile(const char *, char *, size_t);
86 static void install(const char *, const char *, u_long
, u_long
, u_int
);
87 static void install_dir(char *);
88 u_long
numeric_id(const char *, const char *);
89 static void strip(const char *);
90 static int trymmap(int);
91 static void usage(void);
94 main(int argc
, char *argv
[])
96 struct stat from_sb
, to_sb
;
103 const char *group
, *owner
, *to_name
;
112 while ((ch
= getopt(argc
, argv
, "L:B:bCcD:df:g:lMm:o:pSsUv")) != -1)
124 /* For backwards compatibility. */
148 if (!(set
= setmode(optarg
)))
149 errx(EX_USAGE
, "invalid file mode: %s",
151 mode
= getmode(set
, 0);
158 docompare
= dopreserve
= 1;
179 /* some options make no sense when creating directories */
180 if (dostrip
&& dodir
) {
181 warnx("-d and -s may not be specified together");
185 if (getenv("DONTSTRIP") != NULL
) {
186 warnx("DONTSTRIP set - will not strip installed binaries");
190 /* must have at least two arguments, except when creating directories */
191 if (argc
< 2 && !dodir
)
194 /* need to make a temp copy so we can compare stripped version */
195 if (docompare
&& dostrip
)
198 /* no etcdir specified, always try the system */
204 /* get group and owner id's */
205 if (group
!= NULL
&& !dounpriv
) {
206 if (etcdir
&& file_getgroup(etcdir
, group
, &gid
)) {
208 } else if (trysys
&& (gp
= getgrnam(group
)) != NULL
) {
211 gid
= (gid_t
)numeric_id(group
, "group");
215 if (owner
!= NULL
&& !dounpriv
) {
216 if (etcdir
&& file_getowner(etcdir
, owner
, &uid
)) {
218 } else if (trysys
&& (pp
= getpwnam(owner
)) != NULL
) {
221 uid
= (uid_t
)numeric_id(owner
, "user");
225 if (fflags
!= NULL
&& !dounpriv
) {
226 if (strtofflags(&fflags
, &fset
, &fclr
))
227 errx(EX_USAGE
, "%s: invalid flag", fflags
);
232 for (; *argv
!= NULL
; ++argv
)
238 to_name
= argv
[argc
- 1];
239 no_target
= stat(to_name
, &to_sb
);
240 if (!no_target
&& S_ISDIR(to_sb
.st_mode
)) {
241 for (; *argv
!= to_name
; ++argv
)
242 install(*argv
, to_name
, fset
, fclr
, iflags
| DIRECTORY
);
247 /* can't do file1 file2 directory/file */
250 warnx("target directory `%s' does not exist",
253 warnx("target `%s' is not a directory",
259 if (stat(*argv
, &from_sb
))
260 err(EX_OSERR
, "%s", *argv
);
261 if (!S_ISREG(to_sb
.st_mode
)) {
263 err(EX_OSERR
, "%s", to_name
);
265 if (to_sb
.st_dev
== from_sb
.st_dev
&&
266 to_sb
.st_ino
== from_sb
.st_ino
)
268 "%s and %s are the same file", *argv
, to_name
);
270 install(*argv
, to_name
, fset
, fclr
, iflags
);
276 numeric_id(const char *name
, const char *type
)
283 * We know that uid_t's and gid_t's are unsigned longs.
286 val
= strtoul(name
, &ep
, 10);
288 err(EX_NOUSER
, "%s", name
);
290 errx(EX_NOUSER
, "unknown %s %s", type
, name
);
296 file_getgroup(const char *etcdir
, const char *group
, gid_t
*gidret
)
305 grlen
= strlen(group
);
307 if (asprintf(&path
, "%s/group", etcdir
) < 0)
308 errx(EX_OSERR
, "asprintf()");
309 if ((fp
= fopen(path
, "r")) != NULL
) {
310 while ((ptr
= fgetln(fp
, &len
)) != NULL
&& len
) {
312 if ((scan
= strchr(ptr
, ':')) == NULL
)
314 if ((size_t)(scan
- ptr
) != grlen
)
316 if (strncmp(ptr
, group
, grlen
) != 0)
318 if ((scan
= strchr(scan
+ 1, ':')) == NULL
)
320 *gidret
= strtoul(scan
+ 1, NULL
, 10);
326 return((*gidret
== (gid_t
)-1) ? 0 : 1);
331 file_getowner(const char *etcdir
, const char *owner
, uid_t
*uidret
)
340 owner_len
= strlen(owner
);
342 if (asprintf(&path
, "%s/master.passwd", etcdir
) < 0)
343 errx(EX_OSERR
, "asprintf()");
344 if ((fp
= fopen(path
, "r")) != NULL
) {
345 while ((ptr
= fgetln(fp
, &len
)) != NULL
&& len
) {
347 if ((scan
= strchr(ptr
, ':')) == NULL
)
349 if ((size_t)(scan
- ptr
) != owner_len
)
351 if (strncmp(ptr
, owner
, owner_len
) != 0)
353 if ((scan
= strchr(scan
+ 1, ':')) == NULL
)
355 *uidret
= strtoul(scan
+ 1, NULL
, 10);
361 return((*uidret
== (uid_t
)-1) ? 0 : 1);
366 * build a path name and install the file
369 install(const char *from_name
, const char *to_name
, u_long fset
, u_long fclr
,
372 struct stat from_sb
, temp_sb
, to_sb
;
374 int devnull
, files_match
, from_fd
, serrno
, target
;
375 int tempcopy
, temp_fd
, to_fd
;
377 char backup
[MAXPATHLEN
], *p
, pathbuf
[MAXPATHLEN
], tempfile
[MAXPATHLEN
];
382 /* If try to install NULL file to a directory, fails. */
383 if (flags
& DIRECTORY
|| strcmp(from_name
, _PATH_DEVNULL
)) {
384 if (stat(from_name
, &from_sb
))
385 err(EX_OSERR
, "%s", from_name
);
386 if (!S_ISREG(from_sb
.st_mode
)) {
388 err(EX_OSERR
, "%s", from_name
);
390 /* Build the target path. */
391 if (flags
& DIRECTORY
) {
392 (void)snprintf(pathbuf
, sizeof(pathbuf
), "%s/%s",
394 (p
= strrchr(from_name
, '/')) ? ++p
: from_name
);
402 target
= stat(to_name
, &to_sb
) == 0;
404 /* Only install to regular files. */
405 if (target
&& !S_ISREG(to_sb
.st_mode
)) {
411 /* Only copy safe if the target exists. */
412 tempcopy
= safecopy
&& target
;
414 if (!devnull
&& (from_fd
= open(from_name
, O_RDONLY
, 0)) < 0)
415 err(EX_OSERR
, "%s", from_name
);
417 /* If we don't strip, we can compare first. */
418 if (docompare
&& !dostrip
&& target
) {
419 if ((to_fd
= open(to_name
, O_RDONLY
, 0)) < 0)
420 err(EX_OSERR
, "%s", to_name
);
422 files_match
= to_sb
.st_size
== 0;
424 files_match
= !(compare(from_fd
, from_name
,
425 (size_t)from_sb
.st_size
, to_fd
,
426 to_name
, (size_t)to_sb
.st_size
));
428 /* Close "to" file unless we match. */
435 to_fd
= create_tempfile(to_name
, tempfile
,
438 err(EX_OSERR
, "%s", tempfile
);
440 if ((to_fd
= create_newfile(to_name
, target
,
442 err(EX_OSERR
, "%s", to_name
);
444 (void)printf("install: %s -> %s\n",
448 copy(from_fd
, from_name
, to_fd
,
449 tempcopy
? tempfile
: to_name
, from_sb
.st_size
);
453 strip(tempcopy
? tempfile
: to_name
);
456 * Re-open our fd on the target, in case we used a strip
457 * that does not work in-place -- like GNU binutils strip.
460 to_fd
= open(tempcopy
? tempfile
: to_name
, O_RDONLY
, 0);
462 err(EX_OSERR
, "stripping %s", to_name
);
466 * Compare the stripped temp file with the target.
468 if (docompare
&& dostrip
&& target
) {
471 /* Re-open to_fd using the real target name. */
472 if ((to_fd
= open(to_name
, O_RDONLY
, 0)) < 0)
473 err(EX_OSERR
, "%s", to_name
);
475 if (fstat(temp_fd
, &temp_sb
)) {
477 (void)unlink(tempfile
);
479 err(EX_OSERR
, "%s", tempfile
);
482 if (compare(temp_fd
, tempfile
, (size_t)temp_sb
.st_size
, to_fd
,
483 to_name
, (size_t)to_sb
.st_size
) == 0) {
485 * If target has more than one link we need to
486 * replace it in order to snap the extra links.
487 * Need to preserve target file times, though.
489 if (to_sb
.st_nlink
!= 1) {
490 utb
.actime
= to_sb
.st_atime
;
491 utb
.modtime
= to_sb
.st_mtime
;
492 utime(tempfile
, &utb
);
495 (void)unlink(tempfile
);
497 (void) close(temp_fd
);
502 * Move the new file into place if doing a safe copy
503 * and the files are different (or just not compared).
505 if (tempcopy
&& !files_match
) {
506 /* Try to turn off the immutable bits. */
507 if (to_sb
.st_flags
& NOCHANGEBITS
)
508 (void)chflags(to_name
, to_sb
.st_flags
& ~NOCHANGEBITS
);
510 if ((size_t)snprintf(backup
, MAXPATHLEN
, "%s%s", to_name
,
511 suffix
) != strlen(to_name
) + strlen(suffix
)) {
513 errx(EX_OSERR
, "%s: backup filename too long",
517 (void)printf("install: %s -> %s\n", to_name
, backup
);
518 if (rename(to_name
, backup
) < 0) {
522 err(EX_OSERR
, "rename: %s to %s", to_name
,
527 (void)printf("install: %s -> %s\n", from_name
, to_name
);
528 if (rename(tempfile
, to_name
) < 0) {
532 err(EX_OSERR
, "rename: %s to %s",
536 /* Re-open to_fd so we aren't hosed by the rename(2). */
538 if ((to_fd
= open(to_name
, O_RDONLY
, 0)) < 0)
539 err(EX_OSERR
, "%s", to_name
);
543 * Preserve the timestamp of the source file if necessary.
545 if (dopreserve
&& !files_match
&& !devnull
) {
546 utb
.actime
= from_sb
.st_atime
;
547 utb
.modtime
= from_sb
.st_mtime
;
548 utime(to_name
, &utb
);
551 if (fstat(to_fd
, &to_sb
) == -1) {
553 (void)unlink(to_name
);
555 err(EX_OSERR
, "%s", to_name
);
559 * Set owner, group, mode for target; do the chown first,
560 * chown may lose the setuid bits.
562 if (!dounpriv
&& ((gid
!= (gid_t
)-1 && gid
!= to_sb
.st_gid
) ||
563 (uid
!= (uid_t
)-1 && uid
!= to_sb
.st_uid
) ||
564 (mode
!= to_sb
.st_mode
))) {
565 /* Try to turn off the immutable bits. */
566 if (to_sb
.st_flags
& NOCHANGEBITS
)
567 (void)fchflags(to_fd
, to_sb
.st_flags
& ~NOCHANGEBITS
);
571 (gid
!= (gid_t
)-1 && gid
!= to_sb
.st_gid
) ||
572 (uid
!= (uid_t
)-1 && uid
!= to_sb
.st_uid
)))
573 if (fchown(to_fd
, uid
, gid
) == -1) {
575 (void)unlink(to_name
);
577 err(EX_OSERR
,"%s: chown/chgrp", to_name
);
580 if (mode
!= to_sb
.st_mode
) {
582 dounpriv
? mode
& (S_IRWXU
|S_IRWXG
|S_IRWXO
) : mode
)) {
584 (void)unlink(to_name
);
586 err(EX_OSERR
, "%s: chmod", to_name
);
591 * If provided a set of flags, set them, otherwise, preserve the
592 * flags, except for the dump and history flags. The dump flag
593 * is left clear on the target while the history flag from when
594 * the target was created (which is inherited from the target's
595 * parent directory) is retained.
597 if (flags
& SETFLAGS
) {
598 nfset
= (to_sb
.st_flags
| fset
) & ~fclr
;
600 nfset
= (from_sb
.st_flags
& ~(UF_NODUMP
| UF_NOHISTORY
)) |
601 (to_sb
.st_flags
& UF_NOHISTORY
);
605 * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
606 * trying to turn off UF_NODUMP. If we're trying to set real flags,
607 * then warn if the fs doesn't support it, otherwise fail.
609 if (!dounpriv
&& !devnull
&& fchflags(to_fd
, nfset
)) {
610 if (flags
& SETFLAGS
) {
611 if (errno
== EOPNOTSUPP
)
612 warn("%s: chflags", to_name
);
615 (void)unlink(to_name
);
617 err(EX_OSERR
, "%s: chflags", to_name
);
624 (void)close(from_fd
);
629 * compare two files; non-zero means files differ
632 compare(int from_fd
, const char *from_name __unused
, size_t from_len
,
633 int to_fd
, const char *to_name __unused
, size_t to_len
)
640 if (from_len
!= to_len
)
643 if (from_len
<= MAX_CMP_SIZE
) {
645 if (trymmap(from_fd
) && trymmap(to_fd
)) {
646 p
= mmap(NULL
, from_len
, PROT_READ
, MAP_SHARED
, from_fd
, (off_t
)0);
647 if (p
== (char *)MAP_FAILED
)
649 q
= mmap(NULL
, from_len
, PROT_READ
, MAP_SHARED
, to_fd
, (off_t
)0);
650 if (q
== (char *)MAP_FAILED
) {
655 rv
= memcmp(p
, q
, from_len
);
667 lseek(from_fd
, 0, SEEK_SET
);
668 lseek(to_fd
, 0, SEEK_SET
);
670 n1
= read(from_fd
, buf1
, sizeof(buf1
));
674 n2
= read(to_fd
, buf2
, n1
);
676 rv
= memcmp(buf1
, buf2
, n1
);
678 rv
= 1; /* out of sync */
680 rv
= 1; /* read failure */
682 lseek(from_fd
, 0, SEEK_SET
);
683 lseek(to_fd
, 0, SEEK_SET
);
686 rv
= 1; /* don't bother in this case */
693 * create a temporary file based on path and open it
696 create_tempfile(const char *path
, char *temp
, size_t tsize
)
700 (void)strncpy(temp
, path
, tsize
);
701 temp
[tsize
- 1] = '\0';
702 if ((p
= strrchr(temp
, '/')) != NULL
)
706 (void)strncpy(p
, "INS@XXXX", &temp
[tsize
- 1] - p
);
707 temp
[tsize
- 1] = '\0';
708 return (mkstemp(temp
));
713 * create a new file, overwriting an existing one if necessary
716 create_newfile(const char *path
, int target
, struct stat
*sbp
)
718 char backup
[MAXPATHLEN
];
722 * Unlink now... avoid ETXTBSY errors later. Try to turn
723 * off the append/immutable bits -- if we fail, go ahead,
726 if (sbp
->st_flags
& NOCHANGEBITS
)
727 (void)chflags(path
, sbp
->st_flags
& ~NOCHANGEBITS
);
730 if ((size_t)snprintf(backup
, MAXPATHLEN
, "%s%s",
731 path
, suffix
) != strlen(path
) + strlen(suffix
))
732 errx(EX_OSERR
, "%s: backup filename too long",
734 (void)snprintf(backup
, MAXPATHLEN
, "%s%s",
737 (void)printf("install: %s -> %s\n",
739 if (rename(path
, backup
) < 0)
740 err(EX_OSERR
, "rename: %s to %s", path
, backup
);
745 return (open(path
, O_CREAT
| O_RDWR
| O_TRUNC
, S_IRUSR
| S_IWUSR
));
750 * copy from one file to another
753 copy(int from_fd
, const char *from_name
, int to_fd
,
754 const char *to_name
, off_t size
)
762 /* Rewind file descriptors. */
763 if (lseek(from_fd
, (off_t
)0, SEEK_SET
) == (off_t
)-1)
764 err(EX_OSERR
, "lseek: %s", from_name
);
765 if (lseek(to_fd
, (off_t
)0, SEEK_SET
) == (off_t
)-1)
766 err(EX_OSERR
, "lseek: %s", to_name
);
769 * Mmap and write if less than 8M (the limit is so we don't totally
770 * trash memory on big files. This is really a minor hack, but it
771 * wins some CPU back.
774 if (size
<= 8 * 1048576 && trymmap(from_fd
) &&
775 (p
= mmap(NULL
, (size_t)size
, PROT_READ
, MAP_SHARED
,
776 from_fd
, (off_t
)0)) != (char *)MAP_FAILED
) {
777 if ((nw
= write(to_fd
, p
, size
)) != size
) {
779 (void)unlink(to_name
);
780 errno
= nw
> 0 ? EIO
: serrno
;
781 err(EX_OSERR
, "%s", to_name
);
786 while ((nr
= read(from_fd
, buf
, sizeof(buf
))) > 0) {
787 if ((nw
= write(to_fd
, buf
, nr
)) != nr
) {
789 (void)unlink(to_name
);
790 errno
= nw
> 0 ? EIO
: serrno
;
791 err(EX_OSERR
, "%s", to_name
);
796 (void)unlink(to_name
);
798 err(EX_OSERR
, "%s", from_name
);
805 * use strip(1) to strip the target file
808 strip(const char *to_name
)
810 const char *stripbin
;
816 (void)unlink(to_name
);
818 err(EX_TEMPFAIL
, "fork");
820 stripbin
= getenv("STRIPBIN");
821 if (stripbin
== NULL
)
823 execlp(stripbin
, stripbin
, to_name
, NULL
);
824 err(EX_OSERR
, "exec(%s)", stripbin
);
826 if (wait(&status
) == -1 || status
) {
828 (void)unlink(to_name
);
829 errc(EX_SOFTWARE
, serrno
, "wait");
836 * When doing a concurrent make -j N multiple install's can race the mkdir.
840 mkdir_race(const char *path
, int nmode
)
845 res
= mkdir(path
, nmode
);
846 if (res
< 0 && errno
== EEXIST
) {
847 if (stat(path
, &sb
) == 0 && S_ISDIR(sb
.st_mode
))
849 res
= mkdir(path
, nmode
);
856 * build directory hierarchy
859 install_dir(char *path
)
866 if (!*p
|| (p
!= path
&& *p
== '/')) {
869 if (stat(path
, &sb
)) {
870 if (errno
!= ENOENT
||
871 mkdir_race(path
, 0755) < 0) {
872 err(EX_OSERR
, "mkdir %s", path
);
875 (void)printf("install: mkdir %s\n",
877 } else if (!S_ISDIR(sb
.st_mode
))
878 errx(EX_OSERR
, "%s exists but is not a directory", path
);
884 if ((gid
!= (gid_t
)-1 || uid
!= (uid_t
)-1) &&
885 chown(path
, uid
, gid
))
886 warn("chown %u:%u %s", uid
, gid
, path
);
887 /* XXXBED: should we do the chmod in the dounpriv case? */
888 if (chmod(path
, mode
))
889 warn("chmod %o %s", mode
, path
);
895 * print a usage message and die
901 "usage: install [-bCcpSsUv] [-B suffix] [-D dest] [-f flags] [-g group]\n"
902 " [-m mode] [-o owner] file1 file2\n"
903 " install [-bCcpSsUv] [-B suffix] [-D dest] [-f flags] [-g group]\n"
904 " [-m mode] [-o owner] file1 ... fileN directory\n"
905 " install -d [-lUv] [-D dest] [-g group] [-m mode] [-o owner]\n"
913 * return true (1) if mmap should be tried, false (0) if not.
919 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
920 * pre-Lite2-merge systems.
925 if (nommap
|| fstatfs(fd
, &stfs
) != 0)
927 if (strcmp(stfs
.f_fstypename
, "ufs") == 0 ||
928 strcmp(stfs
.f_fstypename
, "cd9660") == 0)