Skip to content

Commit 8b6a8d1

Browse files
Add redox os check (#100)
* Change conditional compiles to include redox as linux variant and add a compile check for it in CI * Fix other linux compiles to include redox * Only check redox on linux
1 parent 27821df commit 8b6a8d1

File tree

9 files changed

+49
-40
lines changed

9 files changed

+49
-40
lines changed

.github/workflows/clippy_build_test.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ jobs:
4747
- name: Clippy
4848
run: make clippy
4949

50+
- name: Compile check on redox
51+
if: runner.os == 'Linux'
52+
run: |
53+
rustup target add x86_64-unknown-redox
54+
cargo +nightly check --target x86_64-unknown-redox
55+
5056
- name: ConfigureCoverage
5157
if: matrix.rust == 'nightly'
5258
run: |
@@ -57,11 +63,11 @@ jobs:
5763
5864
- name: Run Tests on Linux
5965
if: runner.os == 'Linux'
60-
run: env "PATH=$PATH" cargo test
66+
run: env "PATH=$PATH" cargo test
6167

6268
- name: Run Tests as Root on Mac
6369
if: runner.os == 'macOS'
64-
run: sudo env "PATH=$PATH" cargo test
70+
run: sudo env "PATH=$PATH" cargo test
6571

6672
- name: UploadCoverage
6773
if: matrix.rust == 'nightly'

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ path = "src/dmesg.rs"
3333
[build-dependencies]
3434
bindgen = { version = "0.66.1", default-features = false, features = ["runtime"] }
3535

36-
[target.'cfg(target_os = "linux")'.dev-dependencies]
36+
# NOTE: This assumes that there is a procfs compatible FS in redox and the procfs crate
37+
# supports it. It's quite probably that neither of those two things ever happen.
38+
# But making this assumption for now so that everything compiles at least for redox
39+
[target.'cfg(any(target_os = "linux", target_os = "redox"))'.dev-dependencies]
3740
procfs = "0.15.0"
3841
tempfile = "3.3.0"

build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ fn main() {
1919
.expect("Failed to write libproc bindings");
2020
}
2121

22-
#[cfg(not(target_os = "macos"))]
22+
#[cfg(any(target_os = "linux", target_os = "redox"))]
2323
fn main() {}

src/libproc/file_info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ pub fn pidfdinfo<T: PIDFDInfo>(pid: i32, fd: i32) -> Result<T, String> {
169169
}
170170
}
171171

172-
#[cfg(not(target_os = "macos"))]
172+
#[cfg(any(target_os = "linux", target_os = "redox"))]
173173
pub fn pidfdinfo<T: PIDFDInfo>(_pid: i32, _fd: i32) -> Result<T, String> {
174174
unimplemented!()
175175
}

src/libproc/helpers.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::errno::errno;
2-
#[cfg(target_os = "linux")]
2+
#[cfg(any(target_os = "linux", target_os = "redox"))]
33
use std::fs::File;
4-
#[cfg(target_os = "linux")]
4+
#[cfg(any(target_os = "linux", target_os = "redox"))]
55
use std::io::{BufRead, BufReader};
66

77
/// Helper function to get errno and return a String with the passed in return_code, the error
@@ -30,7 +30,7 @@ pub fn check_errno(ret: i32, buf: &mut Vec<u8>) -> Result<String, String> {
3030
}
3131
}
3232

33-
#[cfg(target_os = "linux")]
33+
#[cfg(any(target_os = "linux", target_os = "redox"))]
3434
/// A helper function for finding named fields in specific /proc FS files for processes
3535
/// This will be more useful when implementing more linux functions
3636
pub fn procfile_field(filename: &str, field_name: &str) -> Result<String, String> {
@@ -53,7 +53,7 @@ pub fn procfile_field(filename: &str, field_name: &str) -> Result<String, String
5353
Err(format!("Could not find the field named '{field_name}' in the /proc FS file name '{filename}'"))
5454
}
5555

56-
#[cfg(target_os = "linux")]
56+
#[cfg(any(target_os = "linux", target_os = "redox"))]
5757
/// Parse a memory amount string into integer number of bytes
5858
/// e.g. 220844 kB -->
5959
pub fn parse_memory_string(line: &str) -> Result<u64, String> {
@@ -83,7 +83,7 @@ mod test {
8383
use crate::errno::{set_errno, Errno};
8484
use super::check_errno;
8585

86-
#[cfg(target_os = "linux")]
86+
#[cfg(any(target_os = "linux", target_os = "redox"))]
8787
mod linux {
8888
use crate::libproc::helpers::parse_memory_string;
8989

@@ -143,7 +143,7 @@ mod test {
143143
if let Err(mes) = check_errno(-1, &mut buf) {
144144
#[cfg(target_os = "macos")]
145145
assert_eq!(mes, "return code = -1, errno = -1, message = 'Unknown error: -1'");
146-
#[cfg(target_os = "linux")]
146+
#[cfg(any(target_os = "linux", target_os = "redox"))]
147147
assert_eq!(mes, "return code = -1, errno = -1, message = 'Unknown error -1'");
148148
}
149149
}

src/libproc/kmesg_buffer.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ use std::str;
77
#[cfg(target_os = "macos")]
88
use self::libc::c_void;
99

10-
#[cfg(target_os = "linux")]
10+
#[cfg(any(target_os = "linux", target_os = "redox"))]
1111
use std::fs::File;
12-
#[cfg(target_os = "linux")]
12+
#[cfg(any(target_os = "linux", target_os = "redox"))]
1313
use std::io::{BufRead, BufReader};
14-
#[cfg(target_os = "linux")]
14+
#[cfg(any(target_os = "linux", target_os = "redox"))]
1515
use std::sync::mpsc;
16-
#[cfg(target_os = "linux")]
16+
#[cfg(any(target_os = "linux", target_os = "redox"))]
1717
use std::sync::mpsc::Receiver;
18-
#[cfg(target_os = "linux")]
18+
#[cfg(any(target_os = "linux", target_os = "redox"))]
1919
use std::{thread, time};
2020

2121
#[cfg(target_os = "macos")]
@@ -56,7 +56,7 @@ pub fn kmsgbuf() -> Result<String, String> {
5656
/// reading methods will block at the end of file, so a workaround is required. Do the blocking
5757
/// reads on a thread that sends lines read back through a channel, and then return when the thread
5858
/// has blocked and can't send anymore. Returning will end the thread and the channel.
59-
#[cfg(target_os = "linux")]
59+
#[cfg(any(target_os = "linux", target_os = "redox"))]
6060
pub fn kmsgbuf() -> Result<String, String> {
6161
let mut file = File::open("/dev/kmsg");
6262
if file.is_err() {
@@ -75,7 +75,7 @@ pub fn kmsgbuf() -> Result<String, String> {
7575

7676
// Create a channel to return lines read from a file on, then create a thread that reads the lines
7777
// and sends them back on the channel one by one. Eventually it will get to EOF or block
78-
#[cfg(target_os = "linux")]
78+
#[cfg(any(target_os = "linux", target_os = "redox"))]
7979
fn spawn_kmsg_channel(file: File) -> Receiver<String> {
8080
let mut reader = BufReader::new(file);
8181
let (tx, rx) = mpsc::channel::<String>();

src/libproc/pid_rusage.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::libproc::helpers;
66
#[cfg(target_os = "macos")]
77
use self::libc::c_void;
88

9-
#[cfg(target_os = "linux")]
9+
#[cfg(any(target_os = "linux", target_os = "redox"))]
1010
use crate::libproc::helpers::{procfile_field, parse_memory_string};
1111
#[cfg(target_os = "macos")]
1212
use crate::osx_libproc_bindings::proc_pid_rusage;
@@ -386,7 +386,7 @@ pub fn pidrusage<T: PIDRUsage>(pid : i32) -> Result<T, String> {
386386
}
387387
}
388388

389-
#[cfg(target_os = "linux")]
389+
#[cfg(any(target_os = "linux", target_os = "redox"))]
390390
/// Returns the information about resources of the process that match pid passed in.
391391
///
392392
/// # Examples

src/libproc/proc_pid.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
extern crate libc;
22

33
use std::env;
4-
#[cfg(target_os = "linux")]
4+
#[cfg(any(target_os = "linux", target_os = "redox"))]
55
use std::ffi::CString;
6-
#[cfg(target_os = "linux")]
6+
#[cfg(any(target_os = "linux", target_os = "redox"))]
77
use std::fs;
88
#[cfg(target_os = "macos")]
99
use std::mem;
1010
use std::path::PathBuf;
1111

1212
use libc::pid_t;
13-
#[cfg(target_os = "linux")]
13+
#[cfg(any(target_os = "linux", target_os = "redox"))]
1414
use libc::PATH_MAX;
1515

1616
#[cfg(target_os = "macos")]
@@ -29,7 +29,7 @@ use crate::osx_libproc_bindings::{
2929

3030
#[cfg(target_os = "macos")]
3131
use self::libc::c_void;
32-
#[cfg(target_os = "linux")]
32+
#[cfg(any(target_os = "linux", target_os = "redox"))]
3333
use self::libc::{c_char, readlink};
3434

3535
use crate::processes;
@@ -242,7 +242,7 @@ pub fn pidinfo<T: PIDInfo>(pid: i32, arg: u64) -> Result<T, String> {
242242
}
243243

244244
/// pidinfo not implemented on linux - Pull Requests welcome - TODO
245-
#[cfg(not(target_os = "macos"))]
245+
#[cfg(any(target_os = "linux", target_os = "redox"))]
246246
pub fn pidinfo<T: PIDInfo>(_pid: i32, _arg: u64) -> Result<T, String> {
247247
unimplemented!()
248248
}
@@ -295,7 +295,7 @@ pub fn regionfilename(pid: i32, address: u64) -> Result<String, String> {
295295
/// }
296296
/// }
297297
/// ```
298-
#[cfg(not(target_os = "macos"))]
298+
#[cfg(any(target_os = "linux", target_os = "redox"))]
299299
pub fn regionfilename(_pid: i32, _address: u64) -> Result<String, String> {
300300
Err("'regionfilename' not implemented on linux".to_owned())
301301
}
@@ -340,7 +340,7 @@ pub fn pidpath(pid: i32) -> Result<String, String> {
340340
/// _ => panic!("Unknown error")
341341
/// }
342342
/// ```
343-
#[cfg(target_os = "linux")]
343+
#[cfg(any(target_os = "linux", target_os = "redox"))]
344344
pub fn pidpath(pid: i32) -> Result<String, String> {
345345
let exe_path = CString::new(format!("/proc/{pid}/exe"))
346346
.map_err(|_| "Could not create CString")?;
@@ -396,7 +396,7 @@ pub fn libversion() -> Result<(i32, i32), String> {
396396
/// Err(err) => eprintln!("Error: {}", err)
397397
/// }
398398
/// ```
399-
#[cfg(not(target_os = "macos"))]
399+
#[cfg(any(target_os = "linux", target_os = "redox"))]
400400
pub fn libversion() -> Result<(i32, i32), String> {
401401
Err("Linux does not use a library, so no library version number".to_owned())
402402
}
@@ -440,7 +440,7 @@ pub fn name(pid: i32) -> Result<String, String> {
440440

441441

442442
/// Get the name of a process, using it's process id (pid)
443-
#[cfg(target_os = "linux")]
443+
#[cfg(any(target_os = "linux", target_os = "redox"))]
444444
pub fn name(pid: i32) -> Result<String, String> {
445445
helpers::procfile_field(&format!("/proc/{pid}/status"), "Name")
446446
}
@@ -495,7 +495,7 @@ pub fn listpidinfo<T: ListPIDInfo>(pid: i32, max_len: usize) -> Result<Vec<T::It
495495
}
496496

497497
/// listpidinfo is not implemented on Linux - Pull Requests welcome - TODO
498-
#[cfg(not(target_os = "macos"))]
498+
#[cfg(any(target_os = "linux", target_os = "redox"))]
499499
pub fn listpidinfo<T: ListPIDInfo>(_pid: i32, _max_len: usize) -> Result<Vec<T::Item>, String> {
500500
unimplemented!()
501501
}
@@ -517,7 +517,7 @@ pub fn pidcwd(_pid: pid_t) -> Result<PathBuf, String> {
517517
Err("pidcwd is not implemented for macos".into())
518518
}
519519

520-
#[cfg(target_os = "linux")]
520+
#[cfg(any(target_os = "linux", target_os = "redox"))]
521521
/// Gets the path of current working directory for the process with the provided pid.
522522
///
523523
/// # Examples
@@ -573,7 +573,7 @@ pub fn am_root() -> bool {
573573
}
574574

575575
/// Return true if the calling process is being run by the root user, false otherwise
576-
#[cfg(target_os = "linux")]
576+
#[cfg(any(target_os = "linux", target_os = "redox"))]
577577
pub fn am_root() -> bool {
578578
// when this becomes stable in rust libc then we can remove this function or combine for mac and linux
579579
unsafe { libc::geteuid() == 0 }
@@ -595,10 +595,10 @@ mod test {
595595
#[cfg(target_os = "macos")]
596596
use super::{libversion, listpidinfo, ListThreads, pidinfo};
597597
use super::{name, cwdself, pidpath};
598-
#[cfg(target_os = "linux")]
598+
#[cfg(any(target_os = "linux", target_os = "redox"))]
599599
use super::pidcwd;
600600
use super::am_root;
601-
#[cfg(target_os = "linux")]
601+
#[cfg(any(target_os = "linux", target_os = "redox"))]
602602
use crate::libproc::helpers;
603603
#[cfg(target_os = "macos")]
604604
use crate::libproc::task_info::TaskInfo;
@@ -687,7 +687,7 @@ mod test {
687687

688688
#[test]
689689
fn name_test() {
690-
if am_root() || cfg!(target_os = "linux") {
690+
if am_root() || cfg!(any(target_os = "linux", target_os = "redox")) {
691691
assert!(&name(process::id() as i32).expect("Could not get the process name")
692692
.starts_with("libproc"), "Incorrect process name");
693693
} else {
@@ -700,7 +700,7 @@ mod test {
700700
fn pidpath_test_unknown_pid_test() {
701701
#[cfg(target_os = "macos")]
702702
let error_message = "No such process";
703-
#[cfg(target_os = "linux")]
703+
#[cfg(any(target_os = "linux", target_os = "redox"))]
704704
let error_message = "No such file or directory";
705705

706706
match pidpath(-1) {
@@ -724,7 +724,7 @@ mod test {
724724
cwdself().expect("cwdself() failed"));
725725
}
726726

727-
#[cfg(target_os = "linux")]
727+
#[cfg(any(target_os = "linux", target_os = "redox"))]
728728
#[test]
729729
fn pidcwd_of_self_test() {
730730
assert_eq!(env::current_dir().expect("Could not get current directory"),
@@ -741,7 +741,7 @@ mod test {
741741
}
742742

743743
#[test]
744-
#[cfg(target_os = "linux")]
744+
#[cfg(any(target_os = "linux", target_os = "redox"))]
745745
fn procfile_field_test() {
746746
if am_root() {
747747
assert!(helpers::procfile_field("/proc/1/status", "invalid").is_err());

src/libproc/sys/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// OS-specific implementations of process-related functions
2-
#[cfg(target_os = "linux")]
2+
#[cfg(any(target_os = "linux", target_os = "redox"))]
33
mod linux;
4-
#[cfg(target_os = "linux")]
4+
#[cfg(any(target_os = "linux", target_os = "redox"))]
55
pub(crate) use self::linux::*;
66

77
#[cfg(target_os = "macos")]

0 commit comments

Comments
 (0)