-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rsjudge-runner): 🍻 remove problematic
caps_check
- Loading branch information
1 parent
98c37ca
commit cdec4eb
Showing
6 changed files
with
67 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use std::{cell::RefCell, collections::HashMap, rc::Rc}; | ||
|
||
use caps::{drop as drop_cap, has_cap, raise as raise_cap, Capability}; | ||
|
||
use crate::Result; | ||
|
||
#[derive(Debug, PartialEq, Eq, Hash)] | ||
pub struct CapHandle { | ||
cap: Capability, | ||
ref_count: Rc<()>, | ||
} | ||
|
||
impl CapHandle { | ||
thread_local! { | ||
/// Local capability reference count. | ||
static LOCAL_CAPS: RefCell<HashMap<Capability,Rc<()>>> = RefCell::new(HashMap::new()); | ||
} | ||
|
||
pub fn new(cap: Capability) -> Result<Self> { | ||
let ref_count = Self::LOCAL_CAPS | ||
.with_borrow_mut(|local_caps| local_caps.entry(cap).or_default().clone()); | ||
try_raise_cap(cap)?; | ||
Ok(Self { cap, ref_count }) | ||
} | ||
} | ||
|
||
impl Drop for CapHandle { | ||
fn drop(&mut self) { | ||
if Rc::strong_count(&self.ref_count) == 1 { | ||
// Last reference. | ||
let _ = drop_cap(None, caps::CapSet::Effective, self.cap); | ||
Self::LOCAL_CAPS.with_borrow_mut(|local_caps| { | ||
local_caps.remove(&self.cap); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
fn try_raise_cap(cap: Capability) -> Result<bool> { | ||
if has_cap(None, caps::CapSet::Effective, cap)? { | ||
// Already has cap. | ||
Ok(true) | ||
} else if has_cap(None, caps::CapSet::Permitted, cap)? { | ||
raise_cap(None, caps::CapSet::Effective, cap)?; | ||
Ok(true) | ||
} else { | ||
Ok(false) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters