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