From 616153ad1c15e0112afe664f6f5f50f06fa7e073 Mon Sep 17 00:00:00 2001 From: "Nam T. Nguyen" Date: Wed, 21 Jan 2015 14:37:10 -0800 Subject: cgpt: Close stdout on exec'ing flashrom flashrom spills out "Reading flash... SUCCESS" and so on to stdout. This affects scripts calling to "cgpt". So this CL sets stdout to CLOEXEC before exec'ing flashrom. It still leaves stderr and stdin unclosed. BUG=None BRANCH=None TEST=cgpt show /dev/mtd0 2>/dev/null will not show any unnecessary text Change-Id: Ide1414c56f63ffe8bc2385a797f166476dacd732 Reviewed-on: https://chromium-review.googlesource.com/242295 Reviewed-by: Bill Richardson Tested-by: Nam Nguyen Commit-Queue: Nam Nguyen --- cgpt/cgpt_nor.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/cgpt/cgpt_nor.c b/cgpt/cgpt_nor.c index 95c2f495..bf0a1764 100644 --- a/cgpt/cgpt_nor.c +++ b/cgpt/cgpt_nor.c @@ -209,14 +209,18 @@ int ReadNorFlash(char *temp_dir_template) { // Read RW_GPT section from NOR flash to "rw_gpt". ret++; + int fd_flags = fcntl(1, F_GETFD); + // Close stdout on exec so that flashrom does not muck up cgpt's output. + fcntl(1, F_SETFD, FD_CLOEXEC); if (ForkExecL(temp_dir_template, FLASHROM_PATH, "-i", "RW_GPT:rw_gpt", "-r", NULL) != 0) { Error("Cannot exec flashrom to read from RW_GPT section.\n"); RemoveDir(temp_dir_template); - return ret; + } else { + ret = 0; } - ret = 0; + fcntl(1, F_SETFD, fd_flags); return ret; } @@ -230,6 +234,9 @@ int WriteNorFlash(const char *dir) { } ret++; int nr_fails = 0; + int fd_flags = fcntl(1, F_GETFD); + // Close stdout on exec so that flashrom does not muck up cgpt's output. + fcntl(1, F_SETFD, FD_CLOEXEC); if (ForkExecL(dir, FLASHROM_PATH, "-i", "RW_GPT_PRIMARY:rw_gpt_1", "-w", "--fast-verify", NULL) != 0) { Warning("Cannot write the 1st half of rw_gpt back with flashrom.\n"); @@ -240,6 +247,7 @@ int WriteNorFlash(const char *dir) { Warning("Cannot write the 2nd half of rw_gpt back with flashrom.\n"); nr_fails++; } + fcntl(1, F_SETFD, fd_flags); switch (nr_fails) { case 0: ret = 0; break; case 1: Warning("It might still be okay.\n"); break; -- cgit v1.2.1