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