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