Skip to content

dhcpcd: stdout output sometimes empty when redirected to a file#364

Merged
rsmarples merged 1 commit intoNetworkConfiguration:masterfrom
diego-santacruz:empty-dumplease-fix
Oct 10, 2024
Merged

dhcpcd: stdout output sometimes empty when redirected to a file#364
rsmarples merged 1 commit intoNetworkConfiguration:masterfrom
diego-santacruz:empty-dumplease-fix

Conversation

@diego-santacruz
Copy link
Contributor

Running dhpcd --dumplease with input from stdin and redirecting stdout to a file outputs nothing to the output. The reason is that printf / fprintf is used to write the output but it is not explicitly flushed when done, or before exiting, and stdout to files is fully buffered by default while stdout to terminals is line buffered by default.

This adds calls to fflush() at the end of dhcp_print_option_encoding() and script_dump(), plus one just before exiting dhcpcd.

Running dhpcd --dumplease with input from stdin and redirecting stdout
to a file outputs nothing to the output. The reason is that printf /
fprintf is used to write the output but it is not explicitly flushed
when done, or before exiting, and stdout to files is fully buffered by
default while stdout to terminals is line buffered by default.

This adds calls to fflush() at the end of dhcp_print_option_encoding()
and script_dump(), plus one just before exiting dhcpcd.

Signed-off-by: Diego Santa Cruz <[email protected]>
@cross
Copy link

cross commented Oct 11, 2024

Curious, why does the output not flush when dhcpcd exits? exit() should close stdout, which will implicitly flush it, right?

@diego-santacruz
Copy link
Contributor Author

Curious, why does the output not flush when dhcpcd exits? exit() should close stdout, which will implicitly flush it, right?

Good question, I think it is because with --dumplease the processing in main() pretends that the process forked and by the end of main _exit() is called if the process forked, for which flushing of stdio streams is platform-dependent and I guess not being done in Linux with glibc. The code is a bit difficult to follow though, so I may be mistaken.

@cross
Copy link

cross commented Oct 11, 2024

Ahh. It uses _exit(). Then that explains it. Why it uses _exit() I don't know, but that answers my question. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants