Skip to content
Snippets Groups Projects
Commit adde38d8 authored by Michael Aaron Murphy's avatar Michael Aaron Murphy
Browse files

Add UID and EUID String Variables

parent 8cdf371e
No related branches found
No related tags found
No related merge requests found
......@@ -14,13 +14,13 @@ use super::status::*;
use liner::{Buffer, Context};
use smallvec::SmallVec;
use std::env;
use std::error::Error;
use std::fs::File;
use std::io::{stdout, Write};
use std::io::ErrorKind;
use std::iter::{self, FromIterator};
use std::path::Path;
use std::process;
use std::io::{stdout, Write};
use std::error::Error;
const MAN_ION: &'static str = r#"NAME
ion - ion shell
......@@ -208,7 +208,7 @@ impl Binary for Shell {
match stdout
.write_all(MAN_ION.as_bytes())
.and_then(|_| stdout.flush())
{
{
Ok(_) => return,
Err(err) => panic!("{}", err.description().to_owned()),
}
......
......@@ -6,7 +6,7 @@ use fnv::FnvHashMap;
use liner::Context;
use std::env;
use std::io::{self, BufRead};
use sys::{self, getpid, is_root};
use sys::{self, geteuid, getpid, getuid, is_root};
use sys::variables as self_sys;
use types::{
Array, ArrayVariableContext, HashMap, HashMapVariableContext, Identifier, Key, Value,
......@@ -41,11 +41,20 @@ impl Default for Variables {
${c::reset}"
.into(),
);
// Set the PID variable to the PID of the shell
let pid = getpid()
.map(|p| p.to_string())
.unwrap_or_else(|e| e.to_string());
map.insert("PID".into(), pid.into());
// Set the PID, UID, and EUID variables.
map.insert(
"PID".into(),
getpid().ok().map_or("?".into(), |id| id.to_string()),
);
map.insert(
"UID".into(),
getuid().ok().map_or("?".into(), |id| id.to_string()),
);
map.insert(
"EUID".into(),
geteuid().ok().map_or("?".into(), |id| id.to_string()),
);
// Initialize the HISTFILE variable
if let Ok(base_dirs) = BaseDirectories::with_prefix("ion") {
......@@ -206,8 +215,8 @@ impl Variables {
pub fn get_var(&self, name: &str) -> Option<Value> {
match name {
"SWD" => return Some(self.get_simplified_directory()),
"MWD" => return Some(self.get_minimal_directory()),
"SWD" => return Some(self.get_simplified_directory()),
_ => (),
}
if let Some((name, variable)) = name.find("::").map(|pos| (&name[..pos], &name[pos + 2..]))
......
......@@ -22,6 +22,10 @@ pub(crate) const STDIN_FILENO: RawFd = 0;
pub(crate) const STDOUT_FILENO: RawFd = 1;
pub(crate) const STDERR_FILENO: RawFd = 2;
pub(crate) fn geteuid() -> io::Result<u32> { cvt(syscall::geteuid().map(|pid| pid as u32)) }
pub(crate) fn getuid() -> io::Result<u32> { cvt(syscall::getuid().map(|pid| pid as u32)) }
pub(crate) fn is_root() -> bool { syscall::geteuid().map(|id| id == 0).unwrap_or(false) }
pub unsafe fn fork() -> io::Result<u32> { cvt(syscall::clone(0)).map(|pid| pid as u32) }
......
......@@ -23,6 +23,10 @@ pub(crate) const STDOUT_FILENO: i32 = libc::STDOUT_FILENO;
pub(crate) const STDERR_FILENO: i32 = libc::STDERR_FILENO;
pub(crate) const STDIN_FILENO: i32 = libc::STDIN_FILENO;
pub(crate) fn geteuid() -> io::Result<u32> { Ok(unsafe { libc::geteuid() } as u32) }
pub(crate) fn getuid() -> io::Result<u32> { Ok(unsafe { libc::getuid() } as u32) }
pub(crate) fn is_root() -> bool { unsafe { libc::geteuid() == 0 } }
pub unsafe fn fork() -> io::Result<u32> { cvt(libc::fork()).map(|pid| pid as u32) }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment