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::path
::{self, PathBuf}
;
10 use crate::sync
::atomic
::{AtomicUsize, Ordering}
;
11 use crate::sync
::Mutex
;
12 use crate::sync
::Once
;
13 use crate::sys
::{decode_error_kind, sgx_ineffective, unsupported, Void}
;
16 pub fn errno() -> i32 {
20 pub fn error_string(errno
: i32) -> String
{
21 if errno
== RESULT_SUCCESS
{
22 "operation successful".into()
23 } else if ((Error
::UserRangeStart
as _
)..=(Error
::UserRangeEnd
as _
)).contains(&errno
) {
24 format
!("user-specified error {:08x}", errno
)
26 decode_error_kind(errno
).as_str().into()
30 pub fn getcwd() -> io
::Result
<PathBuf
> {
34 pub fn chdir(_
: &path
::Path
) -> io
::Result
<()> {
38 pub struct SplitPaths
<'a
>(&'a Void
);
40 pub fn split_paths(_unparsed
: &OsStr
) -> SplitPaths
<'_
> {
44 impl<'a
> Iterator
for SplitPaths
<'a
> {
46 fn next(&mut self) -> Option
<PathBuf
> {
52 pub struct JoinPathsError
;
54 pub fn join_paths
<I
, T
>(_paths
: I
) -> Result
<OsString
, JoinPathsError
>
56 I
: Iterator
<Item
= T
>,
62 impl fmt
::Display
for JoinPathsError
{
63 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
64 "not supported in SGX yet".fmt(f
)
68 impl StdError
for JoinPathsError
{
70 fn description(&self) -> &str {
71 "not supported in SGX yet"
75 pub fn current_exe() -> io
::Result
<PathBuf
> {
79 #[cfg_attr(test, linkage = "available_externally")]
80 #[export_name = "_ZN16__rust_internals3std3sys3sgx2os3ENVE"]
81 static ENV
: AtomicUsize
= AtomicUsize
::new(0);
82 #[cfg_attr(test, linkage = "available_externally")]
83 #[export_name = "_ZN16__rust_internals3std3sys3sgx2os8ENV_INITE"]
84 static ENV_INIT
: Once
= Once
::new();
85 type EnvStore
= Mutex
<HashMap
<OsString
, OsString
>>;
87 fn get_env_store() -> Option
<&'
static EnvStore
> {
88 unsafe { (ENV.load(Ordering::Relaxed) as *const EnvStore).as_ref() }
91 fn create_env_store() -> &'
static EnvStore
{
92 ENV_INIT
.call_once(|| {
93 ENV
.store(Box
::into_raw(Box
::new(EnvStore
::default())) as _
, Ordering
::Relaxed
)
95 unsafe { &*(ENV.load(Ordering::Relaxed) as *const EnvStore) }
98 pub type Env
= vec
::IntoIter
<(OsString
, OsString
)>;
100 pub fn env() -> Env
{
101 let clone_to_vec
= |map
: &HashMap
<OsString
, OsString
>| -> Vec
<_
> {
102 map
.iter().map(|(k
, v
)| (k
.clone(), v
.clone())).collect()
105 get_env_store().map(|env
| clone_to_vec(&env
.lock().unwrap())).unwrap_or_default().into_iter()
108 pub fn getenv(k
: &OsStr
) -> io
::Result
<Option
<OsString
>> {
109 Ok(get_env_store().and_then(|s
| s
.lock().unwrap().get(k
).cloned()))
112 pub fn setenv(k
: &OsStr
, v
: &OsStr
) -> io
::Result
<()> {
113 let (k
, v
) = (k
.to_owned(), v
.to_owned());
114 create_env_store().lock().unwrap().insert(k
, v
);
118 pub fn unsetenv(k
: &OsStr
) -> io
::Result
<()> {
119 if let Some(env
) = get_env_store() {
120 env
.lock().unwrap().remove(k
);
125 pub fn temp_dir() -> PathBuf
{
126 panic
!("no filesystem in SGX")
129 pub fn home_dir() -> Option
<PathBuf
> {
133 pub fn exit(code
: i32) -> ! {
134 super::abi
::exit_with_code(code
as _
)
137 pub fn getpid() -> u32 {
138 panic
!("no pids in SGX")