Skip to content

Implemented close_range (sort of) for Mac#1540

Draft
philocalyst wants to merge 8 commits intotrifectatechfoundation:mainfrom
philocalyst:close_range_mca
Draft

Implemented close_range (sort of) for Mac#1540
philocalyst wants to merge 8 commits intotrifectatechfoundation:mainfrom
philocalyst:close_range_mca

Conversation

@philocalyst
Copy link
Copy Markdown

@philocalyst philocalyst commented Apr 6, 2026

Just like on Linux, there really isn't a close_range function (If we consider libc authoritative).

But there are some other solutions! I basically stole from the C sudo project (As recommended in the original thread).

Specifically, this implements using /dev/fd on macOS. In the original C implementation this uses some odd abstraction which vaguely concerns me, but here we use a literal like on Linux.

And also specifically, this uses the recommended proc_pidinfo approach as the main solution (the /dev/fd is a fallback).

Funniest here is the WIGGLE_ROOM constant, which just covers from some expected user entropy. I would like feedback on what could happen if WIGGLE_ROOM is exceeded. All in all, it sticks fairly close to the C implementation, linked in the relevant commit.

Tried to stick to the code style I saw elsewhere in the codebase, but outside of that likely comment, I was interested in what the expectation is for testing? Left out of this PR (for now) so I'm marking it as a draft, just because it is the area of expertise I lack the most in. Commentary there would be MOST appreciated.

Also... I can't compile because I'm on Mac, so the other reason for delayed testing is me setting up a Linux VM.

@squell

Closes #901

@philocalyst philocalyst force-pushed the close_range_mca branch 2 times, most recently from 8261079 to 09daa6a Compare April 6, 2026 17:16
@bjorn3
Copy link
Copy Markdown
Collaborator

bjorn3 commented Apr 7, 2026

Also... I can't compile because I'm on Mac, so the other reason for delayed testing is me setting up a Linux VM.

If you do rustup target add x86_64-unknown-linux-gnu, you should be able to do cargo check --target x86_64-unknown-linux-gnu.

@philocalyst
Copy link
Copy Markdown
Author

Checks cleanly! @bjorn3

@squell
Copy link
Copy Markdown
Member

squell commented Apr 7, 2026

Note that there's a highly experimental longer-lived macos-experiment branch which compiles and (sort of) works; it still is missing some bits you might want to look at (see the final WIP commit).

If you would like to hack to get sudo-rs running on your MacBook, could be fun check it out. It's almost completely untested so use caution.

Also be aware that MacOS support has a rather low priority for us, for a couple of reasons:

  • The commits on that branch shows that a lot of stuff we take for granted on Linux/FreeBSD simply isn't there on that platform and that adds a lot of extra checks/workarounds.
  • It's not likely to be used by many (without Apple shipping it, which seems unlikely)

That said, doesn't mean fun can be had with it, and I would be happy to accept contributions on the macos-experiment branch (this one probably should also go there).

@squell squell added the macos label Apr 7, 2026
@squell squell added this to the External Contribs milestone Apr 7, 2026
@philocalyst
Copy link
Copy Markdown
Author

philocalyst commented Apr 7, 2026 via email

@squell
Copy link
Copy Markdown
Member

squell commented Apr 9, 2026

Yes, there is a path eventually.

There are currently two open issues for MacOS which are the most pressing, one is the one linked to this PR, the other one pertains to implementing these stubs:
fda6eb3

In general the path is:

  • We're already able to compile and run on MacOS, so that at least is something (we are currently here).

  • I would say the first order of business is to get that branch to a point where it not only "compiles and runs", but also isn't lacking some code that is critical (as it currently does), and we can manually test to see that it works. I.e. that job is: closing the open issues. (The close_range issue has already been less a concern since the "fallback" close_range implementation exists nowadays; although having it properly implemented would surely be a very nice thing!)

  • Then we should make sure that it also passes the unit tests (which probably requires further modifications); and after that the compliance testing framework. That latter part will probably be quite annoying.

  • At the point we're probably staring at a situation where the macos-experiment branch has some modifications (e.g. some typecasts, wrappers to work around e.g. the lack of pipe2) and we can think about how to sweep those under the rug nicely so we don't have to introduce too many #[cfg(target_os = "macos")] things (which is what the branch is currently doing without hesistation)

    I would prefer to keep the macos-experiment branch as a collection of commits that can be replayed on top of main, where each commit addresses a particular item, for that purpose.

    For example, one of the WIP commits on that branch also has some fcntl-wrapping similar to as what you're doing here. :-)

This approach is similar to how our FreeBSD port was undertaken; we first did the port and then we gradually introduced the changes needed for it back into main.

If we get it to a point where it works well and the maintenance burden for keeping MacOS-modification in main can probably become acceptable low, I woulnd't see a reason not to integrate that entire branch in main. Nobody else is working on it and even if you just pick up #902 it would bring this one step closer.

@philocalyst
Copy link
Copy Markdown
Author

philocalyst commented Apr 9, 2026 via email

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement fn close_range for MacOS

3 participants