Skip to content

Commit 56b9fca

Browse files
committed
Implemented set_name
1 parent 82fd064 commit 56b9fca

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

src/sys/prctl.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
//! For more documentation, please read [prctl(2)](https://man7.org/linux/man-pages/man2/prctl.2.html).
77
88
use crate::errno::Errno;
9+
use crate::sys::signal::Signal;
910
use crate::Result;
1011

11-
use crate::sys::signal::Signal;
12-
use libc::{c_int, c_ulong};
12+
use libc::{c_char, c_int, c_ulong};
1313
use std::convert::TryFrom;
14+
use std::ffi::{CStr, CString};
1415

1516
pub use self::PrctlOption::*;
1617

@@ -150,17 +151,8 @@ pub fn set_child_subreaper(attribute: bool) -> Result<()> {
150151
pub fn get_child_subreaper() -> Result<bool> {
151152
// prctl writes into this var
152153
let mut subreaper: c_int = 0;
153-
println!("Addr: {:p}", &mut subreaper);
154154

155-
let res = unsafe {
156-
libc::prctl(
157-
PR_GET_CHILD_SUBREAPER as c_int,
158-
&mut subreaper,
159-
0,
160-
0,
161-
0,
162-
)
163-
};
155+
let res = unsafe { libc::prctl(PR_GET_CHILD_SUBREAPER as c_int, &mut subreaper, 0, 0, 0) };
164156

165157
Errno::result(res).map(|_| subreaper != 0)
166158
}
@@ -239,7 +231,6 @@ pub fn set_pdeathsig<T: Into<Option<Signal>>>(signal: T) -> Result<()> {
239231
pub fn get_pdeathsig() -> Result<Option<Signal>> {
240232
// prctl writes into this var
241233
let mut sig: c_int = 0;
242-
println!("Addr: {:p}", &mut sig);
243234

244235
let res = unsafe { libc::prctl(PR_GET_PDEATHSIG as c_int, &mut sig, 0, 0, 0) };
245236

@@ -251,3 +242,28 @@ pub fn get_pdeathsig() -> Result<Option<Signal>> {
251242
Err(e) => Err(e),
252243
}
253244
}
245+
246+
/// Set the name of the calling thread (max 15 bytes)
247+
pub fn set_name(name: &String) -> Result<()> {
248+
let name = CString::new(name.as_bytes()).unwrap();
249+
250+
let res = unsafe { libc::prctl(PR_SET_NAME as c_int, name.as_ptr(), 0, 0, 0) };
251+
252+
Errno::result(res).map(drop)
253+
}
254+
255+
/// Return the name of the calling thread
256+
pub fn get_name() -> Result<String> {
257+
let buf = [32u8; 16];
258+
259+
let res = unsafe { libc::prctl(PR_GET_NAME as c_int, &buf, 0, 0, 0) };
260+
261+
let ptr = buf.as_ptr() as *const c_char;
262+
let cstr = unsafe { CStr::from_ptr(ptr) };
263+
let name = match cstr.to_str() {
264+
Ok(name_str) => name_str.to_owned(),
265+
Err(_) => return Err(Errno::EINVAL),
266+
};
267+
268+
Errno::result(res).map(|_| name)
269+
}

test/sys/test_prctl.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,16 @@ fn test_get_set_pdeathsig() {
5656
let sig = prctl::get_pdeathsig().unwrap();
5757
assert_eq!(sig, Some(Signal::SIGUSR1));
5858
}
59+
60+
#[test]
61+
fn test_get_set_name() {
62+
let long_name = String::from("0123456789abcdefghijklmn");
63+
prctl::set_name(&long_name).unwrap();
64+
let res = prctl::get_name().unwrap();
65+
assert_eq!(&long_name[..15], res);
66+
67+
let short_name = String::from("01234567");
68+
prctl::set_name(&short_name).unwrap();
69+
let res = prctl::get_name().unwrap();
70+
assert_eq!(short_name, res);
71+
}

0 commit comments

Comments
 (0)