├── config.toml ├── Cargo.toml ├── src ├── main.rs ├── agents.rs └── config.rs └── Cargo.lock /config.toml: -------------------------------------------------------------------------------- 1 | [sandbox] 2 | cpu_time = 1 3 | memory = 20971520 4 | 5 | [command] 6 | exec = "./hello" 7 | args = [] -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "quod" 3 | version = "0.1.0" 4 | authors = ["Sankha Narayan Guria "] 5 | license = "MIT" 6 | 7 | [dependencies] 8 | toml = "0.1" 9 | nix = "*" 10 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | #![feature(convert)] 2 | 3 | extern crate toml; 4 | extern crate nix; 5 | 6 | mod config; 7 | mod agents; 8 | 9 | use std::path::Path; 10 | use config::Config; 11 | 12 | fn main() { 13 | let config = Config::new(Path::new("config.toml")); 14 | //agents::cpu_time(config.cpu_time); 15 | println!("{:?}", agents::memory(config.memory)); 16 | agents::run(config.exec); 17 | } 18 | -------------------------------------------------------------------------------- /src/agents.rs: -------------------------------------------------------------------------------- 1 | use nix::sys::ioctl::libc::{rlimit, setrlimit, execv}; 2 | use nix::sys::ioctl::libc::{RLIMIT_AS, RLIMIT_CPU}; 3 | use std::ptr; 4 | 5 | pub fn cpu_time(time: u64) -> i32 { 6 | let limit = rlimit { rlim_cur: time, rlim_max: time }; 7 | unsafe { setrlimit(RLIMIT_CPU, &limit as *const rlimit) } 8 | } 9 | 10 | pub fn memory(size: u64) -> i32 { 11 | let limit = rlimit { rlim_cur: size, rlim_max: size }; 12 | unsafe { setrlimit(RLIMIT_AS, &limit as *const rlimit) } 13 | } 14 | 15 | pub fn run(path: String) -> i32 { 16 | println!("{:?}", path); 17 | unsafe { execv(path.as_bytes().as_ptr() as *const i8, ptr::null()) } 18 | } -------------------------------------------------------------------------------- /src/config.rs: -------------------------------------------------------------------------------- 1 | use std::io::prelude::*; 2 | use std::fs::File; 3 | use std::path::Path; 4 | use toml::{Value}; 5 | 6 | pub struct Config { 7 | pub exec: String, 8 | pub cpu_time: u64, 9 | pub memory: u64 10 | } 11 | 12 | impl Config { 13 | pub fn new(filename: &Path) -> Config { 14 | let mut f = File::open(filename).unwrap(); 15 | let mut data = String::new(); 16 | let _ = f.read_to_string(&mut data); 17 | let config: Value = data.as_str().parse().unwrap(); 18 | let exec = config.lookup("command.exec").unwrap().as_str().unwrap().to_string(); 19 | let cpu_time = config.lookup("sandbox.cpu_time").unwrap().as_integer().unwrap() as u64; 20 | let memory = config.lookup("sandbox.memory").unwrap().as_integer().unwrap() as u64; 21 | Config { 22 | exec: exec, 23 | cpu_time: cpu_time, 24 | memory: memory 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "autocfg" 7 | version = "1.1.0" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" 10 | 11 | [[package]] 12 | name = "bitflags" 13 | version = "1.2.1" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" 16 | 17 | [[package]] 18 | name = "cc" 19 | version = "1.0.73" 20 | source = "registry+https://github.com/rust-lang/crates.io-index" 21 | checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" 22 | 23 | [[package]] 24 | name = "cfg-if" 25 | version = "1.0.0" 26 | source = "registry+https://github.com/rust-lang/crates.io-index" 27 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 28 | 29 | [[package]] 30 | name = "libc" 31 | version = "0.2.126" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" 34 | 35 | [[package]] 36 | name = "memoffset" 37 | version = "0.6.5" 38 | source = "registry+https://github.com/rust-lang/crates.io-index" 39 | checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" 40 | dependencies = [ 41 | "autocfg", 42 | ] 43 | 44 | [[package]] 45 | name = "nix" 46 | version = "0.20.2" 47 | source = "registry+https://github.com/rust-lang/crates.io-index" 48 | checksum = "f5e06129fb611568ef4e868c14b326274959aa70ff7776e9d55323531c374945" 49 | dependencies = [ 50 | "bitflags", 51 | "cc", 52 | "cfg-if", 53 | "libc", 54 | "memoffset", 55 | ] 56 | 57 | [[package]] 58 | name = "quod" 59 | version = "0.1.0" 60 | dependencies = [ 61 | "nix", 62 | "toml", 63 | ] 64 | 65 | [[package]] 66 | name = "rustc-serialize" 67 | version = "0.3.16" 68 | source = "registry+https://github.com/rust-lang/crates.io-index" 69 | checksum = "1a48546a64cae47d06885e9bccadb99d0547d877a94c5167fa451ea33a484456" 70 | 71 | [[package]] 72 | name = "toml" 73 | version = "0.1.25" 74 | source = "registry+https://github.com/rust-lang/crates.io-index" 75 | checksum = "39db5da8747f0487c64cebf966d3bd5bb9be9b03dfd952e53305b41b0b3a79bd" 76 | dependencies = [ 77 | "rustc-serialize", 78 | ] 79 | --------------------------------------------------------------------------------