public inbox for linux-xfs@vger.kernel.org
 help / color / mirror / Atom feed
From: "Darrick J. Wong" <darrick.wong@oracle.com>
To: sandeen@redhat.com
Cc: linux-xfs@vger.kernel.org
Subject: [PATCH v2 3/7] xfs_db: fix metadump redirection (again)
Date: Tue, 1 Aug 2017 09:23:33 -0700	[thread overview]
Message-ID: <20170801162333.GI4477@magnolia> (raw)
In-Reply-To: <150153522749.26081.13898276114157159134.stgit@magnolia>

In patch 4944defad4 ("xfs_db: redirect printfs when metadumping to
stdout"), we solved the problem of xfs_db printfs ending up in the
metadump stream by reassigning stdout for the duration of a stdout
metadump.  Unfortunately, musl doesn't allow stdout to be reassigned (in
their view "extern FILE *stdout" means "extern FILE * const stdout"), so
we abandon the old approach in favor of playing games with dup() to
switch the raw file descriptors.

While we're at it, fix a regression where an unconverted outf test
allows progress info to end up in the metadump stream.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
---
v2: fix STDOUT_FILENO usage
---
 db/metadump.c |   47 ++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 36 insertions(+), 11 deletions(-)

diff --git a/db/metadump.c b/db/metadump.c
index be2e712..6a23d58 100644
--- a/db/metadump.c
+++ b/db/metadump.c
@@ -78,6 +78,7 @@ static int		obfuscate = 1;
 static int		zero_stale_data = 1;
 static int		show_warnings = 0;
 static int		progress_since_warning = 0;
+static bool		stdout_metadump;
 
 void
 metadump_init(void)
@@ -137,7 +138,7 @@ print_progress(const char *fmt, ...)
 	va_end(ap);
 	buf[sizeof(buf)-1] = '\0';
 
-	f = (outf == stdout) ? stderr : stdout;
+	f = stdout_metadump ? stderr : stdout;
 	fprintf(f, "\r%-59s", buf);
 	fflush(f);
 	progress_since_warning = 1;
@@ -2750,7 +2751,8 @@ metadump_f(
 	xfs_agnumber_t	agno;
 	int		c;
 	int		start_iocur_sp;
-	bool		stdout_metadump = false;
+	int		outfd = -1;
+	int		ret;
 	char		*p;
 
 	exitcode = 1;
@@ -2870,16 +2872,35 @@ metadump_f(
 		 * metadump operation so that dbprintf and other messages
 		 * are sent to the console instead of polluting the
 		 * metadump stream.
+		 *
+		 * We get to do this the hard way because musl doesn't
+		 * allow reassignment of stdout.
 		 */
-		outf = stdout;
-		stdout = stderr;
+		fflush(stdout);
+		outfd = dup(STDOUT_FILENO);
+		if (outfd < 0) {
+			perror("opening dump stream");
+			goto out;
+		}
+		ret = dup2(STDERR_FILENO, STDOUT_FILENO);
+		if (ret < 0) {
+			perror("redirecting stdout");
+			close(outfd);
+			goto out;
+		}
+		outf = fdopen(outfd, "a");
+		if (outf == NULL) {
+			fprintf(stderr, "cannot create dump stream\n");
+			dup2(outfd, STDOUT_FILENO);
+			close(outfd);
+			goto out;
+		}
 		stdout_metadump = true;
 	} else {
 		outf = fopen(argv[optind], "wb");
 		if (outf == NULL) {
 			print_warning("cannot create dump file");
-			free(metablock);
-			return 0;
+			goto out;
 		}
 	}
 
@@ -2907,15 +2928,19 @@ metadump_f(
 	if (progress_since_warning)
 		fputc('\n', stdout_metadump ? stderr : stdout);
 
-	if (stdout_metadump)
-		stdout = outf;
-	else
-		fclose(outf);
+	if (stdout_metadump) {
+		fflush(outf);
+		fflush(stdout);
+		ret = dup2(outfd, STDOUT_FILENO);
+		if (ret < 0)
+			perror("un-redirecting stdout");
+	}
+	fclose(outf);
 
 	/* cleanup iocur stack */
 	while (iocur_sp > start_iocur_sp)
 		pop_cur();
-
+out:
 	free(metablock);
 
 	return 0;

  parent reply	other threads:[~2017-08-01 16:23 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-31 21:06 [PATCH 0/7] xfsprogs: 4.13 rollup Darrick J. Wong
2017-07-31 21:06 ` [PATCH 1/7] xfs: remove double-underscore integer types Darrick J. Wong
2017-07-31 21:23   ` Eric Sandeen
2017-07-31 21:25     ` Darrick J. Wong
2017-08-02  9:13   ` Carlos Maiolino
2017-08-02 16:01     ` Darrick J. Wong
2017-07-31 21:07 ` [PATCH 2/7] xfs_repair: fix symlink target length checks by changing MAXPATHLEN to XFS_SYMLINK_MAXLEN Darrick J. Wong
2017-07-31 21:42   ` Eric Sandeen
2017-08-02  9:14   ` Carlos Maiolino
2017-07-31 21:07 ` [PATCH 3/7] xfs_db: fix metadump redirection (again) Darrick J. Wong
2017-07-31 21:57   ` Eric Sandeen
2017-08-01 16:23   ` Darrick J. Wong [this message]
2017-08-02  9:17     ` [PATCH v2 " Carlos Maiolino
2017-07-31 21:07 ` [PATCH 4/7] xfs_db: dump dir/attr btrees Darrick J. Wong
2017-07-31 22:05   ` Eric Sandeen
2017-08-01 14:59     ` Darrick J. Wong
2017-08-01 15:40   ` [PATCH v2 " Darrick J. Wong
2017-08-01 16:21     ` Eric Sandeen
2017-08-02  9:22     ` Carlos Maiolino
2017-08-02  9:24     ` Carlos Maiolino
2017-08-02 16:03       ` Darrick J. Wong
2017-07-31 21:07 ` [PATCH 5/7] xfs_db: print attribute remote value blocks Darrick J. Wong
2017-08-01 17:15   ` Eric Sandeen
2017-08-01 20:29     ` Darrick J. Wong
2017-08-01 21:04   ` [PATCH v2 " Darrick J. Wong
2017-08-02  9:36     ` Carlos Maiolino
2017-07-31 21:07 ` [PATCH 6/7] xfs_db: write values into dir/attr blocks and recalculate CRCs Darrick J. Wong
2017-08-02  9:40   ` Carlos Maiolino
2017-08-03 16:02   ` Eric Sandeen
2017-08-03 16:40     ` Darrick J. Wong
2017-07-31 21:07 ` [PATCH 7/7] xfs_db: introduce fuzz command Darrick J. Wong
2017-08-02 11:06   ` Carlos Maiolino
2017-08-03 16:47   ` [PATCH 8/7] xfs_db: use TYP_F_CRC_FUNC for inodes & dquots Eric Sandeen
2017-08-03 16:58     ` Darrick J. Wong
2017-08-03 17:15     ` [PATCH 8/7 V2] " Eric Sandeen
2017-08-03 18:05       ` Darrick J. Wong
2017-08-03 17:04 ` [PATCH 9/7] xfs_db: btdump should avoid eval for push and pop of cursor Darrick J. Wong
2017-08-03 17:18   ` Eric Sandeen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170801162333.GI4477@magnolia \
    --to=darrick.wong@oracle.com \
    --cc=linux-xfs@vger.kernel.org \
    --cc=sandeen@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox