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