1 /*- 2 * Copyright (c) 1980, 1991, 1993, 1994 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 * 4. 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. 16 * 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 27 * SUCH DAMAGE. 28 */ 29 30 #ifndef lint 31 static const char copyright[] = 32 "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\ 33 The Regents of the University of California. All rights reserved.\n"; 34 #endif /* not lint */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/1/95"; 39 #endif 40 static const char rcsid[] = 41 "$FreeBSD$"; 42 #endif /* not lint */ 43 44 #include <sys/param.h> 45 #include <sys/stat.h> 46 #include <sys/mount.h> 47 #include <sys/disklabel.h> 48 49 #include <ufs/ufs/dinode.h> 50 #include <ufs/ufs/ufsmount.h> 51 #include <ufs/ffs/fs.h> 52 53 #include <protocols/dumprestore.h> 54 55 #include <ctype.h> 56 #include <err.h> 57 #include <errno.h> 58 #include <fcntl.h> 59 #include <fstab.h> 60 #include <limits.h> 61 #include <signal.h> 62 #include <stdint.h> 63 #include <stdio.h> 64 #include <stdlib.h> 65 #include <string.h> 66 #include <time.h> 67 #include <timeconv.h> 68 #include <unistd.h> 69 70 #include "dump.h" 71 #include "pathnames.h" 72 73 int notify = 0; /* notify operator flag */ 74 int snapdump = 0; /* dumping live filesystem, so use snapshot */ 75 int blockswritten = 0; /* number of blocks written on current tape */ 76 int tapeno = 0; /* current tape number */ 77 int density = 0; /* density in bytes/0.1" " <- this is for hilit19 */ 78 int ntrec = NTREC; /* # tape blocks in each tape record */ 79 int cartridge = 0; /* Assume non-cartridge tape */ 80 int cachesize = 0; /* block cache size (in bytes), defaults to 0 */ 81 long dev_bsize = 1; /* recalculated below */ 82 long blocksperfile; /* output blocks per file */ 83 char *host = NULL; /* remote host (if any) */ 84 85 /* 86 * Possible superblock locations ordered from most to least likely. 87 */ 88 static int sblock_try[] = SBLOCKSEARCH; 89 90 static char *getmntpt(char *, int *); 91 static long numarg(const char *, long, long); 92 static void obsolete(int *, char **[]); 93 static void usage(void) __dead2; 94 95 int 96 main(int argc, char *argv[]) 97 { 98 struct stat sb; 99 ino_t ino; 100 int dirty; 101 union dinode *dp; 102 struct fstab *dt; 103 char *map, *mntpt; 104 int ch, mode, mntflags; 105 int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1; 106 int just_estimate = 0; 107 ino_t maxino; 108 char *tmsg; 109 110 spcl.c_date = _time_to_time64(time(NULL)); 111 112 tsize = 0; /* Default later, based on 'c' option for cart tapes */ 113 dumpdates = _PATH_DUMPDATES; 114 popenout = NULL; 115 tape = NULL; 116 temp = _PATH_DTMP; 117 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) 118 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n"); 119 level = 0; 120 rsync_friendly = 0; 121 122 if (argc < 2) 123 usage(); 124 125 obsolete(&argc, &argv); 126 while ((ch = getopt(argc, argv, 127 "0123456789aB:b:C:cD:d:f:h:LnP:RrSs:T:uWw")) != -1) 128 switch (ch) { 129 /* dump level */ 130 case '0': case '1': case '2': case '3': case '4': 131 case '5': case '6': case '7': case '8': case '9': 132 level = 10 * level + ch - '0'; 133 break; 134 135 case 'a': /* `auto-size', Write to EOM. */ 136 unlimited = 1; 137 break; 138 139 case 'B': /* blocks per output file */ 140 blocksperfile = numarg("number of blocks per file", 141 1L, 0L); 142 break; 143 144 case 'b': /* blocks per tape write */ 145 ntrec = numarg("number of blocks per write", 146 1L, 1000L); 147 break; 148 149 case 'C': 150 cachesize = numarg("cachesize", 0, 0) * 1024 * 1024; 151 break; 152 153 case 'c': /* Tape is cart. not 9-track */ 154 cartridge = 1; 155 break; 156 157 case 'D': 158 dumpdates = optarg; 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 'f': /* output file */ 168 if (popenout != NULL) 169 errx(X_STARTUP, "You cannot use the P and f " 170 "flags together.\n"); 171 tape = optarg; 172 break; 173 174 case 'h': 175 honorlevel = numarg("honor level", 0L, 10L); 176 break; 177 178 case 'L': 179 snapdump = 1; 180 break; 181 182 case 'n': /* notify operators */ 183 notify = 1; 184 break; 185 186 case 'P': 187 if (tape != NULL) 188 errx(X_STARTUP, "You cannot use the P and f " 189 "flags together.\n"); 190 popenout = optarg; 191 break; 192 193 case 'r': /* store slightly less data to be friendly to rsync */ 194 if (rsync_friendly < 1) 195 rsync_friendly = 1; 196 break; 197 198 case 'R': /* store even less data to be friendlier to rsync */ 199 if (rsync_friendly < 2) 200 rsync_friendly = 2; 201 break; 202 203 case 'S': /* exit after estimating # of tapes */ 204 just_estimate = 1; 205 break; 206 207 case 's': /* tape size, feet */ 208 tsize = numarg("tape size", 1L, 0L) * 12 * 10; 209 break; 210 211 case 'T': /* time of last dump */ 212 spcl.c_ddate = unctime(optarg); 213 if (spcl.c_ddate < 0) { 214 (void)fprintf(stderr, "bad time \"%s\"\n", 215 optarg); 216 exit(X_STARTUP); 217 } 218 Tflag = 1; 219 lastlevel = -1; 220 break; 221 222 case 'u': /* update /etc/dumpdates */ 223 uflag = 1; 224 break; 225 226 case 'W': /* what to do */ 227 case 'w': 228 lastdump(ch); 229 exit(X_FINOK); /* do nothing else */ 230 231 default: 232 usage(); 233 } 234 argc -= optind; 235 argv += optind; 236 237 if (argc < 1) { 238 (void)fprintf(stderr, "Must specify disk or file system\n"); 239 exit(X_STARTUP); 240 } 241 disk = *argv++; 242 argc--; 243 if (argc >= 1) { 244 (void)fprintf(stderr, "Unknown arguments to dump:"); 245 while (argc--) 246 (void)fprintf(stderr, " %s", *argv++); 247 (void)fprintf(stderr, "\n"); 248 exit(X_STARTUP); 249 } 250 if (rsync_friendly && (level > 0)) { 251 (void)fprintf(stderr, "%s %s\n", "rsync friendly options", 252 "can be used only with level 0 dumps."); 253 exit(X_STARTUP); 254 } 255 if (Tflag && uflag) { 256 (void)fprintf(stderr, 257 "You cannot use the T and u flags together.\n"); 258 exit(X_STARTUP); 259 } 260 if (popenout) { 261 tape = "child pipeline process"; 262 } else if (tape == NULL && (tape = getenv("TAPE")) == NULL) 263 tape = _PATH_DEFTAPE; 264 if (strcmp(tape, "-") == 0) { 265 pipeout++; 266 tape = "standard output"; 267 } 268 269 if (blocksperfile) 270 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */ 271 else if (!unlimited) { 272 /* 273 * Determine how to default tape size and density 274 * 275 * density tape size 276 * 9-track 1600 bpi (160 bytes/.1") 2300 ft. 277 * 9-track 6250 bpi (625 bytes/.1") 2300 ft. 278 * cartridge 8000 bpi (100 bytes/.1") 1700 ft. 279 * (450*4 - slop) 280 * hilit19 hits again: " 281 */ 282 if (density == 0) 283 density = cartridge ? 100 : 160; 284 if (tsize == 0) 285 tsize = cartridge ? 1700L*120L : 2300L*120L; 286 } 287 288 if (strchr(tape, ':')) { 289 host = tape; 290 tape = strchr(host, ':'); 291 *tape++ = '\0'; 292 #ifdef RDUMP 293 if (strchr(tape, '\n')) { 294 (void)fprintf(stderr, "invalid characters in tape\n"); 295 exit(X_STARTUP); 296 } 297 if (rmthost(host) == 0) 298 exit(X_STARTUP); 299 #else 300 (void)fprintf(stderr, "remote dump not enabled\n"); 301 exit(X_STARTUP); 302 #endif 303 } 304 (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */ 305 306 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 307 signal(SIGHUP, sig); 308 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN) 309 signal(SIGTRAP, sig); 310 if (signal(SIGFPE, SIG_IGN) != SIG_IGN) 311 signal(SIGFPE, sig); 312 if (signal(SIGBUS, SIG_IGN) != SIG_IGN) 313 signal(SIGBUS, sig); 314 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN) 315 signal(SIGSEGV, sig); 316 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 317 signal(SIGTERM, sig); 318 if (signal(SIGINT, interrupt) == SIG_IGN) 319 signal(SIGINT, SIG_IGN); 320 321 dump_getfstab(); /* /etc/fstab snarfed */ 322 /* 323 * disk can be either the full special file name, 324 * the suffix of the special file name, 325 * the special name missing the leading '/', 326 * the file system name with or without the leading '/'. 327 */ 328 dt = fstabsearch(disk); 329 if (dt != NULL) { 330 disk = rawname(dt->fs_spec); 331 if (disk == NULL) 332 errx(X_STARTUP, "%s: unknown file system", dt->fs_spec); 333 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN); 334 (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN); 335 } else { 336 (void)strncpy(spcl.c_dev, disk, NAMELEN); 337 (void)strncpy(spcl.c_filesys, "an unlisted file system", 338 NAMELEN); 339 } 340 spcl.c_dev[NAMELEN-1]='\0'; 341 spcl.c_filesys[NAMELEN-1]='\0'; 342 343 if ((mntpt = getmntpt(disk, &mntflags)) != 0) { 344 if (mntflags & MNT_RDONLY) { 345 if (snapdump != 0) { 346 msg("WARNING: %s\n", 347 "-L ignored for read-only filesystem."); 348 snapdump = 0; 349 } 350 } else if (snapdump == 0) { 351 msg("WARNING: %s\n", 352 "should use -L when dumping live read-write " 353 "filesystems!"); 354 } else { 355 char snapname[BUFSIZ], snapcmd[BUFSIZ]; 356 357 snprintf(snapname, sizeof snapname, "%s/.snap", mntpt); 358 if ((stat(snapname, &sb) < 0) || !S_ISDIR(sb.st_mode)) { 359 msg("WARNING: %s %s\n", 360 "-L requested but snapshot location", 361 snapname); 362 msg(" %s: %s\n", 363 "is not a directory", 364 "dump downgraded, -L ignored"); 365 snapdump = 0; 366 } else { 367 snprintf(snapname, sizeof snapname, 368 "%s/.snap/dump_snapshot", mntpt); 369 snprintf(snapcmd, sizeof snapcmd, "%s %s %s", 370 _PATH_MKSNAP_FFS, mntpt, snapname); 371 unlink(snapname); 372 if (system(snapcmd) != 0) 373 errx(X_STARTUP, "Cannot create %s: %s\n", 374 snapname, strerror(errno)); 375 if ((diskfd = open(snapname, O_RDONLY)) < 0) { 376 unlink(snapname); 377 errx(X_STARTUP, "Cannot open %s: %s\n", 378 snapname, strerror(errno)); 379 } 380 unlink(snapname); 381 if (fstat(diskfd, &sb) != 0) 382 err(X_STARTUP, "%s: stat", snapname); 383 spcl.c_date = _time_to_time64(sb.st_mtime); 384 } 385 } 386 } else if (snapdump != 0) { 387 msg("WARNING: Cannot use -L on an unmounted filesystem.\n"); 388 snapdump = 0; 389 } 390 if (snapdump == 0) { 391 if ((diskfd = open(disk, O_RDONLY)) < 0) 392 err(X_STARTUP, "Cannot open %s", disk); 393 if (fstat(diskfd, &sb) != 0) 394 err(X_STARTUP, "%s: stat", disk); 395 if (S_ISDIR(sb.st_mode)) 396 errx(X_STARTUP, "%s: unknown file system", disk); 397 } 398 399 (void)strcpy(spcl.c_label, "none"); 400 (void)gethostname(spcl.c_host, NAMELEN); 401 spcl.c_level = level; 402 spcl.c_type = TS_TAPE; 403 if (rsync_friendly) { 404 /* don't store real dump times */ 405 spcl.c_date = 0; 406 spcl.c_ddate = 0; 407 } 408 if (spcl.c_date == 0) { 409 tmsg = "the epoch\n"; 410 } else { 411 time_t t = _time64_to_time(spcl.c_date); 412 tmsg = ctime(&t); 413 } 414 msg("Date of this level %d dump: %s", level, tmsg); 415 416 if (!Tflag && (!rsync_friendly)) 417 getdumptime(); /* /etc/dumpdates snarfed */ 418 if (spcl.c_ddate == 0) { 419 tmsg = "the epoch\n"; 420 } else { 421 time_t t = _time64_to_time(spcl.c_ddate); 422 tmsg = ctime(&t); 423 } 424 if (lastlevel < 0) 425 msg("Date of last (level unknown) dump: %s", tmsg); 426 else 427 msg("Date of last level %d dump: %s", lastlevel, tmsg); 428 429 msg("Dumping %s%s ", snapdump ? "snapshot of ": "", disk); 430 if (dt != NULL) 431 msgtail("(%s) ", dt->fs_file); 432 if (host) 433 msgtail("to %s on host %s\n", tape, host); 434 else 435 msgtail("to %s\n", tape); 436 437 sync(); 438 sblock = (struct fs *)sblock_buf; 439 for (i = 0; sblock_try[i] != -1; i++) { 440 sblock->fs_fsize = SBLOCKSIZE; /* needed in bread */ 441 bread(sblock_try[i] >> dev_bshift, (char *) sblock, SBLOCKSIZE); 442 if ((sblock->fs_magic == FS_UFS1_MAGIC || 443 (sblock->fs_magic == FS_UFS2_MAGIC && 444 sblock->fs_sblockloc == sblock_try[i])) && 445 sblock->fs_bsize <= MAXBSIZE && 446 sblock->fs_bsize >= sizeof(struct fs)) 447 break; 448 } 449 if (sblock_try[i] == -1) 450 quit("Cannot find file system superblock\n"); 451 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1); 452 dev_bshift = ffs(dev_bsize) - 1; 453 if (dev_bsize != (1 << dev_bshift)) 454 quit("dev_bsize (%ld) is not a power of 2", dev_bsize); 455 tp_bshift = ffs(TP_BSIZE) - 1; 456 if (TP_BSIZE != (1 << tp_bshift)) 457 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE); 458 maxino = sblock->fs_ipg * sblock->fs_ncg; 459 mapsize = roundup(howmany(maxino, CHAR_BIT), TP_BSIZE); 460 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); 461 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char)); 462 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); 463 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 464 465 nonodump = spcl.c_level < honorlevel; 466 467 passno = 1; 468 setproctitle("%s: pass 1: regular files", disk); 469 msg("mapping (Pass I) [regular files]\n"); 470 anydirskipped = mapfiles(maxino, &tapesize); 471 472 passno = 2; 473 setproctitle("%s: pass 2: directories", disk); 474 msg("mapping (Pass II) [directories]\n"); 475 while (anydirskipped) { 476 anydirskipped = mapdirs(maxino, &tapesize); 477 } 478 479 if (pipeout || unlimited) { 480 tapesize += 10; /* 10 trailer blocks */ 481 msg("estimated %ld tape blocks.\n", tapesize); 482 } else { 483 double fetapes; 484 485 if (blocksperfile) 486 fetapes = (double) tapesize / blocksperfile; 487 else if (cartridge) { 488 /* Estimate number of tapes, assuming streaming stops at 489 the end of each block written, and not in mid-block. 490 Assume no erroneous blocks; this can be compensated 491 for with an artificially low tape size. */ 492 fetapes = 493 ( (double) tapesize /* blocks */ 494 * TP_BSIZE /* bytes/block */ 495 * (1.0/density) /* 0.1" / byte " */ 496 + 497 (double) tapesize /* blocks */ 498 * (1.0/ntrec) /* streaming-stops per block */ 499 * 15.48 /* 0.1" / streaming-stop " */ 500 ) * (1.0 / tsize ); /* tape / 0.1" " */ 501 } else { 502 /* Estimate number of tapes, for old fashioned 9-track 503 tape */ 504 int tenthsperirg = (density == 625) ? 3 : 7; 505 fetapes = 506 ( (double) tapesize /* blocks */ 507 * TP_BSIZE /* bytes / block */ 508 * (1.0/density) /* 0.1" / byte " */ 509 + 510 (double) tapesize /* blocks */ 511 * (1.0/ntrec) /* IRG's / block */ 512 * tenthsperirg /* 0.1" / IRG " */ 513 ) * (1.0 / tsize ); /* tape / 0.1" " */ 514 } 515 etapes = fetapes; /* truncating assignment */ 516 etapes++; 517 /* count the dumped inodes map on each additional tape */ 518 tapesize += (etapes - 1) * 519 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 520 tapesize += etapes + 10; /* headers + 10 trailer blks */ 521 msg("estimated %ld tape blocks on %3.2f tape(s).\n", 522 tapesize, fetapes); 523 } 524 525 /* 526 * If the user only wants an estimate of the number of 527 * tapes, exit now. 528 */ 529 if (just_estimate) 530 exit(0); 531 532 /* 533 * Allocate tape buffer. 534 */ 535 if (!alloctape()) 536 quit( 537 "can't allocate tape buffers - try a smaller blocking factor.\n"); 538 539 startnewtape(1); 540 (void)time((time_t *)&(tstart_writing)); 541 dumpmap(usedinomap, TS_CLRI, maxino - 1); 542 543 passno = 3; 544 setproctitle("%s: pass 3: directories", disk); 545 msg("dumping (Pass III) [directories]\n"); 546 dirty = 0; /* XXX just to get gcc to shut up */ 547 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) { 548 if (((ino - 1) % CHAR_BIT) == 0) /* map is offset by 1 */ 549 dirty = *map++; 550 else 551 dirty >>= 1; 552 if ((dirty & 1) == 0) 553 continue; 554 /* 555 * Skip directory inodes deleted and maybe reallocated 556 */ 557 dp = getino(ino, &mode); 558 if (mode != IFDIR) 559 continue; 560 (void)dumpino(dp, ino); 561 } 562 563 passno = 4; 564 setproctitle("%s: pass 4: regular files", disk); 565 msg("dumping (Pass IV) [regular files]\n"); 566 for (map = dumpinomap, ino = 1; ino < maxino; ino++) { 567 if (((ino - 1) % CHAR_BIT) == 0) /* map is offset by 1 */ 568 dirty = *map++; 569 else 570 dirty >>= 1; 571 if ((dirty & 1) == 0) 572 continue; 573 /* 574 * Skip inodes deleted and reallocated as directories. 575 */ 576 dp = getino(ino, &mode); 577 if (mode == IFDIR) 578 continue; 579 (void)dumpino(dp, ino); 580 } 581 582 (void)time((time_t *)&(tend_writing)); 583 spcl.c_type = TS_END; 584 for (i = 0; i < ntrec; i++) 585 writeheader(maxino - 1); 586 if (pipeout) 587 msg("DUMP: %jd tape blocks\n", (intmax_t)spcl.c_tapea); 588 else 589 msg("DUMP: %jd tape blocks on %d volume%s\n", 590 (intmax_t)spcl.c_tapea, spcl.c_volume, 591 (spcl.c_volume == 1) ? "" : "s"); 592 593 /* report dump performance, avoid division through zero */ 594 if (tend_writing - tstart_writing == 0) 595 msg("finished in less than a second\n"); 596 else 597 msg("finished in %jd seconds, throughput %jd KBytes/sec\n", 598 (intmax_t)tend_writing - tstart_writing, 599 (intmax_t)(spcl.c_tapea / 600 (tend_writing - tstart_writing))); 601 602 putdumptime(); 603 trewind(); 604 broadcast("DUMP IS DONE!\a\a\n"); 605 msg("DUMP IS DONE\n"); 606 Exit(X_FINOK); 607 /* NOTREACHED */ 608 } 609 610 static void 611 usage(void) 612 { 613 fprintf(stderr, 614 "usage: dump [-0123456789acLnSu] [-B records] [-b blocksize] [-C cachesize]\n" 615 " [-D dumpdates] [-d density] [-f file | -P pipecommand] [-h level]\n" 616 " [-s feet] [-T date] filesystem\n" 617 " dump -W | -w\n"); 618 exit(X_STARTUP); 619 } 620 621 /* 622 * Check to see if a disk is currently mounted. 623 */ 624 static char * 625 getmntpt(char *name, int *mntflagsp) 626 { 627 long mntsize, i; 628 struct statfs *mntbuf; 629 630 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 631 for (i = 0; i < mntsize; i++) { 632 if (!strcmp(mntbuf[i].f_mntfromname, name)) { 633 *mntflagsp = mntbuf[i].f_flags; 634 return (mntbuf[i].f_mntonname); 635 } 636 } 637 return (0); 638 } 639 640 /* 641 * Pick up a numeric argument. It must be nonnegative and in the given 642 * range (except that a vmax of 0 means unlimited). 643 */ 644 static long 645 numarg(const char *meaning, long vmin, long vmax) 646 { 647 char *p; 648 long val; 649 650 val = strtol(optarg, &p, 10); 651 if (*p) 652 errx(1, "illegal %s -- %s", meaning, optarg); 653 if (val < vmin || (vmax && val > vmax)) 654 errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax); 655 return (val); 656 } 657 658 void 659 sig(int signo) 660 { 661 switch(signo) { 662 case SIGALRM: 663 case SIGBUS: 664 case SIGFPE: 665 case SIGHUP: 666 case SIGTERM: 667 case SIGTRAP: 668 if (pipeout) 669 quit("Signal on pipe: cannot recover\n"); 670 msg("Rewriting attempted as response to unknown signal.\n"); 671 (void)fflush(stderr); 672 (void)fflush(stdout); 673 close_rewind(); 674 exit(X_REWRITE); 675 /* NOTREACHED */ 676 case SIGSEGV: 677 msg("SIGSEGV: ABORTING!\n"); 678 (void)signal(SIGSEGV, SIG_DFL); 679 (void)kill(0, SIGSEGV); 680 /* NOTREACHED */ 681 } 682 } 683 684 char * 685 rawname(char *cp) 686 { 687 struct stat sb; 688 689 /* 690 * Ensure that the device passed in is a raw device. 691 */ 692 if (stat(cp, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFCHR) 693 return (cp); 694 695 /* 696 * Since there's only one device type now, we can't construct any 697 * better name, so we have to return NULL. 698 */ 699 return (NULL); 700 } 701 702 /* 703 * obsolete -- 704 * Change set of key letters and ordered arguments into something 705 * getopt(3) will like. 706 */ 707 static void 708 obsolete(int *argcp, char **argvp[]) 709 { 710 int argc, flags; 711 char *ap, **argv, *flagsp, **nargv, *p; 712 713 /* Setup. */ 714 argv = *argvp; 715 argc = *argcp; 716 717 /* 718 * Return if no arguments or first argument has leading 719 * dash or slash. 720 */ 721 ap = argv[1]; 722 if (argc == 1 || *ap == '-' || *ap == '/') 723 return; 724 725 /* Allocate space for new arguments. */ 726 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL || 727 (p = flagsp = malloc(strlen(ap) + 2)) == NULL) 728 err(1, NULL); 729 730 *nargv++ = *argv; 731 argv += 2; 732 733 for (flags = 0; *ap; ++ap) { 734 switch (*ap) { 735 case 'B': 736 case 'b': 737 case 'd': 738 case 'f': 739 case 'D': 740 case 'C': 741 case 'h': 742 case 's': 743 case 'T': 744 if (*argv == NULL) { 745 warnx("option requires an argument -- %c", *ap); 746 usage(); 747 } 748 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL) 749 err(1, NULL); 750 nargv[0][0] = '-'; 751 nargv[0][1] = *ap; 752 (void)strcpy(&nargv[0][2], *argv); 753 ++argv; 754 ++nargv; 755 break; 756 default: 757 if (!flags) { 758 *p++ = '-'; 759 flags = 1; 760 } 761 *p++ = *ap; 762 break; 763 } 764 } 765 766 /* Terminate flags. */ 767 if (flags) { 768 *p = '\0'; 769 *nargv++ = flagsp; 770 } else 771 free(flagsp); 772 773 /* Copy remaining arguments. */ 774 while ((*nargv++ = *argv++)); 775 776 /* Update argument count. */ 777 *argcp = nargv - *argvp - 1; 778 } 779