| 1 |
//! Global kernel info / tuning miscellaneous stuff |
| 2 |
//! |
| 3 |
//! The files in this directory can be used to tune and monitor miscellaneous |
| 4 |
//! and general things in the operation of the Linux kernel. |
| 5 |
|
| 6 |
use std::cmp; |
| 7 |
use std::collections::HashSet; |
| 8 |
use std::str::FromStr; |
| 9 |
use std::sync::atomic::{AtomicU32, Ordering}; |
| 10 |
|
| 11 |
use bitflags::bitflags; |
| 12 |
|
| 13 |
use crate::{read_value, write_value, ProcError, ProcResult}; |
| 14 |
|
| 15 |
pub mod keys; |
| 16 |
pub mod random; |
| 17 |
|
| 18 |
/// Represents a kernel version, in major.minor.release version. |
| 19 |
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] |
| 20 |
pub struct Version { |
| 21 |
pub major: u8, |
| 22 |
pub minor: u8, |
| 23 |
pub patch: u16, |
| 24 |
} |
| 25 |
|
| 26 |
impl Version { |
| 27 |
pub fn new(major: u8, minor: u8, patch: u16) -> Version { |
| 28 |
Version { major, minor, patch } |
| 29 |
} |
| 30 |
|
| 31 |
/// Returns the kernel version of the currently running kernel. |
| 32 |
/// |
| 33 |
/// This is taken from `/proc/sys/kernel/osrelease`; |
| 34 |
pub fn current() -> ProcResult<Self> { |
| 35 |
read_value("/proc/sys/kernel/osrelease") |
| 36 |
} |
| 37 |
|
| 38 |
/// Cached version of the kernel version. |
| 39 |
pub(crate) fn cached() -> ProcResult<Self> { |
| 40 |
const SENTINEL: u32 = 0; |
| 41 |
static KERNEL: AtomicU32 = AtomicU32::new(SENTINEL); |
| 42 |
|
| 43 |
// Try to load the kernel version. |
| 44 |
let mut kernel = KERNEL.load(Ordering::Relaxed); |
| 45 |
|
| 46 |
// If we haven't loaded the kernel version yet, try to here. |
| 47 |
if kernel == SENTINEL { |
| 48 |
kernel = Self::current()?.to_u32(); |
| 49 |
|
| 50 |
// Try to store it in the cache. |
| 51 |
KERNEL.store(kernel, Ordering::Release); |
| 52 |
} |
| 53 |
|
| 54 |
Ok(Self::from_u32(kernel)) |
| 55 |
} |
| 56 |
|
| 57 |
/// Convert kernel version to an arbitrary `u32` value. |
| 58 |
fn to_u32(self) -> u32 { |
| 59 |
let [lo, hi] = u16::to_ne_bytes(self.patch); |
| 60 |
u32::from_ne_bytes([self.major, self.minor, lo, hi]) |
| 61 |
} |
| 62 |
|
| 63 |
/// Convert kernel version from an arbitrary `u32` value. |
| 64 |
fn from_u32(val: u32) -> Self { |
| 65 |
let [major, minor, lo, hi] = u32::to_ne_bytes(val); |
| 66 |
Self { |
| 67 |
major, |
| 68 |
minor, |
| 69 |
patch: u16::from_ne_bytes([lo, hi]) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/// Parses a kernel version string, in major.minor.release syntax. |
| 74 |
/// |
| 75 |
/// Note that any extra information (stuff after a dash) is ignored. |
| 76 |
/// |
| 77 |
/// # Example |
| 78 |
/// |
| 79 |
/// ``` |
| 80 |
/// # use procfs::KernelVersion; |
| 81 |
/// let a = KernelVersion::from_str("3.16.0-6-amd64").unwrap(); |
| 82 |
/// let b = KernelVersion::new(3, 16, 0); |
| 83 |
/// assert_eq!(a, b); |
| 84 |
/// |
| 85 |
/// ``` |
| 86 |
#[allow(clippy::should_implement_trait)] |
| 87 |
pub fn from_str(s: &str) -> Result<Self, &'static str> { |
| 88 |
let pos = s.find(|c: char| c != '.' && !c.is_ascii_digit()); |
| 89 |
let kernel = if let Some(pos) = pos { |
| 90 |
let (s, _) = s.split_at(pos); |
| 91 |
s |
| 92 |
} else { |
| 93 |
s |
| 94 |
}; |
| 95 |
let mut kernel_split = kernel.split('.'); |
| 96 |
|
| 97 |
let major = kernel_split.next().ok_or("Missing major version component")?; |
| 98 |
let minor = kernel_split.next().ok_or("Missing minor version component")?; |
| 99 |
let patch = kernel_split.next().ok_or("Missing patch version component")?; |
| 100 |
|
| 101 |
let major = major.parse().map_err(|_| "Failed to parse major version")?; |
| 102 |
let minor = minor.parse().map_err(|_| "Failed to parse minor version")?; |
| 103 |
let patch = patch.parse().map_err(|_| "Failed to parse patch version")?; |
| 104 |
|
| 105 |
Ok(Version { major, minor, patch }) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
impl FromStr for Version { |
| 110 |
type Err = &'static str; |
| 111 |
|
| 112 |
/// Parses a kernel version string, in major.minor.release syntax. |
| 113 |
/// |
| 114 |
/// Note that any extra information (stuff after a dash) is ignored. |
| 115 |
/// |
| 116 |
/// # Example |
| 117 |
/// |
| 118 |
/// ``` |
| 119 |
/// # use procfs::KernelVersion; |
| 120 |
/// let a: KernelVersion = "3.16.0-6-amd64".parse().unwrap(); |
| 121 |
/// let b = KernelVersion::new(3, 16, 0); |
| 122 |
/// assert_eq!(a, b); |
| 123 |
/// |
| 124 |
/// ``` |
| 125 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 126 |
Version::from_str(s) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
impl cmp::Ord for Version { |
| 131 |
fn cmp(&self, other: &Self) -> cmp::Ordering { |
| 132 |
match self.major.cmp(&other.major) { |
| 133 |
cmp::Ordering::Equal => match self.minor.cmp(&other.minor) { |
| 134 |
cmp::Ordering::Equal => self.patch.cmp(&other.patch), |
| 135 |
x => x, |
| 136 |
}, |
| 137 |
x => x, |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
impl cmp::PartialOrd for Version { |
| 143 |
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { |
| 144 |
Some(self.cmp(other)) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/// Represents a kernel type |
| 149 |
#[derive(Debug, Clone, Eq, PartialEq)] |
| 150 |
pub struct Type { |
| 151 |
pub sysname: String, |
| 152 |
} |
| 153 |
|
| 154 |
impl Type { |
| 155 |
pub fn new(sysname: String) -> Type { |
| 156 |
Type { sysname } |
| 157 |
} |
| 158 |
|
| 159 |
/// Read the kernel type from current running kernel |
| 160 |
/// |
| 161 |
/// Defined in `include/linux/uts.h` as UTS_SYSNAME, default is "Linux". |
| 162 |
/// The file is located at `/proc/sys/kernel/ostype`. |
| 163 |
pub fn current() -> ProcResult<Self> { |
| 164 |
read_value("/proc/sys/kernel/ostype") |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
impl FromStr for Type { |
| 169 |
type Err = &'static str; |
| 170 |
|
| 171 |
/// Parse a kernel type string |
| 172 |
/// |
| 173 |
/// Notice that in Linux source code, it is defined as a single string. |
| 174 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 175 |
Ok(Type::new(s.to_string())) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
/// Represents a kernel build information |
| 180 |
#[derive(Debug, Clone, Eq, PartialEq)] |
| 181 |
pub struct BuildInfo { |
| 182 |
pub version: String, |
| 183 |
pub flags: HashSet<String>, |
| 184 |
/// This field contains any extra data from the /proc/sys/kernel/version file. It generally contains the build date of the kernel, but the format of the date can vary. |
| 185 |
/// |
| 186 |
/// A method named `extra_date` is provided which would try to parse some date formats. When the date format is not supported, an error will be returned. It depends on chrono feature. |
| 187 |
pub extra: String, |
| 188 |
} |
| 189 |
|
| 190 |
impl BuildInfo { |
| 191 |
pub fn new(version: &str, flags: HashSet<String>, extra: String) -> BuildInfo { |
| 192 |
BuildInfo { |
| 193 |
version: version.to_string(), |
| 194 |
flags, |
| 195 |
extra, |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
/// Read the kernel build information from current running kernel |
| 200 |
/// |
| 201 |
/// Generated by `scripts/mkcompile_h` when building the kernel. |
| 202 |
/// The file is located at `/proc/sys/kernel/version`. |
| 203 |
pub fn current() -> ProcResult<Self> { |
| 204 |
read_value("/proc/sys/kernel/version") |
| 205 |
} |
| 206 |
|
| 207 |
/// Check if SMP is ON |
| 208 |
pub fn smp(&self) -> bool { |
| 209 |
self.flags.contains("SMP") |
| 210 |
} |
| 211 |
|
| 212 |
/// Check if PREEMPT is ON |
| 213 |
pub fn preempt(&self) -> bool { |
| 214 |
self.flags.contains("PREEMPT") |
| 215 |
} |
| 216 |
|
| 217 |
/// Check if PREEMPTRT is ON |
| 218 |
pub fn preemptrt(&self) -> bool { |
| 219 |
self.flags.contains("PREEMPTRT") |
| 220 |
} |
| 221 |
|
| 222 |
/// Return version number |
| 223 |
/// |
| 224 |
/// This would parse number from first digits of version string. For example, #21~1 to 21. |
| 225 |
pub fn version_number(&self) -> ProcResult<u32> { |
| 226 |
let mut version_str = String::new(); |
| 227 |
for c in self.version.chars() { |
| 228 |
if c.is_ascii_digit() { |
| 229 |
version_str.push(c); |
| 230 |
} else { |
| 231 |
break; |
| 232 |
} |
| 233 |
} |
| 234 |
let version_number: u32 = version_str.parse().map_err(|_| "Failed to parse version number")?; |
| 235 |
Ok(version_number) |
| 236 |
} |
| 237 |
|
| 238 |
/// Parse extra field to `DateTime` object |
| 239 |
/// |
| 240 |
/// This function may fail as TIMESTAMP can be various formats. |
| 241 |
#[cfg(feature = "chrono")] |
| 242 |
pub fn extra_date(&self) -> ProcResult<chrono::DateTime<chrono::Local>> { |
| 243 |
if let Ok(dt) = |
| 244 |
chrono::DateTime::parse_from_str(&format!("{} +0000", &self.extra), "%a %b %d %H:%M:%S UTC %Y %z") |
| 245 |
{ |
| 246 |
return Ok(dt.with_timezone(&chrono::Local)); |
| 247 |
} |
| 248 |
if let Ok(dt) = chrono::DateTime::parse_from_str(&self.extra, "%a, %d %b %Y %H:%M:%S %z") { |
| 249 |
return Ok(dt.with_timezone(&chrono::Local)); |
| 250 |
} |
| 251 |
Err(ProcError::Other("Failed to parse extra field to date".to_string())) |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
impl FromStr for BuildInfo { |
| 256 |
type Err = &'static str; |
| 257 |
|
| 258 |
/// Parse a kernel build information string |
| 259 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 260 |
let mut version = String::new(); |
| 261 |
let mut flags: HashSet<String> = HashSet::new(); |
| 262 |
let mut extra: String = String::new(); |
| 263 |
|
| 264 |
let mut splited = s.split(' '); |
| 265 |
let version_str = splited.next(); |
| 266 |
if let Some(version_str) = version_str { |
| 267 |
if let Some(stripped) = version_str.strip_prefix('#') { |
| 268 |
version.push_str(stripped); |
| 269 |
} else { |
| 270 |
return Err("Failed to parse kernel build version"); |
| 271 |
} |
| 272 |
} else { |
| 273 |
return Err("Failed to parse kernel build version"); |
| 274 |
} |
| 275 |
|
| 276 |
for s in &mut splited { |
| 277 |
if s.chars().all(char::is_uppercase) { |
| 278 |
flags.insert(s.to_string()); |
| 279 |
} else { |
| 280 |
extra.push_str(s); |
| 281 |
extra.push(' '); |
| 282 |
break; |
| 283 |
} |
| 284 |
} |
| 285 |
let remains: Vec<&str> = splited.collect(); |
| 286 |
extra.push_str(&remains.join(" ")); |
| 287 |
|
| 288 |
Ok(BuildInfo { version, flags, extra }) |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
/// Returns the maximum process ID number. |
| 293 |
/// |
| 294 |
/// This is taken from `/proc/sys/kernel/pid_max`. |
| 295 |
/// |
| 296 |
/// # Example |
| 297 |
/// |
| 298 |
/// ``` |
| 299 |
/// let pid_max = procfs::sys::kernel::pid_max().unwrap(); |
| 300 |
/// |
| 301 |
/// let pid = 42; // e.g. from user input, CLI args, etc. |
| 302 |
/// |
| 303 |
/// if pid > pid_max { |
| 304 |
/// eprintln!("bad process ID: {}", pid) |
| 305 |
/// } else { |
| 306 |
/// println!("good process ID: {}", pid); |
| 307 |
/// } |
| 308 |
/// ``` |
| 309 |
pub fn pid_max() -> ProcResult<i32> { |
| 310 |
read_value("/proc/sys/kernel/pid_max") |
| 311 |
} |
| 312 |
|
| 313 |
#[derive(Debug, PartialEq, Eq, Copy, Clone)] |
| 314 |
/// Represents the data from `/proc/sys/kernel/sem` |
| 315 |
pub struct SemaphoreLimits { |
| 316 |
/// The maximum semaphores per semaphore set |
| 317 |
pub semmsl: u64, |
| 318 |
/// A system-wide limit on the number of semaphores in all semaphore sets |
| 319 |
pub semmns: u64, |
| 320 |
/// The maximum number of operations that may be specified in a semop(2) call |
| 321 |
pub semopm: u64, |
| 322 |
/// A system-wide limit on the maximum number of semaphore identifiers |
| 323 |
pub semmni: u64, |
| 324 |
} |
| 325 |
|
| 326 |
impl SemaphoreLimits { |
| 327 |
pub fn new() -> ProcResult<Self> { |
| 328 |
read_value("/proc/sys/kernel/sem") |
| 329 |
} |
| 330 |
|
| 331 |
fn from_str(s: &str) -> Result<Self, &'static str> { |
| 332 |
let mut s = s.split_ascii_whitespace(); |
| 333 |
|
| 334 |
let semmsl = s.next().ok_or("Missing SEMMSL")?; |
| 335 |
let semmns = s.next().ok_or("Missing SEMMNS")?; |
| 336 |
let semopm = s.next().ok_or("Missing SEMOPM")?; |
| 337 |
let semmni = s.next().ok_or("Missing SEMMNI")?; |
| 338 |
|
| 339 |
let semmsl = semmsl.parse().map_err(|_| "Failed to parse SEMMSL")?; |
| 340 |
let semmns = semmns.parse().map_err(|_| "Failed to parse SEMMNS")?; |
| 341 |
let semopm = semopm.parse().map_err(|_| "Failed to parse SEMOPM")?; |
| 342 |
let semmni = semmni.parse().map_err(|_| "Failed to parse SEMMNI")?; |
| 343 |
|
| 344 |
Ok(SemaphoreLimits { |
| 345 |
semmsl, |
| 346 |
semmns, |
| 347 |
semopm, |
| 348 |
semmni, |
| 349 |
}) |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
impl FromStr for SemaphoreLimits { |
| 354 |
type Err = &'static str; |
| 355 |
|
| 356 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 357 |
SemaphoreLimits::from_str(s) |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
/// Returns the system-wide limit on the total number of pages of System V shared memory |
| 362 |
/// |
| 363 |
/// This is taken from `/proc/sys/kernel/shmall` |
| 364 |
pub fn shmall() -> ProcResult<u64> { |
| 365 |
read_value("/proc/sys/kernel/shmall") |
| 366 |
} |
| 367 |
|
| 368 |
/// Returns the limit on the maximum (System V IPC) shared memory segment size that can be created. |
| 369 |
/// The value defaults to SHMMAX |
| 370 |
/// |
| 371 |
/// See also [set_shmmax](crate::sys::kernel::set_shmmax) |
| 372 |
/// |
| 373 |
/// This is taken from `/proc/sys/kernel/shmmax` |
| 374 |
pub fn shmmax() -> ProcResult<u64> { |
| 375 |
read_value("/proc/sys/kernel/shmmax") |
| 376 |
} |
| 377 |
|
| 378 |
/// Sets the limit on the maximum (System V IPC) shared memory segment size. |
| 379 |
/// |
| 380 |
/// See also [shmmax](crate::sys::kernel::shmmax) |
| 381 |
pub fn set_shmmax(new_value: u64) -> ProcResult<()> { |
| 382 |
write_value("/proc/sys/kernel/shmmax", new_value) |
| 383 |
} |
| 384 |
|
| 385 |
/// Returns the system-wide maximum number of System V shared memory segments that can be created |
| 386 |
/// |
| 387 |
/// This is taken from `/proc/sys/kernel/shmmni` |
| 388 |
pub fn shmmni() -> ProcResult<u64> { |
| 389 |
read_value("/proc/sys/kernel/shmmni") |
| 390 |
} |
| 391 |
|
| 392 |
bitflags! { |
| 393 |
/// Flags representing allowed sysrq functions |
| 394 |
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)] |
| 395 |
pub struct AllowedFunctions : u16 { |
| 396 |
/// Enable control of console log level |
| 397 |
const ENABLE_CONTROL_LOG_LEVEL = 2; |
| 398 |
/// Enable control of keyboard (SAK, unraw) |
| 399 |
const ENABLE_CONTROL_KEYBOARD = 4; |
| 400 |
/// Enable debugging dumps of processes etc |
| 401 |
const ENABLE_DEBUGGING_DUMPS = 8; |
| 402 |
/// Enable sync command |
| 403 |
const ENABLE_SYNC_COMMAND = 16; |
| 404 |
/// Enable remound read-only |
| 405 |
const ENABLE_REMOUNT_READ_ONLY = 32; |
| 406 |
/// Enable signaling of processes (term, kill, oom-kill) |
| 407 |
const ENABLE_SIGNALING_PROCESSES = 64; |
| 408 |
/// Allow reboot/poweroff |
| 409 |
const ALLOW_REBOOT_POWEROFF = 128; |
| 410 |
/// Allow nicing of all real-time tasks |
| 411 |
const ALLOW_NICING_REAL_TIME_TASKS = 256; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
/// Values controlling functions allowed to be invoked by the SysRq key |
| 416 |
/// |
| 417 |
/// To construct this enum, see [sysrq](crate::sys::kernel::sysrq) |
| 418 |
#[derive(Copy, Clone, Debug)] |
| 419 |
pub enum SysRq { |
| 420 |
/// Disable sysrq completely |
| 421 |
Disable, |
| 422 |
/// Enable all functions of sysrq |
| 423 |
Enable, |
| 424 |
/// Bitmask of allowed sysrq functions |
| 425 |
AllowedFunctions(AllowedFunctions), |
| 426 |
} |
| 427 |
|
| 428 |
impl SysRq { |
| 429 |
fn to_number(self) -> u16 { |
| 430 |
match self { |
| 431 |
SysRq::Disable => 0, |
| 432 |
SysRq::Enable => 1, |
| 433 |
SysRq::AllowedFunctions(allowed) => allowed.bits(), |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
fn from_str(s: &str) -> ProcResult<Self> { |
| 438 |
match s.parse::<u16>()? { |
| 439 |
0 => Ok(SysRq::Disable), |
| 440 |
1 => Ok(SysRq::Enable), |
| 441 |
x => match AllowedFunctions::from_bits(x) { |
| 442 |
Some(allowed) => Ok(SysRq::AllowedFunctions(allowed)), |
| 443 |
None => Err("Invalid value".into()), |
| 444 |
}, |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
impl FromStr for SysRq { |
| 450 |
type Err = ProcError; |
| 451 |
|
| 452 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 453 |
SysRq::from_str(s) |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
/// Return functions allowed to be invoked by the SysRq key |
| 458 |
/// |
| 459 |
/// This is taken from `/proc/sys/kernel/sysrq` |
| 460 |
pub fn sysrq() -> ProcResult<SysRq> { |
| 461 |
read_value("/proc/sys/kernel/sysrq") |
| 462 |
} |
| 463 |
|
| 464 |
/// Set functions allowed to be invoked by the SysRq key |
| 465 |
pub fn set_sysrq(new: SysRq) -> ProcResult<()> { |
| 466 |
write_value("/proc/sys/kernel/sysrq", new.to_number()) |
| 467 |
} |
| 468 |
|
| 469 |
/// The minimum value that can be written to `/proc/sys/kernel/threads-max` on Linux 4.1 or later |
| 470 |
pub const THREADS_MIN: u32 = 20; |
| 471 |
/// The maximum value that can be written to `/proc/sys/kernel/threads-max` on Linux 4.1 or later |
| 472 |
pub const THREADS_MAX: u32 = 0x3fff_ffff; |
| 473 |
|
| 474 |
/// Returns the system-wide limit on the number of threads (tasks) that can be created on the system. |
| 475 |
/// |
| 476 |
/// This is taken from `/proc/sys/kernel/threads-max` |
| 477 |
pub fn threads_max() -> ProcResult<u32> { |
| 478 |
read_value("/proc/sys/kernel/threads-max") |
| 479 |
} |
| 480 |
|
| 481 |
/// Sets the system-wide limit on the number of threads (tasks) that can be created on the system. |
| 482 |
/// |
| 483 |
/// Since Linux 4.1, this value is bounded, and must be in the range [THREADS_MIN]..=[THREADS_MAX]. |
| 484 |
/// This function will return an error if that is not the case. |
| 485 |
pub fn set_threads_max(new_limit: u32) -> ProcResult<()> { |
| 486 |
if let Ok(kernel) = Version::cached() { |
| 487 |
if kernel.major >= 4 && kernel.minor >= 1 && !(THREADS_MIN..=THREADS_MAX).contains(&new_limit) { |
| 488 |
return Err(ProcError::Other(format!( |
| 489 |
"{} is outside the THREADS_MIN..=THREADS_MAX range", |
| 490 |
new_limit |
| 491 |
))); |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
write_value("/proc/sys/kernel/threads-max", new_limit) |
| 496 |
} |
| 497 |
|
| 498 |
#[cfg(test)] |
| 499 |
mod tests { |
| 500 |
use super::*; |
| 501 |
|
| 502 |
#[test] |
| 503 |
fn test_version() { |
| 504 |
let a = Version::from_str("3.16.0-6-amd64").unwrap(); |
| 505 |
let b = Version::new(3, 16, 0); |
| 506 |
assert_eq!(a, b); |
| 507 |
|
| 508 |
let a = Version::from_str("3.16.0").unwrap(); |
| 509 |
let b = Version::new(3, 16, 0); |
| 510 |
assert_eq!(a, b); |
| 511 |
|
| 512 |
let a = Version::from_str("3.16.0_1").unwrap(); |
| 513 |
let b = Version::new(3, 16, 0); |
| 514 |
assert_eq!(a, b); |
| 515 |
} |
| 516 |
|
| 517 |
#[test] |
| 518 |
fn test_type() { |
| 519 |
let a = Type::from_str("Linux").unwrap(); |
| 520 |
assert_eq!(a.sysname, "Linux"); |
| 521 |
} |
| 522 |
|
| 523 |
#[test] |
| 524 |
fn test_build_info() { |
| 525 |
// For Ubuntu, Manjaro, CentOS and others: |
| 526 |
let a = BuildInfo::from_str("#1 SMP PREEMPT Thu Sep 30 15:29:01 UTC 2021").unwrap(); |
| 527 |
let mut flags: HashSet<String> = HashSet::new(); |
| 528 |
flags.insert("SMP".to_string()); |
| 529 |
flags.insert("PREEMPT".to_string()); |
| 530 |
assert_eq!(a.version, "1"); |
| 531 |
assert_eq!(a.version_number().unwrap(), 1); |
| 532 |
assert_eq!(a.flags, flags); |
| 533 |
assert!(a.smp()); |
| 534 |
assert!(a.preempt()); |
| 535 |
assert!(!a.preemptrt()); |
| 536 |
assert_eq!(a.extra, "Thu Sep 30 15:29:01 UTC 2021"); |
| 537 |
#[cfg(feature = "chrono")] |
| 538 |
let _ = a.extra_date().unwrap(); |
| 539 |
|
| 540 |
// For Arch and others: |
| 541 |
let b = BuildInfo::from_str("#1 SMP PREEMPT Fri, 12 Nov 2021 19:22:10 +0000").unwrap(); |
| 542 |
assert_eq!(b.version, "1"); |
| 543 |
assert_eq!(b.version_number().unwrap(), 1); |
| 544 |
assert_eq!(b.flags, flags); |
| 545 |
assert_eq!(b.extra, "Fri, 12 Nov 2021 19:22:10 +0000"); |
| 546 |
assert!(b.smp()); |
| 547 |
assert!(b.preempt()); |
| 548 |
assert!(!b.preemptrt()); |
| 549 |
#[cfg(feature = "chrono")] |
| 550 |
let _ = b.extra_date().unwrap(); |
| 551 |
|
| 552 |
// For Debian and others: |
| 553 |
let c = BuildInfo::from_str("#1 SMP Debian 5.10.46-4 (2021-08-03)").unwrap(); |
| 554 |
let mut flags: HashSet<String> = HashSet::new(); |
| 555 |
flags.insert("SMP".to_string()); |
| 556 |
assert_eq!(c.version, "1"); |
| 557 |
assert_eq!(c.version_number().unwrap(), 1); |
| 558 |
assert_eq!(c.flags, flags); |
| 559 |
assert_eq!(c.extra, "Debian 5.10.46-4 (2021-08-03)"); |
| 560 |
assert!(c.smp()); |
| 561 |
assert!(!c.preempt()); |
| 562 |
assert!(!c.preemptrt()); |
| 563 |
// Skip the date parsing for now |
| 564 |
} |
| 565 |
|
| 566 |
#[test] |
| 567 |
fn test_current() { |
| 568 |
let _ = Version::current().unwrap(); |
| 569 |
let _ = Type::current().unwrap(); |
| 570 |
let _ = BuildInfo::current().unwrap(); |
| 571 |
} |
| 572 |
|
| 573 |
#[test] |
| 574 |
fn test_pid_max() { |
| 575 |
assert!(pid_max().is_ok()); |
| 576 |
} |
| 577 |
|
| 578 |
#[test] |
| 579 |
fn test_semaphore_limits() { |
| 580 |
// Note that the below string has tab characters in it. Make sure to not remove them. |
| 581 |
let a = SemaphoreLimits::from_str("32000 1024000000 500 32000").unwrap(); |
| 582 |
let b = SemaphoreLimits { |
| 583 |
semmsl: 32_000, |
| 584 |
semmns: 1_024_000_000, |
| 585 |
semopm: 500, |
| 586 |
semmni: 32_000, |
| 587 |
}; |
| 588 |
assert_eq!(a, b); |
| 589 |
|
| 590 |
let a = SemaphoreLimits::from_str("1"); |
| 591 |
assert!(a.is_err() && a.err().unwrap() == "Missing SEMMNS"); |
| 592 |
|
| 593 |
let a = SemaphoreLimits::from_str("1 string 500 3200"); |
| 594 |
assert!(a.is_err() && a.err().unwrap() == "Failed to parse SEMMNS"); |
| 595 |
} |
| 596 |
|
| 597 |
#[test] |
| 598 |
fn test_sem() { |
| 599 |
let _ = SemaphoreLimits::new().unwrap(); |
| 600 |
} |
| 601 |
#[test] |
| 602 |
fn test_shmall() { |
| 603 |
let _ = shmall().unwrap(); |
| 604 |
} |
| 605 |
|
| 606 |
#[test] |
| 607 |
fn test_shmmax() { |
| 608 |
let _ = shmmax().unwrap(); |
| 609 |
} |
| 610 |
|
| 611 |
#[test] |
| 612 |
fn test_shmmni() { |
| 613 |
let _ = shmmni().unwrap(); |
| 614 |
} |
| 615 |
|
| 616 |
#[test] |
| 617 |
fn test_sysrq() { |
| 618 |
let sys_rq = sysrq().unwrap(); |
| 619 |
println!("{:?}", sys_rq) |
| 620 |
} |
| 621 |
|
| 622 |
#[test] |
| 623 |
fn test_threads_max() { |
| 624 |
let _ = threads_max().unwrap(); |
| 625 |
} |
| 626 |
} |