xref: /dragonfly/bin/dd/dd.c (revision cb2f0d05)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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  * @(#)dd.c	8.5 (Berkeley) 4/2/94
36  * $FreeBSD: head/bin/dd/dd.c 341257 2018-11-29 19:28:01Z sobomax $
37  */
38 
39 #include <sys/param.h>
40 #include <sys/stat.h>
41 #ifndef BOOTSTRAPPING
42 #include <sys/conf.h>
43 #include <sys/device.h>
44 #include <sys/filio.h>
45 #endif
46 
47 #include <ctype.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <locale.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <signal.h>
56 #include <time.h>
57 #include <unistd.h>
58 
59 #include "dd.h"
60 #include "extern.h"
61 
62 #ifndef SIGINFO
63 #define	SIGINFO	SIGUSR1
64 #endif
65 
66 static void dd_close(void);
67 static void dd_in(void);
68 static void getfdtype(IO *);
69 static int  parity(u_char);
70 static void setup(void);
71 static void speed_limit(void);
72 static void swapbytes(void *, size_t);
73 
74 IO	in, out;		/* input/output state */
75 STAT	st;			/* statistics */
76 void	(*cfunc)(void);		/* conversion function */
77 uintmax_t cpy_cnt;		/* # of blocks to copy */
78 u_int	ddflags = 0;		/* conversion options */
79 size_t	cbsz;			/* conversion block size */
80 uintmax_t files_cnt = 1;	/* # of files to copy */
81 const	u_char *ctab;		/* conversion table */
82 char	fill_char;		/* Character to fill with if defined */
83 size_t	speed = 0;		/* maximum speed, in bytes per second */
84 volatile sig_atomic_t need_summary;
85 volatile sig_atomic_t need_progress;
86 
87 static off_t pending = 0;	/* pending seek if sparse */
88 
89 int
main(int argc __unused,char * argv[])90 main(int argc __unused, char *argv[])
91 {
92 	/* SIGALRM every second, if needed */
93 	struct itimerval itv = { { 1, 0 }, { 1, 0 } };
94 
95 	setlocale(LC_CTYPE, "");
96 	jcl(argv);
97 	setup();
98 
99 	signal(SIGINFO, siginfo_handler);
100 	if (ddflags & C_PROGRESS) {
101 		signal(SIGALRM, sigalarm_handler);
102 		setitimer(ITIMER_REAL, &itv, NULL);
103 	}
104 	signal(SIGINT, terminate);
105 
106 	atexit(summary);
107 
108 	while (files_cnt--)
109 		dd_in();
110 
111 	dd_close();
112 	/*
113 	 * Some devices such as cfi(4) may perform significant amounts
114 	 * of work when a write descriptor is closed.  Close the out
115 	 * descriptor explicitly so that the summary handler (called
116 	 * from an atexit() hook) includes this work.
117 	 */
118 	close(out.fd);
119 	exit(0);
120 }
121 
122 static int
parity(u_char c)123 parity(u_char c)
124 {
125 	int i;
126 
127 	i = c ^ (c >> 1) ^ (c >> 2) ^ (c >> 3) ^
128 	    (c >> 4) ^ (c >> 5) ^ (c >> 6) ^ (c >> 7);
129 	return (i & 1);
130 }
131 
132 static void
setup(void)133 setup(void)
134 {
135 	u_int cnt;
136 
137 	if (in.name == NULL) {
138 		in.name = "stdin";
139 		in.fd = STDIN_FILENO;
140 	} else {
141 		in.fd = open(in.name, O_RDONLY, 0);
142 		if (in.fd == -1)
143 			err(1, "%s", in.name);
144 	}
145 
146 	getfdtype(&in);
147 
148 	if (files_cnt > 1 && !(in.flags & ISTAPE))
149 		errx(1, "files is not supported for non-tape devices");
150 
151 	if (out.name == NULL) {
152 		/* No way to check for read access here. */
153 		out.fd = STDOUT_FILENO;
154 		out.name = "stdout";
155 	} else {
156 #define	OFLAGS \
157     (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
158 		out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE);
159 		/*
160 		 * May not have read access, so try again with write only.
161 		 * Without read we may have a problem if output also does
162 		 * not support seeks.
163 		 */
164 		if (out.fd == -1) {
165 			out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE);
166 			out.flags |= NOREAD;
167 		}
168 		if (out.fd == -1)
169 			err(1, "%s", out.name);
170 	}
171 
172 	getfdtype(&out);
173 
174 	/*
175 	 * Allocate space for the input and output buffers.  If not doing
176 	 * record oriented I/O, only need a single buffer.
177 	 */
178 	if (!(ddflags & (C_BLOCK | C_UNBLOCK))) {
179 		if ((in.db = malloc((size_t)out.dbsz + in.dbsz - 1)) == NULL)
180 			err(1, "input buffer");
181 		out.db = in.db;
182 	} else if ((in.db = malloc(MAX((size_t)in.dbsz, cbsz) + cbsz)) == NULL ||
183 	    (out.db = malloc(out.dbsz + cbsz)) == NULL) {
184 		err(1, "output buffer");
185 	}
186 
187 	/* dbp is the first free position in each buffer. */
188 	in.dbp = in.db;
189 	out.dbp = out.db;
190 
191 	/* Position the input/output streams. */
192 	if (in.offset)
193 		pos_in();
194 	if (out.offset)
195 		pos_out();
196 
197 	/*
198 	 * Truncate the output file.  If it fails on a type of output file
199 	 * that it should _not_ fail on, error out.
200 	 */
201 	if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK) &&
202 	    out.flags & ISTRUNC)
203 		if (ftruncate(out.fd, out.offset * out.dbsz) == -1)
204 			err(1, "truncating %s", out.name);
205 
206 	if (ddflags & (C_LCASE  | C_UCASE | C_ASCII | C_EBCDIC | C_PARITY)) {
207 		if (ctab != NULL) {
208 			for (cnt = 0; cnt <= 0377; ++cnt)
209 				casetab[cnt] = ctab[cnt];
210 		} else {
211 			for (cnt = 0; cnt <= 0377; ++cnt)
212 				casetab[cnt] = cnt;
213 		}
214 		if ((ddflags & C_PARITY) && !(ddflags & C_ASCII)) {
215 			/*
216 			 * If the input is not EBCDIC, and we do parity
217 			 * processing, strip input parity.
218 			 */
219 			for (cnt = 200; cnt <= 0377; ++cnt)
220 				casetab[cnt] = casetab[cnt & 0x7f];
221 		}
222 		if (ddflags & C_LCASE) {
223 			for (cnt = 0; cnt <= 0377; ++cnt)
224 				casetab[cnt] = tolower(casetab[cnt]);
225 		} else if (ddflags & C_UCASE) {
226 			for (cnt = 0; cnt <= 0377; ++cnt)
227 				casetab[cnt] = toupper(casetab[cnt]);
228 		}
229 		if ((ddflags & C_PARITY)) {
230 			/*
231 			 * This should strictly speaking be a no-op, but I
232 			 * wonder what funny LANG settings could get us.
233 			 */
234 			for (cnt = 0; cnt <= 0377; ++cnt)
235 				casetab[cnt] = casetab[cnt] & 0x7f;
236 		}
237 		if ((ddflags & C_PARSET)) {
238 			for (cnt = 0; cnt <= 0377; ++cnt)
239 				casetab[cnt] = casetab[cnt] | 0x80;
240 		}
241 		if ((ddflags & C_PAREVEN)) {
242 			for (cnt = 0; cnt <= 0377; ++cnt)
243 				if (parity(casetab[cnt]))
244 					casetab[cnt] = casetab[cnt] | 0x80;
245 		}
246 		if ((ddflags & C_PARODD)) {
247 			for (cnt = 0; cnt <= 0377; ++cnt)
248 				if (!parity(casetab[cnt]))
249 					casetab[cnt] = casetab[cnt] | 0x80;
250 		}
251 
252 		ctab = casetab;
253 	}
254 
255 	if (clock_gettime(CLOCK_MONOTONIC, &st.start))
256 		err(1, "clock_gettime");
257 }
258 
259 static void
getfdtype(IO * io)260 getfdtype(IO *io)
261 {
262 	struct stat sb;
263 #ifndef BOOTSTRAPPING
264 	int type;
265 #endif
266 
267 	if (fstat(io->fd, &sb) == -1)
268 		err(1, "%s", io->name);
269 	if (S_ISREG(sb.st_mode))
270 		io->flags |= ISTRUNC;
271 	if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
272 #ifdef BOOTSTRAPPING
273 		io->flags |= ISSEEK;	/* assume D_DISK|D_MEM in btools */
274 #else
275 		if (ioctl(io->fd, FIODTYPE, &type) == -1) {
276 			err(1, "%s", io->name);
277 		} else {
278 			if (type & D_TAPE)
279 				io->flags |= ISTAPE;
280 			else if (type & (D_DISK | D_MEM))
281 				io->flags |= ISSEEK;
282 			if (S_ISCHR(sb.st_mode) && (type & D_TAPE) == 0)
283 				io->flags |= ISCHR;
284 		}
285 #endif
286 		return;
287 	}
288 	errno = 0;
289 	if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
290 		io->flags |= ISPIPE;
291 	else
292 		io->flags |= ISSEEK;
293 }
294 
295 /*
296  * Limit the speed by adding a delay before every block read.
297  * The delay (t_usleep) is equal to the time computed from block
298  * size and the specified speed limit (t_target) minus the time
299  * spent on actual read and write operations (t_io).
300  */
301 static void
speed_limit(void)302 speed_limit(void)
303 {
304 	static double t_prev, t_usleep;
305 	double t_now, t_io, t_target;
306 
307 	t_now = secs_elapsed();
308 	t_io = t_now - t_prev - t_usleep;
309 	t_target = (double)in.dbsz / (double)speed;
310 	t_usleep = t_target - t_io;
311 	if (t_usleep > 0)
312 		usleep(t_usleep * 1000000);
313 	else
314 		t_usleep = 0;
315 	t_prev = t_now;
316 }
317 
318 static void
swapbytes(void * v,size_t len)319 swapbytes(void *v, size_t len)
320 {
321 	unsigned char *p = v;
322 	unsigned char t;
323 
324 	while (len > 1) {
325 		t = p[0];
326 		p[0] = p[1];
327 		p[1] = t;
328 		p += 2;
329 		len -= 2;
330 	}
331 }
332 
333 static void
dd_in(void)334 dd_in(void)
335 {
336 	ssize_t n;
337 
338 	for (;;) {
339 		switch (cpy_cnt) {
340 		case -1:			/* count=0 was specified */
341 			return;
342 		case 0:
343 			break;
344 		default:
345 			if (st.in_full + st.in_part >= (uintmax_t)cpy_cnt)
346 				return;
347 			break;
348 		}
349 
350 		if (speed > 0)
351 			speed_limit();
352 
353 		/*
354 		 * Zero the buffer first if sync; if doing block operations,
355 		 * use spaces.
356 		 */
357 		if (ddflags & C_SYNC) {
358 			if (ddflags & C_FILL)
359 				memset(in.dbp, fill_char, in.dbsz);
360 			else if (ddflags & (C_BLOCK | C_UNBLOCK))
361 				memset(in.dbp, ' ', in.dbsz);
362 			else
363 				memset(in.dbp, 0, in.dbsz);
364 		}
365 
366 		n = read(in.fd, in.dbp, in.dbsz);
367 		if (n == 0) {
368 			in.dbrcnt = 0;
369 			return;
370 		}
371 
372 		/* Read error. */
373 		if (n == -1) {
374 			/*
375 			 * If noerror not specified, die.  POSIX requires that
376 			 * the warning message be followed by an I/O display.
377 			 */
378 			if (!(ddflags & C_NOERROR))
379 				err(1, "%s", in.name);
380 			warn("%s", in.name);
381 			summary();
382 
383 			/*
384 			 * If it's a seekable file descriptor, seek past the
385 			 * error.  If your OS doesn't do the right thing for
386 			 * raw disks this section should be modified to re-read
387 			 * in sector size chunks.
388 			 */
389 			if (in.flags & ISSEEK &&
390 			    lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
391 				warn("%s", in.name);
392 
393 			/* If sync not specified, omit block and continue. */
394 			if (!(ddflags & C_SYNC))
395 				continue;
396 
397 			/* Read errors count as full blocks. */
398 			in.dbcnt += in.dbrcnt = in.dbsz;
399 			++st.in_full;
400 
401 		/* Handle full input blocks. */
402 		} else if (n == in.dbsz) {
403 			in.dbcnt += in.dbrcnt = n;
404 			++st.in_full;
405 
406 		/* Handle partial input blocks. */
407 		} else {
408 			/* If sync, use the entire block. */
409 			if (ddflags & C_SYNC)
410 				in.dbcnt += in.dbrcnt = in.dbsz;
411 			else
412 				in.dbcnt += in.dbrcnt = n;
413 			++st.in_part;
414 		}
415 
416 		/*
417 		 * POSIX states that if bs is set and no other conversions
418 		 * than noerror, notrunc or sync are specified, the block
419 		 * is output without buffering as it is read.
420 		 */
421 		if ((ddflags & ~(C_NOERROR | C_NOTRUNC | C_SYNC)) == C_BS) {
422 			out.dbcnt = in.dbcnt;
423 			dd_out(1);
424 			in.dbcnt = 0;
425 			continue;
426 		}
427 
428 		if (ddflags & C_SWAB) {
429 			if ((n = in.dbrcnt) & 1) {
430 				++st.swab;
431 				--n;
432 			}
433 			swapbytes(in.dbp, (size_t)n);
434 		}
435 
436 		in.dbp += in.dbrcnt;
437 		(*cfunc)();
438 		if (need_summary)
439 			summary();
440 		if (need_progress)
441 			progress();
442 	}
443 }
444 
445 /*
446  * Clean up any remaining I/O and flush output.  If necessary, the output file
447  * is truncated.
448  */
449 static void
dd_close(void)450 dd_close(void)
451 {
452 	if (cfunc == def)
453 		def_close();
454 	else if (cfunc == block)
455 		block_close();
456 	else if (cfunc == unblock)
457 		unblock_close();
458 	if (ddflags & C_OSYNC && out.dbcnt && out.dbcnt < out.dbsz) {
459 		if (ddflags & C_FILL)
460 			memset(out.dbp, fill_char, out.dbsz - out.dbcnt);
461 		else if (ddflags & (C_BLOCK | C_UNBLOCK))
462 			memset(out.dbp, ' ', out.dbsz - out.dbcnt);
463 		else
464 			memset(out.dbp, 0, out.dbsz - out.dbcnt);
465 		out.dbcnt = out.dbsz;
466 	}
467 	if (out.dbcnt || pending)
468 		dd_out(1);
469 
470 	/*
471 	 * If the file ends with a hole, ftruncate it to extend its size
472 	 * up to the end of the hole (without having to write any data).
473 	 */
474 	if (out.seek_offset > 0 && (out.flags & ISTRUNC)) {
475 		if (ftruncate(out.fd, out.seek_offset) == -1)
476 			err(1, "truncating %s", out.name);
477 	}
478 }
479 
480 void
dd_out(int force)481 dd_out(int force)
482 {
483 	u_char *outp;
484 	size_t cnt, n;
485 	ssize_t nw;
486 	static int warned;
487 	int sparse;
488 
489 	/*
490 	 * Write one or more blocks out.  The common case is writing a full
491 	 * output block in a single write; increment the full block stats.
492 	 * Otherwise, we're into partial block writes.  If a partial write,
493 	 * and it's a character device, just warn.  If a tape device, quit.
494 	 *
495 	 * The partial writes represent two cases.  1: Where the input block
496 	 * was less than expected so the output block was less than expected.
497 	 * 2: Where the input block was the right size but we were forced to
498 	 * write the block in multiple chunks.  The original versions of dd(1)
499 	 * never wrote a block in more than a single write, so the latter case
500 	 * never happened.
501 	 *
502 	 * One special case is if we're forced to do the write -- in that case
503 	 * we play games with the buffer size, and it's usually a partial write.
504 	 */
505 	outp = out.db;
506 
507 	/*
508 	 * If force, first try to write all pending data, else try to write
509 	 * just one block. Subsequently always write data one full block at
510 	 * a time at most.
511 	 */
512 	for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
513 		cnt = n;
514 		do {
515 			sparse = 0;
516 			if (ddflags & C_SPARSE) {
517 				/* Is buffer sparse? */
518 				sparse = BISZERO(outp, cnt);
519 			}
520 			if (sparse && !force) {
521 				pending += cnt;
522 				nw = cnt;
523 			} else {
524 				if (pending != 0) {
525 					/*
526 					 * Seek past hole.  Note that we need to record the
527 					 * reached offset, because we might have no more data
528 					 * to write, in which case we'll need to call
529 					 * ftruncate to extend the file size.
530 					 */
531 					out.seek_offset = lseek(out.fd, pending, SEEK_CUR);
532 					if (out.seek_offset == -1)
533 						err(2, "%s: seek error creating sparse file",
534 						    out.name);
535 					pending = 0;
536 				}
537 				if (cnt) {
538 					nw = write(out.fd, outp, cnt);
539 					out.seek_offset = 0;
540 				} else {
541 					return;
542 				}
543 			}
544 
545 			if (nw <= 0) {
546 				if (nw == 0)
547 					errx(1, "%s: end of device", out.name);
548 				if (errno != EINTR)
549 					err(1, "%s", out.name);
550 				nw = 0;
551 			}
552 
553 			outp += nw;
554 			st.bytes += nw;
555 
556 			if ((size_t)nw == n && n == (size_t)out.dbsz)
557 				++st.out_full;
558 			else
559 				++st.out_part;
560 
561 			if ((size_t) nw != cnt) {
562 				if (out.flags & ISTAPE)
563 					errx(1, "%s: short write on tape device",
564 				    	out.name);
565 				if (out.flags & ISCHR && !warned) {
566 					warned = 1;
567 					warnx("%s: short write on character device",
568 				    	out.name);
569 				}
570 			}
571 
572 			cnt -= nw;
573 		} while (cnt != 0);
574 
575 		if ((out.dbcnt -= n) < out.dbsz)
576 			break;
577 	}
578 
579 	/* Reassemble the output block. */
580 	if (out.dbcnt)
581 		memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
582 	out.dbp = out.db + out.dbcnt;
583 }
584