1 /* $NetBSD: main.c,v 1.69 2012/04/07 16:44:10 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1980, 1991, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\ 35 The Regents of the University of California. All rights reserved."); 36 #endif /* not lint */ 37 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/1/95"; 41 #else 42 __RCSID("$NetBSD: main.c,v 1.69 2012/04/07 16:44:10 christos Exp $"); 43 #endif 44 #endif /* not lint */ 45 46 #include <sys/param.h> 47 #include <sys/time.h> 48 #include <sys/stat.h> 49 #include <sys/mount.h> 50 51 #include <ufs/ufs/dinode.h> 52 #include <ufs/ffs/fs.h> 53 #include <ufs/ffs/ffs_extern.h> 54 55 #include <protocols/dumprestore.h> 56 57 #include <ctype.h> 58 #include <err.h> 59 #include <errno.h> 60 #include <fcntl.h> 61 #include <fstab.h> 62 #include <signal.h> 63 #include <stdio.h> 64 #include <stdlib.h> 65 #include <string.h> 66 #include <time.h> 67 #include <unistd.h> 68 #include <util.h> 69 70 #include "dump.h" 71 #include "pathnames.h" 72 #include "snapshot.h" 73 74 int timestamp; /* print message timestamps */ 75 int notify; /* notify operator flag */ 76 int blockswritten; /* number of blocks written on current tape */ 77 int tapeno; /* current tape number */ 78 int density; /* density in bytes/0.1" */ 79 int ntrec = NTREC; /* # tape blocks in each tape record */ 80 int cartridge; /* Assume non-cartridge tape */ 81 long dev_bsize = 1; /* recalculated below */ 82 long blocksperfile; /* output blocks per file */ 83 const char *host; /* remote host (if any) */ 84 int readcache = -1; /* read cache size (in readblksize blks) */ 85 int readblksize = 32 * 1024; /* read block size */ 86 char default_time_string[] = "%T %Z"; /* default timestamp string */ 87 char *time_string = default_time_string; /* timestamp string */ 88 89 static long numarg(const char *, long, long); 90 static void obsolete(int *, char **[]); 91 static void usage(void); 92 93 int 94 main(int argc, char *argv[]) 95 { 96 ino_t ino; 97 int dirty; 98 union dinode *dp; 99 struct fstab *dt; 100 struct statvfs *mntinfo, fsbuf; 101 char *map, *cp; 102 int ch; 103 int i, anydirskipped, bflag = 0, Tflag = 0, Fflag = 0, honorlevel = 1; 104 int snap_internal = 0; 105 ino_t maxino; 106 time_t tnow, date; 107 int dirc; 108 char *mountpoint; 109 int just_estimate = 0; 110 char labelstr[LBLSIZE]; 111 char buf[MAXPATHLEN], rbuf[MAXPATHLEN]; 112 char *new_time_format; 113 char *snap_backup = NULL; 114 115 spcl.c_date = 0; 116 (void)time(&tnow); 117 spcl.c_date = tnow; 118 tzset(); /* set up timezone for strftime */ 119 if ((new_time_format = getenv("TIMEFORMAT")) != NULL) 120 time_string = new_time_format; 121 122 tsize = 0; /* Default later, based on 'c' option for cart tapes */ 123 if ((tape = getenv("TAPE")) == NULL) 124 tape = _PATH_DEFTAPE; 125 dumpdates = _PATH_DUMPDATES; 126 temp = _PATH_DTMP; 127 strcpy(labelstr, "none"); /* XXX safe strcpy. */ 128 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) 129 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n"); 130 level = '0'; 131 timestamp = 0; 132 133 if (argc < 2) 134 usage(); 135 136 obsolete(&argc, &argv); 137 while ((ch = getopt(argc, argv, 138 "0123456789aB:b:cd:eFf:h:ik:l:L:nr:s:StT:uWwx:X")) != -1) 139 switch (ch) { 140 /* dump level */ 141 case '0': case '1': case '2': case '3': case '4': 142 case '5': case '6': case '7': case '8': case '9': 143 level = ch; 144 break; 145 146 case 'a': /* `auto-size', Write to EOM. */ 147 unlimited = 1; 148 break; 149 150 case 'B': /* blocks per output file */ 151 blocksperfile = numarg("blocks per file", 1L, 0L); 152 break; 153 154 case 'b': /* blocks per tape write */ 155 ntrec = numarg("blocks per write", 1L, 1000L); 156 bflag = 1; 157 break; 158 159 case 'c': /* Tape is cart. not 9-track */ 160 cartridge = 1; 161 break; 162 163 case 'd': /* density, in bits per inch */ 164 density = numarg("density", 10L, 327670L) / 10; 165 if (density >= 625 && !bflag) 166 ntrec = HIGHDENSITYTREC; 167 break; 168 169 case 'e': /* eject full tapes */ 170 eflag = 1; 171 break; 172 173 case 'F': /* files-to-dump is an fs image */ 174 Fflag = 1; 175 break; 176 177 case 'f': /* output file */ 178 tape = optarg; 179 break; 180 181 case 'h': 182 honorlevel = numarg("honor level", 0L, 10L); 183 break; 184 185 case 'i': /* "true incremental" regardless level */ 186 level = 'i'; 187 trueinc = 1; 188 break; 189 190 case 'k': 191 readblksize = numarg("read block size", 0, 64) * 1024; 192 break; 193 194 case 'l': /* autoload after eject full tapes */ 195 eflag = 1; 196 lflag = numarg("timeout (in seconds)", 1, 0); 197 break; 198 199 case 'L': 200 /* 201 * Note that although there are LBLSIZE characters, 202 * the last must be '\0', so the limit on strlen() 203 * is really LBLSIZE-1. 204 */ 205 if (strlcpy(labelstr, optarg, sizeof(labelstr)) 206 >= sizeof(labelstr)) { 207 msg( 208 "WARNING Label `%s' is larger than limit of %lu characters.\n", 209 optarg, 210 (unsigned long)sizeof(labelstr) - 1); 211 msg("WARNING: Using truncated label `%s'.\n", 212 labelstr); 213 } 214 break; 215 case 'n': /* notify operators */ 216 notify = 1; 217 break; 218 219 case 'r': /* read cache size */ 220 readcache = numarg("read cache size", 0, 512); 221 break; 222 223 case 's': /* tape size, feet */ 224 tsize = numarg("tape size", 1L, 0L) * 12 * 10; 225 break; 226 227 case 'S': /* exit after estimating # of tapes */ 228 just_estimate = 1; 229 break; 230 231 case 't': 232 timestamp = 1; 233 break; 234 235 case 'T': /* time of last dump */ 236 spcl.c_ddate = unctime(optarg); 237 if (spcl.c_ddate < 0) { 238 (void)fprintf(stderr, "bad time \"%s\"\n", 239 optarg); 240 exit(X_STARTUP); 241 } 242 Tflag = 1; 243 lastlevel = '?'; 244 break; 245 246 case 'u': /* update /etc/dumpdates */ 247 uflag = 1; 248 break; 249 250 case 'W': /* what to do */ 251 case 'w': 252 lastdump(ch); 253 exit(X_FINOK); /* do nothing else */ 254 255 case 'x': 256 snap_backup = optarg; 257 break; 258 259 case 'X': 260 snap_internal = 1; 261 break; 262 263 default: 264 usage(); 265 } 266 argc -= optind; 267 argv += optind; 268 269 if (argc < 1) { 270 (void)fprintf(stderr, 271 "Must specify disk or image, or file list\n"); 272 exit(X_STARTUP); 273 } 274 275 276 /* 277 * determine if disk is a subdirectory, and setup appropriately 278 */ 279 getfstab(); /* /etc/fstab snarfed */ 280 disk = NULL; 281 disk_dev = NULL; 282 mountpoint = NULL; 283 dirc = 0; 284 for (i = 0; i < argc; i++) { 285 struct stat sb; 286 287 if (lstat(argv[i], &sb) == -1) 288 quit("Cannot stat %s: %s\n", argv[i], strerror(errno)); 289 if (Fflag || S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) { 290 disk = argv[i]; 291 multicheck: 292 if (dirc != 0) 293 quit( 294 "Can't dump a disk or image at the same time as a file list\n"); 295 break; 296 } 297 if ((dt = fstabsearch(argv[i])) != NULL) { 298 if (getfsspecname(buf, sizeof(buf), dt->fs_spec) 299 == NULL) 300 quit("%s (%s)", buf, strerror(errno)); 301 disk = buf; 302 mountpoint = xstrdup(dt->fs_file); 303 goto multicheck; 304 } 305 if (statvfs(argv[i], &fsbuf) == -1) 306 quit("Cannot statvfs %s: %s\n", argv[i], 307 strerror(errno)); 308 disk = fsbuf.f_mntfromname; 309 if (strcmp(argv[i], fsbuf.f_mntonname) == 0) 310 goto multicheck; 311 if (mountpoint == NULL) { 312 mountpoint = xstrdup(fsbuf.f_mntonname); 313 if (uflag) { 314 msg("Ignoring u flag for subdir dump\n"); 315 uflag = 0; 316 } 317 if (level > '0') { 318 msg("Subdir dump is done at level 0\n"); 319 level = '0'; 320 } 321 msg("Dumping sub files/directories from %s\n", 322 mountpoint); 323 } else { 324 if (strcmp(mountpoint, fsbuf.f_mntonname) != 0) 325 quit("%s is not on %s\n", argv[i], mountpoint); 326 } 327 msg("Dumping file/directory %s\n", argv[i]); 328 dirc++; 329 } 330 if (mountpoint) 331 free(mountpoint); 332 333 if (dirc == 0) { 334 argv++; 335 if (argc != 1) { 336 (void)fprintf(stderr, "Excess arguments to dump:"); 337 while (--argc) 338 (void)fprintf(stderr, " %s", *argv++); 339 (void)fprintf(stderr, "\n"); 340 exit(X_STARTUP); 341 } 342 } 343 if (Tflag && uflag) { 344 (void)fprintf(stderr, 345 "You cannot use the T and u flags together.\n"); 346 exit(X_STARTUP); 347 } 348 if (strcmp(tape, "-") == 0) { 349 pipeout++; 350 tape = "standard output"; 351 } 352 353 if (blocksperfile) 354 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */ 355 else if (!unlimited) { 356 /* 357 * Determine how to default tape size and density 358 * 359 * density tape size 360 * 9-track 1600 bpi (160 bytes/.1") 2300 ft. 361 * 9-track 6250 bpi (625 bytes/.1") 2300 ft. 362 * cartridge 8000 bpi (100 bytes/.1") 1700 ft. 363 * (450*4 - slop) 364 */ 365 if (density == 0) 366 density = cartridge ? 100 : 160; 367 if (tsize == 0) 368 tsize = cartridge ? 1700L*120L : 2300L*120L; 369 } 370 371 if ((cp = strchr(tape, ':')) != NULL) { 372 host = tape; 373 /* This is fine, because all the const strings don't have : */ 374 *cp++ = '\0'; 375 tape = cp; 376 #ifdef RDUMP 377 if (rmthost(host) == 0) 378 exit(X_STARTUP); 379 #else 380 (void)fprintf(stderr, "remote dump not enabled\n"); 381 exit(X_STARTUP); 382 #endif 383 } 384 385 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 386 signal(SIGHUP, sig); 387 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN) 388 signal(SIGTRAP, sig); 389 if (signal(SIGFPE, SIG_IGN) != SIG_IGN) 390 signal(SIGFPE, sig); 391 if (signal(SIGBUS, SIG_IGN) != SIG_IGN) 392 signal(SIGBUS, sig); 393 #if 0 394 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN) 395 signal(SIGSEGV, sig); 396 #endif 397 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 398 signal(SIGTERM, sig); 399 if (signal(SIGINT, interrupt) == SIG_IGN) 400 signal(SIGINT, SIG_IGN); 401 402 /* 403 * disk can be either the full special file name, or 404 * the file system name. 405 */ 406 mountpoint = NULL; 407 mntinfo = mntinfosearch(disk); 408 if ((dt = fstabsearch(disk)) != NULL) { 409 if (getfsspecname(buf, sizeof(buf), dt->fs_spec) == NULL) 410 quit("%s (%s)", buf, strerror(errno)); 411 if (getdiskrawname(rbuf, sizeof(rbuf), buf) == NULL) 412 quit("Can't get disk raw name for `%s' (%s)", 413 buf, strerror(errno)); 414 disk = rbuf; 415 mountpoint = dt->fs_file; 416 msg("Found %s on %s in %s\n", disk, mountpoint, _PATH_FSTAB); 417 } else if (mntinfo != NULL) { 418 if (getdiskrawname(rbuf, sizeof(rbuf), mntinfo->f_mntfromname) 419 == NULL) 420 quit("Can't get disk raw name for `%s' (%s)", 421 mntinfo->f_mntfromname, strerror(errno)); 422 mountpoint = mntinfo->f_mntonname; 423 msg("Found %s on %s in mount table\n", disk, mountpoint); 424 } 425 if (mountpoint != NULL) { 426 if (dirc != 0) 427 (void)snprintf(spcl.c_filesys, sizeof(spcl.c_filesys), 428 "a subset of %s", mountpoint); 429 else 430 (void)strlcpy(spcl.c_filesys, mountpoint, 431 sizeof(spcl.c_filesys)); 432 } else if (Fflag) { 433 (void)strlcpy(spcl.c_filesys, "a file system image", 434 sizeof(spcl.c_filesys)); 435 } else { 436 (void)strlcpy(spcl.c_filesys, "an unlisted file system", 437 sizeof(spcl.c_filesys)); 438 } 439 (void)strlcpy(spcl.c_dev, disk, sizeof(spcl.c_dev)); 440 (void)strlcpy(spcl.c_label, labelstr, sizeof(spcl.c_label)); 441 (void)gethostname(spcl.c_host, sizeof(spcl.c_host)); 442 spcl.c_host[sizeof(spcl.c_host) - 1] = '\0'; 443 444 if ((snap_backup != NULL || snap_internal) && mntinfo == NULL) { 445 msg("WARNING: Cannot use -x or -X on unmounted file system.\n"); 446 snap_backup = NULL; 447 snap_internal = 0; 448 } 449 450 #ifdef DUMP_LFS 451 sync(); 452 if (snap_backup != NULL || snap_internal) { 453 if (lfs_wrap_stop(mountpoint) < 0) { 454 msg("Cannot stop writing on %s\n", mountpoint); 455 exit(X_STARTUP); 456 } 457 } 458 if ((diskfd = open(disk, O_RDONLY)) < 0) { 459 msg("Cannot open %s\n", disk); 460 exit(X_STARTUP); 461 } 462 disk_dev = disk; 463 #else /* ! DUMP_LFS */ 464 if (snap_backup != NULL || snap_internal) { 465 diskfd = snap_open(mntinfo->f_mntonname, snap_backup, 466 &tnow, &disk_dev); 467 if (diskfd < 0) { 468 msg("Cannot open snapshot of %s\n", 469 mntinfo->f_mntonname); 470 exit(X_STARTUP); 471 } 472 spcl.c_date = tnow; 473 } else { 474 if ((diskfd = open(disk, O_RDONLY)) < 0) { 475 msg("Cannot open %s\n", disk); 476 exit(X_STARTUP); 477 } 478 disk_dev = disk; 479 } 480 sync(); 481 #endif /* ! DUMP_LFS */ 482 483 needswap = fs_read_sblock(sblock_buf); 484 485 /* true incremental is always a level 10 dump */ 486 spcl.c_level = trueinc? iswap32(10): iswap32(level - '0'); 487 spcl.c_type = iswap32(TS_TAPE); 488 spcl.c_date = iswap32(spcl.c_date); 489 spcl.c_ddate = iswap32(spcl.c_ddate); 490 if (!Tflag) 491 getdumptime(); /* /etc/dumpdates snarfed */ 492 493 date = iswap32(spcl.c_date); 494 msg("Date of this level %c dump: %s", level, 495 spcl.c_date == 0 ? "the epoch\n" : ctime(&date)); 496 date = iswap32(spcl.c_ddate); 497 msg("Date of last level %c dump: %s", lastlevel, 498 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&date)); 499 msg("Dumping "); 500 if (snap_backup != NULL || snap_internal) 501 msgtail("a snapshot of "); 502 if (dirc != 0) 503 msgtail("a subset of "); 504 msgtail("%s (%s) ", disk, spcl.c_filesys); 505 if (host) 506 msgtail("to %s on host %s\n", tape, host); 507 else 508 msgtail("to %s\n", tape); 509 msg("Label: %s\n", labelstr); 510 511 ufsib = fs_parametrize(); 512 513 dev_bshift = ffs(dev_bsize) - 1; 514 if (dev_bsize != (1 << dev_bshift)) 515 quit("dev_bsize (%ld) is not a power of 2", dev_bsize); 516 tp_bshift = ffs(TP_BSIZE) - 1; 517 if (TP_BSIZE != (1 << tp_bshift)) 518 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE); 519 maxino = fs_maxino(); 520 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE); 521 usedinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char)); 522 dumpdirmap = (char *)xcalloc((unsigned) mapsize, sizeof(char)); 523 dumpinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char)); 524 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 525 526 nonodump = iswap32(spcl.c_level) < honorlevel; 527 528 initcache(readcache, readblksize); 529 530 (void)signal(SIGINFO, statussig); 531 532 msg("mapping (Pass I) [regular files]\n"); 533 anydirskipped = mapfiles(maxino, &tapesize, mountpoint, 534 (dirc ? argv : NULL)); 535 536 msg("mapping (Pass II) [directories]\n"); 537 while (anydirskipped) { 538 anydirskipped = mapdirs(maxino, &tapesize); 539 } 540 541 if (pipeout || unlimited) { 542 tapesize += 10; /* 10 trailer blocks */ 543 msg("estimated %llu tape blocks.\n", 544 (unsigned long long)tapesize); 545 } else { 546 double fetapes; 547 548 if (blocksperfile) 549 fetapes = (double) tapesize / blocksperfile; 550 else if (cartridge) { 551 /* Estimate number of tapes, assuming streaming stops at 552 the end of each block written, and not in mid-block. 553 Assume no erroneous blocks; this can be compensated 554 for with an artificially low tape size. */ 555 fetapes = 556 ( (double) tapesize /* blocks */ 557 * TP_BSIZE /* bytes/block */ 558 * (1.0/density) /* 0.1" / byte */ 559 + 560 (double) tapesize /* blocks */ 561 * (1.0/ntrec) /* streaming-stops per block */ 562 * 15.48 /* 0.1" / streaming-stop */ 563 ) * (1.0 / tsize ); /* tape / 0.1" */ 564 } else { 565 /* Estimate number of tapes, for old fashioned 9-track 566 tape */ 567 int tenthsperirg = (density == 625) ? 3 : 7; 568 fetapes = 569 ( tapesize /* blocks */ 570 * TP_BSIZE /* bytes / block */ 571 * (1.0/density) /* 0.1" / byte */ 572 + 573 tapesize /* blocks */ 574 * (1.0/ntrec) /* IRG's / block */ 575 * tenthsperirg /* 0.1" / IRG */ 576 ) * (1.0 / tsize ); /* tape / 0.1" */ 577 } 578 etapes = fetapes; /* truncating assignment */ 579 etapes++; 580 /* count the dumped inodes map on each additional tape */ 581 tapesize += (etapes - 1) * 582 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 583 tapesize += etapes + 10; /* headers + 10 trailer blks */ 584 msg("estimated %llu tape blocks on %3.2f tape(s).\n", 585 (unsigned long long)tapesize, fetapes); 586 } 587 /* 588 * If the user only wants an estimate of the number of 589 * tapes, exit now. 590 */ 591 if (just_estimate) 592 exit(X_FINOK); 593 594 /* 595 * Allocate tape buffer. 596 */ 597 if (!alloctape()) 598 quit("can't allocate tape buffers - try a smaller blocking factor.\n"); 599 600 startnewtape(1); 601 (void)time((time_t *)&(tstart_writing)); 602 xferrate = 0; 603 dumpmap(usedinomap, TS_CLRI, maxino - 1); 604 605 msg("dumping (Pass III) [directories]\n"); 606 dirty = 0; /* XXX just to get gcc to shut up */ 607 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) { 608 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 609 dirty = *map++; 610 else 611 dirty >>= 1; 612 if ((dirty & 1) == 0) 613 continue; 614 /* 615 * Skip directory inodes deleted and maybe reallocated 616 */ 617 dp = getino(ino); 618 if ((DIP(dp, mode) & IFMT) != IFDIR) 619 continue; 620 (void)dumpino(dp, ino); 621 } 622 623 msg("dumping (Pass IV) [regular files]\n"); 624 for (map = dumpinomap, ino = 1; ino < maxino; ino++) { 625 int mode; 626 627 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 628 dirty = *map++; 629 else 630 dirty >>= 1; 631 if ((dirty & 1) == 0) 632 continue; 633 /* 634 * Skip inodes deleted and reallocated as directories. 635 */ 636 dp = getino(ino); 637 mode = DIP(dp, mode) & IFMT; 638 if (mode == IFDIR) 639 continue; 640 (void)dumpino(dp, ino); 641 } 642 643 spcl.c_type = iswap32(TS_END); 644 for (i = 0; i < ntrec; i++) 645 writeheader(maxino - 1); 646 if (pipeout) 647 msg("%d tape blocks\n",iswap32(spcl.c_tapea)); 648 else 649 msg("%d tape blocks on %d volume%s\n", 650 iswap32(spcl.c_tapea), iswap32(spcl.c_volume), 651 (iswap32(spcl.c_volume) == 1) ? "" : "s"); 652 tnow = do_stats(); 653 date = iswap32(spcl.c_date); 654 msg("Date of this level %c dump: %s", level, 655 spcl.c_date == 0 ? "the epoch\n" : ctime(&date)); 656 msg("Date this dump completed: %s", ctime(&tnow)); 657 msg("Average transfer rate: %d KB/s\n", xferrate / tapeno); 658 putdumptime(); 659 trewind(0); 660 broadcast("DUMP IS DONE!\a\a\n"); 661 #ifdef DUMP_LFS 662 lfs_wrap_go(); 663 #endif /* DUMP_LFS */ 664 msg("DUMP IS DONE\n"); 665 Exit(X_FINOK); 666 /* NOTREACHED */ 667 exit(X_FINOK); /* XXX: to satisfy gcc */ 668 } 669 670 static void 671 usage(void) 672 { 673 const char *prog = getprogname(); 674 675 (void)fprintf(stderr, 676 "usage: %s [-0123456789aceFinStuX] [-B records] [-b blocksize]\n" 677 " [-d density] [-f file] [-h level] [-k read-blocksize]\n" 678 " [-L label] [-l timeout] [-r cachesize] [-s feet]\n" 679 " [-T date] [-x snap-backup] files-to-dump\n" 680 " %s [-W | -w]\n", prog, prog); 681 exit(X_STARTUP); 682 } 683 684 /* 685 * Pick up a numeric argument. It must be nonnegative and in the given 686 * range (except that a vmax of 0 means unlimited). 687 */ 688 static long 689 numarg(const char *meaning, long vmin, long vmax) 690 { 691 char *p; 692 long val; 693 694 val = strtol(optarg, &p, 10); 695 if (*p) 696 errx(X_STARTUP, "illegal %s -- %s", meaning, optarg); 697 if (val < vmin || (vmax && val > vmax)) 698 errx(X_STARTUP, "%s must be between %ld and %ld", 699 meaning, vmin, vmax); 700 return (val); 701 } 702 703 void 704 sig(int signo) 705 { 706 707 switch(signo) { 708 case SIGALRM: 709 case SIGBUS: 710 case SIGFPE: 711 case SIGHUP: 712 case SIGTERM: 713 case SIGTRAP: 714 if (pipeout) 715 quit("Signal on pipe: cannot recover\n"); 716 msg("Rewriting attempted as response to signal %s.\n", sys_siglist[signo]); 717 (void)fflush(stderr); 718 (void)fflush(stdout); 719 close_rewind(); 720 exit(X_REWRITE); 721 /* NOTREACHED */ 722 case SIGSEGV: 723 msg("SIGSEGV: ABORTING!\n"); 724 (void)signal(SIGSEGV, SIG_DFL); 725 (void)kill(0, SIGSEGV); 726 /* NOTREACHED */ 727 } 728 } 729 730 /* 731 * obsolete -- 732 * Change set of key letters and ordered arguments into something 733 * getopt(3) will like. 734 */ 735 static void 736 obsolete(int *argcp, char **argvp[]) 737 { 738 int argc, flags; 739 char *ap, **argv, *flagsp, **nargv, *p; 740 741 /* Setup. */ 742 argv = *argvp; 743 argc = *argcp; 744 745 /* Return if no arguments or first argument has leading dash. */ 746 ap = argv[1]; 747 if (argc == 1 || *ap == '-') 748 return; 749 750 /* Allocate space for new arguments. */ 751 *argvp = nargv = xmalloc((argc + 1) * sizeof(char *)); 752 p = flagsp = xmalloc(strlen(ap) + 2); 753 754 *nargv++ = *argv; 755 argv += 2; 756 757 for (flags = 0; *ap; ++ap) { 758 switch (*ap) { 759 case 'B': 760 case 'b': 761 case 'd': 762 case 'f': 763 case 'h': 764 case 's': 765 case 'T': 766 case 'x': 767 if (*argv == NULL) { 768 warnx("option requires an argument -- %c", *ap); 769 usage(); 770 } 771 nargv[0] = xmalloc(strlen(*argv) + 2 + 1); 772 nargv[0][0] = '-'; 773 nargv[0][1] = *ap; 774 (void)strcpy(&nargv[0][2], *argv); /* XXX safe strcpy */ 775 ++argv; 776 ++nargv; 777 break; 778 default: 779 if (!flags) { 780 *p++ = '-'; 781 flags = 1; 782 } 783 *p++ = *ap; 784 break; 785 } 786 } 787 788 /* Terminate flags. */ 789 if (flags) { 790 *p = '\0'; 791 *nargv++ = flagsp; 792 } else 793 free(flagsp); 794 795 /* Copy remaining arguments. */ 796 while ((*nargv++ = *argv++) != NULL) 797 ; 798 799 /* Update argument count. */ 800 *argcp = nargv - *argvp - 1; 801 } 802 803 804 void * 805 xcalloc(size_t number, size_t size) 806 { 807 void *p; 808 809 p = calloc(number, size); 810 if (p == NULL) 811 quit("%s\n", strerror(errno)); 812 return (p); 813 } 814 815 void * 816 xmalloc(size_t size) 817 { 818 void *p; 819 820 p = malloc(size); 821 if (p == NULL) 822 quit("%s\n", strerror(errno)); 823 return (p); 824 } 825 826 char * 827 xstrdup(const char *str) 828 { 829 char *p; 830 831 p = strdup(str); 832 if (p == NULL) 833 quit("%s\n", strerror(errno)); 834 return (p); 835 } 836