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 #include <sys/types.h> 37 38 #include <err.h> 39 #include <errno.h> 40 #include <inttypes.h> 41 #include <libutil.h> 42 #include <signal.h> 43 #include <stdatomic.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <time.h> 48 #include <unistd.h> 49 50 #include "dd.h" 51 #include "extern.h" 52 53 double 54 secs_elapsed(void) 55 { 56 struct timespec end, ts_res; 57 double secs, res; 58 59 if (clock_gettime(CLOCK_MONOTONIC, &end)) 60 err(1, "clock_gettime"); 61 if (clock_getres(CLOCK_MONOTONIC, &ts_res)) 62 err(1, "clock_getres"); 63 secs = (end.tv_sec - st.start.tv_sec) + \ 64 (end.tv_nsec - st.start.tv_nsec) * 1e-9; 65 res = ts_res.tv_sec + ts_res.tv_nsec * 1e-9; 66 if (secs < res) 67 secs = res; 68 69 return (secs); 70 } 71 72 void 73 summary(void) 74 { 75 double secs; 76 77 if (ddflags & C_NOINFO) 78 return; 79 80 if (ddflags & C_PROGRESS) 81 fprintf(stderr, "\n"); 82 83 secs = secs_elapsed(); 84 85 (void)fprintf(stderr, 86 "%ju+%ju records in\n%ju+%ju records out\n", 87 st.in_full, st.in_part, st.out_full, st.out_part); 88 if (st.swab) 89 (void)fprintf(stderr, "%ju odd length swab %s\n", 90 st.swab, (st.swab == 1) ? "block" : "blocks"); 91 if (st.trunc) 92 (void)fprintf(stderr, "%ju truncated %s\n", 93 st.trunc, (st.trunc == 1) ? "block" : "blocks"); 94 if (!(ddflags & C_NOXFER)) { 95 (void)fprintf(stderr, 96 "%ju bytes transferred in %.6f secs (%.0f bytes/sec)\n", 97 st.bytes, secs, st.bytes / secs); 98 } 99 need_summary = 0; 100 } 101 102 void 103 progress(void) 104 { 105 static int outlen; 106 char si[4 + 1 + 2 + 1]; /* 123 <space> <suffix> NUL */ 107 char iec[4 + 1 + 3 + 1]; /* 123 <space> <suffix> NUL */ 108 char persec[4 + 1 + 2 + 1]; /* 123 <space> <suffix> NUL */ 109 char *buf; 110 double secs; 111 112 secs = secs_elapsed(); 113 humanize_number(si, sizeof(si), (int64_t)st.bytes, "B", HN_AUTOSCALE, 114 HN_DECIMAL | HN_DIVISOR_1000); 115 humanize_number(iec, sizeof(iec), (int64_t)st.bytes, "B", HN_AUTOSCALE, 116 HN_DECIMAL | HN_IEC_PREFIXES); 117 humanize_number(persec, sizeof(persec), (int64_t)(st.bytes / secs), "B", 118 HN_AUTOSCALE, HN_DECIMAL | HN_DIVISOR_1000); 119 asprintf(&buf, " %'ju bytes (%s, %s) transferred %.3fs, %s/s", 120 (uintmax_t)st.bytes, si, iec, secs, persec); 121 outlen = fprintf(stderr, "%-*s\r", outlen, buf) - 1; 122 fflush(stderr); 123 free(buf); 124 need_progress = 0; 125 } 126 127 /* ARGSUSED */ 128 void 129 siginfo_handler(int signo __unused) 130 { 131 132 need_summary = 1; 133 } 134 135 /* ARGSUSED */ 136 void 137 sigalarm_handler(int signo __unused) 138 { 139 140 need_progress = 1; 141 } 142 143 static void terminate(int signo) __dead2; 144 static void 145 terminate(int signo) 146 { 147 kill_signal = signo; 148 summary(); 149 (void)fflush(stderr); 150 raise(kill_signal); 151 /* NOT REACHED */ 152 _exit(1); 153 } 154 155 static sig_atomic_t in_io = 0; 156 static sig_atomic_t sigint_seen = 0; 157 158 static void 159 sigint_handler(int signo __unused) 160 { 161 atomic_signal_fence(memory_order_acquire); 162 if (in_io) 163 terminate(SIGINT); 164 sigint_seen = 1; 165 } 166 167 void 168 prepare_io(void) 169 { 170 struct sigaction sa; 171 int error; 172 173 memset(&sa, 0, sizeof(sa)); 174 sa.sa_flags = SA_NODEFER | SA_RESETHAND; 175 sa.sa_handler = sigint_handler; 176 error = sigaction(SIGINT, &sa, 0); 177 if (error != 0) 178 err(1, "sigaction"); 179 } 180 181 void 182 before_io(void) 183 { 184 in_io = 1; 185 atomic_signal_fence(memory_order_seq_cst); 186 if (sigint_seen) 187 terminate(SIGINT); 188 } 189 190 void 191 after_io(void) 192 { 193 in_io = 0; 194 atomic_signal_fence(memory_order_seq_cst); 195 if (sigint_seen) 196 terminate(SIGINT); 197 } 198