From 8c00a8c01e99dcdb8ef723f02b90e98fb6f2444c Mon Sep 17 00:00:00 2001 From: kevans Date: Wed, 29 Aug 2018 02:18:13 +0000 Subject: [PATCH] MFC r337505, r337865, r337869: dd status=progress r337505: dd: add status=progress support This reports the current status on a single line every second, mirroring similar functionality in GNU dd, and carefully interacts with SIGINFO. PR: 229615 r337865: dd: Incorporate some changes from imp for status=progress Notable changes from what landed in r337505: - sigalarm handler isn't setup unless we're actually using it - Humanized versions of the amount of data transferred in the progress update r337869: dd(1): Kill off duplicate progress definition following r337865 --- bin/dd/Makefile | 1 + bin/dd/args.c | 2 ++ bin/dd/dd.1 | 6 ++++-- bin/dd/dd.c | 13 +++++++++++-- bin/dd/dd.h | 1 + bin/dd/extern.h | 3 +++ bin/dd/misc.c | 37 +++++++++++++++++++++++++++++++++++++ bin/dd/position.c | 2 ++ 8 files changed, 61 insertions(+), 4 deletions(-) diff --git a/bin/dd/Makefile b/bin/dd/Makefile index df877c854f0..99fa1c72c74 100644 --- a/bin/dd/Makefile +++ b/bin/dd/Makefile @@ -6,6 +6,7 @@ PACKAGE=runtime PROG= dd SRCS= args.c conv.c conv_tab.c dd.c misc.c position.c +LIBADD= util # # Test the character conversion functions. We have to be explicit about diff --git a/bin/dd/args.c b/bin/dd/args.c index 40aff559de4..d5751c99ee4 100644 --- a/bin/dd/args.c +++ b/bin/dd/args.c @@ -303,6 +303,8 @@ f_status(char *arg) ddflags |= C_NOINFO; else if (strcmp(arg, "noxfer") == 0) ddflags |= C_NOXFER; + else if (strcmp(arg, "progress") == 0) + ddflags |= C_PROGRESS; else errx(1, "unknown status %s", arg); } diff --git a/bin/dd/dd.1 b/bin/dd/dd.1 index 9292144da78..65ed98fd09f 100644 --- a/bin/dd/dd.1 +++ b/bin/dd/dd.1 @@ -32,7 +32,7 @@ .\" @(#)dd.1 8.2 (Berkeley) 1/13/94 .\" $FreeBSD$ .\" -.Dd April 2, 2017 +.Dd August 8, 2018 .Dt DD 1 .Os .Sh NAME @@ -164,12 +164,14 @@ bytes per second. Where .Cm value is one of the symbols from the following list. -.Bl -tag -width "noxfer" +.Bl -tag -width "progress" .It Cm noxfer Do not print the transfer statistics as the last line of status output. .It Cm none Do not print the status output. Error messages are shown; informational messages are not. +.It Cm progress +Print basic transfer statistics once per second. .El .It Cm conv Ns = Ns Ar value Ns Op , Ns Ar value ... Where diff --git a/bin/dd/dd.c b/bin/dd/dd.c index 94199e77c68..c3392b69508 100644 --- a/bin/dd/dd.c +++ b/bin/dd/dd.c @@ -50,6 +50,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include @@ -84,15 +85,22 @@ const u_char *ctab; /* conversion table */ char fill_char; /* Character to fill with if defined */ size_t speed = 0; /* maximum speed, in bytes per second */ volatile sig_atomic_t need_summary; +volatile sig_atomic_t need_progress; int main(int argc __unused, char *argv[]) { + struct itimerval itv = { { 1, 0 }, { 1, 0 } }; /* SIGALARM every second, if needed */ + (void)setlocale(LC_CTYPE, ""); jcl(argv); setup(); (void)signal(SIGINFO, siginfo_handler); + if (ddflags & C_PROGRESS) { + (void)signal(SIGALRM, sigalarm_handler); + setitimer(ITIMER_REAL, &itv, NULL); + } (void)signal(SIGINT, terminate); atexit(summary); @@ -420,9 +428,10 @@ dd_in(void) in.dbp += in.dbrcnt; (*cfunc)(); - if (need_summary) { + if (need_summary) summary(); - } + if (need_progress) + progress(); } } diff --git a/bin/dd/dd.h b/bin/dd/dd.h index 196f804c2eb..7054d9cd0df 100644 --- a/bin/dd/dd.h +++ b/bin/dd/dd.h @@ -99,5 +99,6 @@ typedef struct { #define C_STATUS 0x08000000 #define C_NOXFER 0x10000000 #define C_NOINFO 0x20000000 +#define C_PROGRESS 0x40000000 #define C_PARITY (C_PAREVEN | C_PARODD | C_PARNONE | C_PARSET) diff --git a/bin/dd/extern.h b/bin/dd/extern.h index 25440ca1288..ecd59abb3d7 100644 --- a/bin/dd/extern.h +++ b/bin/dd/extern.h @@ -43,7 +43,9 @@ void jcl(char **); void pos_in(void); void pos_out(void); double secs_elapsed(void); +void progress(void); void summary(void); +void sigalarm_handler(int); void siginfo_handler(int); void terminate(int); void unblock(void); @@ -64,3 +66,4 @@ extern const u_char a2ibm_32V[], a2ibm_POSIX[]; extern u_char casetab[]; extern char fill_char; extern volatile sig_atomic_t need_summary; +extern volatile sig_atomic_t need_progress; diff --git a/bin/dd/misc.c b/bin/dd/misc.c index ea0f8d3d643..ea4fcc2955b 100644 --- a/bin/dd/misc.c +++ b/bin/dd/misc.c @@ -44,6 +44,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -81,6 +82,9 @@ summary(void) if (ddflags & C_NOINFO) return; + if (ddflags & C_PROGRESS) + fprintf(stderr, "\n"); + secs = secs_elapsed(); (void)fprintf(stderr, @@ -100,6 +104,31 @@ summary(void) need_summary = 0; } +void +progress(void) +{ + static int outlen; + char si[4 + 1 + 2 + 1]; /* 123 NUL */ + char iec[4 + 1 + 2 + 1]; /* 123 NUL */ + char persec[4 + 1 + 2 + 1]; /* 123 NUL */ + char *buf; + double secs; + + secs = secs_elapsed(); + humanize_number(si, sizeof(si), (int64_t)st.bytes, "B", HN_AUTOSCALE, + HN_DECIMAL | HN_DIVISOR_1000); + humanize_number(iec, sizeof(iec), (int64_t)st.bytes, "B", HN_AUTOSCALE, + HN_DECIMAL | HN_IEC_PREFIXES); + humanize_number(persec, sizeof(iec), (int64_t)(st.bytes / secs), "B", + HN_AUTOSCALE, HN_DECIMAL | HN_DIVISOR_1000); + asprintf(&buf, " %'ju bytes (%s, %s) transferred %.3fs, %s/s", + (uintmax_t)st.bytes, si, iec, secs, persec); + outlen = fprintf(stderr, "%-*s\r", outlen, buf); + fflush(stderr); + free(buf); + need_progress = 0; +} + /* ARGSUSED */ void siginfo_handler(int signo __unused) @@ -108,6 +137,14 @@ siginfo_handler(int signo __unused) need_summary = 1; } +/* ARGSUSED */ +void +sigalarm_handler(int signo __unused) +{ + + need_progress = 1; +} + /* ARGSUSED */ void terminate(int sig) diff --git a/bin/dd/position.c b/bin/dd/position.c index 50542955322..e00b67b9d50 100644 --- a/bin/dd/position.c +++ b/bin/dd/position.c @@ -123,6 +123,8 @@ pos_in(void) --cnt; if (need_summary) summary(); + if (need_progress) + progress(); continue; } -- 2.45.0