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