1 use fortanix_sgx_abi::{Error, RESULT_SUCCESS};
3 use crate::collections::HashMap;
4 use crate::error::Error as StdError;
5 use crate::ffi::{OsStr, OsString};
8 use crate::marker::PhantomData;
9 use crate::path::{self, PathBuf};
11 use crate::sync::atomic::{AtomicUsize, Ordering};
12 use crate::sync::Mutex;
13 use crate::sync::Once;
14 use crate::sys::{decode_error_kind, sgx_ineffective, unsupported};
17 pub fn errno() -> i32 {
21 pub fn error_string(errno: i32) -> String {
22 if errno == RESULT_SUCCESS {
23 "operation successful".into()
24 } else if ((Error::UserRangeStart as _)..=(Error::UserRangeEnd as _)).contains(&errno) {
25 format!("user-specified error {errno:08x}")
27 decode_error_kind(errno).as_str().into()
31 pub fn getcwd() -> io::Result<PathBuf> {
35 pub fn chdir(_: &path::Path) -> io::Result<()> {
39 pub struct SplitPaths<'a>(!, PhantomData<&'a ()>);
41 pub fn split_paths(_unparsed: &OsStr) -> SplitPaths<'_> {
45 impl<'a> Iterator for SplitPaths<'a> {
47 fn next(&mut self) -> Option<PathBuf> {
53 pub struct JoinPathsError;
55 pub fn join_paths<I, T>(_paths: I) -> Result<OsString, JoinPathsError>
57 I: Iterator<Item = T>,
63 impl fmt::Display for JoinPathsError {
64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 "not supported in SGX yet".fmt(f)
69 impl StdError for JoinPathsError {
71 fn description(&self) -> &str {
72 "not supported in SGX yet"
76 pub fn current_exe() -> io::Result<PathBuf> {
80 #[cfg_attr(test, linkage = "available_externally")]
81 #[export_name = "_ZN16__rust_internals3std3sys3sgx2os3ENVE"]
82 static ENV: AtomicUsize = AtomicUsize::new(0);
83 #[cfg_attr(test, linkage = "available_externally")]
84 #[export_name = "_ZN16__rust_internals3std3sys3sgx2os8ENV_INITE"]
85 static ENV_INIT: Once = Once::new();
86 type EnvStore = Mutex<HashMap<OsString, OsString>>;
88 fn get_env_store() -> Option<&'static EnvStore> {
89 unsafe { (ENV.load(Ordering::Relaxed) as *const EnvStore).as_ref() }
92 fn create_env_store() -> &'static EnvStore {
93 ENV_INIT.call_once(|| {
94 ENV.store(Box::into_raw(Box::new(EnvStore::default())) as _, Ordering::Relaxed)
96 unsafe { &*(ENV.load(Ordering::Relaxed) as *const EnvStore) }
99 pub type Env = vec::IntoIter<(OsString, OsString)>;
101 pub fn env() -> Env {
102 let clone_to_vec = |map: &HashMap<OsString, OsString>| -> Vec<_> {
103 map.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
106 get_env_store().map(|env| clone_to_vec(&env.lock().unwrap())).unwrap_or_default().into_iter()
109 pub fn getenv(k: &OsStr) -> Option<OsString> {
110 get_env_store().and_then(|s| s.lock().unwrap().get(k).cloned())
113 pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
114 let (k, v) = (k.to_owned(), v.to_owned());
115 create_env_store().lock().unwrap().insert(k, v);
119 pub fn unsetenv(k: &OsStr) -> io::Result<()> {
120 if let Some(env) = get_env_store() {
121 env.lock().unwrap().remove(k);
126 pub fn temp_dir() -> PathBuf {
127 panic!("no filesystem in SGX")
130 pub fn home_dir() -> Option<PathBuf> {
134 pub fn exit(code: i32) -> ! {
135 super::abi::exit_with_code(code as _)
138 pub fn getpid() -> u32 {
139 panic!("no pids in SGX")