]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/sgx/os.rs
2725e66ce5de4001d43489e1e517b8264e0118fd
[rust.git] / src / libstd / sys / sgx / os.rs
1 use fortanix_sgx_abi::{Error, RESULT_SUCCESS};
2
3 use crate::error::Error as StdError;
4 use crate::ffi::{OsString, OsStr};
5 use crate::fmt;
6 use crate::io;
7 use crate::path::{self, PathBuf};
8 use crate::str;
9 use crate::sys::{unsupported, Void, sgx_ineffective, decode_error_kind};
10 use crate::collections::HashMap;
11 use crate::vec;
12 use crate::sync::Mutex;
13 use crate::sync::atomic::{AtomicUsize, Ordering};
14 use crate::sync::Once;
15
16 pub fn errno() -> i32 {
17     RESULT_SUCCESS
18 }
19
20 pub fn error_string(errno: i32) -> String {
21     if errno == RESULT_SUCCESS {
22         "operation succesful".into()
23     } else if ((Error::UserRangeStart as _)..=(Error::UserRangeEnd as _)).contains(&errno) {
24         format!("user-specified error {:08x}", errno)
25     } else {
26         decode_error_kind(errno).as_str().into()
27     }
28 }
29
30 pub fn getcwd() -> io::Result<PathBuf> {
31     unsupported()
32 }
33
34 pub fn chdir(_: &path::Path) -> io::Result<()> {
35     sgx_ineffective(())
36 }
37
38 pub struct SplitPaths<'a>(&'a Void);
39
40 pub fn split_paths(_unparsed: &OsStr) -> SplitPaths {
41     panic!("unsupported")
42 }
43
44 impl<'a> Iterator for SplitPaths<'a> {
45     type Item = PathBuf;
46     fn next(&mut self) -> Option<PathBuf> {
47         match *self.0 {}
48     }
49 }
50
51 #[derive(Debug)]
52 pub struct JoinPathsError;
53
54 pub fn join_paths<I, T>(_paths: I) -> Result<OsString, JoinPathsError>
55     where I: Iterator<Item=T>, T: AsRef<OsStr>
56 {
57     Err(JoinPathsError)
58 }
59
60 impl fmt::Display for JoinPathsError {
61     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62         "not supported in SGX yet".fmt(f)
63     }
64 }
65
66 impl StdError for JoinPathsError {
67     fn description(&self) -> &str {
68         "not supported in SGX yet"
69     }
70 }
71
72 pub fn current_exe() -> io::Result<PathBuf> {
73     unsupported()
74 }
75
76 static ENV: AtomicUsize = AtomicUsize::new(0);
77 static ENV_INIT: Once = Once::new();
78 type EnvStore = Mutex<HashMap<OsString, OsString>>;
79
80 fn get_env_store() -> Option<&'static EnvStore> {
81     unsafe { (ENV.load(Ordering::Relaxed) as *const EnvStore).as_ref() }
82 }
83
84 fn create_env_store() -> &'static EnvStore {
85     ENV_INIT.call_once(|| {
86         ENV.store(Box::into_raw(Box::new(EnvStore::default())) as _, Ordering::Relaxed)
87     });
88     unsafe {
89         &*(ENV.load(Ordering::Relaxed) as *const EnvStore)
90     }
91 }
92
93 pub type Env = vec::IntoIter<(OsString, OsString)>;
94
95 pub fn env() -> Env {
96     let clone_to_vec = |map: &HashMap<OsString, OsString>| -> Vec<_> {
97         map.iter().map(|(k, v)| (k.clone(), v.clone()) ).collect()
98     };
99
100     get_env_store()
101         .map(|env| clone_to_vec(&env.lock().unwrap()) )
102         .unwrap_or_default()
103         .into_iter()
104 }
105
106 pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> {
107     Ok(get_env_store().and_then(|s| s.lock().unwrap().get(k).cloned() ))
108 }
109
110 pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
111     let (k, v) = (k.to_owned(), v.to_owned());
112     create_env_store().lock().unwrap().insert(k, v);
113     Ok(())
114 }
115
116 pub fn unsetenv(k: &OsStr) -> io::Result<()> {
117     if let Some(env) = get_env_store() {
118         env.lock().unwrap().remove(k);
119     }
120     Ok(())
121 }
122
123 pub fn temp_dir() -> PathBuf {
124     panic!("no filesystem in SGX")
125 }
126
127 pub fn home_dir() -> Option<PathBuf> {
128     None
129 }
130
131 pub fn exit(code: i32) -> ! {
132     super::abi::exit_with_code(code as _)
133 }
134
135 pub fn getpid() -> u32 {
136     panic!("no pids in SGX")
137 }