Home | History | Annotate | Line # | Download | only in dd
dd.c revision 1.51
      1 /*	$NetBSD: dd.c,v 1.51 2016/09/05 01:00:07 sevan Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Keith Muller of the University of California, San Diego and Lance
      9  * Visser of Convex Computer Corporation.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. 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 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\
     39  The Regents of the University of California.  All rights reserved.");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)dd.c	8.5 (Berkeley) 4/2/94";
     45 #else
     46 __RCSID("$NetBSD: dd.c,v 1.51 2016/09/05 01:00:07 sevan Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/stat.h>
     52 #include <sys/ioctl.h>
     53 #include <sys/mtio.h>
     54 #include <sys/time.h>
     55 
     56 #include <ctype.h>
     57 #include <err.h>
     58 #include <errno.h>
     59 #include <fcntl.h>
     60 #include <locale.h>
     61 #include <signal.h>
     62 #include <stdio.h>
     63 #include <stdlib.h>
     64 #include <string.h>
     65 #include <unistd.h>
     66 
     67 #include "dd.h"
     68 #include "extern.h"
     69 
     70 static void dd_close(void);
     71 static void dd_in(void);
     72 static void getfdtype(IO *);
     73 static void redup_clean_fd(IO *);
     74 static void setup(void);
     75 
     76 IO		in, out;		/* input/output state */
     77 STAT		st;			/* statistics */
     78 void		(*cfunc)(void);		/* conversion function */
     79 uint64_t	cpy_cnt;		/* # of blocks to copy */
     80 static off_t	pending = 0;		/* pending seek if sparse */
     81 u_int		ddflags;		/* conversion options */
     82 #ifdef NO_IOFLAG
     83 #define		iflag	O_RDONLY
     84 #define		oflag	O_CREAT
     85 #else
     86 u_int		iflag = O_RDONLY;	/* open(2) flags for input file */
     87 u_int		oflag = O_CREAT;	/* open(2) flags for output file */
     88 #endif /* NO_IOFLAG */
     89 uint64_t	cbsz;			/* conversion block size */
     90 u_int		files_cnt = 1;		/* # of files to copy */
     91 uint64_t	progress = 0;		/* display sign of life */
     92 const u_char	*ctab;			/* conversion table */
     93 sigset_t	infoset;		/* a set blocking SIGINFO */
     94 const char	*msgfmt = "posix";	/* default summary() message format */
     95 
     96 /*
     97  * Ops for stdin/stdout and crunch'd dd.  These are always host ops.
     98  */
     99 static const struct ddfops ddfops_stdfd = {
    100 	.op_open = open,
    101 	.op_close = close,
    102 	.op_fcntl = fcntl,
    103 	.op_ioctl = ioctl,
    104 	.op_fstat = fstat,
    105 	.op_fsync = fsync,
    106 	.op_ftruncate = ftruncate,
    107 	.op_lseek = lseek,
    108 	.op_read = read,
    109 	.op_write = write,
    110 };
    111 extern const struct ddfops ddfops_prog;
    112 
    113 int
    114 main(int argc, char *argv[])
    115 {
    116 	int ch;
    117 
    118 	setprogname(argv[0]);
    119 	(void)setlocale(LC_ALL, "");
    120 
    121 	while ((ch = getopt(argc, argv, "")) != -1) {
    122 		switch (ch) {
    123 		default:
    124 			errx(EXIT_FAILURE, "usage: dd [operand ...]");
    125 			/* NOTREACHED */
    126 		}
    127 	}
    128 	argc -= (optind - 1);
    129 	argv += (optind - 1);
    130 
    131 	jcl(argv);
    132 #ifndef CRUNCHOPS
    133 	if (ddfops_prog.op_init && ddfops_prog.op_init() == -1)
    134 		err(1, "prog init");
    135 #endif
    136 	setup();
    137 
    138 	(void)signal(SIGINFO, summaryx);
    139 	(void)signal(SIGINT, terminate);
    140 	(void)sigemptyset(&infoset);
    141 	(void)sigaddset(&infoset, SIGINFO);
    142 
    143 	(void)atexit(summary);
    144 
    145 	while (files_cnt--)
    146 		dd_in();
    147 
    148 	dd_close();
    149 	exit(0);
    150 	/* NOTREACHED */
    151 }
    152 
    153 static void
    154 setup(void)
    155 {
    156 #ifdef CRUNCHOPS
    157 	const struct ddfops *prog_ops = &ddfops_stdfd;
    158 #else
    159 	const struct ddfops *prog_ops = &ddfops_prog;
    160 #endif
    161 
    162 	if (in.name == NULL) {
    163 		in.name = "stdin";
    164 		in.fd = STDIN_FILENO;
    165 		in.ops = &ddfops_stdfd;
    166 	} else {
    167 		in.ops = prog_ops;
    168 		in.fd = ddop_open(in, in.name, iflag, 0);
    169 		if (in.fd < 0)
    170 			err(EXIT_FAILURE, "%s", in.name);
    171 			/* NOTREACHED */
    172 
    173 		/* Ensure in.fd is outside the stdio descriptor range */
    174 		redup_clean_fd(&in);
    175 	}
    176 
    177 	getfdtype(&in);
    178 
    179 	if (files_cnt > 1 && !(in.flags & ISTAPE)) {
    180 		errx(EXIT_FAILURE, "files is not supported for non-tape devices");
    181 		/* NOTREACHED */
    182 	}
    183 
    184 	if (out.name == NULL) {
    185 		/* No way to check for read access here. */
    186 		out.fd = STDOUT_FILENO;
    187 		out.name = "stdout";
    188 		out.ops = &ddfops_stdfd;
    189 	} else {
    190 		out.ops = prog_ops;
    191 
    192 #ifndef NO_IOFLAG
    193 		if ((oflag & O_TRUNC) && (ddflags & C_SEEK)) {
    194 			errx(EXIT_FAILURE, "oflag=trunc is incompatible "
    195 			     "with seek or oseek operands, giving up.");
    196 			/* NOTREACHED */
    197 		}
    198 		if ((oflag & O_TRUNC) && (ddflags & C_NOTRUNC)) {
    199 			errx(EXIT_FAILURE, "oflag=trunc is incompatible "
    200 			     "with conv=notrunc operand, giving up.");
    201 			/* NOTREACHED */
    202 		}
    203 #endif /* NO_IOFLAG */
    204 #define	OFLAGS \
    205     (oflag | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
    206 		out.fd = ddop_open(out, out.name, O_RDWR | OFLAGS, DEFFILEMODE);
    207 		/*
    208 		 * May not have read access, so try again with write only.
    209 		 * Without read we may have a problem if output also does
    210 		 * not support seeks.
    211 		 */
    212 		if (out.fd < 0) {
    213 			out.fd = ddop_open(out, out.name, O_WRONLY | OFLAGS,
    214 			    DEFFILEMODE);
    215 			out.flags |= NOREAD;
    216 		}
    217 		if (out.fd < 0) {
    218 			err(EXIT_FAILURE, "%s", out.name);
    219 			/* NOTREACHED */
    220 		}
    221 
    222 		/* Ensure out.fd is outside the stdio descriptor range */
    223 		redup_clean_fd(&out);
    224 	}
    225 
    226 	getfdtype(&out);
    227 
    228 	/*
    229 	 * Allocate space for the input and output buffers.  If not doing
    230 	 * record oriented I/O, only need a single buffer.
    231 	 */
    232 	if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
    233 		size_t dbsz = out.dbsz;
    234 		if (!(ddflags & C_BS))
    235 			dbsz += in.dbsz - 1;
    236 		if ((in.db = malloc(dbsz)) == NULL) {
    237 			err(EXIT_FAILURE, NULL);
    238 			/* NOTREACHED */
    239 		}
    240 		out.db = in.db;
    241 	} else if ((in.db =
    242 	    malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL ||
    243 	    (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL) {
    244 		err(EXIT_FAILURE, NULL);
    245 		/* NOTREACHED */
    246 	}
    247 	in.dbp = in.db;
    248 	out.dbp = out.db;
    249 
    250 	/* Position the input/output streams. */
    251 	if (in.offset)
    252 		pos_in();
    253 	if (out.offset)
    254 		pos_out();
    255 
    256 	/*
    257 	 * Truncate the output file; ignore errors because it fails on some
    258 	 * kinds of output files, tapes, for example.
    259 	 */
    260 	if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK))
    261 		(void)ddop_ftruncate(out, out.fd, (off_t)out.offset * out.dbsz);
    262 
    263 	/*
    264 	 * If converting case at the same time as another conversion, build a
    265 	 * table that does both at once.  If just converting case, use the
    266 	 * built-in tables.
    267 	 */
    268 	if (ddflags & (C_LCASE|C_UCASE)) {
    269 #ifdef	NO_CONV
    270 		/* Should not get here, but just in case... */
    271 		errx(EXIT_FAILURE, "case conv and -DNO_CONV");
    272 		/* NOTREACHED */
    273 #else	/* NO_CONV */
    274 		u_int cnt;
    275 
    276 		if (ddflags & C_ASCII || ddflags & C_EBCDIC) {
    277 			if (ddflags & C_LCASE) {
    278 				for (cnt = 0; cnt < 256; ++cnt)
    279 					casetab[cnt] = tolower(ctab[cnt]);
    280 			} else {
    281 				for (cnt = 0; cnt < 256; ++cnt)
    282 					casetab[cnt] = toupper(ctab[cnt]);
    283 			}
    284 		} else {
    285 			if (ddflags & C_LCASE) {
    286 				for (cnt = 0; cnt < 256; ++cnt)
    287 					casetab[cnt] = tolower(cnt);
    288 			} else {
    289 				for (cnt = 0; cnt < 256; ++cnt)
    290 					casetab[cnt] = toupper(cnt);
    291 			}
    292 		}
    293 
    294 		ctab = casetab;
    295 #endif	/* NO_CONV */
    296 	}
    297 
    298 	(void)gettimeofday(&st.start, NULL);	/* Statistics timestamp. */
    299 }
    300 
    301 static void
    302 getfdtype(IO *io)
    303 {
    304 	struct mtget mt;
    305 	struct stat sb;
    306 
    307 	if (io->ops->op_fstat(io->fd, &sb)) {
    308 		err(EXIT_FAILURE, "%s", io->name);
    309 		/* NOTREACHED */
    310 	}
    311 	if (S_ISCHR(sb.st_mode))
    312 		io->flags |= io->ops->op_ioctl(io->fd, MTIOCGET, &mt)
    313 		    ? ISCHR : ISTAPE;
    314 	else if (io->ops->op_lseek(io->fd, (off_t)0, SEEK_CUR) == -1
    315 	    && errno == ESPIPE)
    316 		io->flags |= ISPIPE;		/* XXX fixed in 4.4BSD */
    317 }
    318 
    319 /*
    320  * Move the parameter file descriptor to a descriptor that is outside the
    321  * stdio descriptor range, if necessary.  This is required to avoid
    322  * accidentally outputting completion or error messages into the
    323  * output file that were intended for the tty.
    324  */
    325 static void
    326 redup_clean_fd(IO *io)
    327 {
    328 	int fd = io->fd;
    329 	int newfd;
    330 
    331 	if (fd != STDIN_FILENO && fd != STDOUT_FILENO &&
    332 	    fd != STDERR_FILENO)
    333 		/* File descriptor is ok, return immediately. */
    334 		return;
    335 
    336 	/*
    337 	 * 3 is the first descriptor greater than STD*_FILENO.  Any
    338 	 * free descriptor valued 3 or above is acceptable...
    339 	 */
    340 	newfd = io->ops->op_fcntl(fd, F_DUPFD, 3);
    341 	if (newfd < 0) {
    342 		err(EXIT_FAILURE, "dupfd IO");
    343 		/* NOTREACHED */
    344 	}
    345 
    346 	io->ops->op_close(fd);
    347 	io->fd = newfd;
    348 }
    349 
    350 static void
    351 dd_in(void)
    352 {
    353 	int flags;
    354 	int64_t n;
    355 
    356 	for (flags = ddflags;;) {
    357 		if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt)
    358 			return;
    359 
    360 		/*
    361 		 * Clear the buffer first if doing "sync" on input.
    362 		 * If doing block operations use spaces.  This will
    363 		 * affect not only the C_NOERROR case, but also the
    364 		 * last partial input block which should be padded
    365 		 * with zero and not garbage.
    366 		 */
    367 		if (flags & C_SYNC) {
    368 			if (flags & (C_BLOCK|C_UNBLOCK))
    369 				(void)memset(in.dbp, ' ', in.dbsz);
    370 			else
    371 				(void)memset(in.dbp, 0, in.dbsz);
    372 		}
    373 
    374 		n = ddop_read(in, in.fd, in.dbp, in.dbsz);
    375 		if (n == 0) {
    376 			in.dbrcnt = 0;
    377 			return;
    378 		}
    379 
    380 		/* Read error. */
    381 		if (n < 0) {
    382 
    383 			/*
    384 			 * If noerror not specified, die.  POSIX requires that
    385 			 * the warning message be followed by an I/O display.
    386 			 */
    387 			if (!(flags & C_NOERROR)) {
    388 				err(EXIT_FAILURE, "%s", in.name);
    389 				/* NOTREACHED */
    390 			}
    391 			warn("%s", in.name);
    392 			summary();
    393 
    394 			/*
    395 			 * If it's not a tape drive or a pipe, seek past the
    396 			 * error.  If your OS doesn't do the right thing for
    397 			 * raw disks this section should be modified to re-read
    398 			 * in sector size chunks.
    399 			 */
    400 			if (!(in.flags & (ISPIPE|ISTAPE)) &&
    401 			    ddop_lseek(in, in.fd, (off_t)in.dbsz, SEEK_CUR))
    402 				warn("%s", in.name);
    403 
    404 			/* If sync not specified, omit block and continue. */
    405 			if (!(ddflags & C_SYNC))
    406 				continue;
    407 
    408 			/* Read errors count as full blocks. */
    409 			in.dbcnt += in.dbrcnt = in.dbsz;
    410 			++st.in_full;
    411 
    412 		/* Handle full input blocks. */
    413 		} else if ((uint64_t)n == in.dbsz) {
    414 			in.dbcnt += in.dbrcnt = n;
    415 			++st.in_full;
    416 
    417 		/* Handle partial input blocks. */
    418 		} else {
    419 			/* If sync, use the entire block. */
    420 			if (ddflags & C_SYNC)
    421 				in.dbcnt += in.dbrcnt = in.dbsz;
    422 			else
    423 				in.dbcnt += in.dbrcnt = n;
    424 			++st.in_part;
    425 		}
    426 
    427 		/*
    428 		 * POSIX states that if bs is set and no other conversions
    429 		 * than noerror, notrunc or sync are specified, the block
    430 		 * is output without buffering as it is read.
    431 		 */
    432 		if (ddflags & C_BS) {
    433 			out.dbcnt = in.dbcnt;
    434 			dd_out(1);
    435 			in.dbcnt = 0;
    436 			continue;
    437 		}
    438 
    439 		if (ddflags & C_SWAB) {
    440 			if ((n = in.dbrcnt) & 1) {
    441 				++st.swab;
    442 				--n;
    443 			}
    444 			swab(in.dbp, in.dbp, n);
    445 		}
    446 
    447 		in.dbp += in.dbrcnt;
    448 		(*cfunc)();
    449 	}
    450 }
    451 
    452 /*
    453  * Cleanup any remaining I/O and flush output.  If necessary, output file
    454  * is truncated.
    455  */
    456 static void
    457 dd_close(void)
    458 {
    459 
    460 	if (cfunc == def)
    461 		def_close();
    462 	else if (cfunc == block)
    463 		block_close();
    464 	else if (cfunc == unblock)
    465 		unblock_close();
    466 	if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) {
    467 		(void)memset(out.dbp, 0, out.dbsz - out.dbcnt);
    468 		out.dbcnt = out.dbsz;
    469 	}
    470 	/* If there are pending sparse blocks, make sure
    471 	 * to write out the final block un-sparse
    472 	 */
    473 	if ((out.dbcnt == 0) && pending) {
    474 		memset(out.db, 0, out.dbsz);
    475 		out.dbcnt = out.dbsz;
    476 		out.dbp = out.db + out.dbcnt;
    477 		pending -= out.dbsz;
    478 	}
    479 	if (out.dbcnt)
    480 		dd_out(1);
    481 
    482 	/*
    483 	 * Reporting nfs write error may be deferred until next
    484 	 * write(2) or close(2) system call.  So, we need to do an
    485 	 * extra check.  If an output is stdout, the file structure
    486 	 * may be shared with other processes and close(2) just
    487 	 * decreases the reference count.
    488 	 */
    489 	if (out.fd == STDOUT_FILENO && ddop_fsync(out, out.fd) == -1
    490 	    && errno != EINVAL) {
    491 		err(EXIT_FAILURE, "fsync stdout");
    492 		/* NOTREACHED */
    493 	}
    494 	if (ddop_close(out, out.fd) == -1) {
    495 		err(EXIT_FAILURE, "close");
    496 		/* NOTREACHED */
    497 	}
    498 }
    499 
    500 void
    501 dd_out(int force)
    502 {
    503 	static int warned;
    504 	int64_t cnt, n, nw;
    505 	u_char *outp;
    506 
    507 	/*
    508 	 * Write one or more blocks out.  The common case is writing a full
    509 	 * output block in a single write; increment the full block stats.
    510 	 * Otherwise, we're into partial block writes.  If a partial write,
    511 	 * and it's a character device, just warn.  If a tape device, quit.
    512 	 *
    513 	 * The partial writes represent two cases.  1: Where the input block
    514 	 * was less than expected so the output block was less than expected.
    515 	 * 2: Where the input block was the right size but we were forced to
    516 	 * write the block in multiple chunks.  The original versions of dd(1)
    517 	 * never wrote a block in more than a single write, so the latter case
    518 	 * never happened.
    519 	 *
    520 	 * One special case is if we're forced to do the write -- in that case
    521 	 * we play games with the buffer size, and it's usually a partial write.
    522 	 */
    523 	outp = out.db;
    524 	for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
    525 		for (cnt = n;; cnt -= nw) {
    526 
    527 			if (!force && ddflags & C_SPARSE) {
    528 				int sparse, i;
    529 				sparse = 1;	/* Is buffer sparse? */
    530 				for (i = 0; i < cnt; i++)
    531 					if (outp[i] != 0) {
    532 						sparse = 0;
    533 						break;
    534 					}
    535 				if (sparse) {
    536 					pending += cnt;
    537 					outp += cnt;
    538 					nw = 0;
    539 					break;
    540 				}
    541 			}
    542 			if (pending != 0) {
    543 				if (ddop_lseek(out,
    544 				    out.fd, pending, SEEK_CUR) == -1)
    545 					err(EXIT_FAILURE, "%s: seek error creating sparse file",
    546 					    out.name);
    547 			}
    548 			nw = bwrite(&out, outp, cnt);
    549 			if (nw <= 0) {
    550 				if (nw == 0)
    551 					errx(EXIT_FAILURE,
    552 						"%s: end of device", out.name);
    553 					/* NOTREACHED */
    554 				if (errno != EINTR)
    555 					err(EXIT_FAILURE, "%s", out.name);
    556 					/* NOTREACHED */
    557 				nw = 0;
    558 			}
    559 			if (pending) {
    560 				st.bytes += pending;
    561 				st.sparse += pending/out.dbsz;
    562 				st.out_full += pending/out.dbsz;
    563 				pending = 0;
    564 			}
    565 			outp += nw;
    566 			st.bytes += nw;
    567 			if (nw == n) {
    568 				if ((uint64_t)n != out.dbsz)
    569 					++st.out_part;
    570 				else
    571 					++st.out_full;
    572 				break;
    573 			}
    574 			++st.out_part;
    575 			if (nw == cnt)
    576 				break;
    577 			if (out.flags & ISCHR && !warned) {
    578 				warned = 1;
    579 				warnx("%s: short write on character device", out.name);
    580 			}
    581 			if (out.flags & ISTAPE)
    582 				errx(EXIT_FAILURE,
    583 					"%s: short write on tape device", out.name);
    584 				/* NOTREACHED */
    585 
    586 		}
    587 		if ((out.dbcnt -= n) < out.dbsz)
    588 			break;
    589 	}
    590 
    591 	/* Reassemble the output block. */
    592 	if (out.dbcnt)
    593 		(void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
    594 	out.dbp = out.db + out.dbcnt;
    595 
    596 	if (progress && (st.out_full + st.out_part) % progress == 0)
    597 		(void)write(STDERR_FILENO, ".", 1);
    598 }
    599 
    600 /*
    601  * A protected against SIGINFO write
    602  */
    603 ssize_t
    604 bwrite(IO *io, const void *buf, size_t len)
    605 {
    606 	sigset_t oset;
    607 	ssize_t rv;
    608 	int oerrno;
    609 
    610 	(void)sigprocmask(SIG_BLOCK, &infoset, &oset);
    611 	rv = io->ops->op_write(io->fd, buf, len);
    612 	oerrno = errno;
    613 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
    614 	errno = oerrno;
    615 	return (rv);
    616 }
    617