Skip to content

Commit 12240c2

Browse files
authored
support for android(termux) (#101)
1 parent 1ef7c05 commit 12240c2

File tree

8 files changed

+41
-41
lines changed

8 files changed

+41
-41
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ bindgen = { version = "0.66.1", default-features = false, features = ["runtime"]
3636
# NOTE: This assumes that there is a procfs compatible FS in redox and the procfs crate
3737
# supports it. It's quite probably that neither of those two things ever happen.
3838
# 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]
39+
[target.'cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))'.dev-dependencies]
4040
procfs = "0.15.0"
4141
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(any(target_os = "linux", target_os = "redox"))]
22+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
2323
fn main() {}

src/libproc/file_info.rs

Lines changed: 2 additions & 2 deletions
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(any(target_os = "linux", target_os = "redox"))]
172+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
173173
pub fn pidfdinfo<T: PIDFDInfo>(_pid: i32, _fd: i32) -> Result<T, String> {
174174
unimplemented!()
175175
}
@@ -206,4 +206,4 @@ mod test {
206206
}
207207
}
208208
}
209-
}
209+
}

src/libproc/helpers.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::errno::errno;
2-
#[cfg(any(target_os = "linux", target_os = "redox"))]
2+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
33
use std::fs::File;
4-
#[cfg(any(target_os = "linux", target_os = "redox"))]
4+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
33+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
56+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
86+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
146+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
147147
assert_eq!(mes, "return code = -1, errno = -1, message = 'Unknown error -1'");
148148
}
149149
}
@@ -158,4 +158,4 @@ mod test {
158158
assert_eq!(mes, "return code = 0, errno = 2, message = 'No such file or directory'")
159159
}
160160
}
161-
}
161+
}

src/libproc/kmesg_buffer.rs

Lines changed: 8 additions & 8 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(any(target_os = "linux", target_os = "redox"))]
10+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
1111
use std::fs::File;
12-
#[cfg(any(target_os = "linux", target_os = "redox"))]
12+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
1313
use std::io::{BufRead, BufReader};
14-
#[cfg(any(target_os = "linux", target_os = "redox"))]
14+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
1515
use std::sync::mpsc;
16-
#[cfg(any(target_os = "linux", target_os = "redox"))]
16+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
1717
use std::sync::mpsc::Receiver;
18-
#[cfg(any(target_os = "linux", target_os = "redox"))]
18+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
59+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
78+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
7979
fn spawn_kmsg_channel(file: File) -> Receiver<String> {
8080
let mut reader = BufReader::new(file);
8181
let (tx, rx) = mpsc::channel::<String>();
@@ -109,4 +109,4 @@ mod test {
109109
println!("test skipped as it needs to be run as root");
110110
}
111111
}
112-
}
112+
}

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(any(target_os = "linux", target_os = "redox"))]
9+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
389+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
4+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
55
use std::ffi::CString;
6-
#[cfg(any(target_os = "linux", target_os = "redox"))]
6+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
13+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
32+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
245+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
298+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
343+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
399+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
443+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
498+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
520+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
576+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
598+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
599599
use super::pidcwd;
600600
use super::am_root;
601-
#[cfg(any(target_os = "linux", target_os = "redox"))]
601+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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!(any(target_os = "linux", target_os = "redox")) {
690+
if am_root() || cfg!(any(target_os = "linux", target_os = "redox", target_os = "android")) {
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(any(target_os = "linux", target_os = "redox"))]
703+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
727+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
744+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
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(any(target_os = "linux", target_os = "redox"))]
2+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
33
mod linux;
4-
#[cfg(any(target_os = "linux", target_os = "redox"))]
4+
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
55
pub(crate) use self::linux::*;
66

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

0 commit comments

Comments
 (0)