6
6
//! For more documentation, please read [prctl(2)](https://man7.org/linux/man-pages/man2/prctl.2.html).
7
7
8
8
use crate :: errno:: Errno ;
9
+ use crate :: sys:: signal:: Signal ;
9
10
use crate :: Result ;
10
11
11
- use crate :: sys:: signal:: Signal ;
12
- use libc:: { c_int, c_ulong} ;
12
+ use libc:: { c_char, c_int, c_ulong} ;
13
13
use std:: convert:: TryFrom ;
14
+ use std:: ffi:: { CStr , CString } ;
14
15
15
16
pub use self :: PrctlOption :: * ;
16
17
@@ -150,17 +151,8 @@ pub fn set_child_subreaper(attribute: bool) -> Result<()> {
150
151
pub fn get_child_subreaper ( ) -> Result < bool > {
151
152
// prctl writes into this var
152
153
let mut subreaper: c_int = 0 ;
153
- println ! ( "Addr: {:p}" , & mut subreaper) ;
154
154
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 ) } ;
164
156
165
157
Errno :: result ( res) . map ( |_| subreaper != 0 )
166
158
}
@@ -239,7 +231,6 @@ pub fn set_pdeathsig<T: Into<Option<Signal>>>(signal: T) -> Result<()> {
239
231
pub fn get_pdeathsig ( ) -> Result < Option < Signal > > {
240
232
// prctl writes into this var
241
233
let mut sig: c_int = 0 ;
242
- println ! ( "Addr: {:p}" , & mut sig) ;
243
234
244
235
let res = unsafe { libc:: prctl ( PR_GET_PDEATHSIG as c_int , & mut sig, 0 , 0 , 0 ) } ;
245
236
@@ -251,3 +242,28 @@ pub fn get_pdeathsig() -> Result<Option<Signal>> {
251
242
Err ( e) => Err ( e) ,
252
243
}
253
244
}
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
+ }
0 commit comments