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