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 support for rlimits #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/child.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ptr;

use libc;
use nix;
use libc::{c_void, c_ulong, sigset_t, size_t};
use libc::{c_void, c_ulong, sigset_t, size_t, rlimit};
use libc::{kill, signal};
use libc::{F_GETFD, F_SETFD, F_DUPFD_CLOEXEC, FD_CLOEXEC, MNT_DETACH};
use libc::{SIG_DFL, SIG_SETMASK};
Expand Down Expand Up @@ -215,6 +215,13 @@ pub unsafe fn child_after_clone(child: &ChildInfo) -> ! {
}
}

for &(rlim_type, rlim_value) in child.rlimits {
let limit = rlimit{rlim_cur: rlim_value, rlim_max: rlim_value};
if libc::setrlimit(rlim_type, &limit) < 0 {
fail(Err::RLimit, epipe);
}
}

if let Some(callback) = child.pre_exec {
if let Err(e) = callback() {
fail_errno(Err::PreExec,
Expand Down
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum ErrorCode {
SetNs = 12,
CapSet = 13,
PreExec = 14,
RLimit = 16,
}

/// Error runnning process
Expand Down Expand Up @@ -93,6 +94,8 @@ pub enum Error {
BeforeUnfreeze(Box<dyn (::std::error::Error) + Send + Sync + 'static>),
/// Before exec callback error
PreExec(i32),
/// Error when calling setrlimit syscall
SetRLimit(i32),
}

impl Error {
Expand Down Expand Up @@ -120,6 +123,7 @@ impl Error {
&CapSet(x) => Some(x),
&BeforeUnfreeze(..) => None,
&PreExec(x) => Some(x),
&SetRLimit(x) => Some(x),
}
}
}
Expand Down Expand Up @@ -148,6 +152,7 @@ impl Error {
&CapSet(_) => "error when setting capabilities",
&BeforeUnfreeze(_) => "error in before_unfreeze callback",
&PreExec(_) => "error in pre_exec callback",
&SetRLimit(_) => "error setting rlimit",
}
}
}
Expand Down Expand Up @@ -240,6 +245,7 @@ impl ErrorCode {
C::SetNs => E::SetNs(errno),
C::CapSet => E::CapSet(errno),
C::PreExec => E::PreExec(errno),
C::RLimit => E::SetRLimit(errno),
}
}
pub fn from_i32(code: i32, errno: i32) -> Error {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub struct Command {
id_map_commands: Option<(PathBuf, PathBuf)>,
pid_env_vars: HashSet<OsString>,
keep_caps: Option<[u32; 2]>,
rlimits: Vec<(libc::__rlimit_resource_t, libc::rlim_t)>,
before_unfreeze: Option<Box<dyn FnMut(u32) -> Result<(), BoxError>>>,
pre_exec: Option<Box<dyn Fn() -> Result<(), io::Error>>>,
}
Expand Down
10 changes: 9 additions & 1 deletion src/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::ffi::OsStr;
use std::io;
use std::os::unix::io::AsRawFd;
use std::path::Path;

use libc::{__rlimit_resource_t, rlim_t};
use nix::sys::signal::{Signal};

use crate::ffi_util::ToCString;
Expand Down Expand Up @@ -293,4 +293,12 @@ impl Command {
}
self.keep_caps = Some(buf);
}

/// Set resource limits (rlimit / ulimit)
pub fn set_rlimit(&mut self, resource: __rlimit_resource_t, limit: rlim_t)
-> &mut Command
{
self.rlimits.push((resource, limit));
self
}
}
4 changes: 3 additions & 1 deletion src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::os::unix::io::{RawFd, AsRawFd};
use std::path::{Path, PathBuf};
use std::ptr;

use libc::{c_char, close};
use libc::{__rlimit_resource_t, c_char, close, rlim_t};
use nix;
use nix::errno::Errno::EINTR;
use nix::fcntl::{fcntl, FcntlArg, open};
Expand Down Expand Up @@ -52,6 +52,7 @@ pub struct ChildInfo<'a> {
pub pid_env_vars: &'a [(usize, usize)],
pub keep_caps: &'a Option<[u32; 2]>,
pub pre_exec: &'a Option<Box<dyn Fn() -> Result<(), io::Error>>>,
pub rlimits: &'a [(__rlimit_resource_t, rlim_t)],
}

fn raw_with_null(arr: &Vec<CString>) -> Vec<*const c_char> {
Expand Down Expand Up @@ -258,6 +259,7 @@ impl Command {
pid_env_vars: &pid_env_vars,
keep_caps: &self.keep_caps,
pre_exec: &self.pre_exec,
rlimits: &self.rlimits,
};
child::child_after_clone(&child_info);
}), &mut nstack[..], self.config.namespaces, Some(SIGCHLD as i32)))?;
Expand Down
1 change: 1 addition & 0 deletions src/std_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl Command {
pid_env_vars: HashSet::new(),
keep_caps: None,
before_unfreeze: None,
rlimits: Vec::new(),
pre_exec: None,
}
}
Expand Down