Thu Jan 7 12:02:52 2021 UTC ()
progress: handle EINTR in writes. PR/55914


(lukem)
diff -r1.22 -r1.23 src/usr.bin/progress/progress.c

cvs diff -r1.22 -r1.23 src/usr.bin/progress/progress.c (expand / switch to unified diff)

--- src/usr.bin/progress/progress.c 2020/04/25 11:12:39 1.22
+++ src/usr.bin/progress/progress.c 2021/01/07 12:02:52 1.23
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: progress.c,v 1.22 2020/04/25 11:12:39 simonb Exp $ */ 1/* $NetBSD: progress.c,v 1.23 2021/01/07 12:02:52 lukem Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc. 4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by John Hawkinson. 8 * by John Hawkinson.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -21,27 +21,27 @@ @@ -21,27 +21,27 @@
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33#ifndef lint 33#ifndef lint
34__RCSID("$NetBSD: progress.c,v 1.22 2020/04/25 11:12:39 simonb Exp $"); 34__RCSID("$NetBSD: progress.c,v 1.23 2021/01/07 12:02:52 lukem Exp $");
35#endif /* not lint */ 35#endif /* not lint */
36 36
37#include <sys/types.h> 37#include <sys/types.h>
38#include <sys/ioctl.h> 38#include <sys/ioctl.h>
39#include <sys/stat.h> 39#include <sys/stat.h>
40#include <sys/wait.h> 40#include <sys/wait.h>
41 41
42#include <err.h> 42#include <err.h>
43#include <errno.h> 43#include <errno.h>
44#include <fcntl.h> 44#include <fcntl.h>
45#include <inttypes.h> 45#include <inttypes.h>
46#include <limits.h> 46#include <limits.h>
47#include <signal.h> 47#include <signal.h>
@@ -226,26 +226,30 @@ main(int argc, char *argv[]) @@ -226,26 +226,30 @@ main(int argc, char *argv[])
226 226
227 signal(SIGPIPE, broken_pipe); 227 signal(SIGPIPE, broken_pipe);
228 progressmeter(-1); 228 progressmeter(-1);
229 229
230 while (1) { 230 while (1) {
231 do { 231 do {
232 nr = read(fd, fb_buf, buffersize); 232 nr = read(fd, fb_buf, buffersize);
233 } while (nr < 0 && errno == EINTR); 233 } while (nr < 0 && errno == EINTR);
234 if (nr <= 0) 234 if (nr <= 0)
235 break; 235 break;
236 for (off = 0; nr; nr -= nw, off += nw, bytes += nw) 236 for (off = 0; nr; nr -= nw, off += nw, bytes += nw)
237 if ((nw = write(outpipe[1], fb_buf + off, 237 if ((nw = write(outpipe[1], fb_buf + off,
238 (size_t) nr)) < 0) { 238 (size_t) nr)) < 0) {
 239 if (errno == EINTR) {
 240 nw = 0;
 241 continue;
 242 }
239 progressmeter(1); 243 progressmeter(1);
240 err(1, "writing %u bytes to output pipe", 244 err(1, "writing %u bytes to output pipe",
241 (unsigned) nr); 245 (unsigned) nr);
242 } 246 }
243 } 247 }
244 close(outpipe[1]); 248 close(outpipe[1]);
245 249
246 gzipstat = 0; 250 gzipstat = 0;
247 cmdstat = 0; 251 cmdstat = 0;
248 while (pid || gzippid) { 252 while (pid || gzippid) {
249 deadpid = wait(&ws); 253 deadpid = wait(&ws);
250 /* 254 /*
251 * We need to exit with an error if the command (or gzip) 255 * We need to exit with an error if the command (or gzip)