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