Skip to content

Commit

Permalink
readme: Handle alarm signal
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Aug 5, 2024
1 parent 6384ea6 commit ef4550c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,29 @@ let ret = unsafe { nc::kill(pid, nc::SIGTERM) };
println!("ret: {:?}", ret);
```

Or handle signals:
```rust
fn handle_alarm(signum: i32) {
assert_eq!(signum, nc::SIGALRM);
}

fn main() {
let sa = nc::sigaction_t {
sa_handler: handle_alarm as nc::sighandler_t,
sa_flags: nc::SA_RESTORER,
sa_restorer: nc::restore::get_sa_restorer(),
..nc::sigaction_t::default()
};
let ret = unsafe { nc::rt_sigaction(nc::SIGALRM, Some(&sa), None) };
assert!(ret.is_ok());
let remaining = unsafe { nc::alarm(1) };
let ret = unsafe { nc::pause() };
assert!(ret.is_err());
assert_eq!(ret, Err(nc::EINTR));
assert_eq!(remaining, 0);
}
```

## Stable version

For stable version of rustc, please install a C compiler (`gcc` or `clang`) first.
Expand Down
6 changes: 0 additions & 6 deletions examples/alarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,9 @@ fn main() {

let seconds = 1;
let remaining = alarm(seconds);

let mask = nc::sigset_t::default();
let ret = unsafe { nc::rt_sigsuspend(&mask) };
assert!(ret.is_err());
assert_eq!(ret, Err(nc::EINTR));

assert_eq!(remaining.unwrap(), 0);

unsafe {
nc::exit(1);
}
}
23 changes: 23 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,29 @@
//! assert_eq!(ret, Err(nc::EPERM));
//! ```
//!
//! Or handle signals:
//! ```
//! fn handle_alarm(signum: i32) {
//! assert_eq!(signum, nc::SIGALRM);
//! }
//!
//! fn main() {
//! let sa = nc::sigaction_t {
//! sa_handler: handle_alarm as nc::sighandler_t,
//! sa_flags: nc::SA_RESTORER,
//! sa_restorer: nc::restore::get_sa_restorer(),
//! ..nc::sigaction_t::default()
//! };
//! let ret = unsafe { nc::rt_sigaction(nc::SIGALRM, Some(&sa), None) };
//! assert!(ret.is_ok());
//! let remaining = unsafe { nc::alarm(1) };
//! let ret = unsafe { nc::pause() };
//! assert!(ret.is_err());
//! assert_eq!(ret, Err(nc::EINTR));
//! assert_eq!(remaining, 0);
//! }
//! ```
//!
//! Or get system info:
//! ```rust
//! pub fn cstr_to_str(input: &[u8]) -> &str {
Expand Down

0 comments on commit ef4550c

Please sign in to comment.