On Wed, Mar 10, 2021 at 09:41:01AM -0800, Cole Helbling wrote:
On Tue Mar 9, 2021 at 7:40 AM PST, Alyssa Ross wrote:
stdio can buffer output, so if we close stdout without going through stdio, there might be buffered output that is never written. --- vsockserver.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/vsockserver.c b/vsockserver.c index dd9a74a..196056a 100644 --- a/vsockserver.c +++ b/vsockserver.c @@ -90,7 +90,7 @@ int main(int argc, char *argv[])
if (notify) { printf("%" PRIu32 "\n", lport); - close(STDOUT_FILENO); + fclose(stdout); }
setenvf("VSOCKLOCALCID", 1, "%" PRIu32, lcid); -- 2.30.0
I don't write much (if any) C, so I never knew there was both STDOUT_FILENO (as an int) _and_ stdout (as a FILE*). I suppose the most important part is that fclose() uses fflush(3) under the hood to make sure we actually write everything.
Yeah, C on UNIX essentially gives you two different IO interfaces (open/write/close/etc. from POSIX, and fopen/fprintf/fclose/etc. from stdio in the C standard library). The POSIX stuff doesn't know anything about the buffering that stdio does on top of it. Mixing them is probably never a good idea if you're not doing it very deliberately.