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