Skip to content

Commit

Permalink
Set default isolation settings to env maximums
Browse files Browse the repository at this point in the history
  • Loading branch information
quantumsheep committed Jul 25, 2021
1 parent 1ebbda5 commit 03dd63e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 28 deletions.
16 changes: 9 additions & 7 deletions src/isolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::process::Command;
use std::process::ExitStatus;
use std::{collections::HashMap, process::Stdio};

use crate::utils;

#[derive(Debug)]
pub struct ExecutedCommandResult {
pub status: ExitStatus,
Expand Down Expand Up @@ -137,25 +139,25 @@ pub struct IsolatedBoxOptions {
#[builder(default = "false")]
pub profiling: bool,

#[builder(default = "5")]
#[builder(default = "utils::parsed_env::get(\"MAX_RUN_TIME_LIMIT\", 5)")]
pub run_time_limit: u64,

#[builder(default = "0")]
#[builder(default = "utils::parsed_env::get(\"MAX_EXTRA_TIME_LIMIT\", 0)")]
pub extra_time_limit: u64,

#[builder(default = "10")]
#[builder(default = "utils::parsed_env::get(\"MAX_WALL_TIME_LIMIT\", 10)")]
pub wall_time_limit: u64,

#[builder(default = "128000")]
#[builder(default = "utils::parsed_env::get(\"MAX_STACK_SIZE_LIMIT\", 128000)")]
pub stack_size_limit: u64,

#[builder(default = "120")]
#[builder(default = "utils::parsed_env::get(\"MAX_PROCESS_COUNT_LIMIT\", 120)")]
pub process_count_limit: u64,

#[builder(default = "512000")]
#[builder(default = "utils::parsed_env::get(\"MAX_MEMORY_LIMIT\", 512000)")]
pub memory_limit: u64,

#[builder(default = "10240")]
#[builder(default = "utils::parsed_env::get(\"MAX_STORAGE_LIMIT\", 10240)")]
pub storage_limit: u64,
}

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern crate derive_more;
#[macro_use]
extern crate derive_builder;

mod utils;
mod api_helpers;
mod isolate;
mod routes;
Expand Down
23 changes: 2 additions & 21 deletions src/routes/run_post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::api_helpers::{ApiError, ApiResult};
use crate::runner::phase_settings::{PhaseSandboxSettings, PhaseSettings};
use crate::runner::runner::Runner;
use crate::runner::runner::RunnerPhaseResult;
use crate::utils;
use actix_web::{post, web::Json};
use merge::Merge;
use serde::{Deserialize, Serialize};
Expand All @@ -27,27 +28,7 @@ pub struct RunResponseDTO {

fn is_over_cap_limit_env(current_option: Option<u64>, name: &str) -> bool {
match current_option {
Some(current) => {
let max = match env::var(name) {
Ok(value) => match value.as_str() {
"-1" => u64::MAX,
_ => match value.parse() {
Ok(max) => max,
Err(e) => {
eprintln!(
"Failed to parse environment variable '{}' as an `u64`: {}",
name, e
);

u64::MAX
}
},
},
Err(_) => u64::MAX,
};

current > max
}
Some(current) => current > utils::parsed_env::get(name, u64::MAX),
None => false,
}
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod parsed_env;
26 changes: 26 additions & 0 deletions src/utils/parsed_env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::env;

pub fn get<S>(name: S, default: u64) -> u64
where
S: Into<String>,
{
let name_string = name.into();

match env::var(name_string.clone()) {
Ok(value) => match value.as_str() {
"-1" => default,
_ => match value.parse() {
Ok(max) => max,
Err(e) => {
eprintln!(
"Failed to parse environment variable '{}' as an `u64`: {}",
name_string, e
);

default
}
},
},
Err(_) => default,
}
}

0 comments on commit 03dd63e

Please sign in to comment.