-
Notifications
You must be signed in to change notification settings - Fork 32
pidwait: optimize & cross-platform #400
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
FurryR
wants to merge
11
commits into
uutils:main
Choose a base branch
from
FurryR:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7ae897c
pidwait: optimize & cross-platform
FurryR 7e2813b
fix(pidwait): add length check for procs
FurryR dc7b000
chore(pidwait): use workspace `windows-sys` crate
FurryR 903fef8
Merge branch 'main' into main
FurryR c41708d
fix(pidwait): sync with master
FurryR 5029113
chore(pidwait): add newline at eof
FurryR df176fe
Merge branch 'main' into main
Krysztal112233 1836a9d
chore(pidwait): make clippy happy
FurryR ce291dd
chore(pidwait): update structure
FurryR 76310ac
chore(pidwait): styling
FurryR 1e9a3dd
refactor(pidwait): merge suggestions
FurryR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,51 @@ | ||
// This file is part of the uutils procps package. | ||
// | ||
// For the full copyright and license information, please view the LICENSE | ||
// file that was distributed with this source code. | ||
|
||
// Reference: pidwait-any crate. | ||
// Thanks to @oxalica's implementation. | ||
|
||
// FIXME: Test this implementation | ||
|
||
use rustix::event::kqueue::{kevent, kqueue, Event, EventFilter, EventFlags, ProcessEvents}; | ||
use rustix::process::Pid; | ||
use std::io::{Error, ErrorKind, Result}; | ||
use std::mem::MaybeUninit; | ||
use std::time::Duration; | ||
use uu_pgrep::process::ProcessInformation; | ||
|
||
pub fn wait(procs: &[ProcessInformation], timeout: Option<Duration>) -> Result<Option<()>> { | ||
let mut events = Vec::with_capacity(procs.len()); | ||
let kqueue = kqueue()?; | ||
for proc in procs { | ||
let pid = Pid::from_raw(proc.pid as i32).ok_or_else(|| { | ||
Error::new( | ||
ErrorKind::InvalidInput, | ||
format!("Invalid PID: {}", proc.pid), | ||
) | ||
})?; | ||
let event = Event::new( | ||
EventFilter::Proc { | ||
pid, | ||
flags: ProcessEvents::EXIT, | ||
}, | ||
EventFlags::ADD, | ||
std::ptr::null_mut(), | ||
); | ||
events.push(event); | ||
} | ||
let ret = unsafe { kevent::<_, &mut [Event; 0]>(&kqueue, &events, &mut [], None)? }; | ||
debug_assert_eq!(ret, 0); | ||
let mut buf = [MaybeUninit::uninit()]; | ||
let (events, _rest_buf) = unsafe { kevent(&kqueue, &[], &mut buf, timeout)? }; | ||
if events.is_empty() { | ||
return Ok(None); | ||
}; | ||
debug_assert!(matches!( | ||
events[0].filter(), | ||
EventFilter::Proc { flags, .. } | ||
if flags.contains(ProcessEvents::EXIT) | ||
)); | ||
Ok(Some(())) | ||
} |
This file contains hidden or 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,45 @@ | ||
// This file is part of the uutils procps package. | ||
// | ||
// For the full copyright and license information, please view the LICENSE | ||
// file that was distributed with this source code. | ||
|
||
// Reference: pidwait-any crate. | ||
// Thanks to @oxalica's implementation. | ||
|
||
use std::io::{Error, ErrorKind}; | ||
use std::os::fd::OwnedFd; | ||
|
||
use rustix::event::{poll, PollFd, PollFlags}; | ||
use rustix::io::Errno; | ||
use rustix::process::{pidfd_open, Pid, PidfdFlags}; | ||
use std::io::Result; | ||
use std::time::Duration; | ||
use uu_pgrep::process::ProcessInformation; | ||
|
||
pub fn wait(procs: &[ProcessInformation], timeout: Option<Duration>) -> Result<Option<()>> { | ||
let mut pidfds: Vec<OwnedFd> = Vec::with_capacity(procs.len()); | ||
for proc in procs { | ||
let pid = Pid::from_raw(proc.pid as i32).ok_or_else(|| { | ||
Error::new( | ||
ErrorKind::InvalidInput, | ||
format!("Invalid PID: {}", proc.pid), | ||
) | ||
})?; | ||
let pidfd = pidfd_open(pid, PidfdFlags::empty())?; | ||
pidfds.push(pidfd); | ||
} | ||
let timespec = match timeout { | ||
Some(timeout) => Some(timeout.try_into().map_err(|_| Errno::INVAL)?), | ||
None => None, | ||
}; | ||
let mut fds: Vec<PollFd> = Vec::with_capacity(pidfds.len()); | ||
for pidfd in &pidfds { | ||
fds.push(PollFd::new(pidfd, PollFlags::IN)); | ||
} | ||
let ret = poll(&mut fds, timespec.as_ref())?; | ||
if ret == 0 { | ||
return Ok(None); | ||
} | ||
debug_assert!(fds[0].revents().contains(PollFlags::IN)); | ||
Ok(Some(())) | ||
} |
This file contains hidden or 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,68 @@ | ||
// This file is part of the uutils procps package. | ||
// | ||
// For the full copyright and license information, please view the LICENSE | ||
// file that was distributed with this source code. | ||
|
||
// Reference: pidwait-any crate. | ||
// Thanks to @oxalica's implementation. | ||
|
||
use std::time::Duration; | ||
use uu_pgrep::process::ProcessInformation; | ||
|
||
use std::ffi::c_void; | ||
use std::io::{Error, Result}; | ||
use std::ptr::NonNull; | ||
|
||
use windows_sys::Win32::Foundation::{CloseHandle, WAIT_OBJECT_0, WAIT_TIMEOUT}; | ||
use windows_sys::Win32::System::Threading::{ | ||
OpenProcess, WaitForMultipleObjects, INFINITE, PROCESS_SYNCHRONIZE, | ||
}; | ||
|
||
struct HandleWrapper(NonNull<c_void>); | ||
unsafe impl Send for HandleWrapper {} | ||
impl Drop for HandleWrapper { | ||
fn drop(&mut self) { | ||
unsafe { | ||
CloseHandle(self.0.as_ptr()); | ||
}; | ||
} | ||
} | ||
|
||
pub fn wait(procs: &[ProcessInformation], timeout: Option<Duration>) -> Result<Option<()>> { | ||
let hprocess = unsafe { | ||
let mut result = Vec::with_capacity(procs.len()); | ||
for proc in procs { | ||
let handle = OpenProcess(PROCESS_SYNCHRONIZE, 0, proc.pid as u32); | ||
result.push(HandleWrapper( | ||
NonNull::new(handle).ok_or_else(Error::last_os_error)?, | ||
)); | ||
} | ||
result | ||
}; | ||
const _: [(); 1] = [(); (INFINITE == u32::MAX) as usize]; | ||
let timeout = match timeout { | ||
Some(timeout) => timeout | ||
.as_millis() | ||
.try_into() | ||
.unwrap_or(INFINITE - 1) | ||
.min(INFINITE - 1), | ||
None => INFINITE, | ||
}; | ||
let ret = unsafe { | ||
WaitForMultipleObjects( | ||
hprocess.len() as u32, | ||
hprocess | ||
.into_iter() | ||
.map(|proc| proc.0.as_ptr()) | ||
.collect::<Vec<_>>() | ||
.as_ptr(), | ||
1, | ||
timeout, | ||
) | ||
}; | ||
match ret { | ||
WAIT_OBJECT_0 => Ok(Some(())), | ||
WAIT_TIMEOUT => Ok(None), | ||
_ => Err(Error::last_os_error()), | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.