I'm trying to learn more about FFI in Rust and linking with C libraries, specifically libc
. While on my "quest", I came across the following problem.
Normal pattern in C
void(* sig_set(int sig, void(*handler)(int))) {
// uninitialized sigaction structs
struct sigaction new_action, old_action;
// assign options to new action
new_action.sa_flags = SA_RESTART;
new_action.sa_handler = handler;
sigemptyset(&new_action.sa_mask);
if(sigaction(sig, &new_action, &old_action) < 0) {
fprintf(stderr, "Error: %s!
", "signal error");
exit(1);
}
return old_action.sa_handler;
}
Attempt in Rust
use libc; // 0.2.77
fn sig_init(sig: i32, handler: fn(i32) -> ()) -> usize {
unsafe {
let mut new_action: libc::sigaction;
let mut old_action: libc::sigaction;
new_action.sa_flags = 0x10000000;
new_action.sa_sigaction = handler as usize;
libc::sigemptyset(&mut new_action.sa_mask as *mut libc::sigset_t);
libc::sigaction(
sig,
&mut new_action as *mut libc::sigaction,
&mut old_action as *mut libc::sigaction,
);
old_action.sa_sigaction
}
}
The compiler will throw the following error:
error[E0381]: assign to part of possibly-uninitialized variable: `new_action`
--> src/lib.rs:8:9
|
8 | new_action.sa_flags = 0x10000000;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ use of possibly-uninitialized `new_action`
error[E0381]: borrow of possibly-uninitialized variable: `old_action`
--> src/lib.rs:15:13
|
15 | &mut old_action as *mut libc::sigaction,
| ^^^^^^^^^^^^^^^ use of possibly-uninitialized `old_action`
This makes sense as very bad things could happen if sigemptyset
were to read from sa_mask
. So I tried the following on line 3 of the above.
let mut new_action: libc::sigaction = libc::sigaction {
sa_sigaction: handler as usize,
sa_flags: 0x10000000,
sa_mask: mask,
};
This will not work as _restorer
is missing in the above example, but _restorer
is private. How would I get around this problem or a similar situation? Would you use something like mem::transmute
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…