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