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