summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cpc
diff options
context:
space:
mode:
authorBryan Cantrill <bryan@joyent.com>2019-01-28 17:42:45 -0500
committerDan McDonald <danmcd@joyent.com>2019-01-28 17:42:45 -0500
commit09d80cead6ef4d1a3a186dd195dd0de1ccb19aaa (patch)
treed9263dc925b724c377dd2fbf7bd6e19413b513f5 /usr/src/cmd/cpc
parent918e0d92ec24e67f572737a68faf135dc6409d26 (diff)
downloadillumos-gate-09d80cead6ef4d1a3a186dd195dd0de1ccb19aaa.tar.gz
10288 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> Reviewed by: Andy Fiddaman <af@citrus-it.net> Approved by: Dan McDonald <danmcd@joyent.com>
Diffstat (limited to 'usr/src/cmd/cpc')
-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.
*/