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