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