1 /* $NetBSD: main.c,v 1.8 1996/03/15 22:39:32 scottr 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 static char copyright[] = 38 "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 4/15/94"; 45 #else 46 static char rcsid[] = "$NetBSD: main.c,v 1.8 1996/03/15 22:39:32 scottr Exp $"; 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/param.h> 51 #include <sys/time.h> 52 #ifdef sunos 53 #include <sys/vnode.h> 54 55 #include <ufs/inode.h> 56 #include <ufs/fs.h> 57 #else 58 #include <ufs/ffs/fs.h> 59 #include <ufs/ufs/dinode.h> 60 #endif 61 62 #include <protocols/dumprestore.h> 63 64 #include <ctype.h> 65 #include <err.h> 66 #include <errno.h> 67 #include <fcntl.h> 68 #include <fstab.h> 69 #include <signal.h> 70 #include <stdio.h> 71 #include <stdlib.h> 72 #include <string.h> 73 #include <unistd.h> 74 75 #include "dump.h" 76 #include "pathnames.h" 77 78 #ifndef SBOFF 79 #define SBOFF (SBLOCK * DEV_BSIZE) 80 #endif 81 82 int notify = 0; /* notify operator flag */ 83 int blockswritten = 0; /* number of blocks written on current tape */ 84 int tapeno = 0; /* current tape number */ 85 int density = 0; /* density in bytes/0.1" */ 86 int ntrec = NTREC; /* # tape blocks in each tape record */ 87 int cartridge = 0; /* Assume non-cartridge tape */ 88 long dev_bsize = 1; /* recalculated below */ 89 long blocksperfile; /* output blocks per file */ 90 char *host = NULL; /* remote host (if any) */ 91 uid_t uid; /* real uid */ 92 uid_t euid; /* effective uid */ 93 94 static long numarg __P((char *, long, long)); 95 static void obsolete __P((int *, char **[])); 96 static void usage __P((void)); 97 98 int 99 main(argc, argv) 100 int argc; 101 char *argv[]; 102 { 103 register ino_t ino; 104 register int dirty; 105 register struct dinode *dp; 106 register struct fstab *dt; 107 register char *map; 108 register int ch; 109 int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1; 110 ino_t maxino; 111 112 uid = getuid(); 113 euid = geteuid(); 114 (void) seteuid(uid); 115 116 spcl.c_date = 0; 117 (void)time((time_t *)&spcl.c_date); 118 119 tsize = 0; /* Default later, based on 'c' option for cart tapes */ 120 if ((tape = getenv("TAPE")) == NULL) 121 tape = _PATH_DEFTAPE; 122 dumpdates = _PATH_DUMPDATES; 123 temp = _PATH_DTMP; 124 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) 125 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n"); 126 level = '0'; 127 128 if (argc < 2) 129 usage(); 130 131 obsolete(&argc, &argv); 132 while ((ch = getopt(argc, argv, "0123456789B:b:cd:f:h:ns:T:uWw")) != -1) 133 switch (ch) { 134 /* dump level */ 135 case '0': case '1': case '2': case '3': case '4': 136 case '5': case '6': case '7': case '8': case '9': 137 level = ch; 138 break; 139 140 case 'B': /* blocks per output file */ 141 blocksperfile = numarg("blocks per file", 1L, 0L); 142 break; 143 144 case 'b': /* blocks per tape write */ 145 ntrec = numarg("blocks per write", 1L, 1000L); 146 break; 147 148 case 'c': /* Tape is cart. not 9-track */ 149 cartridge = 1; 150 break; 151 152 case 'd': /* density, in bits per inch */ 153 density = numarg("density", 10L, 327670L) / 10; 154 if (density >= 625 && !bflag) 155 ntrec = HIGHDENSITYTREC; 156 break; 157 158 case 'f': /* output file */ 159 tape = optarg; 160 break; 161 162 case 'h': 163 honorlevel = numarg("honor level", 0L, 10L); 164 break; 165 166 case 'n': /* notify operators */ 167 notify = 1; 168 break; 169 170 case 's': /* tape size, feet */ 171 tsize = numarg("tape size", 1L, 0L) * 12 * 10; 172 break; 173 174 case 'T': /* time of last dump */ 175 spcl.c_ddate = unctime(optarg); 176 if (spcl.c_ddate < 0) { 177 (void)fprintf(stderr, "bad time \"%s\"\n", 178 optarg); 179 exit(X_ABORT); 180 } 181 Tflag = 1; 182 lastlevel = '?'; 183 break; 184 185 case 'u': /* update /etc/dumpdates */ 186 uflag = 1; 187 break; 188 189 case 'W': /* what to do */ 190 case 'w': 191 lastdump(ch); 192 exit(0); /* do nothing else */ 193 194 default: 195 usage(); 196 } 197 argc -= optind; 198 argv += optind; 199 200 if (argc < 1) { 201 (void)fprintf(stderr, "Must specify disk or filesystem\n"); 202 exit(X_ABORT); 203 } 204 disk = *argv++; 205 argc--; 206 if (argc >= 1) { 207 (void)fprintf(stderr, "Unknown arguments to dump:"); 208 while (argc--) 209 (void)fprintf(stderr, " %s", *argv++); 210 (void)fprintf(stderr, "\n"); 211 exit(X_ABORT); 212 } 213 if (Tflag && uflag) { 214 (void)fprintf(stderr, 215 "You cannot use the T and u flags together.\n"); 216 exit(X_ABORT); 217 } 218 if (strcmp(tape, "-") == 0) { 219 pipeout++; 220 tape = "standard output"; 221 } 222 223 if (blocksperfile) 224 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */ 225 else { 226 /* 227 * Determine how to default tape size and density 228 * 229 * density tape size 230 * 9-track 1600 bpi (160 bytes/.1") 2300 ft. 231 * 9-track 6250 bpi (625 bytes/.1") 2300 ft. 232 * cartridge 8000 bpi (100 bytes/.1") 1700 ft. 233 * (450*4 - slop) 234 */ 235 if (density == 0) 236 density = cartridge ? 100 : 160; 237 if (tsize == 0) 238 tsize = cartridge ? 1700L*120L : 2300L*120L; 239 } 240 241 if (strchr(tape, ':')) { 242 host = tape; 243 tape = strchr(host, ':'); 244 *tape++ = '\0'; 245 #ifdef RDUMP 246 if (rmthost(host) == 0) 247 exit(X_ABORT); 248 #else 249 (void)fprintf(stderr, "remote dump not enabled\n"); 250 exit(X_ABORT); 251 #endif 252 } 253 (void) setuid(uid); /* rmthost() is the only reason to be setuid */ 254 255 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 256 signal(SIGHUP, sig); 257 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN) 258 signal(SIGTRAP, sig); 259 if (signal(SIGFPE, SIG_IGN) != SIG_IGN) 260 signal(SIGFPE, sig); 261 if (signal(SIGBUS, SIG_IGN) != SIG_IGN) 262 signal(SIGBUS, sig); 263 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN) 264 signal(SIGSEGV, sig); 265 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 266 signal(SIGTERM, sig); 267 if (signal(SIGINT, interrupt) == SIG_IGN) 268 signal(SIGINT, SIG_IGN); 269 270 set_operators(); /* /etc/group snarfed */ 271 getfstab(); /* /etc/fstab snarfed */ 272 /* 273 * disk can be either the full special file name, 274 * the suffix of the special file name, 275 * the special name missing the leading '/', 276 * the file system name with or without the leading '/'. 277 */ 278 dt = fstabsearch(disk); 279 if (dt != NULL) { 280 disk = rawname(dt->fs_spec); 281 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN); 282 (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN); 283 } else { 284 (void)strncpy(spcl.c_dev, disk, NAMELEN); 285 (void)strncpy(spcl.c_filesys, "an unlisted file system", 286 NAMELEN); 287 } 288 (void)strcpy(spcl.c_label, "none"); 289 (void)gethostname(spcl.c_host, NAMELEN); 290 spcl.c_level = level - '0'; 291 spcl.c_type = TS_TAPE; 292 if (!Tflag) 293 getdumptime(); /* /etc/dumpdates snarfed */ 294 295 msg("Date of this level %c dump: %s", level, 296 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); 297 msg("Date of last level %c dump: %s", lastlevel, 298 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate)); 299 msg("Dumping %s ", disk); 300 if (dt != NULL) 301 msgtail("(%s) ", dt->fs_file); 302 if (host) 303 msgtail("to %s on host %s\n", tape, host); 304 else 305 msgtail("to %s\n", tape); 306 307 if ((diskfd = open(disk, O_RDONLY)) < 0) { 308 msg("Cannot open %s\n", disk); 309 exit(X_ABORT); 310 } 311 sync(); 312 sblock = (struct fs *)sblock_buf; 313 bread(SBOFF, (char *) sblock, SBSIZE); 314 if (sblock->fs_magic != FS_MAGIC) 315 quit("bad sblock magic number\n"); 316 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1); 317 dev_bshift = ffs(dev_bsize) - 1; 318 if (dev_bsize != (1 << dev_bshift)) 319 quit("dev_bsize (%d) is not a power of 2", dev_bsize); 320 tp_bshift = ffs(TP_BSIZE) - 1; 321 if (TP_BSIZE != (1 << tp_bshift)) 322 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE); 323 #ifdef FS_44INODEFMT 324 if (sblock->fs_inodefmt >= FS_44INODEFMT) 325 spcl.c_flags |= DR_NEWINODEFMT; 326 #endif 327 maxino = sblock->fs_ipg * sblock->fs_ncg; 328 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE); 329 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); 330 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char)); 331 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); 332 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 333 334 nonodump = spcl.c_level < honorlevel; 335 336 msg("mapping (Pass I) [regular files]\n"); 337 anydirskipped = mapfiles(maxino, &tapesize); 338 339 msg("mapping (Pass II) [directories]\n"); 340 while (anydirskipped) { 341 anydirskipped = mapdirs(maxino, &tapesize); 342 } 343 344 if (pipeout) { 345 tapesize += 10; /* 10 trailer blocks */ 346 msg("estimated %ld tape blocks.\n", tapesize); 347 } else { 348 double fetapes; 349 350 if (blocksperfile) 351 fetapes = (double) tapesize / blocksperfile; 352 else if (cartridge) { 353 /* Estimate number of tapes, assuming streaming stops at 354 the end of each block written, and not in mid-block. 355 Assume no erroneous blocks; this can be compensated 356 for with an artificially low tape size. */ 357 fetapes = 358 ( tapesize /* blocks */ 359 * TP_BSIZE /* bytes/block */ 360 * (1.0/density) /* 0.1" / byte */ 361 + 362 tapesize /* blocks */ 363 * (1.0/ntrec) /* streaming-stops per block */ 364 * 15.48 /* 0.1" / streaming-stop */ 365 ) * (1.0 / tsize ); /* tape / 0.1" */ 366 } else { 367 /* Estimate number of tapes, for old fashioned 9-track 368 tape */ 369 int tenthsperirg = (density == 625) ? 3 : 7; 370 fetapes = 371 ( tapesize /* blocks */ 372 * TP_BSIZE /* bytes / block */ 373 * (1.0/density) /* 0.1" / byte */ 374 + 375 tapesize /* blocks */ 376 * (1.0/ntrec) /* IRG's / block */ 377 * tenthsperirg /* 0.1" / IRG */ 378 ) * (1.0 / tsize ); /* tape / 0.1" */ 379 } 380 etapes = fetapes; /* truncating assignment */ 381 etapes++; 382 /* count the dumped inodes map on each additional tape */ 383 tapesize += (etapes - 1) * 384 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 385 tapesize += etapes + 10; /* headers + 10 trailer blks */ 386 msg("estimated %ld tape blocks on %3.2f tape(s).\n", 387 tapesize, fetapes); 388 } 389 390 /* 391 * Allocate tape buffer. 392 */ 393 if (!alloctape()) 394 quit("can't allocate tape buffers - try a smaller blocking factor.\n"); 395 396 startnewtape(1); 397 (void)time((time_t *)&(tstart_writing)); 398 dumpmap(usedinomap, TS_CLRI, maxino - 1); 399 400 msg("dumping (Pass III) [directories]\n"); 401 dirty = 0; /* XXX just to get gcc to shut up */ 402 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) { 403 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 404 dirty = *map++; 405 else 406 dirty >>= 1; 407 if ((dirty & 1) == 0) 408 continue; 409 /* 410 * Skip directory inodes deleted and maybe reallocated 411 */ 412 dp = getino(ino); 413 if ((dp->di_mode & IFMT) != IFDIR) 414 continue; 415 (void)dumpino(dp, ino); 416 } 417 418 msg("dumping (Pass IV) [regular files]\n"); 419 for (map = dumpinomap, ino = 1; ino < maxino; ino++) { 420 int mode; 421 422 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 423 dirty = *map++; 424 else 425 dirty >>= 1; 426 if ((dirty & 1) == 0) 427 continue; 428 /* 429 * Skip inodes deleted and reallocated as directories. 430 */ 431 dp = getino(ino); 432 mode = dp->di_mode & IFMT; 433 if (mode == IFDIR) 434 continue; 435 (void)dumpino(dp, ino); 436 } 437 438 spcl.c_type = TS_END; 439 for (i = 0; i < ntrec; i++) 440 writeheader(maxino - 1); 441 if (pipeout) 442 msg("DUMP: %ld tape blocks\n",spcl.c_tapea); 443 else 444 msg("DUMP: %ld tape blocks on %d volumes(s)\n", 445 spcl.c_tapea, spcl.c_volume); 446 putdumptime(); 447 trewind(); 448 broadcast("DUMP IS DONE!\7\7\n"); 449 msg("DUMP IS DONE\n"); 450 Exit(X_FINOK); 451 /* NOTREACHED */ 452 } 453 454 static void 455 usage() 456 { 457 458 (void)fprintf(stderr, "usage: dump [-0123456789cnu] [-B records] [-b blocksize] [-d density] [-f file]\n [-h level] [-s feet] [-T date] filesystem\n"); 459 (void)fprintf(stderr, " dump [-W | -w]\n"); 460 exit(1); 461 } 462 463 /* 464 * Pick up a numeric argument. It must be nonnegative and in the given 465 * range (except that a vmax of 0 means unlimited). 466 */ 467 static long 468 numarg(meaning, vmin, vmax) 469 char *meaning; 470 long vmin, vmax; 471 { 472 char *p; 473 long val; 474 475 val = strtol(optarg, &p, 10); 476 if (*p) 477 errx(1, "illegal %s -- %s", meaning, optarg); 478 if (val < vmin || (vmax && val > vmax)) 479 errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax); 480 return (val); 481 } 482 483 void 484 sig(signo) 485 int signo; 486 { 487 switch(signo) { 488 case SIGALRM: 489 case SIGBUS: 490 case SIGFPE: 491 case SIGHUP: 492 case SIGTERM: 493 case SIGTRAP: 494 if (pipeout) 495 quit("Signal on pipe: cannot recover\n"); 496 msg("Rewriting attempted as response to unknown signal.\n"); 497 (void)fflush(stderr); 498 (void)fflush(stdout); 499 close_rewind(); 500 exit(X_REWRITE); 501 /* NOTREACHED */ 502 case SIGSEGV: 503 msg("SIGSEGV: ABORTING!\n"); 504 (void)signal(SIGSEGV, SIG_DFL); 505 (void)kill(0, SIGSEGV); 506 /* NOTREACHED */ 507 } 508 } 509 510 char * 511 rawname(cp) 512 char *cp; 513 { 514 static char rawbuf[MAXPATHLEN]; 515 char *dp = strrchr(cp, '/'); 516 517 if (dp == NULL) 518 return (NULL); 519 *dp = '\0'; 520 (void)strcpy(rawbuf, cp); 521 *dp = '/'; 522 (void)strcat(rawbuf, "/r"); 523 (void)strcat(rawbuf, dp + 1); 524 return (rawbuf); 525 } 526 527 /* 528 * obsolete -- 529 * Change set of key letters and ordered arguments into something 530 * getopt(3) will like. 531 */ 532 static void 533 obsolete(argcp, argvp) 534 int *argcp; 535 char **argvp[]; 536 { 537 int argc, flags; 538 char *ap, **argv, *flagsp, **nargv, *p; 539 540 /* Setup. */ 541 argv = *argvp; 542 argc = *argcp; 543 544 /* Return if no arguments or first argument has leading dash. */ 545 ap = argv[1]; 546 if (argc == 1 || *ap == '-') 547 return; 548 549 /* Allocate space for new arguments. */ 550 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL || 551 (p = flagsp = malloc(strlen(ap) + 2)) == NULL) 552 err(1, NULL); 553 554 *nargv++ = *argv; 555 argv += 2; 556 557 for (flags = 0; *ap; ++ap) { 558 switch (*ap) { 559 case 'B': 560 case 'b': 561 case 'd': 562 case 'f': 563 case 'h': 564 case 's': 565 case 'T': 566 if (*argv == NULL) { 567 warnx("option requires an argument -- %c", *ap); 568 usage(); 569 } 570 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL) 571 err(1, NULL); 572 nargv[0][0] = '-'; 573 nargv[0][1] = *ap; 574 (void)strcpy(&nargv[0][2], *argv); 575 ++argv; 576 ++nargv; 577 break; 578 default: 579 if (!flags) { 580 *p++ = '-'; 581 flags = 1; 582 } 583 *p++ = *ap; 584 break; 585 } 586 } 587 588 /* Terminate flags. */ 589 if (flags) { 590 *p = '\0'; 591 *nargv++ = flagsp; 592 } 593 594 /* Copy remaining arguments. */ 595 while (*nargv++ = *argv++); 596 597 /* Update argument count. */ 598 *argcp = nargv - *argvp - 1; 599 } 600