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

Provide clearenv() #1185

Merged
merged 1 commit into from
Feb 13, 2020
Merged

Provide clearenv() #1185

merged 1 commit into from
Feb 13, 2020

Conversation

jgallagher
Copy link
Contributor

Maybe closes #481.

"Maybe" for a couple reasons that are discussed on that issue:

  • The issue mentions adding the other environment-variable-related functions (setenv, getenv, etc.), but I omitted those because they're already covered by std::env AFAICT, plus they're wildly unthreadsafe. It looks like std::env avoids the thread unsafety via a library-level lock and a warning to be careful when using ffi functions that might modify the environment concurrently.
  • I opted to return Error::UnsupportedOperation in the (impossible?) case of libc::clearenv() returning non-zero instead of having the function return a Result<(), ()>. The latter seems like it would be onerous (since every other possibly-failing function in nix returns a nix::Result), but if you'd prefer a different error return I'm not going to put up any argument.

Copy link
Member

@asomers asomers left a comment

Choose a reason for hiding this comment

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

Nice! This looks pretty good already.

src/env.rs Outdated Show resolved Hide resolved
src/env.rs Outdated Show resolved Hide resolved
src/env.rs Outdated Show resolved Hide resolved
/// environment access in the program is via `std::env`, but the requirement on
/// thread safety must still be upheld.
pub unsafe fn clearenv() -> Result<()> {
let ret;
Copy link
Member

Choose a reason for hiding this comment

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

As a matter of style, I suggest:

let ret = cfg_if! {
    if something {
        libc::clearenv()
    } else {
        0
    }
};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried that first, but it looks like that's not allowed with cfg_if!. It generates a lot of compilation errors; the most relevant looks like:

error: macro expansion ignores token `$crate` and any following
  --> <::cfg_if::cfg_if macros>:23:37
   |
23 |       { @ __identity $ ($ tokens) * } $ crate :: cfg_if !
   |                                       ^^^^^^^
   |
  ::: src/env.rs:28:15
   |
28 |       let ret = cfg_if! {
   |  _______________-
29 | |         if #[cfg(any(target_os = "fuchsia",
30 | |                      target_os = "wasi",
31 | |                      target_env = "wasi",
...  |
43 | |         }
44 | |     };
   | |     -
   | |     |
   | |_____caused by the macro expansion here
   |       help: you might be missing a semicolon here: `;`
   |
   = note: the usage of `cfg_if!` is likely invalid in expression context
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

Copy link
Member

@asomers asomers left a comment

Choose a reason for hiding this comment

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

Looks great. Now it just needs a squash.

@asomers
Copy link
Member

asomers commented Feb 11, 2020

The build failed on nightly. Could you please investigate, @jgallagher ?

@jgallagher
Copy link
Contributor Author

@asomers Squashed down to one commit. I'm not sure what to make of the nightly failure; I can't reproduce it locally, and the error message itself is pretty bizarre.

Do you know off hand if there's a description of the travis environment somewhere? I could try spinning up a VM that more closely matches it.

@asomers
Copy link
Member

asomers commented Feb 11, 2020

That test was run on the latest nightly. It may well be a bug in Rust. Could you try updating your nightly toolchain and reproducing?

@jgallagher
Copy link
Contributor Author

I had already tried updating nightly without any luck, but I was just able to reproduce it. I hadn't noticed the cargo update -Zminimal-versions in the travis log, but if I do that first, I get the same error. Bumping cfg-if in Cargo.toml to 0.1.10 fixes the error (the error is still there on 0.1.9).

Do you want me to squash that bump into this PR?

@asomers
Copy link
Member

asomers commented Feb 11, 2020

Yes please! Good sleuthing.

@jgallagher
Copy link
Contributor Author

Looks like that did the trick. 👍

@@ -18,7 +18,7 @@ exclude = [
[dependencies]
libc = { version = "0.2.60", features = [ "extra_traits" ] }
bitflags = "1.1"
cfg-if = "0.1.2"
cfg-if = "0.1.10"
Copy link
Member

Choose a reason for hiding this comment

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

Did you actually verify that 0.1.10 is the minimum version that works?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I mentioned before that the error is still there on 0.1.9. I didn't try every version in between 0.1.2 and 0.1.9; it seems unlikely any of those work work if the same error is in both 0.1.2 and 0.1.9, but I guess it's not impossible. Is there an impact to jumping to 0.1.10?

Copy link
Member

Choose a reason for hiding this comment

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

Well, given that 0.1.10 comes immediately after 0.1.9, I'd say that confirms it. Too bad that the cfg-if crate doesn't maintain a CHANGELOG.

Copy link
Member

@asomers asomers left a comment

Choose a reason for hiding this comment

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

bors r+

bors bot added a commit that referenced this pull request Feb 13, 2020
1185: Provide clearenv() r=asomers a=jgallagher

Maybe closes #481.

"Maybe" for a couple reasons that are discussed on that issue:

* The issue mentions adding the other environment-variable-related functions (`setenv`, `getenv`, etc.), but I omitted those because they're already covered by `std::env` AFAICT, plus they're wildly unthreadsafe. It looks like `std::env` avoids the thread unsafety via a library-level lock and a warning to be careful when using ffi functions that might modify the environment concurrently.
* I opted to return `Error::UnsupportedOperation` in the (impossible?) case of `libc::clearenv()` returning non-zero instead of having the function return a `Result<(), ()>`. The latter seems like it would be onerous (since every other possibly-failing function in nix returns a `nix::Result`), but if you'd prefer a different error return I'm not going to put up any argument.

Co-authored-by: John Gallagher <[email protected]>
@bors
Copy link
Contributor

bors bot commented Feb 13, 2020

Build succeeded

@bors bors bot merged commit 06824e6 into nix-rust:master Feb 13, 2020
@jgallagher jgallagher deleted the clearenv branch February 18, 2020 21:33
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.

Provide clearenv?
3 participants