Skip to content

Commit 374f618

Browse files
pgrep: use safe wrappers for libc functions
1 parent 08af5de commit 374f618

File tree

2 files changed

+7
-34
lines changed

2 files changed

+7
-34
lines changed

src/uu/pgrep/src/process_matcher.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::{collections::HashSet, io};
1313
use clap::{arg, Arg, ArgAction, ArgMatches};
1414
use regex::Regex;
1515
#[cfg(unix)]
16-
use uucore::libc::{getpgrp, getsid};
16+
use uucore::process::{getpgrp, getsid};
1717
#[cfg(unix)]
1818
use uucore::{
1919
display::Quotable,
@@ -91,19 +91,13 @@ pub fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
9191
.get_many::<u32>("group")
9292
.map(|ids| ids.cloned().collect()),
9393
pgroup: matches.get_many::<u64>("pgroup").map(|xs| {
94-
xs.map(|pg| {
95-
if *pg == 0 {
96-
unsafe { getpgrp() as u64 }
97-
} else {
98-
*pg
99-
}
100-
})
101-
.collect()
94+
xs.map(|pg| if *pg == 0 { getpgrp() as u64 } else { *pg })
95+
.collect()
10296
}),
10397
session: matches.get_many::<u64>("session").map(|xs| {
10498
xs.map(|sid| {
10599
if *sid == 0 {
106-
unsafe { getsid(0) as u64 }
100+
getsid(0).unwrap() as u64
107101
} else {
108102
*sid
109103
}
@@ -445,7 +439,7 @@ pub fn grp2gid(_name: &str) -> io::Result<u32> {
445439
///
446440
/// Dummy implementation for unsupported platforms.
447441
#[cfg(not(unix))]
448-
pub unsafe fn getpgrp() -> u32 {
442+
pub fn getpgrp() -> u32 {
449443
panic!("unsupported on this platform");
450444
}
451445

src/uu/ps/src/collector.rs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,9 @@
44
// file that was distributed with this source code.
55

66
use clap::ArgMatches;
7-
#[cfg(target_family = "unix")]
8-
use nix::errno::Errno;
97
use std::{cell::RefCell, path::PathBuf, rc::Rc, str::FromStr};
108
use uu_pgrep::process::{ProcessInformation, Teletype};
11-
12-
// TODO: Temporary add to this file, this function will add to uucore.
13-
#[cfg(not(target_os = "redox"))]
14-
#[cfg(target_family = "unix")]
15-
fn getsid(pid: i32) -> Option<i32> {
16-
unsafe {
17-
let result = libc::getsid(pid);
18-
if Errno::last() == Errno::UnknownErrno {
19-
Some(result)
20-
} else {
21-
None
22-
}
23-
}
24-
}
25-
26-
// TODO: Temporary add to this file, this function will add to uucore.
27-
#[cfg(target_family = "windows")]
28-
fn getsid(_pid: i32) -> Option<i32> {
29-
Some(0)
30-
}
9+
use uucore::process::getsid;
3110

3211
// Guessing it matches the current terminal
3312
pub(crate) fn basic_collector(
@@ -96,7 +75,7 @@ pub(crate) fn session_collector(
9675
for it in proc_snapshot {
9776
let pid = it.borrow().pid;
9877

99-
if let Some(sid) = getsid(pid as i32) {
78+
if let Ok(sid) = getsid(pid as i32) {
10079
// Check is session leader
10180
if sid != (pid as i32) && tty(it) != Teletype::Unknown {
10281
result.push(it.clone());

0 commit comments

Comments
 (0)