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