Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for the different signal handler types. #241

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,17 +464,36 @@ impl AsRef<sigset_t> for SigSet {
}
}

pub use self::signal::siginfo;

pub enum SigHandler {
SigDfl,
SigIgn,
Handler(extern fn(SigNum)),
SigAction(extern fn(SigNum, *mut siginfo, *mut libc::c_void))
}

type sigaction_t = self::signal::sigaction;

pub struct SigAction {
sigaction: sigaction_t
}

impl SigAction {
pub fn new(handler: extern fn(libc::c_int), flags: SockFlag, mask: SigSet) -> SigAction {
/// This function will set or unset the flag `SA_SIGINFO` depending on the
/// type of the `handler` argument.
pub fn new(handler: SigHandler, flags: SockFlag, mask: SigSet) -> SigAction {
let mut s = unsafe { mem::uninitialized::<sigaction_t>() };
s.sa_handler = handler;
s.sa_flags = flags;
s.sa_handler = match handler {
SigHandler::SigDfl => unsafe { mem::transmute(libc::SIG_DFL) },
SigHandler::SigIgn => unsafe { mem::transmute(libc::SIG_IGN) },
SigHandler::Handler(f) => f,
SigHandler::SigAction(f) => unsafe { mem::transmute(f) },
};
s.sa_flags = match handler {
SigHandler::SigAction(_) => flags | SA_SIGINFO,
_ => flags - SA_SIGINFO,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think flags & !SA_SIGINFO is nicer when removing a single flag. As it is, I had to go and double check that we actually had a bitflags type here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the minus sign, which represents set difference. And that is exactly what we want to express and achieve here. In addition it is more concise.
I do not think that preventing confusion is a valid argument since the type is stated in the parameter list and (now) by convention we use bitflags for such types.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a very reasonable position!

};
s.sa_mask = mask.sigset;

SigAction { sigaction: s }
Expand Down