GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-144130: support getrandom syscall on non-linux by dankm · Pull Request #144131 · python/cpython

Conversation

@dankm

@dankm dankm commented Jan 22, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

At least FreeBSD and NetBSD also implement getrandom using the same semantics as Linux >= 3.17. They don't define any of it it in linux/random.h, however. They do define all the necessary bits in sys/random.h. glibc >= 2.25 also defines it there as well as in linux/random.h. Simply use sys/random.h when checking for its existence.

  • Issue: Support getrandom syscall on FreeBSD #144130

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@dankm
dankm requested review from AA-Turner, corona10, emmatyping and erlend-aasland as code owners January 22, 2026 00:54
@bedevere-app

bedevere-app Bot commented Jan 22, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@python-cla-bot

python-cla-bot Bot commented Jan 22, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added the awaiting review label Jan 22, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Jan 22, 2026
Support getrandom syscall on FreeBSD #144130
Open
@dankm
dankm force-pushed the dankm/getrandom branch from 7b9fb51 to 5da4196 Compare January 22, 2026 01:26
@bedevere-app

bedevere-app Bot commented Jan 22, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@dankm
dankm force-pushed the dankm/getrandom branch from fe5aacd to 3e595a5 Compare January 22, 2026 21:11
dankm and others added 2 commits January 22, 2026 16:25
@dankm
support getrandom syscall on non-linux
47afea8
At least FreeBSD and NetBSD also implement getrandom using the same
semantics as Linux >= 3.17. They don't define any of it it in
linux/random.h, however. They do define all the necessary bits in
sys/random.h. glibc >= 2.25 also defines it there as well as in
linux/random.h. Simply use sys/random.h when checking for its existence.
@blurb-it @dankm
📜🤖 Added by blurb_it.
f3af163
@dankm
dankm force-pushed the dankm/getrandom branch from 3e595a5 to f3af163 Compare January 22, 2026 22:25
serhiy-storchaka
serhiy-storchaka reviewed Jan 23, 2026
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

See Python/bootstrap_hash.c. Could you make the code similar?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
vstinner
vstinner reviewed Jan 23, 2026
View reviewed changes
Comment thread Modules/posixmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Jan 23, 2026

Copy link
Copy Markdown
Member

According to the FreeBSD manual page:

The getrandom() system call first appeared in FreeBSD 12.0.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@dankm

dankm commented Jan 23, 2026

Copy link
Copy Markdown
Author

According to the FreeBSD manual page:

The getrandom() system call first appeared in FreeBSD 12.0.

Yup, FreeBSD since 12.0 (December 2018), and NetBSD since 10.0 (March 2024) both claim to support the syscall now. I've not tested this on NetBSD, but their documentation is similar to FreeBSD's.

To satisfy my own curiosity I checked DragonFly and OpenBSD's documentation too. DragonFly claims support as of version 5.7. OpenBSD does not claim support, so if it has it it's undocumented, but OpenBSD's man pages are usually complete.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

dankm added 2 commits January 23, 2026 10:03
@dankm
posixmodule: Be more specific in include comment.
7df1eb7
@dankm
bootstrap_hash: add note about GRND_NONBLOCK on the BSDs
a3d4868
vstinner
vstinner approved these changes Jan 23, 2026
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM.

See Python/bootstrap_hash.c. Could you make the code similar?

@serhiy-storchaka: I didn't understand your comment. Which code should be made similar to Python/bootstrap_hash.c?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Modules/posixmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Jan 23, 2026
@dankm @vstinner
Update Modules/posixmodule.c
b203399
Co-authored-by: Victor Stinner <[email protected]>
@dankm

dankm commented Jan 23, 2026

Copy link
Copy Markdown
Author

@serhiy-storchaka: I didn't understand your comment. Which code should be made similar to Python/bootstrap_hash.c?

I wasn't sure either. The code in bootstrap_hash.c already does the right thing, so I added a note about the BSDs.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Jan 23, 2026

Copy link
Copy Markdown
Member

I meant that the code in Python/bootstrap_hash.c already supports sys/random.h (and it is presumably tested), so it would be better to just copy it from Python/bootstrap_hash.c to Modules/posixmodule.c.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@dankm

dankm commented Jan 23, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Author

I meant that the code in Python/bootstrap_hash.c already supports sys/random.h (and it is presumably tested), so it would be better to just copy it from Python/bootstrap_hash.c to Modules/posixmodule.c.

Ah, okay, that makes sense. You're suggesting I make Modules/posixmodule.c look like Python/bootstrap_hash.c, keeping Python/bootstrap_hash.c unmodified? I interpreted your suggestion as making modifications to Python/bootstrap_hash.c to reflect the current changes to Modules/posixmodule.c.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@dankm
Merge branch 'main' into dankm/getrandom
c333508
@vstinner

vstinner commented Feb 6, 2026

Copy link
Copy Markdown
Member

@serhiy-storchaka: with this change, Modules/posixmodule.c and Python/bootstrap_hash.c use the same includes. The change LGTM. I don't see what else should be done. I suggest merging the change as it is.

Linux getrandom() manual page mentions #include <sys/random.h>: https://man7.org/linux/man-pages/man2/getrandom.2.html

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Feb 6, 2026

Copy link
Copy Markdown
Member
//#ifndef MS_WINDOWS
#  ifdef HAVE_LINUX_RANDOM_H
#    include <linux/random.h>     // GRND_NONBLOCK
#  endif
#  if defined(HAVE_SYS_RANDOM_H) && (defined(HAVE_GETRANDOM) || defined(HAVE_GETENTROPY))
#    include <sys/random.h>       // getrandom()
#  endif
#  if !defined(HAVE_GETRANDOM) && defined(HAVE_GETRANDOM_SYSCALL)
#    include <sys/syscall.h>      // SYS_getrandom
#  endif

The proposed code:

#ifdef HAVE_SYS_RANDOM_H
#  include <sys/random.h>         // GRND_RANDOM on FreeBSD and NetBSD
#endif
#ifdef HAVE_LINUX_RANDOM_H
#  include <linux/random.h>       // GRND_RANDOM
#endif
#ifdef HAVE_GETRANDOM_SYSCALL
#  include <sys/syscall.h>        // syscall()
#endif

They are not identical. Different order of includes. Different conditions. I suggest to make them identical, so we will not introduce subtle bugs in future.

Also, if the code in Python/bootstrap_hash.c already works, are changes in configure required?

emmatyping reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Mar 10, 2026

Copy link
Copy Markdown
Member

@dankm: Can you apply @serhiy-storchaka's suggestions?

dankm reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@dankm

dankm commented Mar 11, 2026

Copy link
Copy Markdown
Author

Well that's what I get for not testing a proposed change, I'll fix it in the morning.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@dankm
Make the header include order match the bootstrap one
6d10c3b
This was suggested by @serhiy-storchaka during review.
@dankm
dankm force-pushed the dankm/getrandom branch from b563b01 to 6d10c3b Compare March 11, 2026 15:58
@dankm
Merge branch 'main' into dankm/getrandom
a40fd75
@vstinner

vstinner commented Mar 11, 2026

Copy link
Copy Markdown
Member

"Merge branch 'main' into dankm/getrandom": please avoid that unless there is a good reason to do that.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@dankm

dankm commented Mar 11, 2026

Copy link
Copy Markdown
Author

Noted for the future. If figured it might be the way to get past some failures on MacOS I was seeing with my previous update.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka reviewed Mar 12, 2026
View reviewed changes
Comment thread Modules/posixmodule.c
#if defined(HAVE_SYS_RANDOM_H) && (defined(HAVE_GETRANDOM) || defined(HAVE_GETENTROPY))
# include <sys/random.h> // getrandom(), GRND_NONBLOCK on FreeBSD and NetBSD
#endif
#ifdef HAVE_GETRANDOM_SYSCALL

@serhiy-storchaka serhiy-storchaka Mar 12, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Suggested change
#ifdef HAVE_GETRANDOM_SYSCALL
#if !defined(HAVE_GETRANDOM) && defined(HAVE_GETRANDOM_SYSCALL)

Also, in the code below which checks HAVE_GETRANDOM_SYSCALL, we should also check HAVE_GETRANDOM and use getrandom() if available.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Mar 12, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I would prefer to leave the PR as it is (#ifdef HAVE_GETRANDOM_SYSCALL). I will write a following PR to reuse Python/bootstrap_hash.c code in Modules/posixmodule.c. The bootstrap_hash.c code is more complete, it handles properly Solaris for example.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Modules/posixmodule.c
#ifdef HAVE_LINUX_RANDOM_H
# include <linux/random.h> // GRND_RANDOM
#endif
#if defined(HAVE_SYS_RANDOM_H) && (defined(HAVE_GETRANDOM) || defined(HAVE_GETENTROPY))

@serhiy-storchaka serhiy-storchaka Mar 12, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The code below does not use getentropy(), so it should be removed.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vstinner

vstinner commented Mar 12, 2026

Copy link
Copy Markdown
Member

Currently on my FreeBSD 15 (VM), HAVE_GETRANDOM_SYSCALL macro is not defined in pyconfig.h and so there is no os.getrandom() function.

With this change, the HAVE_GETRANDOM_SYSCALL macro macro is defined in pyconfig.h and Python has an os.getrandom() function. The change works as expected.

Note: I had to remove my configure cache to update properly the HAVE_GETRANDOM_SYSCALL macro.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions

github-actions Bot commented May 5, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner approved these changes
@serhiy-storchaka serhiy-storchaka serhiy-storchaka left review comments
@erlend-aasland erlend-aasland Awaiting requested review from erlend-aasland erlend-aasland is a code owner
@corona10 corona10 Awaiting requested review from corona10 corona10 is a code owner
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner is a code owner
@emmatyping emmatyping Awaiting requested review from emmatyping emmatyping is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting merge stale Stale PR or inactive for long period of time.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@dankm @vstinner @serhiy-storchaka
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.