Debian Codegraph @

third_party/rust/nix/test/sys/test_prctl.rs

raw
1 #[cfg(target_os = "linux")]
2 #[cfg(feature = "process")]
3 mod test_prctl {
4 use std::ffi::CStr;
5
6 use nix::sys::prctl;
7
8 #[cfg_attr(qemu, ignore)]
9 #[test]
10 fn test_get_set_subreaper() {
11 let original = prctl::get_child_subreaper().unwrap();
12
13 prctl::set_child_subreaper(true).unwrap();
14 let subreaper = prctl::get_child_subreaper().unwrap();
15 assert!(subreaper);
16
17 prctl::set_child_subreaper(original).unwrap();
18 }
19
20 #[test]
21 fn test_get_set_dumpable() {
22 let original = prctl::get_dumpable().unwrap();
23
24 prctl::set_dumpable(false).unwrap();
25 let dumpable = prctl::get_dumpable().unwrap();
26 assert!(!dumpable);
27
28 prctl::set_dumpable(original).unwrap();
29 }
30
31 #[test]
32 fn test_get_set_keepcaps() {
33 let original = prctl::get_keepcaps().unwrap();
34
35 prctl::set_keepcaps(true).unwrap();
36 let keepcaps = prctl::get_keepcaps().unwrap();
37 assert!(keepcaps);
38
39 prctl::set_keepcaps(original).unwrap();
40 }
41
42 #[test]
43 fn test_get_set_clear_mce_kill() {
44 use prctl::PrctlMCEKillPolicy::*;
45
46 prctl::set_mce_kill(PR_MCE_KILL_LATE).unwrap();
47 let mce = prctl::get_mce_kill().unwrap();
48 assert_eq!(mce, PR_MCE_KILL_LATE);
49
50 prctl::clear_mce_kill().unwrap();
51 let mce = prctl::get_mce_kill().unwrap();
52 assert_eq!(mce, PR_MCE_KILL_DEFAULT);
53 }
54
55 #[cfg_attr(qemu, ignore)]
56 #[test]
57 fn test_get_set_pdeathsig() {
58 use nix::sys::signal::Signal;
59
60 let original = prctl::get_pdeathsig().unwrap();
61
62 prctl::set_pdeathsig(Signal::SIGUSR1).unwrap();
63 let sig = prctl::get_pdeathsig().unwrap();
64 assert_eq!(sig, Some(Signal::SIGUSR1));
65
66 prctl::set_pdeathsig(original).unwrap();
67 }
68
69 #[test]
70 fn test_get_set_name() {
71 let original = prctl::get_name().unwrap();
72
73 let long_name =
74 CStr::from_bytes_with_nul(b"0123456789abcdefghijklmn\0").unwrap();
75 prctl::set_name(long_name).unwrap();
76 let res = prctl::get_name().unwrap();
77
78 // name truncated by kernel to TASK_COMM_LEN
79 assert_eq!(&long_name.to_str().unwrap()[..15], res.to_str().unwrap());
80
81 let short_name = CStr::from_bytes_with_nul(b"01234567\0").unwrap();
82 prctl::set_name(short_name).unwrap();
83 let res = prctl::get_name().unwrap();
84 assert_eq!(short_name.to_str().unwrap(), res.to_str().unwrap());
85
86 prctl::set_name(&original).unwrap();
87 }
88
89 #[cfg_attr(qemu, ignore)]
90 #[test]
91 fn test_get_set_timerslack() {
92 let original = prctl::get_timerslack().unwrap() as libc::c_ulong;
93
94 let slack = 60_000;
95 prctl::set_timerslack(slack).unwrap();
96 let res = prctl::get_timerslack().unwrap() as libc::c_ulong;
97 assert_eq!(slack, res);
98
99 prctl::set_timerslack(original).unwrap();
100 }
101
102 // Loongarch need to use a newer QEMU that disabled these PRCTL subcodes/methods.
103 // https://github.com/qemu/qemu/commit/220717a6f46a99031a5b1af964bbf4dec1310440
104 // So we should ignore them when testing in QEMU environments.
105 #[cfg_attr(all(qemu, target_arch = "loongarch64"), ignore)]
106 #[test]
107 fn test_disable_enable_perf_events() {
108 prctl::task_perf_events_disable().unwrap();
109 prctl::task_perf_events_enable().unwrap();
110 }
111
112 #[test]
113 fn test_get_set_no_new_privs() {
114 prctl::set_no_new_privs().unwrap();
115 let no_new_privs = prctl::get_no_new_privs().unwrap();
116 assert!(no_new_privs);
117 }
118
119 // Loongarch need to use a newer QEMU that disabled these PRCTL subcodes/methods
120 // https://github.com/qemu/qemu/commit/220717a6f46a99031a5b1af964bbf4dec1310440
121 // So we should ignore them when testing in QEMU environments.
122 #[cfg_attr(all(qemu, target_arch = "loongarch64"), ignore)]
123 #[test]
124 fn test_get_set_thp_disable() {
125 let original = prctl::get_thp_disable().unwrap();
126
127 prctl::set_thp_disable(true).unwrap();
128 let thp_disable = prctl::get_thp_disable().unwrap();
129 assert!(thp_disable);
130
131 prctl::set_thp_disable(original).unwrap();
132 }
133
134 // Ignore this test under QEMU, as it started failing after updating the Linux CI
135 // runner image, for reasons unknown.
136 //
137 // See: https://github.com/nix-rust/nix/issues/2418
138 #[test]
139 #[cfg_attr(qemu, ignore)]
140 fn test_set_vma_anon_name() {
141 use nix::errno::Errno;
142 use nix::sys::mman;
143 use std::num::NonZeroUsize;
144
145 const ONE_K: libc::size_t = 1024;
146 let sz = NonZeroUsize::new(ONE_K).unwrap();
147 let ptr = unsafe {
148 mman::mmap_anonymous(
149 None,
150 sz,
151 mman::ProtFlags::PROT_READ,
152 mman::MapFlags::MAP_SHARED,
153 )
154 .unwrap()
155 };
156 let err = prctl::set_vma_anon_name(
157 ptr,
158 sz,
159 Some(CStr::from_bytes_with_nul(b"[,$\0").unwrap()),
160 )
161 .unwrap_err();
162 assert_eq!(err, Errno::EINVAL);
163 // `CONFIG_ANON_VMA_NAME` kernel config might not be set
164 prctl::set_vma_anon_name(
165 ptr,
166 sz,
167 Some(CStr::from_bytes_with_nul(b"Nix\0").unwrap()),
168 )
169 .unwrap_or_default();
170 prctl::set_vma_anon_name(ptr, sz, None).unwrap_or_default();
171 }
172 }