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