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