Last active
June 18, 2020 12:38
-
-
Save unpluggedcoder/773aa6b0d20fbb469c625967ab0bcfed to your computer and use it in GitHub Desktop.
Get current thread name by prctl syscall in Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given by Alice: https://users.rust-lang.org/t/cstring-into-string-s-behavior-on-android/44470/3 | |
use std::ffi::CStr; | |
use std::os::raw::c_char; | |
const PR_GET_NAME: libc::c_int = 16; | |
unsafe fn call_prctl(allocation: &mut Vec<u8>) -> &CStr { | |
let ptr = allocation.as_mut_ptr() as *mut c_char; | |
let ret: libc::c_int = libc::prctl(PR_GET_NAME, ptr); | |
if ret != 0 { | |
panic!("prctl failed: {}", ret); | |
} | |
CStr::from_ptr(allocation.as_ptr() as *const c_char) | |
} | |
fn main() { | |
unsafe { | |
let mut allocation = Vec::with_capacity(32); | |
let c_str_slice = call_prctl(&mut allocation); | |
match c_str_slice.to_str() { | |
Ok(s) => { | |
println!("Return: {}, debug print: {:#?}, len: {}.", s, s, s.len()); | |
}, | |
Err(_) => println!("into_string failed.!"), | |
}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::ffi::CString; | |
const PR_GET_NAME: libc::c_int = 16; | |
fn main() { | |
unsafe { | |
// WRONG WAY!! | |
let cs = CString::from_vec_unchecked(Vec::with_capacity(32) as Vec<u8>); | |
let ptr = cs.into_raw(); | |
let ret: libc::c_int = libc::prctl(PR_GET_NAME, ptr); | |
if ret != 0 { | |
println!("prctl failed: {}", ret); | |
return; | |
} | |
let c_string = CString::from_raw(ptr); | |
match c_string.into_string() { | |
Ok(s) => { | |
println!("Return: {}, debug print: {:#?}, len: {}.", s, s, s.len()); | |
}, | |
Err(_) => println!("into_string failed.!"), | |
}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::ffi::CString; | |
const PR_GET_NAME: libc::c_int = 16; | |
fn main() { | |
unsafe { | |
let cs = CString::from_vec_unchecked(Vec::with_capacity(32) as Vec<u8>); | |
let ptr = cs.into_raw(); | |
let ret: libc::c_int = libc::prctl(PR_GET_NAME, ptr); | |
if ret != 0 { | |
println!("prctl failed: {}", ret); | |
return; | |
} | |
let c_string = CString::from_raw(ptr); | |
let bytes = c_string.into_bytes_with_nul(); | |
println!("Bytes: {:#?}", bytes); | |
match String::from_utf8(bytes) { | |
Ok(s) => { | |
// s.retain(|c| c.is_alphabetic()); | |
println!("Return: {}, debug print: {:#?}, len: {}.", s, s, s.len()); | |
}, | |
Err(e) => println!("into_string failed: {}!", e), | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment