1 /* $NetBSD: dd.c,v 1.48 2011/11/06 21:22:23 jym Exp $ */ 2 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 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\ 39 The Regents of the University of California. All rights reserved."); 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94"; 45 #else 46 __RCSID("$NetBSD: dd.c,v 1.48 2011/11/06 21:22:23 jym Exp $"); 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/param.h> 51 #include <sys/stat.h> 52 #include <sys/ioctl.h> 53 #include <sys/mtio.h> 54 #include <sys/time.h> 55 56 #include <ctype.h> 57 #include <err.h> 58 #include <errno.h> 59 #include <fcntl.h> 60 #include <locale.h> 61 #include <signal.h> 62 #include <stdio.h> 63 #include <stdlib.h> 64 #include <string.h> 65 #include <unistd.h> 66 67 #include "dd.h" 68 #include "extern.h" 69 70 static void dd_close(void); 71 static void dd_in(void); 72 static void getfdtype(IO *); 73 static void redup_clean_fd(IO *); 74 static void setup(void); 75 76 int main(int, char *[]); 77 78 IO in, out; /* input/output state */ 79 STAT st; /* statistics */ 80 void (*cfunc)(void); /* conversion function */ 81 uint64_t cpy_cnt; /* # of blocks to copy */ 82 static off_t pending = 0; /* pending seek if sparse */ 83 u_int ddflags; /* conversion options */ 84 uint64_t cbsz; /* conversion block size */ 85 u_int files_cnt = 1; /* # of files to copy */ 86 uint64_t progress = 0; /* display sign of life */ 87 const u_char *ctab; /* conversion table */ 88 sigset_t infoset; /* a set blocking SIGINFO */ 89 const char *msgfmt = "posix"; /* default summary() message format */ 90 91 /* 92 * Ops for stdin/stdout and crunch'd dd. These are always host ops. 93 */ 94 static const struct ddfops ddfops_stdfd = { 95 .op_open = open, 96 .op_close = close, 97 .op_fcntl = fcntl, 98 .op_ioctl = ioctl, 99 .op_fstat = fstat, 100 .op_fsync = fsync, 101 .op_ftruncate = ftruncate, 102 .op_lseek = lseek, 103 .op_read = read, 104 .op_write = write, 105 }; 106 extern const struct ddfops ddfops_prog; 107 108 int 109 main(int argc, char *argv[]) 110 { 111 int ch; 112 113 setprogname(argv[0]); 114 (void)setlocale(LC_ALL, ""); 115 116 while ((ch = getopt(argc, argv, "")) != -1) { 117 switch (ch) { 118 default: 119 errx(EXIT_FAILURE, "usage: dd [operand ...]"); 120 /* NOTREACHED */ 121 } 122 } 123 argc -= (optind - 1); 124 argv += (optind - 1); 125 126 jcl(argv); 127 #ifndef CRUNCHOPS 128 if (ddfops_prog.op_init && ddfops_prog.op_init() == -1) 129 err(1, "prog init"); 130 #endif 131 setup(); 132 133 (void)signal(SIGINFO, summaryx); 134 (void)signal(SIGINT, terminate); 135 (void)sigemptyset(&infoset); 136 (void)sigaddset(&infoset, SIGINFO); 137 138 (void)atexit(summary); 139 140 while (files_cnt--) 141 dd_in(); 142 143 dd_close(); 144 exit(0); 145 /* NOTREACHED */ 146 } 147 148 static void 149 setup(void) 150 { 151 #ifdef CRUNCHOPS 152 const struct ddfops *prog_ops = &ddfops_stdfd; 153 #else 154 const struct ddfops *prog_ops = &ddfops_prog; 155 #endif 156 157 if (in.name == NULL) { 158 in.name = "stdin"; 159 in.fd = STDIN_FILENO; 160 in.ops = &ddfops_stdfd; 161 } else { 162 in.ops = prog_ops; 163 in.fd = ddop_open(in, in.name, O_RDONLY, 0); 164 if (in.fd < 0) 165 err(EXIT_FAILURE, "%s", in.name); 166 /* NOTREACHED */ 167 168 /* Ensure in.fd is outside the stdio descriptor range */ 169 redup_clean_fd(&in); 170 } 171 172 getfdtype(&in); 173 174 if (files_cnt > 1 && !(in.flags & ISTAPE)) { 175 errx(EXIT_FAILURE, "files is not supported for non-tape devices"); 176 /* NOTREACHED */ 177 } 178 179 if (out.name == NULL) { 180 /* No way to check for read access here. */ 181 out.fd = STDOUT_FILENO; 182 out.name = "stdout"; 183 out.ops = &ddfops_stdfd; 184 } else { 185 out.ops = prog_ops; 186 #define OFLAGS \ 187 (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC)) 188 out.fd = ddop_open(out, out.name, O_RDWR | OFLAGS, DEFFILEMODE); 189 /* 190 * May not have read access, so try again with write only. 191 * Without read we may have a problem if output also does 192 * not support seeks. 193 */ 194 if (out.fd < 0) { 195 out.fd = ddop_open(out, out.name, O_WRONLY | OFLAGS, 196 DEFFILEMODE); 197 out.flags |= NOREAD; 198 } 199 if (out.fd < 0) { 200 err(EXIT_FAILURE, "%s", out.name); 201 /* NOTREACHED */ 202 } 203 204 /* Ensure out.fd is outside the stdio descriptor range */ 205 redup_clean_fd(&out); 206 } 207 208 getfdtype(&out); 209 210 /* 211 * Allocate space for the input and output buffers. If not doing 212 * record oriented I/O, only need a single buffer. 213 */ 214 if (!(ddflags & (C_BLOCK|C_UNBLOCK))) { 215 if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL) { 216 err(EXIT_FAILURE, NULL); 217 /* NOTREACHED */ 218 } 219 out.db = in.db; 220 } else if ((in.db = 221 malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL || 222 (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL) { 223 err(EXIT_FAILURE, NULL); 224 /* NOTREACHED */ 225 } 226 in.dbp = in.db; 227 out.dbp = out.db; 228 229 /* Position the input/output streams. */ 230 if (in.offset) 231 pos_in(); 232 if (out.offset) 233 pos_out(); 234 235 /* 236 * Truncate the output file; ignore errors because it fails on some 237 * kinds of output files, tapes, for example. 238 */ 239 if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK)) 240 (void)ddop_ftruncate(out, out.fd, (off_t)out.offset * out.dbsz); 241 242 /* 243 * If converting case at the same time as another conversion, build a 244 * table that does both at once. If just converting case, use the 245 * built-in tables. 246 */ 247 if (ddflags & (C_LCASE|C_UCASE)) { 248 #ifdef NO_CONV 249 /* Should not get here, but just in case... */ 250 errx(EXIT_FAILURE, "case conv and -DNO_CONV"); 251 /* NOTREACHED */ 252 #else /* NO_CONV */ 253 u_int cnt; 254 255 if (ddflags & C_ASCII || ddflags & C_EBCDIC) { 256 if (ddflags & C_LCASE) { 257 for (cnt = 0; cnt < 256; ++cnt) 258 casetab[cnt] = tolower(ctab[cnt]); 259 } else { 260 for (cnt = 0; cnt < 256; ++cnt) 261 casetab[cnt] = toupper(ctab[cnt]); 262 } 263 } else { 264 if (ddflags & C_LCASE) { 265 for (cnt = 0; cnt < 256; ++cnt) 266 casetab[cnt] = tolower(cnt); 267 } else { 268 for (cnt = 0; cnt < 256; ++cnt) 269 casetab[cnt] = toupper(cnt); 270 } 271 } 272 273 ctab = casetab; 274 #endif /* NO_CONV */ 275 } 276 277 (void)gettimeofday(&st.start, NULL); /* Statistics timestamp. */ 278 } 279 280 static void 281 getfdtype(IO *io) 282 { 283 struct mtget mt; 284 struct stat sb; 285 286 if (io->ops->op_fstat(io->fd, &sb)) { 287 err(EXIT_FAILURE, "%s", io->name); 288 /* NOTREACHED */ 289 } 290 if (S_ISCHR(sb.st_mode)) 291 io->flags |= io->ops->op_ioctl(io->fd, MTIOCGET, &mt) 292 ? ISCHR : ISTAPE; 293 else if (io->ops->op_lseek(io->fd, (off_t)0, SEEK_CUR) == -1 294 && errno == ESPIPE) 295 io->flags |= ISPIPE; /* XXX fixed in 4.4BSD */ 296 } 297 298 /* 299 * Move the parameter file descriptor to a descriptor that is outside the 300 * stdio descriptor range, if necessary. This is required to avoid 301 * accidentally outputting completion or error messages into the 302 * output file that were intended for the tty. 303 */ 304 static void 305 redup_clean_fd(IO *io) 306 { 307 int fd = io->fd; 308 int newfd; 309 310 if (fd != STDIN_FILENO && fd != STDOUT_FILENO && 311 fd != STDERR_FILENO) 312 /* File descriptor is ok, return immediately. */ 313 return; 314 315 /* 316 * 3 is the first descriptor greater than STD*_FILENO. Any 317 * free descriptor valued 3 or above is acceptable... 318 */ 319 newfd = io->ops->op_fcntl(fd, F_DUPFD, 3); 320 if (newfd < 0) { 321 err(EXIT_FAILURE, "dupfd IO"); 322 /* NOTREACHED */ 323 } 324 325 io->ops->op_close(fd); 326 io->fd = newfd; 327 } 328 329 static void 330 dd_in(void) 331 { 332 int flags; 333 int64_t n; 334 335 for (flags = ddflags;;) { 336 if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt) 337 return; 338 339 /* 340 * Clear the buffer first if doing "sync" on input. 341 * If doing block operations use spaces. This will 342 * affect not only the C_NOERROR case, but also the 343 * last partial input block which should be padded 344 * with zero and not garbage. 345 */ 346 if (flags & C_SYNC) { 347 if (flags & (C_BLOCK|C_UNBLOCK)) 348 (void)memset(in.dbp, ' ', in.dbsz); 349 else 350 (void)memset(in.dbp, 0, in.dbsz); 351 } 352 353 n = ddop_read(in, in.fd, in.dbp, in.dbsz); 354 if (n == 0) { 355 in.dbrcnt = 0; 356 return; 357 } 358 359 /* Read error. */ 360 if (n < 0) { 361 362 /* 363 * If noerror not specified, die. POSIX requires that 364 * the warning message be followed by an I/O display. 365 */ 366 if (!(flags & C_NOERROR)) { 367 err(EXIT_FAILURE, "%s", in.name); 368 /* NOTREACHED */ 369 } 370 warn("%s", in.name); 371 summary(); 372 373 /* 374 * If it's not a tape drive or a pipe, seek past the 375 * error. If your OS doesn't do the right thing for 376 * raw disks this section should be modified to re-read 377 * in sector size chunks. 378 */ 379 if (!(in.flags & (ISPIPE|ISTAPE)) && 380 ddop_lseek(in, in.fd, (off_t)in.dbsz, SEEK_CUR)) 381 warn("%s", in.name); 382 383 /* If sync not specified, omit block and continue. */ 384 if (!(ddflags & C_SYNC)) 385 continue; 386 387 /* Read errors count as full blocks. */ 388 in.dbcnt += in.dbrcnt = in.dbsz; 389 ++st.in_full; 390 391 /* Handle full input blocks. */ 392 } else if ((uint64_t)n == in.dbsz) { 393 in.dbcnt += in.dbrcnt = n; 394 ++st.in_full; 395 396 /* Handle partial input blocks. */ 397 } else { 398 /* If sync, use the entire block. */ 399 if (ddflags & C_SYNC) 400 in.dbcnt += in.dbrcnt = in.dbsz; 401 else 402 in.dbcnt += in.dbrcnt = n; 403 ++st.in_part; 404 } 405 406 /* 407 * POSIX states that if bs is set and no other conversions 408 * than noerror, notrunc or sync are specified, the block 409 * is output without buffering as it is read. 410 */ 411 if (ddflags & C_BS) { 412 out.dbcnt = in.dbcnt; 413 dd_out(1); 414 in.dbcnt = 0; 415 continue; 416 } 417 418 if (ddflags & C_SWAB) { 419 if ((n = in.dbrcnt) & 1) { 420 ++st.swab; 421 --n; 422 } 423 swab(in.dbp, in.dbp, n); 424 } 425 426 in.dbp += in.dbrcnt; 427 (*cfunc)(); 428 } 429 } 430 431 /* 432 * Cleanup any remaining I/O and flush output. If necessary, output file 433 * is truncated. 434 */ 435 static void 436 dd_close(void) 437 { 438 439 if (cfunc == def) 440 def_close(); 441 else if (cfunc == block) 442 block_close(); 443 else if (cfunc == unblock) 444 unblock_close(); 445 if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) { 446 (void)memset(out.dbp, 0, out.dbsz - out.dbcnt); 447 out.dbcnt = out.dbsz; 448 } 449 /* If there are pending sparse blocks, make sure 450 * to write out the final block un-sparse 451 */ 452 if ((out.dbcnt == 0) && pending) { 453 memset(out.db, 0, out.dbsz); 454 out.dbcnt = out.dbsz; 455 out.dbp = out.db + out.dbcnt; 456 pending -= out.dbsz; 457 } 458 if (out.dbcnt) 459 dd_out(1); 460 461 /* 462 * Reporting nfs write error may be deferred until next 463 * write(2) or close(2) system call. So, we need to do an 464 * extra check. If an output is stdout, the file structure 465 * may be shared with other processes and close(2) just 466 * decreases the reference count. 467 */ 468 if (out.fd == STDOUT_FILENO && ddop_fsync(out, out.fd) == -1 469 && errno != EINVAL) { 470 err(EXIT_FAILURE, "fsync stdout"); 471 /* NOTREACHED */ 472 } 473 if (ddop_close(out, out.fd) == -1) { 474 err(EXIT_FAILURE, "close"); 475 /* NOTREACHED */ 476 } 477 } 478 479 void 480 dd_out(int force) 481 { 482 static int warned; 483 int64_t cnt, n, nw; 484 u_char *outp; 485 486 /* 487 * Write one or more blocks out. The common case is writing a full 488 * output block in a single write; increment the full block stats. 489 * Otherwise, we're into partial block writes. If a partial write, 490 * and it's a character device, just warn. If a tape device, quit. 491 * 492 * The partial writes represent two cases. 1: Where the input block 493 * was less than expected so the output block was less than expected. 494 * 2: Where the input block was the right size but we were forced to 495 * write the block in multiple chunks. The original versions of dd(1) 496 * never wrote a block in more than a single write, so the latter case 497 * never happened. 498 * 499 * One special case is if we're forced to do the write -- in that case 500 * we play games with the buffer size, and it's usually a partial write. 501 */ 502 outp = out.db; 503 for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) { 504 for (cnt = n;; cnt -= nw) { 505 506 if (!force && ddflags & C_SPARSE) { 507 int sparse, i; 508 sparse = 1; /* Is buffer sparse? */ 509 for (i = 0; i < cnt; i++) 510 if (outp[i] != 0) { 511 sparse = 0; 512 break; 513 } 514 if (sparse) { 515 pending += cnt; 516 outp += cnt; 517 nw = 0; 518 break; 519 } 520 } 521 if (pending != 0) { 522 if (ddop_lseek(out, 523 out.fd, pending, SEEK_CUR) == -1) 524 err(EXIT_FAILURE, "%s: seek error creating sparse file", 525 out.name); 526 } 527 nw = bwrite(&out, outp, cnt); 528 if (nw <= 0) { 529 if (nw == 0) 530 errx(EXIT_FAILURE, 531 "%s: end of device", out.name); 532 /* NOTREACHED */ 533 if (errno != EINTR) 534 err(EXIT_FAILURE, "%s", out.name); 535 /* NOTREACHED */ 536 nw = 0; 537 } 538 if (pending) { 539 st.bytes += pending; 540 st.sparse += pending/out.dbsz; 541 st.out_full += pending/out.dbsz; 542 pending = 0; 543 } 544 outp += nw; 545 st.bytes += nw; 546 if (nw == n) { 547 if ((uint64_t)n != out.dbsz) 548 ++st.out_part; 549 else 550 ++st.out_full; 551 break; 552 } 553 ++st.out_part; 554 if (nw == cnt) 555 break; 556 if (out.flags & ISCHR && !warned) { 557 warned = 1; 558 warnx("%s: short write on character device", out.name); 559 } 560 if (out.flags & ISTAPE) 561 errx(EXIT_FAILURE, 562 "%s: short write on tape device", out.name); 563 /* NOTREACHED */ 564 565 } 566 if ((out.dbcnt -= n) < out.dbsz) 567 break; 568 } 569 570 /* Reassemble the output block. */ 571 if (out.dbcnt) 572 (void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt); 573 out.dbp = out.db + out.dbcnt; 574 575 if (progress && (st.out_full + st.out_part) % progress == 0) 576 (void)write(STDERR_FILENO, ".", 1); 577 } 578 579 /* 580 * A protected against SIGINFO write 581 */ 582 ssize_t 583 bwrite(IO *io, const void *buf, size_t len) 584 { 585 sigset_t oset; 586 ssize_t rv; 587 int oerrno; 588 589 (void)sigprocmask(SIG_BLOCK, &infoset, &oset); 590 rv = io->ops->op_write(io->fd, buf, len); 591 oerrno = errno; 592 (void)sigprocmask(SIG_SETMASK, &oset, NULL); 593 errno = oerrno; 594 return (rv); 595 } 596