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

Add getrlimit(2) and setrlimit(2) #364

Closed
wants to merge 1 commit into from

Conversation

kamalmarhubi
Copy link
Member

No description provided.

use {Errno, Result};

#[repr(i32)]
pub enum Resource {
Copy link
Member Author

Choose a reason for hiding this comment

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

should this be Resource or Rlimit / RLimit?

Copy link
Member Author

Choose a reason for hiding this comment

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

@fiveop thoughts? since you added the convention on enums...

Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer Resource, because it very clearly expresses what it stands for.

Copy link
Member

Choose a reason for hiding this comment

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

Resource as a name still feels a little off give the values in the enum. You are not really getting the rlimit of a resource, you are getting the value of a specific resource limit in the system (one "resource" may have multiple related limits).

I'm not completely sold on this, but I could see naming the enum ResourceLimit and allowing get/set on the limit. E.g.

let limit =  ResourceLimit::RLIMIT_STACK.get().unwrap();
ResourceLimit::RLIMIT_STACK.set(limit).unwrap();

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah I thought of this kind of approach but I don't know yet how I feel about going so far from the C API.

In related news: I just created https://github.com/nix-rust/rfcs and will create RFC 0000 soon (probably tomorrow) to bootstrap the process. Then we'll have a good place to discuss stuff like this.

Copy link
Contributor

Choose a reason for hiding this comment

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

If we add the get and set methods to the type, the name ResourceLimit does make more sense. However, if we don't do that, the variants of the enum actually represent the Resource, whose limit is set/get by the methods setrlimit, getrlimit.

The more I think about it, the more I like @posborne's proposal.

use std::mem;

use libc::{self, c_int};
pub use libc::{rlimit, RLIM_INFINITY};
Copy link
Member Author

Choose a reason for hiding this comment

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

I think these should be reexported, but I'm not sure if we are consistently doing that.

Copy link
Contributor

Choose a reason for hiding this comment

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

What about RLIM_SAVED_CUR and RLIM_SAVED_MAX?

Why don't we wrap rlimit and provide proper accessor methods? I admit that it would be purely cosmetic in this case, as far as I can see.

Copy link
Member Author

Choose a reason for hiding this comment

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

forgotten! fixed.

Copy link
Contributor

Choose a reason for hiding this comment

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

What about the rlimit wrapper?

Copy link
Member Author

Choose a reason for hiding this comment

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

oh I was deciding not to do that since we aren't doing anything with the type. I wanted it to be easy to specify inline, eg

setrlimit(RLIMIT_STACK, rlimit { rlim_cur: 1024*1024, rlim_max: RLIM_INFINITY }).unwrap()

Copy link
Contributor

Choose a reason for hiding this comment

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

What about ResourceLimit::new(1024 * 1024, RLIM_INFINITY) ?

@kamalmarhubi kamalmarhubi mentioned this pull request Apr 30, 2016
@kamalmarhubi
Copy link
Member Author

Apple failure is because RLIMIT_RSS isn't defined in libc for the platform. I have rust-lang/libc#272 to fix it.

@fiveop
Copy link
Contributor

fiveop commented May 6, 2016

With the new libc version, this should be mergeable. Could you address the open discussion topics, so that we can get this merged.

@kamalmarhubi
Copy link
Member Author

I've been busy but should get to this in the next few days. Thanks for the ping!

@fiveop
Copy link
Contributor

fiveop commented Jul 1, 2016

Any chance for an update?

@Susurrus
Copy link
Contributor

@kamalmarhubi You still planning on merging this?

@ccope
Copy link

ccope commented Aug 15, 2017

I experimented with this today, and ran into strange behavior when trying to change RLIMIT_STACK inside main(). I think the rust runtime interferes by initializing the heap and creating guard pages before main() gets invoked. I made an issue here: rust-lang/rust#43879

@Susurrus
Copy link
Contributor

Susurrus commented Nov 5, 2017

@ccope It appears that setrlimit can't be implemented with the current rustc implementation based on the conversation in rust-lang/rust#43879, correct?

@ccope
Copy link

ccope commented Nov 10, 2017

I was only specifically testing testing setrlimit with RLIMIT_STACK, and while it doesn't work in rust < 1.20.0 inside the main thread (due to the main thread guard page), I am unable to find any issues* with rust 1.20.0+.

*: No matter what version of rust you use, you need to make sure that variables larger than the initial stack size don't get inlined into the main function before you can adjust the stack size.

@Susurrus
Copy link
Contributor

@ccope Can we expose these failures at run time or will things just Not Work and the user will be confused? Additionally we should make sure to clearly document all the invariants involved with using this functionality.

@kamalmarhubi are you still willing to work on this PR and get it merged? If not, @ccope, you willing to step in here?

@ccope
Copy link

ccope commented Nov 16, 2017

@Susurrus rust will exit with a stack overflow error in rust 1.20+, and the user will have to figure out why RLIMIT_STACK didin't take effect. I'm not sure there's a way to implement automatic checking for this, but documentation would help.

@Susurrus
Copy link
Contributor

@ccope As long as this is clearly documented, I think it'll be fine, though I wonder if this falls under Rust's definition of unsafe given that it's dealing with memory.

Additionally, I'm not certain I like the API we expose here. I think we're mimicking the C API a little more closely than we should. For example I'd see us replacing the use of RLIM_INFINITY with an Option type. I also think we should hide the rlimit struct entirely from the user and instead do a (Option<HardLimit>, Option<SoftLimit>) instead.

@Susurrus
Copy link
Contributor

Susurrus commented Dec 4, 2017

@kamalmarhubi I'm going to go ahead and close this PR because you haven't touched this in over a year.

@ccope If you're interested in pushing this forward, please clone into your repo and file your own PR that we can then iterate on.

@Susurrus Susurrus closed this Dec 4, 2017
This was referenced Mar 31, 2018
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.

None yet

5 participants