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(); |
93 | |
94 | let slack = 60_000; |
95 | prctl::set_timerslack(slack).unwrap(); |
96 | let res = prctl::get_timerslack().unwrap(); |
97 | assert_eq!(slack, res as u64); |
98 | |
99 | prctl::set_timerslack(original as u64).unwrap(); |
100 | } |
101 | |
102 | #[test] |
103 | fn test_disable_enable_perf_events() { |
104 | prctl::task_perf_events_disable().unwrap(); |
105 | prctl::task_perf_events_enable().unwrap(); |
106 | } |
107 | |
108 | #[test] |
109 | fn test_get_set_no_new_privs() { |
110 | prctl::set_no_new_privs().unwrap(); |
111 | let no_new_privs = prctl::get_no_new_privs().unwrap(); |
112 | assert!(no_new_privs); |
113 | } |
114 | |
115 | #[test] |
116 | fn test_get_set_thp_disable() { |
117 | let original = prctl::get_thp_disable().unwrap(); |
118 | |
119 | prctl::set_thp_disable(true).unwrap(); |
120 | let thp_disable = prctl::get_thp_disable().unwrap(); |
121 | assert!(thp_disable); |
122 | |
123 | prctl::set_thp_disable(original).unwrap(); |
124 | } |
125 | } |
126 | |