Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-112970: Detect and use closefrom() on platforms where it is available #112969

Merged
merged 4 commits into from
Dec 12, 2023

Conversation

thesamesam
Copy link
Contributor

@thesamesam thesamesam commented Dec 11, 2023

glibc-2.34 implements closefrom(3) using the same semantics as on BSD. Check for closefrom in configure and use the check result in fileutils.c rather than hardcoding a FreeBSD check (which was wrong for other BSDs anyway).

@thesamesam thesamesam changed the title Wire up closefrom for Linux gh-112970: Wire up closefrom for Linux Dec 11, 2023
glibc-2.34 implements closefrom(3) using the same semantics as on BSD. Check
for closefrom in configure and use the check result in fileutils.c rather than
hardcoding a FreeBSD check (which was wrong for other BSDs anyway).

Signed-off-by: Sam James <sam@gentoo.org>
Copy link
Contributor

@erlend-aasland erlend-aasland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this makes sense.

Python/fileutils.c Show resolved Hide resolved
Signed-off-by: Sam James <sam@gentoo.org>
@erlend-aasland
Copy link
Contributor

I noticed that closefrom comes in two (or possibly more?) flavours:

  1. void closefrom(int)
  2. int closefrom(int)

We should probably explicitly tell the compiler that we're throwing away the return value, to avoid possible future compiler warnings (-Wunused-result):

diff --git a/Python/fileutils.c b/Python/fileutils.c
index 9d12bc89c9..05cd70cdd0 100644
--- a/Python/fileutils.c
+++ b/Python/fileutils.c
@@ -2922,7 +2922,7 @@ _Py_closerange(int first, int last)
 #ifdef USE_CLOSEFROM
     if (last >= sysconf(_SC_OPEN_MAX)) {
         /* Any errors encountered while closing file descriptors are ignored */
-        closefrom(first);
+        (void)closefrom(first);
     }
     else
 #endif /* USE_CLOSEFROM */

@erlend-aasland erlend-aasland changed the title gh-112970: Wire up closefrom for Linux gh-112970: Detect and use closefrom() on platforms where it is available Dec 11, 2023
There are two variants of closefrom, one returns void, but another returns int,
so let's cast the return value to void to explicitly discard it to avoid -Wunused-result
warnings.

Signed-off-by: Sam James <sam@gentoo.org>
@thesamesam
Copy link
Contributor Author

Ah, my bad for forgetting about that. I did a bunch of porting for packages getting this wrong a few years ago when glibc-2.34 came out, and forgot about it since.

Done, thanks!

@erlend-aasland
Copy link
Contributor

Thanks; I'll land this tomorrow.

@erlend-aasland erlend-aasland merged commit c454e93 into python:main Dec 12, 2023
32 checks passed
@erlend-aasland
Copy link
Contributor

Thanks for the report and patch, @thesamesam. Thanks for the additional review, @serhiy-storchaka.

@thesamesam thesamesam deleted the wire-up-closefrom-linux branch December 12, 2023 14:09
@thesamesam
Copy link
Contributor Author

Thank you folks!

aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
…2969)

glibc-2.34 implements closefrom(3) using the same semantics as on BSD.
Check for closefrom() in configure and use the check result in
fileutils.c, rather than hardcoding a FreeBSD check.

Some implementations of closefrom() return an int. Explicitly discard 
the return value by casting it to void, to avoid future compiler
warnings.

Signed-off-by: Sam James <sam@gentoo.org>
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
…2969)

glibc-2.34 implements closefrom(3) using the same semantics as on BSD.
Check for closefrom() in configure and use the check result in
fileutils.c, rather than hardcoding a FreeBSD check.

Some implementations of closefrom() return an int. Explicitly discard 
the return value by casting it to void, to avoid future compiler
warnings.

Signed-off-by: Sam James <sam@gentoo.org>
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