Skip to content

Documentation for sigaction function and related structs. Solves #319 #839

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

serejkus
Copy link

Sigaction docs with an example and links to docs and security best practicies. Solves #319 .

Copy link
Contributor

@Susurrus Susurrus left a comment

Choose a reason for hiding this comment

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

Nice, this is a great improvement to our docs, thanks! Mostly minor grammar/readability changes.

SigAction(extern fn(libc::c_int, *mut libc::siginfo_t, *mut libc::c_void))
}

/// Struct holding data for [`sigaction`](fn.sigaction.html) function.
///
/// `SigAction` holds the signal action itself (which can be ignoring, custom handler or a default
Copy link
Contributor

Choose a reason for hiding this comment

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

Get rid of everything within the parens.

@@ -350,15 +350,38 @@ impl AsRef<libc::sigset_t> for SigSet {
}
}

/// Enum holding signal action for [`sigaction`](fn.sigaction.html) function.
Copy link
Contributor

Choose a reason for hiding this comment

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

"Specifies the signal action for the ... function"

@@ -350,15 +350,38 @@ impl AsRef<libc::sigset_t> for SigSet {
}
}

/// Enum holding signal action for [`sigaction`](fn.sigaction.html) function.
///
/// Signal can be ignored, may have a custom handler or default one.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is redundant with the variants which have their own descriptions, delete thei sline.

#[allow(unknown_lints)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SigHandler {
/// Signal action should be set to a default one.
Copy link
Contributor

Choose a reason for hiding this comment

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

"Specifies that the default action should be used"

SigDfl,
/// Signal should be ignored.
Copy link
Contributor

Choose a reason for hiding this comment

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

Specifies that the signal should be ignored.

///
/// Custom signal handlers should call only
/// [signal-safe](https://wiki.sei.cmu.edu/confluence/display/c/SIG30-C.+Call+only+asynchronous-safe+functions+within+signal+handlers)
/// functions. Here is the [list](http://man7.org/linux/man-pages/man7/signal-safety.7.html) of
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there lists specified for POSIX or any other platforms? We should try to provide docs for other platforms as well.

/// signal-safe functions for Linux. Great care should be taken if the signal handler needs access
/// to global state. Global state used in signal handler must not be protected with
/// [`Mutex`](https://doc.rust-lang.org/std/sync/struct.Mutex.html), it is better to stick to
/// [`atomic`](https://doc.rust-lang.org/std/sync/atomic/index.html) module or to use
Copy link
Contributor

Choose a reason for hiding this comment

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

"atomics or to use"

///
/// Here is an example of building asynchronous-safe signal handler. We're building a function that
/// waits until `SIGINT` arrives and reports it. The problem is that reporting right from the
/// signal handler may be unsafe if reporting code calls allocator or locking functions. This might
Copy link
Contributor

Choose a reason for hiding this comment

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

"allocating"

/// static ref SIGNAL_SET_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
/// }
///
/// // The signal handler itself: it uses write() system call, which is signal-safe in Linux.
Copy link
Contributor

Choose a reason for hiding this comment

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

"it uses the write() system call"

/// extern "C" fn sighandler(_: c_int) {
/// let buf: [u8; 1] = [0];
/// unsafe {
/// // throw-away write since it is subject to race conditions with close
Copy link
Contributor

Choose a reason for hiding this comment

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

Capitalize "throw-away"

@Susurrus
Copy link
Contributor

Susurrus commented Feb 8, 2018

@serejkus Mostly simple edits required before we can merge this; can you fix those and rebase?

@Susurrus
Copy link
Contributor

@serejkus Just pinging you on this. We're pretty close to wrapping this one up and merging!

@serejkus
Copy link
Author

Sorry, got a lot of work on my day job, will try my best to fix these issues on a weekend

@anth1y
Copy link

anth1y commented Mar 26, 2021

Is there anything I can do here to get this across the line ?

Copy link
Member

@asomers asomers left a comment

Choose a reason for hiding this comment

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

@anth1y it would be nice to merge this. If you want to help, could you please rebase against current master, and fix the two issues I mentioned inline?

/// Signal handler should report to a signal reaction handler via write to a pipe.
///
/// ```no_run
/// #[macro_use] extern crate lazy_static;
Copy link
Member

Choose a reason for hiding this comment

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

These three lines are no longer needed in Rust edition 2018

/// // We're not using Mutex<RawFd>, because calling lock/unlock in signal handler
/// // is an unsafe operation, but we need a lock to set up SIGNAL_FD.
/// lazy_static! {
/// static ref SIGNAL_SET_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
Copy link
Member

Choose a reason for hiding this comment

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

If you don't take the lock when reading SIGNAL_FD, then there's no point in taking the lock when writing it. I advise deleting the mutex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants