summaryrefslogtreecommitdiff
path: root/usr/src
diff options
context:
space:
mode:
authorBryan Cantrill <bryan@joyent.com>2018-10-02 23:04:06 +0000
committerBryan Cantrill <bryan@joyent.com>2018-10-02 23:27:14 +0000
commitf5903b691655d2bffd8dfb3bcee76486d7d59240 (patch)
tree6bfce72ad2d79ecb09f3a6311ab82cfe5c1cdc01 /usr/src
parentfe10e0740dd87c89538183dcb46f8787d84d1c54 (diff)
downloadillumos-joyent-f5903b691655d2bffd8dfb3bcee76486d7d59240.tar.gz
OS-7257 race in cpustat(1) results in dropped output
Reviewed by: Robert Mustacchi <rm@joyent.com> Reviewed by: Joshua M. Clulow <jmc@joyent.com> Reviewed by: Rob Johnston <rob.johnston@joyent.com> Approved by: Robert Mustacchi <rm@joyent.com>
Diffstat (limited to 'usr/src')
-rw-r--r--usr/src/cmd/cpc/common/cpustat.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/usr/src/cmd/cpc/common/cpustat.c b/usr/src/cmd/cpc/common/cpustat.c
index 965fbadfea..79daedc50b 100644
--- a/usr/src/cmd/cpc/common/cpustat.c
+++ b/usr/src/cmd/cpc/common/cpustat.c
@@ -23,6 +23,10 @@
* Use is subject to license terms.
*/
+/*
+ * Copyright 2018 Joyent, Inc.
+ */
+
#include <sys/types.h>
#include <sys/processor.h>
#include <sys/pset.h>
@@ -289,6 +293,21 @@ main(int argc, char *argv[])
(void) setvbuf(stdout, NULL, _IOLBF, 0);
/*
+ * By design, cpustat (regrettably) has multiple threads racing in
+ * write(2) to generate output. As there are no guarantees made with
+ * respect to the atomicity of concurrent writes on non-O_APPEND file
+ * descriptors, we must set O_APPEND on stdout to assure that no output
+ * is lost. If cpustat is rearchitected such that only one thread is
+ * generating output (which would also assure that the output is always
+ * in a consistent order), this code should be removed.
+ */
+ if (fcntl(1, F_SETFL, fcntl(1, F_GETFL) | O_APPEND) == -1) {
+ (void) fprintf(stderr, gettext("%s: cannot set output to be "
+ "append-only - %s\n"), opts->pgmname, strerror(errno));
+ return (1);
+ }
+
+ /*
* If no system-mode only sets were created, no soaker threads will be
* needed.
*/