-
Notifications
You must be signed in to change notification settings - Fork 36
pgrep: remove libc in pgrep #626
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
Franklin-Qi
wants to merge
1
commit into
uutils:main
Choose a base branch
from
Franklin-Qi:remove-libc-in-pgrep
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 all commits
Commits
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
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
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 |
|---|---|---|
|
|
@@ -6,15 +6,11 @@ | |
| // Common process matcher logic shared by pgrep, pkill and pidwait | ||
|
|
||
| use std::hash::Hash; | ||
| #[cfg(unix)] | ||
| use std::os::fd::AsRawFd; | ||
| use std::{collections::HashSet, io}; | ||
|
|
||
| use clap::{arg, Arg, ArgAction, ArgMatches}; | ||
| use regex::Regex; | ||
| #[cfg(unix)] | ||
| use uucore::libc::{getpgrp, getsid}; | ||
| #[cfg(unix)] | ||
| use uucore::{ | ||
| display::Quotable, | ||
| entries::{grp2gid, usr2uid}, | ||
|
|
@@ -91,24 +87,12 @@ pub fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> { | |
| .get_many::<u32>("group") | ||
| .map(|ids| ids.cloned().collect()), | ||
| pgroup: matches.get_many::<u64>("pgroup").map(|xs| { | ||
| xs.map(|pg| { | ||
| if *pg == 0 { | ||
| unsafe { getpgrp() as u64 } | ||
| } else { | ||
| *pg | ||
| } | ||
| }) | ||
| .collect() | ||
| xs.map(|pg| if *pg == 0 { getpgrp() } else { *pg }) | ||
| .collect() | ||
| }), | ||
| session: matches.get_many::<u64>("session").map(|xs| { | ||
| xs.map(|sid| { | ||
| if *sid == 0 { | ||
| unsafe { getsid(0) as u64 } | ||
| } else { | ||
| *sid | ||
| } | ||
| }) | ||
| .collect() | ||
| xs.map(|sid| if *sid == 0 { getsid(0) } else { *sid }) | ||
| .collect() | ||
| }), | ||
| cgroup: matches | ||
| .get_many::<String>("cgroup") | ||
|
|
@@ -445,17 +429,32 @@ pub fn grp2gid(_name: &str) -> io::Result<u32> { | |
| /// # Safety | ||
| /// | ||
| /// Dummy implementation for unsupported platforms. | ||
| #[cfg(not(unix))] | ||
| pub unsafe fn getpgrp() -> u32 { | ||
| panic!("unsupported on this platform"); | ||
| pub fn getpgrp() -> u64 { | ||
| #[cfg(unix)] | ||
| { | ||
| rustix::process::getpgrp().as_raw_nonzero().get() as u64 | ||
| } | ||
| #[cfg(not(unix))] | ||
| { | ||
| 0 | ||
| } | ||
| } | ||
|
|
||
| /// # Safety | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
| /// | ||
| /// Dummy implementation for unsupported platforms. | ||
| #[cfg(not(unix))] | ||
| pub unsafe fn getsid(_pid: u32) -> u32 { | ||
| panic!("unsupported on this platform"); | ||
| pub fn getsid(_pid: u32) -> u64 { | ||
| #[cfg(unix)] | ||
| { | ||
| rustix::process::getsid(None) | ||
| .ok() | ||
| .map(|pid: rustix::process::Pid| pid.as_raw_nonzero().get() as u64) | ||
| .unwrap_or(0) | ||
| } | ||
| #[cfg(not(unix))] | ||
| { | ||
| 0 | ||
| } | ||
| } | ||
|
|
||
| fn parse_uid_or_username(uid_or_username: &str) -> io::Result<u32> { | ||
|
|
@@ -493,26 +492,27 @@ fn test_parse_pidfile_content_valid() { | |
|
|
||
| #[cfg(unix)] | ||
| fn is_locked(file: &std::fs::File) -> bool { | ||
| // On Linux, fcntl and flock locks are independent, so need to check both | ||
| let mut flock_struct = uucore::libc::flock { | ||
| l_type: uucore::libc::F_RDLCK as uucore::libc::c_short, | ||
| l_whence: uucore::libc::SEEK_SET as uucore::libc::c_short, | ||
| l_start: 0, | ||
| l_len: 0, | ||
| l_pid: 0, | ||
| }; | ||
| let fd = file.as_raw_fd(); | ||
| let result = unsafe { uucore::libc::fcntl(fd, uucore::libc::F_GETLK, &mut flock_struct) }; | ||
| if result == 0 && flock_struct.l_type != uucore::libc::F_UNLCK as uucore::libc::c_short { | ||
| return true; | ||
| } | ||
| use rustix::fs::FlockOperation; | ||
| use std::os::fd::{AsRawFd, BorrowedFd}; | ||
|
|
||
| let result = unsafe { uucore::libc::flock(fd, uucore::libc::LOCK_SH | uucore::libc::LOCK_NB) }; | ||
| if result == -1 && std::io::Error::last_os_error().kind() == std::io::ErrorKind::WouldBlock { | ||
| return true; | ||
| let fd = file.as_raw_fd(); | ||
| // Safety: The file descriptor is valid for the duration of this function | ||
| let borrowed_fd = unsafe { BorrowedFd::borrow_raw(fd) }; | ||
|
|
||
| // Try to acquire a shared lock without blocking | ||
| match rustix::fs::flock(borrowed_fd, FlockOperation::NonBlockingLockShared) { | ||
| Ok(_) => { | ||
| let _ = rustix::fs::flock(borrowed_fd, FlockOperation::Unlock); | ||
| false | ||
| } | ||
| Err(e) | ||
| if e.kind() == io::ErrorKind::WouldBlock | ||
| || e.kind() == io::ErrorKind::PermissionDenied => | ||
| { | ||
| true | ||
| } | ||
| Err(_) => false, | ||
| } | ||
|
|
||
| false | ||
| } | ||
|
|
||
| #[cfg(not(unix))] | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this function is no longer unsafe, the Safety comment is useless now.