]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/sgx/os.rs
Rollup merge of #105655 - RedDocMD:bug-105645, r=oli-obk
[rust.git] / library / std / src / sys / sgx / os.rs
1 use fortanix_sgx_abi::{Error, RESULT_SUCCESS};
2
3 use crate::collections::HashMap;
4 use crate::error::Error as StdError;
5 use crate::ffi::{OsStr, OsString};
6 use crate::fmt;
7 use crate::io;
8 use crate::marker::PhantomData;
9 use crate::path::{self, PathBuf};
10 use crate::str;
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};
15 use crate::vec;
16
17 pub fn errno() -> i32 {
18     RESULT_SUCCESS
19 }
20
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}")
26     } else {
27         decode_error_kind(errno).as_str().into()
28     }
29 }
30
31 pub fn getcwd() -> io::Result<PathBuf> {
32     unsupported()
33 }
34
35 pub fn chdir(_: &path::Path) -> io::Result<()> {
36     sgx_ineffective(())
37 }
38
39 pub struct SplitPaths<'a>(!, PhantomData<&'a ()>);
40
41 pub fn split_paths(_unparsed: &OsStr) -> SplitPaths<'_> {
42     panic!("unsupported")
43 }
44
45 impl<'a> Iterator for SplitPaths<'a> {
46     type Item = PathBuf;
47     fn next(&mut self) -> Option<PathBuf> {
48         self.0
49     }
50 }
51
52 #[derive(Debug)]
53 pub struct JoinPathsError;
54
55 pub fn join_paths<I, T>(_paths: I) -> Result<OsString, JoinPathsError>
56 where
57     I: Iterator<Item = T>,
58     T: AsRef<OsStr>,
59 {
60     Err(JoinPathsError)
61 }
62
63 impl fmt::Display for JoinPathsError {
64     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65         "not supported in SGX yet".fmt(f)
66     }
67 }
68
69 impl StdError for JoinPathsError {
70     #[allow(deprecated)]
71     fn description(&self) -> &str {
72         "not supported in SGX yet"
73     }
74 }
75
76 pub fn current_exe() -> io::Result<PathBuf> {
77     unsupported()
78 }
79
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>>;
87
88 fn get_env_store() -> Option<&'static EnvStore> {
89     unsafe { (ENV.load(Ordering::Relaxed) as *const EnvStore).as_ref() }
90 }
91
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)
95     });
96     unsafe { &*(ENV.load(Ordering::Relaxed) as *const EnvStore) }
97 }
98
99 pub type Env = vec::IntoIter<(OsString, OsString)>;
100
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()
104     };
105
106     get_env_store().map(|env| clone_to_vec(&env.lock().unwrap())).unwrap_or_default().into_iter()
107 }
108
109 pub fn getenv(k: &OsStr) -> Option<OsString> {
110     get_env_store().and_then(|s| s.lock().unwrap().get(k).cloned())
111 }
112
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);
116     Ok(())
117 }
118
119 pub fn unsetenv(k: &OsStr) -> io::Result<()> {
120     if let Some(env) = get_env_store() {
121         env.lock().unwrap().remove(k);
122     }
123     Ok(())
124 }
125
126 pub fn temp_dir() -> PathBuf {
127     panic!("no filesystem in SGX")
128 }
129
130 pub fn home_dir() -> Option<PathBuf> {
131     None
132 }
133
134 pub fn exit(code: i32) -> ! {
135     super::abi::exit_with_code(code as _)
136 }
137
138 pub fn getpid() -> u32 {
139     panic!("no pids in SGX")
140 }