1use core::{ffi::c_long, usize};
4
5#[repr(C)]
6#[derive(Debug, Clone, Copy)]
7pub struct Rusage {
9 pub ru_utime: timeval,
11
12 pub ru_stime: timeval,
14
15 pub ru_maxrss: c_long,
17
18 pub ru_ixrss: c_long,
20
21 pub ru_idrss: c_long,
23
24 pub ru_isrss: c_long,
26
27 pub ru_minflt: c_long,
29
30 pub ru_majflt: c_long,
32
33 pub ru_nswap: c_long,
35
36 pub ru_inblock: c_long,
38
39 pub ru_oublock: c_long,
41
42 pub ru_msgsnd: c_long,
44
45 pub ru_msgrcv: c_long,
47
48 pub ru_nsignals: c_long,
50
51 pub ru_nvcsw: c_long,
53
54 pub ru_nivcsw: c_long,
56}
57
58pub mod rlimit_value {
60 pub type RlimT = usize;
62
63 pub const RLIM_INFINITY: RlimT = usize::MAX;
65
66 pub const STACK_DEFAULT_LIMIT: RlimT = 8 * 1024 * 1024;
69 pub const FILE_OPEN_CUR_DEFAULT: RlimT = 1024;
71 pub const FILE_OPEN_MAX_DEFAULT: RlimT = 4096;
73 pub const MEMLOCK_DEFAULT_LIMIT: RlimT = 64 * 1024;
75 pub const MQ_BYTES_MAX_DEFAULT: RlimT = 819200;
77}
78
79use rlimit_value::*;
80
81use crate::uapi::time::timeval;
82
83#[derive(Debug, Clone, Copy, PartialEq, Eq)]
87#[repr(i32)]
88pub enum ResourceId {
89 Cpu = 0,
91 Fsize = 1,
93 Data = 2,
95 Stack = 3,
97 Core = 4,
99
100 Rss = 5,
102 Nproc = 6,
104 Nofile = 7,
106 Memlock = 8,
108 As = 9,
110
111 Locks = 10,
113 Sigpending = 11,
115 Msgqueue = 12,
117 Nice = 13,
119 Rtprio = 14,
121 Rttime = 15,
123}
124
125#[repr(C)]
127#[derive(Debug, Clone, Copy)]
128pub struct Rlimit {
129 pub rlim_cur: RlimT,
131 pub rlim_max: RlimT,
133}
134
135impl Rlimit {
136 pub const fn new(rlim_cur: RlimT, rlim_max: RlimT) -> Self {
138 Rlimit { rlim_cur, rlim_max }
139 }
140
141 pub const fn inf() -> Self {
143 Rlimit {
144 rlim_cur: RLIM_INFINITY,
145 rlim_max: RLIM_INFINITY,
146 }
147 }
148
149 pub const fn default() -> Self {
151 Rlimit {
152 rlim_cur: 0,
153 rlim_max: 0,
154 }
155 }
156}
157
158pub const RLIM_NLIMITS: usize = 16;
160
161pub const INIT_RLIMITS: [Rlimit; RLIM_NLIMITS] = {
163 let mut limits = [Rlimit::default(); RLIM_NLIMITS];
164
165 const fn id_to_index(id: ResourceId) -> usize {
166 id as u32 as usize
167 }
168
169 limits[id_to_index(ResourceId::Cpu)] = Rlimit::inf();
170 limits[id_to_index(ResourceId::Fsize)] = Rlimit::inf();
171 limits[id_to_index(ResourceId::Data)] = Rlimit::inf();
172 limits[id_to_index(ResourceId::Stack)] = Rlimit::new(STACK_DEFAULT_LIMIT, RLIM_INFINITY);
173 limits[id_to_index(ResourceId::Core)] = Rlimit::new(0, RLIM_INFINITY);
174 limits[id_to_index(ResourceId::Rss)] = Rlimit::inf();
175 limits[id_to_index(ResourceId::Nproc)] = Rlimit::new(0, 0);
176 limits[id_to_index(ResourceId::Nofile)] =
177 Rlimit::new(FILE_OPEN_CUR_DEFAULT, FILE_OPEN_MAX_DEFAULT);
178 limits[id_to_index(ResourceId::Memlock)] =
179 Rlimit::new(MEMLOCK_DEFAULT_LIMIT, MEMLOCK_DEFAULT_LIMIT);
180 limits[id_to_index(ResourceId::As)] = Rlimit::inf();
181 limits[id_to_index(ResourceId::Locks)] = Rlimit::inf();
182 limits[id_to_index(ResourceId::Sigpending)] = Rlimit::new(0, 0);
183 limits[id_to_index(ResourceId::Msgqueue)] =
184 Rlimit::new(MQ_BYTES_MAX_DEFAULT, MQ_BYTES_MAX_DEFAULT);
185 limits[id_to_index(ResourceId::Nice)] = Rlimit::new(0, 0);
186 limits[id_to_index(ResourceId::Rtprio)] = Rlimit::new(0, 0);
187 limits[id_to_index(ResourceId::Rttime)] = Rlimit::inf();
188 limits
189};
190
191#[repr(C)]
193#[derive(Debug, Clone, Copy)]
194pub struct RlimitStruct {
195 pub limits: [Rlimit; RLIM_NLIMITS],
196}
197
198impl RlimitStruct {
199 pub const fn new(limits: [Rlimit; RLIM_NLIMITS]) -> Self {
201 RlimitStruct { limits }
202 }
203}