From 1ba53eead1ab9787e77dc43196b8402ec96e7fd4 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 23 Feb 2021 14:24:19 -0800 Subject: dd: simplify signal handling and fix status output. Other dd implementations always show the status on exit, whether success or failure. Fix that by using xsigatexit() (and clarify the comment for that function a little, since it didn't previously address the "at exit" part of its behavior at all). This also fixes SIGUSR1 behavior so that we show the status immediately rather than on the next trip round the read/write loop. Tested with `dd of=/dev/full`, sending SIGUSR1 twice from another shell (to see the status immediately each time, without exiting), then hitting ^C (to see the status and then exiting), then restarting dd and hitting enter (to see a write error followed by the status before exiting). Bug: https://issuetracker.google.com/177017283 --- toys/pending/dd.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) (limited to 'toys/pending') diff --git a/toys/pending/dd.c b/toys/pending/dd.c index 8f4e711e..199e5914 100644 --- a/toys/pending/dd.c +++ b/toys/pending/dd.c @@ -98,8 +98,8 @@ static void status() } } -static void dd_sigint(int sig) { - status(); +static void dd_sigint(int sig) +{ toys.exitval = sig|128; xexit(); } @@ -174,8 +174,9 @@ void dd_main() } if (bs) TT.in.sz = TT.out.sz = bs; - signal(SIGINT, dd_sigint); - signal(SIGUSR1, generic_signal); + sigatexit(status); + xsignal(SIGINT, dd_sigint); + xsignal(SIGUSR1, status); gettimeofday(&TT.start, NULL); // For bs=, in/out is done as it is. so only in.sz is enough. @@ -238,13 +239,6 @@ void dd_main() int chunk = bytes_left < TT.in.sz ? bytes_left : TT.in.sz; ssize_t n; - // Show progress and exit on SIGINT or just continue on SIGUSR1. - if (toys.signal) { - status(); - if (toys.signal==SIGINT) exit_signal(toys.signal); - toys.signal = 0; - } - TT.in.bp = TT.in.buff + TT.in.count; if (TT.conv & _DD_conv_sync) memset(TT.in.bp, 0, TT.in.sz); if (!(n = read(TT.in.fd, TT.in.bp, chunk))) break; @@ -288,6 +282,4 @@ void dd_main() close(TT.in.fd); close(TT.out.fd); if (TT.in.buff) free(TT.in.buff); - - status(); } -- cgit v1.2.3