2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Keith Muller of the University of California, San Diego and Lance
7 * Visser of Convex Computer Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char const copyright[] =
37 "@(#) Copyright (c) 1991, 1993, 1994\n\
38 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94";
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
50 #include <sys/capsicum.h>
52 #include <sys/disklabel.h>
53 #include <sys/filio.h>
57 #include <capsicum_helpers.h>
73 static void dd_close(void);
74 static void dd_in(void);
75 static void getfdtype(IO *);
76 static void setup(void);
78 IO in, out; /* input/output state */
79 STAT st; /* statistics */
80 void (*cfunc)(void); /* conversion function */
81 uintmax_t cpy_cnt; /* # of blocks to copy */
82 static off_t pending = 0; /* pending seek if sparse */
83 u_int ddflags = 0; /* conversion options */
84 size_t cbsz; /* conversion block size */
85 uintmax_t files_cnt = 1; /* # of files to copy */
86 const u_char *ctab; /* conversion table */
87 char fill_char; /* Character to fill with if defined */
88 size_t speed = 0; /* maximum speed, in bytes per second */
89 volatile sig_atomic_t need_summary;
92 main(int argc __unused, char *argv[])
94 (void)setlocale(LC_CTYPE, "");
98 caph_cache_catpages();
99 if (cap_enter() == -1 && errno != ENOSYS)
100 err(1, "unable to enter capability mode");
102 (void)signal(SIGINFO, siginfo_handler);
103 (void)signal(SIGINT, terminate);
112 * Some devices such as cfi(4) may perform significant amounts
113 * of work when a write descriptor is closed. Close the out
114 * descriptor explicitly so that the summary handler (called
115 * from an atexit() hook) includes this work.
126 i = c ^ (c >> 1) ^ (c >> 2) ^ (c >> 3) ^
127 (c >> 4) ^ (c >> 5) ^ (c >> 6) ^ (c >> 7);
136 unsigned long cmds[] = { FIODTYPE, MTIOCTOP };
138 if (in.name == NULL) {
140 in.fd = STDIN_FILENO;
142 in.fd = open(in.name, O_RDONLY, 0);
144 err(1, "%s", in.name);
149 cap_rights_init(&rights, CAP_READ, CAP_SEEK);
150 if (cap_rights_limit(in.fd, &rights) == -1 && errno != ENOSYS)
151 err(1, "unable to limit capability rights");
153 if (files_cnt > 1 && !(in.flags & ISTAPE))
154 errx(1, "files is not supported for non-tape devices");
156 cap_rights_set(&rights, CAP_FTRUNCATE, CAP_IOCTL, CAP_WRITE);
157 if (out.name == NULL) {
158 /* No way to check for read access here. */
159 out.fd = STDOUT_FILENO;
163 (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
164 out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE);
166 * May not have read access, so try again with write only.
167 * Without read we may have a problem if output also does
171 out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE);
173 cap_rights_clear(&rights, CAP_READ);
176 err(1, "%s", out.name);
181 if (cap_rights_limit(out.fd, &rights) == -1 && errno != ENOSYS)
182 err(1, "unable to limit capability rights");
183 if (cap_ioctls_limit(out.fd, cmds, nitems(cmds)) == -1 &&
185 err(1, "unable to limit capability rights");
187 if (in.fd != STDIN_FILENO && out.fd != STDIN_FILENO) {
188 if (caph_limit_stdin() == -1)
189 err(1, "unable to limit capability rights");
192 if (in.fd != STDOUT_FILENO && out.fd != STDOUT_FILENO) {
193 if (caph_limit_stdout() == -1)
194 err(1, "unable to limit capability rights");
197 if (in.fd != STDERR_FILENO && out.fd != STDERR_FILENO) {
198 if (caph_limit_stderr() == -1)
199 err(1, "unable to limit capability rights");
203 * Allocate space for the input and output buffers. If not doing
204 * record oriented I/O, only need a single buffer.
206 if (!(ddflags & (C_BLOCK | C_UNBLOCK))) {
207 if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL)
208 err(1, "input buffer");
210 } else if ((in.db = malloc(MAX(in.dbsz, cbsz) + cbsz)) == NULL ||
211 (out.db = malloc(out.dbsz + cbsz)) == NULL)
212 err(1, "output buffer");
214 /* dbp is the first free position in each buffer. */
218 /* Position the input/output streams. */
225 * Truncate the output file. If it fails on a type of output file
226 * that it should _not_ fail on, error out.
228 if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK) &&
230 if (ftruncate(out.fd, out.offset * out.dbsz) == -1)
231 err(1, "truncating %s", out.name);
233 if (ddflags & (C_LCASE | C_UCASE | C_ASCII | C_EBCDIC | C_PARITY)) {
235 for (cnt = 0; cnt <= 0377; ++cnt)
236 casetab[cnt] = ctab[cnt];
238 for (cnt = 0; cnt <= 0377; ++cnt)
241 if ((ddflags & C_PARITY) && !(ddflags & C_ASCII)) {
243 * If the input is not EBCDIC, and we do parity
244 * processing, strip input parity.
246 for (cnt = 200; cnt <= 0377; ++cnt)
247 casetab[cnt] = casetab[cnt & 0x7f];
249 if (ddflags & C_LCASE) {
250 for (cnt = 0; cnt <= 0377; ++cnt)
251 casetab[cnt] = tolower(casetab[cnt]);
252 } else if (ddflags & C_UCASE) {
253 for (cnt = 0; cnt <= 0377; ++cnt)
254 casetab[cnt] = toupper(casetab[cnt]);
256 if ((ddflags & C_PARITY)) {
258 * This should strictly speaking be a no-op, but I
259 * wonder what funny LANG settings could get us.
261 for (cnt = 0; cnt <= 0377; ++cnt)
262 casetab[cnt] = casetab[cnt] & 0x7f;
264 if ((ddflags & C_PARSET)) {
265 for (cnt = 0; cnt <= 0377; ++cnt)
266 casetab[cnt] = casetab[cnt] | 0x80;
268 if ((ddflags & C_PAREVEN)) {
269 for (cnt = 0; cnt <= 0377; ++cnt)
270 if (parity(casetab[cnt]))
271 casetab[cnt] = casetab[cnt] | 0x80;
273 if ((ddflags & C_PARODD)) {
274 for (cnt = 0; cnt <= 0377; ++cnt)
275 if (!parity(casetab[cnt]))
276 casetab[cnt] = casetab[cnt] | 0x80;
282 if (clock_gettime(CLOCK_MONOTONIC, &st.start))
283 err(1, "clock_gettime");
292 if (fstat(io->fd, &sb) == -1)
293 err(1, "%s", io->name);
294 if (S_ISREG(sb.st_mode))
295 io->flags |= ISTRUNC;
296 if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
297 if (ioctl(io->fd, FIODTYPE, &type) == -1) {
298 err(1, "%s", io->name);
302 else if (type & (D_DISK | D_MEM))
304 if (S_ISCHR(sb.st_mode) && (type & D_TAPE) == 0)
310 if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
317 * Limit the speed by adding a delay before every block read.
318 * The delay (t_usleep) is equal to the time computed from block
319 * size and the specified speed limit (t_target) minus the time
320 * spent on actual read and write operations (t_io).
325 static double t_prev, t_usleep;
326 double t_now, t_io, t_target;
328 t_now = secs_elapsed();
329 t_io = t_now - t_prev - t_usleep;
330 t_target = (double)in.dbsz / (double)speed;
331 t_usleep = t_target - t_io;
333 usleep(t_usleep * 1000000);
346 case -1: /* count=0 was specified */
351 if (st.in_full + st.in_part >= (uintmax_t)cpy_cnt)
360 * Zero the buffer first if sync; if doing block operations,
363 if (ddflags & C_SYNC) {
364 if (ddflags & C_FILL)
365 memset(in.dbp, fill_char, in.dbsz);
366 else if (ddflags & (C_BLOCK | C_UNBLOCK))
367 memset(in.dbp, ' ', in.dbsz);
369 memset(in.dbp, 0, in.dbsz);
372 n = read(in.fd, in.dbp, in.dbsz);
381 * If noerror not specified, die. POSIX requires that
382 * the warning message be followed by an I/O display.
384 if (!(ddflags & C_NOERROR))
385 err(1, "%s", in.name);
390 * If it's a seekable file descriptor, seek past the
391 * error. If your OS doesn't do the right thing for
392 * raw disks this section should be modified to re-read
393 * in sector size chunks.
395 if (in.flags & ISSEEK &&
396 lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
399 /* If sync not specified, omit block and continue. */
400 if (!(ddflags & C_SYNC))
403 /* Read errors count as full blocks. */
404 in.dbcnt += in.dbrcnt = in.dbsz;
407 /* Handle full input blocks. */
408 } else if ((size_t)n == in.dbsz) {
409 in.dbcnt += in.dbrcnt = n;
412 /* Handle partial input blocks. */
414 /* If sync, use the entire block. */
415 if (ddflags & C_SYNC)
416 in.dbcnt += in.dbrcnt = in.dbsz;
418 in.dbcnt += in.dbrcnt = n;
423 * POSIX states that if bs is set and no other conversions
424 * than noerror, notrunc or sync are specified, the block
425 * is output without buffering as it is read.
427 if ((ddflags & ~(C_NOERROR | C_NOTRUNC | C_SYNC)) == C_BS) {
428 out.dbcnt = in.dbcnt;
434 if (ddflags & C_SWAB) {
435 if ((n = in.dbrcnt) & 1) {
439 swab(in.dbp, in.dbp, (size_t)n);
451 * Clean up any remaining I/O and flush output. If necessary, the output file
459 else if (cfunc == block)
461 else if (cfunc == unblock)
463 if (ddflags & C_OSYNC && out.dbcnt && out.dbcnt < out.dbsz) {
464 if (ddflags & C_FILL)
465 memset(out.dbp, fill_char, out.dbsz - out.dbcnt);
466 else if (ddflags & (C_BLOCK | C_UNBLOCK))
467 memset(out.dbp, ' ', out.dbsz - out.dbcnt);
469 memset(out.dbp, 0, out.dbsz - out.dbcnt);
470 out.dbcnt = out.dbsz;
472 if (out.dbcnt || pending)
476 * If the file ends with a hole, ftruncate it to extend its size
477 * up to the end of the hole (without having to write any data).
479 if (out.seek_offset > 0 && (out.flags & ISTRUNC)) {
480 if (ftruncate(out.fd, out.seek_offset) == -1)
481 err(1, "truncating %s", out.name);
495 * Write one or more blocks out. The common case is writing a full
496 * output block in a single write; increment the full block stats.
497 * Otherwise, we're into partial block writes. If a partial write,
498 * and it's a character device, just warn. If a tape device, quit.
500 * The partial writes represent two cases. 1: Where the input block
501 * was less than expected so the output block was less than expected.
502 * 2: Where the input block was the right size but we were forced to
503 * write the block in multiple chunks. The original versions of dd(1)
504 * never wrote a block in more than a single write, so the latter case
507 * One special case is if we're forced to do the write -- in that case
508 * we play games with the buffer size, and it's usually a partial write.
513 * If force, first try to write all pending data, else try to write
514 * just one block. Subsequently always write data one full block at
517 for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
521 if (ddflags & C_SPARSE) {
522 sparse = 1; /* Is buffer sparse? */
523 for (i = 0; i < cnt; i++)
529 if (sparse && !force) {
535 * Seek past hole. Note that we need to record the
536 * reached offset, because we might have no more data
537 * to write, in which case we'll need to call
538 * ftruncate to extend the file size.
540 out.seek_offset = lseek(out.fd, pending, SEEK_CUR);
541 if (out.seek_offset == -1)
542 err(2, "%s: seek error creating sparse file",
547 nw = write(out.fd, outp, cnt);
556 errx(1, "%s: end of device", out.name);
558 err(1, "%s", out.name);
565 if ((size_t)nw == n && n == out.dbsz)
570 if ((size_t) nw != cnt) {
571 if (out.flags & ISTAPE)
572 errx(1, "%s: short write on tape device",
574 if (out.flags & ISCHR && !warned) {
576 warnx("%s: short write on character device",
584 if ((out.dbcnt -= n) < out.dbsz)
588 /* Reassemble the output block. */
590 (void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
591 out.dbp = out.db + out.dbcnt;