Commit graph

82 commits

Author SHA1 Message Date
Warner Losh 0b8224d1cc Remove copyright strings ifdef'd out
We've ifdef'd out the copyright strings for some time now. Go ahead and
remove the ifdefs. Plus whatever other detritis was left over from other
recent removals. These copyright strings are present in the comments and
are largely from CSRG's attempt at adding their copyright to every
binary file (which modern interpretations of the license doesn't
require).

Sponsored by:		Netflix
2023-11-26 22:23:58 -07:00
Warner Losh 90aea514c6 bin: Remove ancient SCCS tags.
Remove ancient SCCS tags from the tree, automated scripting, with two
minor fixup to keep things compiling. All the common forms in the tree
were removed with a perl script.

Sponsored by:		Netflix
2023-11-26 22:23:28 -07:00
Warner Losh 1d386b48a5 Remove $FreeBSD$: one-line .c pattern
Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
2023-08-16 11:54:42 -06:00
Konstantin Belousov 8dad5ece49 dd(1): neutralize SIGINT while non-async-signal safe code is executing
making the SIGINT handler (the terminate() function) safe to execute at
any interruption moment.  This fixes a race in
5807f35c54 where SIGINT delivered right
after the check_terminate() but before a blocking syscall would not
cause abort.

Do it by setting the in_io flag around potentially blocking io syscalls.
If handler sees the flag, it terminates the program.  Otherwise,
termination is delegated to the before_io/after_io fences.

Reviewed by:	Andrew Gierth <andrew@tao146.riddles.org.uk>
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D40281
2023-06-03 01:06:27 +03:00
Dag-Erling Smørgrav 5807f35c54 dd: Fix SIGINT handling.
Currently, we handle SIGINT by calling summary() and _exit() directly from the signal handler, which we install after setup().  There are several issues with this:

* summary() is not signal safe;
* the parent is not informed about the signal;
* setup() can block on open(), and catching SIGINT at that stage will produce the correct exit status but will not print anything to stderr as POSIX demands.

Fix this by making SIGINT non-restartable, changing our signal handler to only set a flag, installing it before setup(), and checking the termination flag before and after every blocking operation, i.e. open(), read(), write().

Also add two test cases, one for catching SIGINT while opening the input and one for catching it while reading.  I couldn't think of an easy way to test catching SIGINT while writing (it's certainly feasible, but perhaps not from a shell script).

MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	cracauer, ngie, imp
Differential Revision:	https://reviews.freebsd.org/D39641
2023-05-05 12:42:32 +00:00
Richard Scheffenegger f4b4526f23 Add O_DIRECT flag to DD for cache bypass
FreeBSD DD utility has not had support for the O_DIRECT flag, which
is useful to bypass local caching, e.g. for unconditionally issuing
NFS IO requests during testing.

Reviewed by:	rgrimes (mentor)
Approved by:	rgrimes (mentor, blanket)
MFC after:	3 weeks
Sponsored by:	NetApp, Inc.
Differential Revision:	https://reviews.freebsd.org/D25066
2020-06-04 20:47:11 +00:00
Matt Macy b52c534bff Add iflag=fullblock to dd
Normally, count=n means read(2) will be called n times on the input to dd. If
the read() returns short, as may happen when reading from a pipe, fewer bytes
will be copied from the input. With conv=sync the buffer is padded with zeros
to fill the rest of the block.

iflag=fullblock causes dd to continue reading until the block is full, so that
count=n means n full blocks are copied. This flag is compatible with illumos
and GNU dd and is used in the ZFS test suite.

Submitted by:	Ryan Moeller
Reviewed by:	manpages, mmacy@
MFC after:	1 week
Sponsored by:	 iXsystems, Inc.
Differential Revision:	https://reviews.freebsd.org/D21441
2019-09-30 22:00:48 +00:00
Matt Macy 919156e34c Add oflag=fsync and oflag=sync capability to dd
Sets the O_FSYNC flag on the output file. oflag=fsync and oflag=sync are
synonyms just as O_FSYNC and O_SYNC are synonyms. This functionality is
intended to improve portability of dd commands in the ZFS test suite.

Submitted by:	Ryan Moeller
Reviewed by:	manpages, mmacy@
MFC after:	1 week
Sponsored by:	 iXsytems, Inc.
Differential Revision:	https://reviews.freebsd.org/D21422
2019-09-30 21:56:42 +00:00
Matt Macy 2048fe7098 dd: Check result of close(2) for errors
close(2) can return errors from previous operations which should not be ignored.

PR: 229616
Submitted by:	Thomas Hurst
Reported by:	Thomas Hurst
Reviewed by:	mmacy@
Obtained from:	Ryan Moeller
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D21376
2019-09-30 21:53:26 +00:00
Matt Macy 3b96efbda0 Add conv=fdatasync flag to dd
The fdatasync flag performs an fdatasync(2) on the output file before closing it.
This will be useful for the ZFS test suite.

Submitted by:	Ryan Moeller
Reviewed by:	manpages, mmacy@
MFC after:	1 week
Sponsored by:	iXSystems, Inc.
Differential Revision:	https://reviews.freebsd.org/D21373
2019-09-30 21:48:12 +00:00
Matt Macy ce1b19d8c8 Add conv=fsync flag to dd
The fsync flag performs an fsync(2) on the output file before closing it.
This will be useful for the ZFS test suite.

Submitted by:	ryan@ixsystems.com
Reviewed by:	jilles@, imp@
MFC after:	1 week
Sponsored by:	iXsystems, Inc.
2019-09-03 18:35:55 +00:00
Maxim Sobolev dead7b5e47 Replace hand-crafted naive byte-by-byte zero block detection routine
with macro based around memcmp(). The latter is expected to be some
8 times faster on a modern 64-bit architectures.

In practice, throughput of doing conv=sparse from /dev/zero to /dev/null
went up some 5-fold here from 1.9GB/sec to 9.7GB/sec with this change
(bs=128k).

MFC after:	2 weeks
2018-11-29 19:28:01 +00:00
Mariusz Zaborski 377421df96 capsicum: use a new capsicum helpers in tools
Use caph_{rights,ioctls,fcntls}_limit to simplify the code.
2018-11-04 19:24:49 +00:00
Kyle Evans 8acbb227d9 dd: Incorporate some changes from imp for status=progress
Notable changes from what landed in r337505:
- sigalarm handler isn't setup unless we're actually using it
- Humanized versions of the amount of data transferred in the progress
  update

Submitted by:	imp
Reviewed by:	kevans
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D16642
2018-08-15 19:46:13 +00:00
Kyle Evans 4767c42c11 dd: add status=progress support
This reports the current status on a single line every second, mirroring
similar functionality in GNU dd, and carefully interacts with SIGINFO.

PR:		229615
Submitted by:	Thomas Hurst <tom@hur.st> (modified for style(9) nits by me)
MFC after:	1 week
2018-08-08 21:37:02 +00:00
Mariusz Zaborski 7672a0148f Convert cap_enter() < 0 && errno != ENOSYS to caph_enter() < 0.
No functional change intended.
2018-06-19 23:43:14 +00:00
Eitan Adler 44e0a832f2 dd(1): Use a local swapbytes() function.
swab(3) has restrict qualifiers for src and dst.
Avoid relying on undefined overlapping swab behavior.

Obtained From: OpenBSD
2018-01-26 03:30:05 +00:00
Pedro F. Giffuni 8a16b7a18f General further adoption of SPDX licensing ID tags.
Mainly focus on files that use BSD 3-Clause license.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

Special thanks to Wind River for providing access to "The Duke of
Highlander" tool: an older (2014) run over FreeBSD tree was useful as a
starting point.
2017-11-20 19:49:47 +00:00
Alan Somers 77a798b30a dd(1): Incorrect casting of arguments
dd(1) casts many of its numeric arguments from uintmax_t to intmax_t and
back again to detect whether or not the original arguments were negative.
This is not correct, and causes problems with boundary cases, for example
when count is SSIZE_MAX-1.

PR:		191263
Submitted by:	will@worrbase.com
Reviewed by:	pi, asomers
MFC after:	3 weeks
2017-08-25 15:31:55 +00:00
Warner Losh fbbd9655e5 Renumber copyright clause 4
Renumber cluase 4 to 3, per what everybody else did when BSD granted
them permission to remove clause 3. My insistance on keeping the same
numbering for legal reasons is too pedantic, so give up on that point.

Submitted by:	Jan Schaumann <jschauma@stevens.edu>
Pull Request:	https://github.com/freebsd/freebsd/pull/96
2017-02-28 23:42:47 +00:00
Bartek Rutkowski 94161f30b7 Fix regression when stdin/out/err fds are are overridden by shell.
Submitted by:	Pawel Biernacki <pawel.biernacki@gmail.com>
Reported by:	ngie
Approved by:	ngie
Sponsored by:	Mysterious Code Ltd.
Differential Revision:	https://reviews.freebsd.org/D8543
2016-12-12 18:56:40 +00:00
Bartek Rutkowski 2a65657fc8 Capsicum support for dd(1)
Adds Capsicum sandboxing to dd utility.

Submitted by:	Pawel Biernacki <pawel.biernacki@gmail.com>
Reviewed by:	allanjude, emaste, oshogbo
Approved by:	oshogbo
Sponsored by:	Mysterious Code Ltd.
Differential Revision:	https://reviews.freebsd.org/D8543
2016-12-09 14:51:05 +00:00
Adrian Chadd 29aee14890 [dd] Revert the capsicum bits for now until it gets fixed.
dd is a bootstrap tool and that header isn't installed as part of the
bootstrap environment for previous releases (eg freebsd-10.)

We'll figure it out in post and then re-commit it.
2016-11-19 18:19:21 +00:00
Ruslan Bukin 6193edf4c5 Restore dd changes included accidentally in r308857. 2016-11-19 17:51:02 +00:00
Ruslan Bukin 9a8f61fb5b Bring in support for Ingenic XBurst JZ4780 and
X1000 systems on chips.

Imgtec CI20 and Ingenic CANNA boards supported.

Submitted by:	Alexander Kabaev <kan@FreeBSD.org>
Reviewed by:	Ruslan Bukin <br@FreeBSD.org>
Sponsored by:	DARPA, AFRL
2016-11-19 17:46:18 +00:00
Bartek Rutkowski 947b83c090 Capsicum support for dd(1)
Adds Capsicum sandboxing to dd utility.

Submitted by:	Pawel Biernacki <pawel.biernacki@gmail.com>
Reviewed by:	allanjude, emaste, oshogbo
Approved by:	oshogbo
Sponsored by:	Mysterious Code Ltd.
Differential Revision:	https://reviews.freebsd.org/D8543
2016-11-18 21:09:57 +00:00
Edward Tomasz Napierala 75e5511214 Add speed limit to dd(1). This is useful for testing RCTL disk io limits
(when they actually get committed, that is), and might also come in handy
in other situations.

Reviewed by:	wblock@ (man page)
MFC after:	1 month
Sponsored by:	The FreeBSD Foundation
2016-02-28 10:27:12 +00:00
Thomas Quinot cd1832fe92 Reorganize the handling all-zeroes terminal block in sparse mode
The intent of the previous code in that case was to force
an explicit write, but the implementation was incorrect, and
as a result the write was never performed. This new implementation
instead uses ftruncate(2) to extend the file with a trailing hole.

Also introduce regression tests for these cases.

PR: 189284
(original PR whose fix introduced this bug)

PR: 207092

Differential Revision:	D5248
Reviewed by:	sobomax,kib
MFC after:	2 weeks
2016-02-18 08:44:16 +00:00
Kurt Jaeger f4cfd28bb5 bin/dd: revert 273734, as it fails on 32bit platforms
Revert: insufficient testing on 32bit platforms

PR:		191263
2014-10-27 17:39:37 +00:00
Kurt Jaeger 179eb7112b bin/dd: Fix incorrect casting of arguments
dd(1) casts many of its numeric arguments from uintmax_t to intmax_t
and back again to detect whether or not the original arguments were
negative. This caused wrong behaviour in some boundary cases:

$ dd if=/dev/zero of=/dev/null count=18446744073709551615
dd: count cannot be negative

After the fix:

$ dd if=/dev/zero of=/dev/null count=18446744073709551615
dd: count: Result too large

PR:		191263
Submitted by:	will@worrbase.com
Approved by:	cognet@
2014-10-27 11:38:17 +00:00
Alan Somers 540c78258c Incorporate feedback from bde and jilles regarding r265472 to dd(1).
* Don't use sysexits.h.  Just exit 1 on error and 0 otherwise.
* Don't sacrifice precision by converting the output of clock_gettime() to a
  double and then comparing the results.  Instead, subtract the values of
  the two clock_gettime() calls, then convert to double.
* Don't use CLOCK_MONOTONIC_PRECISE.  It's an unportable synonym for
  CLOCK_MONOTONIC.
* Use more appropriate names for some local variables.
* In the summary message, round elapsed time to the nearest microsecond.

Reported by:	bde, jilles
MFC after:	3 days
X-MFC-With:	265472
2014-05-08 19:10:04 +00:00
Thomas Quinot aa7a161b40 (dd_out): Fix handling of all-zeroes block at end of input with
conv=sparse.

This change fixes two separate issues observed when the last output
block is all zeroes, and conv=sparse is in use. In this case, care
must be taken to roll back the last seek and write the entire last zero
block at the original offset where it should have occurred: when the
destination file is a block device, it is not possible to roll back
by just one character as the write would then not be properly aligned.

Furthermore, the buffer used to write this last all-zeroes block
needs to be properly zeroed-out. This was not the case previously,
resulting in a junk data byte appearing instead of a zero in the
output stream.

PR:		bin/189174
PR:		bin/189284
Reviewed by:	kib
MFC after:	2 weeks
2014-05-07 19:33:29 +00:00
Alan Somers d1d66eac15 dd(1) uses gettimeofday(2) to compute the throughput statistics. However,
gettimeofday returns the system clock, which may jump forward or back,
especially if NTP is in use.  If the time jumps backwards, then dd will see
negative elapsed time, round it up to 1usec, and print an absurdly fast
transfer rate.

The solution is to use clock_gettime(2) with CLOCK_MONOTONIC_PRECISE as the
clock_id.  That clock advances steadily, regardless of changes to the system
clock.

Reviewed by:	delphij
MFC after:	3 days
Sponsored by:	Spectra Logic
2014-05-06 22:06:39 +00:00
Xin LI 413ef2a3d1 Make C_* contants fit in 32 bits again by using 4 unused bits.
Noticed by:	bde
2014-04-03 06:32:02 +00:00
Xin LI c3f5e9c515 Implement GNU's extension of 'status' operand. The GNU syntax is
borrowed where syntax status=noxfer means no transfer statistics
and status=none means no status information at all.

This feature is useful because the statistics information can
sometimes be annoying, and redirecting stderr to /dev/null would
mean error messages also gets silenced.

Obtained from:	OpenBSD
MFC after:	2 weeks
2014-04-03 00:55:16 +00:00
Eitan Adler 4ac1163902 Make dd's signal handler async safe.
PR:		bin/75258
Submitted by:	"Oleg V. Nauman" <oleg@reis.zp.ua>
Arrival Date:	Sun Dec 19 14:50:21 GMT 2004
Reviewed by:	mjg, jhb
Reviewed by:	jilles (earlier version)
MFC after:	1 week
2013-05-10 18:43:36 +00:00
Konstantin Belousov 6784d416a5 Literally follow POSIX:
If the bs= expr operand is specified and no conversions other than sync,
noerror, or notrunc are requested, the data returned from each input
block shall be written as a separate output block.

In particular, when both bs=size and conv=sparce were specified, the
resulted file was fully filled, instead of sparce.

PR:	standards/177742
Submitted by:	Matthew Rezny <mrezny@hexaneinc.com>
MFC after:	2 weeks
2013-04-23 16:08:24 +00:00
Brooks Davis c183a03b17 IFP4 change 222074.
Introduce an explicit close of the output descriptor so that work done
on close is accounted for in the summary output triggered at exit
(implicit close()s occur after atexit() hooks).

This is useful because some devices such as cfi(4) may perform
signficant work after a close occurs (e.g. erasing and rewriting a
block of flash).
2013-04-03 19:19:45 +00:00
Ed Schouten 902d9eafbf Rework all non-contributed files that use `struct timezone'.
This structure is not part of POSIX. According to POSIX, gettimeofday()
has the following prototype:

	int gettimeofday(struct timeval *restrict tp, void *restrict tzp);

Also, POSIX states that gettimeofday() shall return 0 (as long as tzp is
not used). Remove dead error handling code. Also use NULL for a
nul-pointer instead of integer 0.

While there, change all pieces of code that only use tv_sec to use
time(3), as this provides less overhead.
2012-09-01 14:45:15 +00:00
Robert Watson e3edab4a91 Add a "fillchar" command line argument to dd(1) that permits the user
to specify an alternative padding character when using a conversion
mode, or when using noerror with sync and an input error occurs.  This
facilities reading old and error-prone media by allowing the user to
more effectively mark error blocks in the output stream.
2004-08-15 19:10:05 +00:00
Mark Murray 6195fb4102 Remove clause 3 from the UCB licenses.
OK'ed by:	imp, core
2004-04-06 20:06:54 +00:00
Poul-Henning Kamp 6a3d33ac5e Teach dd(1) about parity bits. 2004-03-05 19:35:51 +00:00
David E. O'Brien 09a80d4867 Quiet warnings about copyright[]. 2003-05-01 16:58:57 +00:00
Mark Murray 7503d74f54 WARNS=4 fixes. This would be WARNS=9 if we were -std=99 instead of
-ansi, due to 'long long'.

Reviewed by:	green (slightly earlier version)
2003-02-27 18:04:54 +00:00
Poul-Henning Kamp cd967e322a Don't call DIOCWLABEL on disks, it is not implemented, and calling it
like this negated any practical value of the feature.
2003-01-26 11:13:40 +00:00
David E. O'Brien 5eb43ac2f7 Consistently use __FBSDID 2002-06-30 05:13:54 +00:00
Mark Murray afa5289a98 1) Rev.1.35 of dd.c has a more serious regression. It backs out rev.1.31,
thus breaking systems with unpolluted <sys/stat.h>'s.

2) Back out an initialisation of a variable in BSS.

Reported by:	bde (1), many(2)
2002-03-07 14:00:33 +00:00
Mark Murray 9afa09cd0e Fix warnings inspired by lint, a commercial lint and WARNS=4. 2002-02-22 20:51:00 +00:00
Warner Losh f9bcb0beb5 o __P has been reoved
o Old-style K&R declarations have been converted to new C89 style
o register has been removed
o prototype for main() has been removed (gcc3 makes it an error)
o int main(int argc, char *argv[]) is the preferred main definition.
o Attempt to not break style(9) conformance for declarations more than
  they already are.

Approved by: arch@, new style(9)
2002-02-02 06:24:13 +00:00
Brian Feldman 4ed955372c Commit general cleanups (separate get_num() and get_off_t() functions to
debogosify some of the command-line string-number conversions into
an unsigned and signed variant.)
2002-01-25 17:44:47 +00:00