Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion heim-common/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,11 @@ impl From<convert::Infallible> for Error {
match e {}
}
}

impl From<std::string::FromUtf16Error> for Error {
fn from(e: std::string::FromUtf16Error) -> Self {
Error::from(io::Error::new(io::ErrorKind::InvalidData, e))
}
}
#[cfg(unix)]
impl From<nix::Error> for Error {
fn from(e: nix::Error) -> Self {
Expand Down
9 changes: 9 additions & 0 deletions heim-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ pub use self::errors::{Context, Error, Result};
/// Process identifier type.
#[cfg(unix)]
pub type Pid = libc::pid_t;
/// User identifier type
#[cfg(unix)]
pub type Uid = libc::uid_t;
/// Group identifier type
#[cfg(unix)]
pub type Gid = libc::gid_t;
/// Process umask
#[cfg(unix)]
pub type Umask = libc::mode_t;

/// Process identifier type.
// TODO: Is it a correct type for pid?
Expand Down
13 changes: 11 additions & 2 deletions heim-host/src/os/linux.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Linux-specific extensions.

use std::net::IpAddr;
use std::{convert::TryFrom, net::IpAddr};

use crate::Pid;
use heim_common::{Error, Pid, Result, Uid};

cfg_if::cfg_if! {
// aarch64-unknown-linux-gnu has different type
Expand Down Expand Up @@ -80,3 +80,12 @@ impl UserExt for crate::User {
self.as_ref().session_id()
}
}

#[cfg(target_os = "linux")]
impl TryFrom<Uid> for crate::User {
type Error = Error;
fn try_from(uid: Uid) -> Result<Self> {
let user = crate::sys::User::try_from(uid)?;
Ok(crate::User::try_from(user)?)
}
}
12 changes: 11 additions & 1 deletion heim-host/src/os/macos.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! macOS-specific extensions.

use crate::Pid;
use heim_common::{Error, Pid, Result, Uid};
use std::convert::TryFrom;

/// macOS-specific extensions for [User].
///
Expand Down Expand Up @@ -40,3 +41,12 @@ impl UserExt for crate::User {
self.as_ref().hostname()
}
}

#[cfg(target_os = "macos")]
impl TryFrom<Uid> for crate::User {
type Error = Error;
fn try_from(uid: Uid) -> Result<Self> {
let user = crate::sys::User::try_from(uid)?;
Ok(crate::User::try_from(user)?)
}
}
15 changes: 14 additions & 1 deletion heim-host/src/os/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

use std::net::IpAddr;

#[cfg(target_os = "windows")]
use winapi::um::winnt::PSID;

use heim_common::Result;

/// Extension for [User] struct.
///
/// [User]: ../../struct.User.html
pub trait UserExt {
#[cfg(target_os = "windows")]
pub trait UserExt: Sized {
#[doc(hidden)]
fn try_from_sid(sid: PSID) -> Result<Self>;

/// Domain name that the user belongs to.
fn domain(&self) -> &str;

Expand All @@ -26,6 +35,10 @@ pub trait UserExt {

#[cfg(target_os = "windows")]
impl UserExt for crate::User {
fn try_from_sid(sid: PSID) -> Result<Self> {
crate::sys::User::try_from_sid(sid).map(crate::User::from)
}

fn domain(&self) -> &str {
self.as_ref().domain()
}
Expand Down
11 changes: 9 additions & 2 deletions heim-host/src/sys/linux/users/musl.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::net::IpAddr;
use std::{convert::TryFrom, net::IpAddr};

use heim_common::prelude::*;
use heim_common::Pid;
use heim_common::{Pid, Uid};

use crate::os::linux::SessionId;

Expand Down Expand Up @@ -41,3 +41,10 @@ impl User {
pub async fn users() -> Result<impl Stream<Item = Result<User>>> {
Ok(stream::empty())
}

impl TryFrom<Uid> for User {
type Error = Error;
fn try_from(_uid: Uid) -> Result<Self> {
unimplemented!("https://github.com/heim-rs/heim/issues/141")
}
}
37 changes: 35 additions & 2 deletions heim-host/src/sys/linux/users/other.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::ffi::CStr;
use std::net::IpAddr;
use std::{convert::TryFrom, net::IpAddr};

use heim_common::prelude::*;
use heim_common::Pid;
use heim_common::{Pid, Uid};

use crate::os::linux::SessionId;
use crate::sys::unix::{from_ut_addr_v6, get_users};
Expand Down Expand Up @@ -88,3 +88,36 @@ pub async fn users() -> Result<impl Stream<Item = Result<User>>> {

Ok(stream::iter(users).map(Ok))
}

//TODO: Figureout how to get rest of data
impl From<*mut libc::passwd> for User {
fn from(entry: *mut libc::passwd) -> Self {
let username = unsafe {
CStr::from_ptr((*entry).pw_name)
.to_string_lossy()
.into_owned()
};

User {
username,
id: "".to_string(),
terminal: "".to_string(),
hostname: "".to_string(),
pid: 0,
session_id: 0,
addr: None,
}
}
}

impl TryFrom<Uid> for User {
type Error = Error;
fn try_from(uid: Uid) -> Result<Self> {
let passwd = unsafe { libc::getpwuid(uid) };
if passwd.is_null() {
return Err(Error::last_os_error().with_ffi("getpwuid"));
}
let user = User::from(passwd);
Ok(user)
}
}
34 changes: 32 additions & 2 deletions heim-host/src/sys/macos/users.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ffi::CStr;
use std::{convert::TryFrom, ffi::CStr};

use heim_common::prelude::*;
use heim_common::Pid;
use heim_common::{Pid, Uid};

use super::super::unix::get_users;

Expand Down Expand Up @@ -69,6 +69,36 @@ impl From<libc::utmpx> for User {
}
}

impl From<*mut libc::passwd> for User {
fn from(entry: *mut libc::passwd) -> Self {
let username = unsafe {
CStr::from_ptr((*entry).pw_name)
.to_string_lossy()
.into_owned()
};

User {
username,
id: "".to_string(),
terminal: "".to_string(),
hostname: "".to_string(),
pid: 0,
}
}
}

impl TryFrom<Uid> for User {
type Error = Error;
fn try_from(uid: Uid) -> Result<Self> {
let passwd = unsafe { libc::getpwuid(uid) };
if passwd.is_null() {
return Err(Error::last_os_error().with_ffi("getpwuid"));
}
let user = User::from(passwd);
Ok(user)
}
}

pub async fn users() -> Result<impl Stream<Item = Result<User>>> {
let users = get_users::<User>();

Expand Down
39 changes: 39 additions & 0 deletions heim-host/src/sys/windows/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use std::net::IpAddr;

use super::wrappers::{Session, Sessions};
use heim_common::prelude::*;
use std::ptr;
use winapi::shared::minwindef::DWORD;
use winapi::um::winbase::LookupAccountSidW;
use winapi::um::winnt::{SidTypeUser, PSID, SID_NAME_USE, WCHAR};

#[derive(Debug)]
pub struct User {
Expand All @@ -26,6 +30,41 @@ impl User {
address: session.address()?,
}))
}
pub fn try_from_sid(sid: PSID) -> Result<Self> {
// name and domain cannot be longer than 256
let mut name_cch: DWORD = 256;
let mut name: Vec<WCHAR> = Vec::with_capacity(name_cch as usize);
let mut domain_cch: DWORD = 256;
let mut domain: Vec<WCHAR> = Vec::with_capacity(domain_cch as usize);
let mut account_type: SID_NAME_USE = 0;

let result = unsafe {
LookupAccountSidW(
ptr::null(),
sid,
name.as_mut_ptr(),
&mut name_cch,
domain.as_mut_ptr(),
&mut domain_cch,
&mut account_type,
)
};

if result == 0 || account_type != SidTypeUser {
return Err(Error::last_os_error().with_ffi("LookupAccountSidW"));
}

unsafe {
name.set_len(name_cch as usize);
domain.set_len(domain_cch as usize);
}

Ok(Self {
domain: String::from_utf16(domain.as_slice())?,
username: String::from_utf16(name.as_slice())?,
address: None,
})
}

pub fn domain(&self) -> &str {
self.domain.as_str()
Expand Down
4 changes: 3 additions & 1 deletion heim-process/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ features = [
"psapi",
"processthreadsapi",
"winerror",
"tlhelp32"
"tlhelp32",
"securitybaseapi"
]

[target.'cfg(target_os = "macos")'.dependencies]
heim-host = { version = "0.1.0-beta.1", path = "../heim-host" }
mach = "0.3.2"
darwin-libproc = "0.2.0"

Expand Down
9 changes: 9 additions & 0 deletions heim-process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,12 @@ pub use heim_common::Pid;

#[cfg(target_os = "linux")]
pub use heim_net::IoCounters;

#[cfg(target_os = "linux")]
pub use heim_common::Uid;

#[cfg(target_os = "linux")]
pub use heim_common::Gid;

#[cfg(target_os = "linux")]
pub use heim_common::Umask;
9 changes: 7 additions & 2 deletions heim-process/src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::fmt;
use std::path::PathBuf;
use std::time::Instant;

use crate::{sys, Pid, ProcessResult};
use heim_common::prelude::*;
use heim_common::units::Time;

use crate::{sys, Pid, ProcessResult};
use heim_host::User;

mod command;
mod cpu_times;
Expand Down Expand Up @@ -99,6 +99,11 @@ impl Process {
self.as_ref().status().await
}

/// Returns user who owns this process.
pub async fn user(&self) -> ProcessResult<User> {
self.as_ref().user().await.map(Into::into)
}

/// Returns process environment.
pub async fn environment(&self) -> ProcessResult<Environment> {
self.as_ref().environment().await.map(Into::into)
Expand Down
8 changes: 7 additions & 1 deletion heim-process/src/sys/linux/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ use std::path::{Path, PathBuf};

use heim_common::prelude::*;
use heim_common::units::Time;
use heim_host::User;
use heim_runtime as rt;

use super::{pid_exists, pids};
use crate::os::unix::Signal;
use crate::sys::common::UniqueId;
use crate::sys::unix::{pid_kill, pid_priority, pid_setpriority, pid_wait};
use crate::{Pid, ProcessError, ProcessResult, Status};

mod procfs;

pub use self::procfs::{Command, CommandIter, CpuTime, Environment, IoCounters, Memory};
use std::convert::TryFrom;

#[derive(Debug)]
pub struct Process {
Expand Down Expand Up @@ -109,6 +110,11 @@ impl Process {
Ok(state)
}

pub async fn user(&self) -> ProcessResult<User> {
let status = procfs::status(self.pid).await?;
Ok(User::try_from(status.uid.real)?)
}

pub async fn environment(&self) -> ProcessResult<Environment> {
procfs::environment(self.pid).await
}
Expand Down
2 changes: 2 additions & 0 deletions heim-process/src/sys/linux/process/procfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ mod env;
mod io;
mod stat;
mod statm;
mod status;

pub use self::command::{command, Command, CommandIter};
pub use self::cpu_times::CpuTime;
pub use self::env::{environment, Environment, IntoEnvironmentIter};
pub use self::io::{io, IoCounters};
pub use self::stat::{stat, Stat};
pub use self::statm::{stat_memory, Memory};
pub use self::status::{status, Status};
Loading