From c6b1d1dd870b3370d0a09fb4600e3a6b03326f35 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 23 Nov 2022 08:57:45 +0200 Subject: Rework diag_buffer interface to facilitate correct destruction order --- libbuild2/script/run.cxx | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) (limited to 'libbuild2/script') diff --git a/libbuild2/script/run.cxx b/libbuild2/script/run.cxx index cbd750f..550fdc1 100644 --- a/libbuild2/script/run.cxx +++ b/libbuild2/script/run.cxx @@ -2059,31 +2059,45 @@ namespace build2 { case redirect_type::pass: { - if (dfd == 2) // stderr? + try { - // Deduce the args0 argument similar to cmd_path(). - // - process::pipe ep ( + if (dfd == 2) // stderr? + { + fdpipe p; + if (diag_buffer::pipe (env.context) == -1) // Are we buffering? + p = fdopen_pipe (); + + // @@ TODO: perhaps this lambda should return process::pipe? + // (see the simple test rule implementatio for an + // example). + // + // Note that we must return non-owning fd to our end of the + // pipe (see the process class for details). + // + // process::pipe r (p.in.get (), move (p.out)); + + // Deduce the args0 argument similar to cmd_path(). + // + // Note that we must open the diag buffer regardless of the + // diag_buffer::pipe() result. + // pc.dbuf.open ((c.program.initial == nullptr ? c.program.recall.string ().c_str () : c.program.recall_string ()), - false /* force */, - fdstream_mode::non_blocking)); + move (p.in), + fdstream_mode::non_blocking); - if (ep.out != 2) // Are we buffering stderr? - { - ep.own_out = false; - return auto_fd (ep.out); + if (p.out != nullfd) + return move (p.out); + + // Fall through. } - } - try - { return fddup (dfd); } catch (const io_error& e) { - fail (ll) << "unable to duplicate " << what << ": " << e; + fail (ll) << "unable to redirect " << what << ": " << e; } } -- cgit v1.1