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