2018-11-21 15:10:03 -08:00
|
|
|
/// NOTE: These syscalls only support wasm_32 for now because they take u32 offset
|
2019-02-07 10:16:55 -08:00
|
|
|
use libc::{c_int, c_long, getenv};
|
2019-02-06 18:57:11 -08:00
|
|
|
|
2019-02-07 15:24:22 -08:00
|
|
|
use std::ffi::CString;
|
2018-11-26 20:28:13 -08:00
|
|
|
use std::mem;
|
2018-11-26 20:29:26 -08:00
|
|
|
use std::os::raw::c_char;
|
2018-11-21 15:10:03 -08:00
|
|
|
|
2019-02-07 15:24:22 -08:00
|
|
|
use crate::env::call_malloc;
|
|
|
|
use crate::utils::{copy_cstr_into_wasm, read_string_from_wasm};
|
2019-01-24 00:00:38 -06:00
|
|
|
use wasmer_runtime_core::vm::Ctx;
|
2018-11-21 15:10:03 -08:00
|
|
|
|
2019-02-06 18:57:11 -08:00
|
|
|
extern "C" {
|
|
|
|
#[link_name = "_putenv"]
|
|
|
|
pub fn putenv(s: *const c_char) -> c_int;
|
|
|
|
}
|
|
|
|
|
2018-12-18 13:44:15 +01:00
|
|
|
// #[no_mangle]
|
2018-12-14 00:09:07 +01:00
|
|
|
/// emscripten: _getenv // (name: *const char) -> *const c_char;
|
2019-02-09 13:58:05 -08:00
|
|
|
pub fn _getenv(ctx: &mut Ctx, name: u32) -> u32 {
|
2018-12-18 13:44:15 +01:00
|
|
|
debug!("emscripten::_getenv");
|
2019-02-07 14:23:13 -08:00
|
|
|
let name_string = read_string_from_wasm(ctx.memory(0), name);
|
|
|
|
debug!("=> name({:?})", name_string);
|
|
|
|
let c_str = unsafe { getenv(name_string.as_ptr() as *const libc::c_char) };
|
2018-12-18 09:43:59 -08:00
|
|
|
if c_str.is_null() {
|
|
|
|
return 0;
|
2018-11-21 15:10:03 -08:00
|
|
|
}
|
2019-02-07 14:23:13 -08:00
|
|
|
unsafe { copy_cstr_into_wasm(ctx, c_str as *const c_char) }
|
2018-11-21 15:10:03 -08:00
|
|
|
}
|
2018-11-26 01:17:33 -05:00
|
|
|
|
2018-12-21 17:16:40 -08:00
|
|
|
/// emscripten: _setenv // (name: *const char, name: *const value, overwrite: int);
|
2019-03-15 14:10:17 -07:00
|
|
|
pub fn _setenv(ctx: &mut Ctx, name: u32, value: u32, _overwrite: u32) -> c_int {
|
2018-12-21 17:16:40 -08:00
|
|
|
debug!("emscripten::_setenv");
|
2019-02-06 18:57:11 -08:00
|
|
|
// setenv does not exist on windows, so we hack it with _putenv
|
|
|
|
let name = read_string_from_wasm(ctx.memory(0), name);
|
|
|
|
let value = read_string_from_wasm(ctx.memory(0), value);
|
|
|
|
let putenv_string = format!("{}={}", name, value);
|
|
|
|
let putenv_cstring = CString::new(putenv_string).unwrap();
|
|
|
|
let putenv_raw_ptr = putenv_cstring.as_ptr();
|
|
|
|
debug!("=> name({:?})", name);
|
|
|
|
debug!("=> value({:?})", value);
|
|
|
|
unsafe { putenv(putenv_raw_ptr) }
|
2018-12-21 17:16:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// emscripten: _putenv // (name: *const char);
|
2019-02-09 13:58:05 -08:00
|
|
|
pub fn _putenv(ctx: &mut Ctx, name: c_int) -> c_int {
|
2018-12-21 17:16:40 -08:00
|
|
|
debug!("emscripten::_putenv");
|
2019-01-24 13:04:12 -08:00
|
|
|
let name_addr = emscripten_memory_pointer!(ctx.memory(0), name) as *const c_char;
|
2019-03-15 14:10:17 -07:00
|
|
|
debug!("=> name({:?})", unsafe {
|
|
|
|
std::ffi::CStr::from_ptr(name_addr)
|
|
|
|
});
|
2019-02-06 18:57:11 -08:00
|
|
|
unsafe { putenv(name_addr) }
|
2018-12-21 17:16:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// emscripten: _unsetenv // (name: *const char);
|
2019-02-09 13:58:05 -08:00
|
|
|
pub fn _unsetenv(ctx: &mut Ctx, name: u32) -> c_int {
|
2018-12-21 17:16:40 -08:00
|
|
|
debug!("emscripten::_unsetenv");
|
2019-02-07 10:51:19 -08:00
|
|
|
let name = read_string_from_wasm(ctx.memory(0), name);
|
|
|
|
// no unsetenv on windows, so use putenv with an empty value
|
|
|
|
let unsetenv_string = format!("{}=", name);
|
|
|
|
let unsetenv_cstring = CString::new(unsetenv_string).unwrap();
|
|
|
|
let unsetenv_raw_ptr = unsetenv_cstring.as_ptr();
|
|
|
|
debug!("=> name({:?})", name);
|
|
|
|
unsafe { putenv(unsetenv_raw_ptr) }
|
2018-12-21 17:16:40 -08:00
|
|
|
}
|
|
|
|
|
2019-01-18 00:33:46 -06:00
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
2019-02-09 13:58:05 -08:00
|
|
|
pub fn _getpwnam(ctx: &mut Ctx, name_ptr: c_int) -> c_int {
|
2018-11-26 14:30:28 -08:00
|
|
|
debug!("emscripten::_getpwnam {}", name_ptr);
|
2019-03-15 14:10:17 -07:00
|
|
|
#[cfg(not(feature = "debug"))]
|
|
|
|
let _ = name_ptr;
|
2018-11-26 14:30:28 -08:00
|
|
|
|
2018-11-26 01:17:33 -05:00
|
|
|
#[repr(C)]
|
|
|
|
struct GuestPasswd {
|
|
|
|
pw_name: u32,
|
|
|
|
pw_passwd: u32,
|
|
|
|
pw_uid: u32,
|
|
|
|
pw_gid: u32,
|
|
|
|
pw_gecos: u32,
|
|
|
|
pw_dir: u32,
|
|
|
|
pw_shell: u32,
|
|
|
|
}
|
|
|
|
|
2019-02-07 10:16:55 -08:00
|
|
|
// stub this in windows as it is not valid
|
2018-11-26 01:17:33 -05:00
|
|
|
unsafe {
|
2019-02-09 13:58:05 -08:00
|
|
|
let passwd_struct_offset = call_malloc(ctx, mem::size_of::<GuestPasswd>() as _);
|
2019-01-24 15:30:13 -08:00
|
|
|
let passwd_struct_ptr =
|
|
|
|
emscripten_memory_pointer!(ctx.memory(0), passwd_struct_offset) as *mut GuestPasswd;
|
2019-02-07 10:16:55 -08:00
|
|
|
(*passwd_struct_ptr).pw_name = 0;
|
|
|
|
(*passwd_struct_ptr).pw_passwd = 0;
|
|
|
|
(*passwd_struct_ptr).pw_gecos = 0;
|
|
|
|
(*passwd_struct_ptr).pw_dir = 0;
|
|
|
|
(*passwd_struct_ptr).pw_shell = 0;
|
|
|
|
(*passwd_struct_ptr).pw_uid = 0;
|
|
|
|
(*passwd_struct_ptr).pw_gid = 0;
|
2018-11-26 01:17:33 -05:00
|
|
|
|
|
|
|
passwd_struct_offset as c_int
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 00:33:46 -06:00
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
2019-02-09 13:58:05 -08:00
|
|
|
pub fn _getgrnam(ctx: &mut Ctx, name_ptr: c_int) -> c_int {
|
2018-11-26 14:30:28 -08:00
|
|
|
debug!("emscripten::_getgrnam {}", name_ptr);
|
2019-03-15 14:10:17 -07:00
|
|
|
#[cfg(not(feature = "debug"))]
|
|
|
|
let _ = name_ptr;
|
2018-11-26 14:30:28 -08:00
|
|
|
|
2018-11-26 01:17:33 -05:00
|
|
|
#[repr(C)]
|
|
|
|
struct GuestGroup {
|
|
|
|
gr_name: u32,
|
|
|
|
gr_passwd: u32,
|
|
|
|
gr_gid: u32,
|
|
|
|
gr_mem: u32,
|
|
|
|
}
|
|
|
|
|
2019-02-07 10:16:55 -08:00
|
|
|
// stub the group struct as it is not supported on windows
|
2018-11-26 01:17:33 -05:00
|
|
|
unsafe {
|
2019-02-09 13:58:05 -08:00
|
|
|
let group_struct_offset = call_malloc(ctx, mem::size_of::<GuestGroup>() as _);
|
2019-01-24 15:30:13 -08:00
|
|
|
let group_struct_ptr =
|
|
|
|
emscripten_memory_pointer!(ctx.memory(0), group_struct_offset) as *mut GuestGroup;
|
2019-02-07 10:16:55 -08:00
|
|
|
(*group_struct_ptr).gr_name = 0;
|
|
|
|
(*group_struct_ptr).gr_passwd = 0;
|
|
|
|
(*group_struct_ptr).gr_gid = 0;
|
|
|
|
(*group_struct_ptr).gr_mem = 0;
|
2018-11-26 01:17:33 -05:00
|
|
|
group_struct_offset as c_int
|
|
|
|
}
|
2018-11-26 22:01:15 +01:00
|
|
|
}
|
2018-11-26 15:42:47 -05:00
|
|
|
|
2019-02-09 13:58:05 -08:00
|
|
|
pub fn _sysconf(_ctx: &mut Ctx, name: c_int) -> c_long {
|
2018-12-05 15:14:58 -08:00
|
|
|
debug!("emscripten::_sysconf {}", name);
|
2019-03-15 14:10:17 -07:00
|
|
|
#[cfg(not(feature = "debug"))]
|
|
|
|
let _ = name;
|
2019-02-07 10:16:55 -08:00
|
|
|
// stub because sysconf is not valid on windows
|
|
|
|
0
|
2018-12-05 15:14:58 -08:00
|
|
|
}
|