]> git.lizzy.rs Git - rust.git/blobdiff - tests/run-pass/libc.rs
Move prctl test to the same file as other libc tests.
[rust.git] / tests / run-pass / libc.rs
index fc154c05c8fc38c1797d27e82af2d0ca56f1d8a9..5873d429695002789469d8bbd031db673226e290 100644 (file)
@@ -2,14 +2,12 @@
 // compile-flags: -Zmiri-disable-isolation
 
 #![feature(rustc_private)]
-#![allow(unused)] // necessary on macos due to conditional compilation
-
-use std::path::PathBuf;
 
 extern crate libc;
 
-fn tmp() -> PathBuf {
-    std::env::var("MIRI_TEMP").map(PathBuf::from).unwrap_or_else(|_| std::env::temp_dir())
+#[cfg(target_os = "linux")]
+fn tmp() -> std::path::PathBuf {
+    std::env::var("MIRI_TEMP").map(std::path::PathBuf::from).unwrap_or_else(|_| std::env::temp_dir())
 }
 
 #[cfg(target_os = "linux")]
@@ -143,6 +141,20 @@ fn test_rwlock_libc_static_initializer() {
     }
 }
 
+/// Test whether the `prctl` shim correctly sets the thread name.
+///
+/// Note: `prctl` exists only on Linux.
+fn test_prctl_thread_name() {
+    use std::ffi::CString;
+    unsafe {
+        let thread_name = CString::new("hello").expect("CString::new failed");
+        assert_eq!(libc::prctl(libc::PR_SET_NAME, thread_name.as_ptr() as libc::c_long, 0 as libc::c_long, 0 as libc::c_long, 0 as libc::c_long), 0);
+        let mut buf = [0; 6];
+        assert_eq!(libc::prctl(libc::PR_GET_NAME, buf.as_mut_ptr() as libc::c_long, 0 as libc::c_long, 0 as libc::c_long, 0 as libc::c_long), 0);
+        assert_eq!(thread_name.as_bytes_with_nul(), buf);
+    }
+}
+
 fn main() {
     #[cfg(target_os = "linux")]
     test_posix_fadvise();
@@ -154,4 +166,7 @@ fn main() {
 
     #[cfg(target_os = "linux")]
     test_mutex_libc_static_initializer_recursive();
+
+    #[cfg(target_os = "linux")]
+    test_prctl_thread_name();
 }