14b88c807SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
44b88c807SRodney W. Grimes * Copyright (c) 1991, 1993, 1994
54b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved.
64b88c807SRodney W. Grimes *
74b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by
84b88c807SRodney W. Grimes * Keith Muller of the University of California, San Diego and Lance
94b88c807SRodney W. Grimes * Visser of Convex Computer Corporation.
104b88c807SRodney W. Grimes *
114b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
124b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions
134b88c807SRodney W. Grimes * are met:
144b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
154b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
164b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
174b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
184b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
204b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software
214b88c807SRodney W. Grimes * without specific prior written permission.
224b88c807SRodney W. Grimes *
234b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
244b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
254b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
264b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
274b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
284b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
294b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
304b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
314b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
324b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
334b88c807SRodney W. Grimes * SUCH DAMAGE.
344b88c807SRodney W. Grimes */
354b88c807SRodney W. Grimes
361ba0e048SPhilippe Charnier #include <sys/types.h>
374b88c807SRodney W. Grimes
38d1d66eacSAlan Somers #include <err.h>
391ee36a5bSEivind Eklund #include <errno.h>
407503d74fSMark Murray #include <inttypes.h>
418acbb227SKyle Evans #include <libutil.h>
424ac11639SEitan Adler #include <signal.h>
43*8dad5eceSKonstantin Belousov #include <stdatomic.h>
444b88c807SRodney W. Grimes #include <stdio.h>
4526f6b0fbSDag-Erling Smørgrav #include <stdlib.h>
46a7c1ac2fSGiorgos Keramidas #include <string.h>
47d1d66eacSAlan Somers #include <time.h>
484b88c807SRodney W. Grimes #include <unistd.h>
494b88c807SRodney W. Grimes
504b88c807SRodney W. Grimes #include "dd.h"
514b88c807SRodney W. Grimes #include "extern.h"
524b88c807SRodney W. Grimes
5375e55112SEdward Tomasz Napierala double
secs_elapsed(void)5475e55112SEdward Tomasz Napierala secs_elapsed(void)
554b88c807SRodney W. Grimes {
56540c7825SAlan Somers struct timespec end, ts_res;
57d1d66eacSAlan Somers double secs, res;
584b88c807SRodney W. Grimes
59540c7825SAlan Somers if (clock_gettime(CLOCK_MONOTONIC, &end))
60540c7825SAlan Somers err(1, "clock_gettime");
61540c7825SAlan Somers if (clock_getres(CLOCK_MONOTONIC, &ts_res))
62540c7825SAlan Somers err(1, "clock_getres");
63540c7825SAlan Somers secs = (end.tv_sec - st.start.tv_sec) + \
64540c7825SAlan Somers (end.tv_nsec - st.start.tv_nsec) * 1e-9;
65540c7825SAlan Somers res = ts_res.tv_sec + ts_res.tv_nsec * 1e-9;
66d1d66eacSAlan Somers if (secs < res)
67d1d66eacSAlan Somers secs = res;
6875e55112SEdward Tomasz Napierala
6975e55112SEdward Tomasz Napierala return (secs);
7075e55112SEdward Tomasz Napierala }
7175e55112SEdward Tomasz Napierala
7275e55112SEdward Tomasz Napierala void
summary(void)7375e55112SEdward Tomasz Napierala summary(void)
7475e55112SEdward Tomasz Napierala {
7575e55112SEdward Tomasz Napierala double secs;
7675e55112SEdward Tomasz Napierala
7775e55112SEdward Tomasz Napierala if (ddflags & C_NOINFO)
7875e55112SEdward Tomasz Napierala return;
7975e55112SEdward Tomasz Napierala
808acbb227SKyle Evans if (ddflags & C_PROGRESS)
814767c42cSKyle Evans fprintf(stderr, "\n");
824767c42cSKyle Evans
8375e55112SEdward Tomasz Napierala secs = secs_elapsed();
8475e55112SEdward Tomasz Napierala
854ac11639SEitan Adler (void)fprintf(stderr,
867503d74fSMark Murray "%ju+%ju records in\n%ju+%ju records out\n",
874b88c807SRodney W. Grimes st.in_full, st.in_part, st.out_full, st.out_part);
884ac11639SEitan Adler if (st.swab)
894ac11639SEitan Adler (void)fprintf(stderr, "%ju odd length swab %s\n",
904b88c807SRodney W. Grimes st.swab, (st.swab == 1) ? "block" : "blocks");
914ac11639SEitan Adler if (st.trunc)
924ac11639SEitan Adler (void)fprintf(stderr, "%ju truncated %s\n",
934b88c807SRodney W. Grimes st.trunc, (st.trunc == 1) ? "block" : "blocks");
94c3f5e9c5SXin LI if (!(ddflags & C_NOXFER)) {
954ac11639SEitan Adler (void)fprintf(stderr,
96540c7825SAlan Somers "%ju bytes transferred in %.6f secs (%.0f bytes/sec)\n",
97ad66f7eeSPoul-Henning Kamp st.bytes, secs, st.bytes / secs);
98c3f5e9c5SXin LI }
994ac11639SEitan Adler need_summary = 0;
1004b88c807SRodney W. Grimes }
1014b88c807SRodney W. Grimes
1024767c42cSKyle Evans void
progress(void)1034767c42cSKyle Evans progress(void)
1044767c42cSKyle Evans {
1058acbb227SKyle Evans static int outlen;
1068acbb227SKyle Evans char si[4 + 1 + 2 + 1]; /* 123 <space> <suffix> NUL */
107ac5d5b05SKyle Evans char iec[4 + 1 + 3 + 1]; /* 123 <space> <suffix> NUL */
1088acbb227SKyle Evans char persec[4 + 1 + 2 + 1]; /* 123 <space> <suffix> NUL */
1098acbb227SKyle Evans char *buf;
1104767c42cSKyle Evans double secs;
1114767c42cSKyle Evans
1124767c42cSKyle Evans secs = secs_elapsed();
1138acbb227SKyle Evans humanize_number(si, sizeof(si), (int64_t)st.bytes, "B", HN_AUTOSCALE,
1148acbb227SKyle Evans HN_DECIMAL | HN_DIVISOR_1000);
1158acbb227SKyle Evans humanize_number(iec, sizeof(iec), (int64_t)st.bytes, "B", HN_AUTOSCALE,
1168acbb227SKyle Evans HN_DECIMAL | HN_IEC_PREFIXES);
117ac5d5b05SKyle Evans humanize_number(persec, sizeof(persec), (int64_t)(st.bytes / secs), "B",
1188acbb227SKyle Evans HN_AUTOSCALE, HN_DECIMAL | HN_DIVISOR_1000);
1198acbb227SKyle Evans asprintf(&buf, " %'ju bytes (%s, %s) transferred %.3fs, %s/s",
1208acbb227SKyle Evans (uintmax_t)st.bytes, si, iec, secs, persec);
121ac5d5b05SKyle Evans outlen = fprintf(stderr, "%-*s\r", outlen, buf) - 1;
1228acbb227SKyle Evans fflush(stderr);
1238acbb227SKyle Evans free(buf);
1244767c42cSKyle Evans need_progress = 0;
1254767c42cSKyle Evans }
1264767c42cSKyle Evans
1274b88c807SRodney W. Grimes /* ARGSUSED */
1284b88c807SRodney W. Grimes void
siginfo_handler(int signo __unused)1294ac11639SEitan Adler siginfo_handler(int signo __unused)
1304b88c807SRodney W. Grimes {
1314b88c807SRodney W. Grimes
1324ac11639SEitan Adler need_summary = 1;
1334b88c807SRodney W. Grimes }
1344b88c807SRodney W. Grimes
1354b88c807SRodney W. Grimes /* ARGSUSED */
1364b88c807SRodney W. Grimes void
sigalarm_handler(int signo __unused)1378acbb227SKyle Evans sigalarm_handler(int signo __unused)
1384767c42cSKyle Evans {
1394767c42cSKyle Evans
1404767c42cSKyle Evans need_progress = 1;
1414767c42cSKyle Evans }
1424767c42cSKyle Evans
143*8dad5eceSKonstantin Belousov static void terminate(int signo) __dead2;
144*8dad5eceSKonstantin Belousov static void
terminate(int signo)1455807f35cSDag-Erling Smørgrav terminate(int signo)
1464b88c807SRodney W. Grimes {
1475807f35cSDag-Erling Smørgrav kill_signal = signo;
148*8dad5eceSKonstantin Belousov summary();
149*8dad5eceSKonstantin Belousov (void)fflush(stderr);
150*8dad5eceSKonstantin Belousov raise(kill_signal);
151*8dad5eceSKonstantin Belousov /* NOT REACHED */
152*8dad5eceSKonstantin Belousov _exit(1);
153*8dad5eceSKonstantin Belousov }
154*8dad5eceSKonstantin Belousov
155*8dad5eceSKonstantin Belousov static sig_atomic_t in_io = 0;
156*8dad5eceSKonstantin Belousov static sig_atomic_t sigint_seen = 0;
157*8dad5eceSKonstantin Belousov
158*8dad5eceSKonstantin Belousov static void
sigint_handler(int signo __unused)159*8dad5eceSKonstantin Belousov sigint_handler(int signo __unused)
160*8dad5eceSKonstantin Belousov {
161*8dad5eceSKonstantin Belousov atomic_signal_fence(memory_order_acquire);
162*8dad5eceSKonstantin Belousov if (in_io)
163*8dad5eceSKonstantin Belousov terminate(SIGINT);
164*8dad5eceSKonstantin Belousov sigint_seen = 1;
1655807f35cSDag-Erling Smørgrav }
1665807f35cSDag-Erling Smørgrav
1675807f35cSDag-Erling Smørgrav void
prepare_io(void)168*8dad5eceSKonstantin Belousov prepare_io(void)
1695807f35cSDag-Erling Smørgrav {
170*8dad5eceSKonstantin Belousov struct sigaction sa;
171*8dad5eceSKonstantin Belousov int error;
1725807f35cSDag-Erling Smørgrav
173*8dad5eceSKonstantin Belousov memset(&sa, 0, sizeof(sa));
174*8dad5eceSKonstantin Belousov sa.sa_flags = SA_NODEFER | SA_RESETHAND;
175*8dad5eceSKonstantin Belousov sa.sa_handler = sigint_handler;
176*8dad5eceSKonstantin Belousov error = sigaction(SIGINT, &sa, 0);
177*8dad5eceSKonstantin Belousov if (error != 0)
178*8dad5eceSKonstantin Belousov err(1, "sigaction");
1795807f35cSDag-Erling Smørgrav }
180*8dad5eceSKonstantin Belousov
181*8dad5eceSKonstantin Belousov void
before_io(void)182*8dad5eceSKonstantin Belousov before_io(void)
183*8dad5eceSKonstantin Belousov {
184*8dad5eceSKonstantin Belousov in_io = 1;
185*8dad5eceSKonstantin Belousov atomic_signal_fence(memory_order_seq_cst);
186*8dad5eceSKonstantin Belousov if (sigint_seen)
187*8dad5eceSKonstantin Belousov terminate(SIGINT);
188*8dad5eceSKonstantin Belousov }
189*8dad5eceSKonstantin Belousov
190*8dad5eceSKonstantin Belousov void
after_io(void)191*8dad5eceSKonstantin Belousov after_io(void)
192*8dad5eceSKonstantin Belousov {
193*8dad5eceSKonstantin Belousov in_io = 0;
194*8dad5eceSKonstantin Belousov atomic_signal_fence(memory_order_seq_cst);
195*8dad5eceSKonstantin Belousov if (sigint_seen)
196*8dad5eceSKonstantin Belousov terminate(SIGINT);
1974b88c807SRodney W. Grimes }
198