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