Skip to content

Conversation

faho
Copy link
Member

@faho faho commented Nov 8, 2023

Description

EDIT: Switched to our own wrappers, nix has problems compiling on FreeBSD with the "fs" feature.

This replaces our uses of libc's geteuid, getegid, getpid and isatty with the corresponding functions from the nix crate.

The main advantage is that it removes a bunch of awkward unsafe blocks. In turn nix has made some type choices like wrap pid_t in a Pid newtype that we then need to cast into an i32 again so we can use it.

This isn't the only solution. Alternatively we could:

  • Make our own wrappers, possibly more direct
  • Live with the unsafes
  • Use nix more, integrating it more fully (e.g. add to_wstring to Pid)

Fundamentally, I would like for unsafe to be a rare thing that's easy to audit. In this case libc gets in the way because it defaults to marking things as unsafe, which includes things like getpid where I do not understand what the unsafety is supposed to be.

So I would like to wrap these things away, and these four functions were an easy start.

There are some other rust "style" things that I would like to change, including our uses of .unwrap(), which scares me (it's effectively an "assert"!), and I would like to remove a bunch of them.

@faho faho added this to the fish next-3.x milestone Nov 8, 2023
@faho
Copy link
Member Author

faho commented Nov 9, 2023

Lovely. This tries to build fspacectl on FreeBSD, which fails for reasons not entirely clear to me.

I've found nix-rust/nix#2122, which seems related, but isn't in a nix release yet.

So we would have to wait for a release or adopt our own wrappers. To be honest this decreases my trust in nix.

@faho
Copy link
Member Author

faho commented Nov 9, 2023

Alright, added our own wrappers.

I do not believe that fiddling around with getting this nix feature to build on FreeBSD is in any way worth it given the wrappers are a single line each, and we get to avoid the newtypes which in our use would just get in the way.

This doesn't mean we couldn't switch to nix for other things and then also switch this.

@faho faho changed the title Replace some direct uses of libc with nix Replace some direct uses of libc with wrappers Nov 10, 2023
This removes some spurious unsafe blocks and makes usage a bit nicer
@@ -84,8 +84,7 @@ mod test_expressions {
// Return true if the number is a tty().
fn isatty(&self, streams: &mut IoStreams) -> bool {
fn istty(fd: libc::c_int) -> bool {
// Safety: isatty cannot crash.
unsafe { libc::isatty(fd) > 0 }
crate::nix::isatty(fd)
Copy link
Contributor

@krobelus krobelus Nov 18, 2023

Choose a reason for hiding this comment

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

I've been using the libc:: qualifier for C functions, to give a hint to folks who don't know the function.
But no qualifier for constants like SIGHLD because those are usually used in a libc:: function call already.

Not sure what's the future. Here I think it's fine to use unqualified isatty(fd) always

@@ -84,8 +84,7 @@ mod test_expressions {
// Return true if the number is a tty().
fn isatty(&self, streams: &mut IoStreams) -> bool {
fn istty(fd: libc::c_int) -> bool {
// Safety: isatty cannot crash.
unsafe { libc::isatty(fd) > 0 }
crate::nix::isatty(fd)
Copy link
Contributor

Choose a reason for hiding this comment

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

this function can be inlined now

@faho faho merged commit 6361362 into fish-shell:master Nov 19, 2023
@faho faho deleted the use-more-nix branch November 19, 2023 19:07
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants