From 10b5c1163c25792142c13f5eee0df4f93c27aaa2 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Mon, 23 Sep 2024 09:16:14 +0100 Subject: [PATCH] pageant --foreground: close stdout after printing env setup. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a small refinement of my own to Marco Ricci's new mode introduced by the previous commit. If Pageant is being run by a parent process intending to make requests to it, then it's probably put a pipe on Pageant's stdout, and will be reading from that pipe to retrieve the environment setup commands. So it needs to know when it's read enough. Closing stdout immediately makes this as easy as possible, freeing the parent process of the need to count lines of output (and also know how many lines to expect): it can simply read until there's no more data. This also means there's no need to make stdout line-buffered, of course – the fclose will flush it anyway. --- doc/man-pageant.but | 9 +++++++++ unix/pageant.c | 4 +++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/doc/man-pageant.but b/doc/man-pageant.but index 1ece3aec..55c15612 100644 --- a/doc/man-pageant.but +++ b/doc/man-pageant.but @@ -193,6 +193,15 @@ to standard input. This is useful if Pageant is spawned by a parent process that controls or otherwise programmatically interfaces with Pageant. +\lcont{ + +After Pageant prints its environment setup commands, it closes its +standard output, so that the parent process can simply read until it +receives EOF, instead of having to know how many lines of output to +expect. + +} + \S{pageant-manpage-client} CLIENT OPTIONS The following options tell Pageant to operate in client mode, diff --git a/unix/pageant.c b/unix/pageant.c index 43eb1e88..4d7a4fa5 100644 --- a/unix/pageant.c +++ b/unix/pageant.c @@ -1224,8 +1224,10 @@ void run_agent(FILE *logfp, const char *symlink_path) } else if (life == LIFE_PERM) { pageant_fork_and_print_env(false); } else if (life == LIFE_FOREGROUND) { - setvbuf(stdout, NULL, _IOLBF, 0); pageant_print_env(getpid()); + /* Close stdout, so that a parent process at the other end of a pipe + * can do the simple thing of reading up to EOF */ + fclose(stdout); } else if (life == LIFE_DEBUG) { /* Force stdout to be line-buffered in preference to unbuffered, so * that if diagnostic output is being piped somewhere, it will arrive