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