Skip to content

Commit 4359fb5

Browse files
committed
api: clean up timeout APIs
1 parent 727251b commit 4359fb5

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

src/api.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use std::mem;
3838
use std::ptr;
3939
use std::result;
4040
use std::str;
41+
use std::time::Duration;
4142

4243
/// Reexport of `Errno` as `Error`.
4344
pub type Error = errno::Errno;
@@ -423,10 +424,12 @@ impl Keyring {
423424
.and_then(|desc| Description::parse(desc).ok_or(errno::Errno(libc::EINVAL)))
424425
}
425426

426-
/// Set an expiration timer on the keyring to `timeout` seconds in the future. A timeout of 0
427-
/// means "no expiration". Requires the `setattr` permission on the keyring.
428-
pub fn set_timeout(&mut self, timeout: u32) -> Result<()> {
429-
check_call(unsafe { keyctl_set_timeout(self.id, timeout) }, ())
427+
/// Set an expiration timer on the keyring to `timeout`.
428+
///
429+
/// Any partial seconds are ignored. A timeout of 0 means "no expiration". Requires the
430+
/// `setattr` permission on the keyring.
431+
pub fn set_timeout(&mut self, timeout: Duration) -> Result<()> {
432+
check_call(unsafe { keyctl_set_timeout(self.id, timeout.as_secs() as u32) }, ())
430433
}
431434

432435
/// The security context of the keyring. Depends on the security manager loaded into the kernel
@@ -560,10 +563,11 @@ impl Key {
560563
Ok(buffer)
561564
}
562565

563-
/// Set an expiration timer on the key to `timeout` seconds in the future.
566+
/// Set an expiration timer on the keyring to `timeout`.
564567
///
565-
/// A timeout of `0` means "no expiration". Requires the `setattr` permission on the key.
566-
pub fn set_timeout(&mut self, timeout: u32) -> Result<()> {
568+
/// Any partial seconds are ignored. A timeout of 0 means "no expiration". Requires the
569+
/// `setattr` permission on the key.
570+
pub fn set_timeout(&mut self, timeout: Duration) -> Result<()> {
567571
Keyring::new_impl(self.id).set_timeout(timeout)
568572
}
569573

@@ -669,21 +673,27 @@ impl KeyManager {
669673

670674
/// Reject the key with the given `error`.
671675
///
672-
/// Requests for the key will fail until `timeout` seconds have elapsed. This is to prevent a
673-
/// denial-of-service by requesting a non-existant key repeatedly. The requester must have
676+
/// Requests for the key will fail until `timeout` has elapsed (partial
677+
/// seconds are ignored). This is to prevent a denial-of-service by
678+
/// requesting a non-existant key repeatedly. The requester must have
674679
/// `write` permission on the keyring.
675680
///
676681
/// TODO: Accept `SpecialKeyring` values here. They are special in that they refer to the
677682
/// *requester's* special keyring and not this one.
678-
pub fn reject(self, keyring: &Keyring, timeout: u32, error: errno::Errno) -> Result<()> {
683+
pub fn reject(self, keyring: &Keyring, timeout: Duration, error: errno::Errno) -> Result<()> {
679684
let errno::Errno(errval) = error;
680-
check_call(unsafe { keyctl_reject(self.key.id, timeout, errval as u32, keyring.id) },
685+
check_call(unsafe { keyctl_reject(self.key.id, timeout.as_secs() as u32, errval as u32, keyring.id) },
681686
())
682687
}
683688

684689
/// Reject the key with `ENOKEY`.
685-
pub fn negate(self, keyring: &Keyring, timeout: u32) -> Result<()> {
686-
check_call(unsafe { keyctl_negate(self.key.id, timeout, keyring.id) },
690+
///
691+
/// Requests for the key will fail until `timeout` has elapsed (partial
692+
/// seconds are ignored). This is to prevent a denial-of-service by
693+
/// requesting a non-existant key repeatedly. The requester must have
694+
/// `write` permission on the keyring.
695+
pub fn negate(self, keyring: &Keyring, timeout: Duration) -> Result<()> {
696+
check_call(unsafe { keyctl_negate(self.key.id, timeout.as_secs() as u32, keyring.id) },
687697
())
688698
}
689699
}

0 commit comments

Comments
 (0)