diff --git a/lib/emscripten/src/io/unix.rs b/lib/emscripten/src/io/unix.rs index d120e754d..6ff711bda 100644 --- a/lib/emscripten/src/io/unix.rs +++ b/lib/emscripten/src/io/unix.rs @@ -1,4 +1,7 @@ -use libc::{chroot as _chroot, printf as _printf}; +use std::mem; +use libc::{chroot as _chroot, printf as _printf, getpwuid as _getpwuid}; +use super::super::utils::copy_cstr_into_wasm; +use super::super::env::call_malloc; use wasmer_runtime_core::vm::Ctx; @@ -24,7 +27,36 @@ pub fn chroot(ctx: &mut Ctx, name_ptr: i32) -> i32 { } /// getpwuid -pub fn getpwuid(_ctx: &mut Ctx, _uid: i32) -> i32 { - debug!("emscripten::getpwuid"); - 0 +pub fn getpwuid(ctx: &mut Ctx, uid: i32) -> i32 { + debug!("emscripten::getpwuid {}", uid); + + #[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, + } + + unsafe { + let passwd = &*_getpwuid(uid as _); + let passwd_struct_offset = call_malloc(ctx, mem::size_of::() as _); + + let passwd_struct_ptr = + emscripten_memory_pointer!(ctx.memory(0), passwd_struct_offset) as *mut GuestPasswd; + (*passwd_struct_ptr).pw_name = copy_cstr_into_wasm(ctx, passwd.pw_name); + (*passwd_struct_ptr).pw_passwd = copy_cstr_into_wasm(ctx, passwd.pw_passwd); + (*passwd_struct_ptr).pw_gecos = copy_cstr_into_wasm(ctx, passwd.pw_gecos); + (*passwd_struct_ptr).pw_dir = copy_cstr_into_wasm(ctx, passwd.pw_dir); + (*passwd_struct_ptr).pw_shell = copy_cstr_into_wasm(ctx, passwd.pw_shell); + (*passwd_struct_ptr).pw_uid = passwd.pw_uid; + (*passwd_struct_ptr).pw_gid = passwd.pw_gid; + + passwd_struct_offset as _ + } + // unsafe { _getpwuid(uid as _) as _} + // 0 } diff --git a/lib/emscripten/src/syscalls/mod.rs b/lib/emscripten/src/syscalls/mod.rs index 88e382750..99ec1afd3 100644 --- a/lib/emscripten/src/syscalls/mod.rs +++ b/lib/emscripten/src/syscalls/mod.rs @@ -39,6 +39,7 @@ use libc::{ // writev, stat, write, + getuid, // readlink, }; use wasmer_runtime_core::{ @@ -611,9 +612,12 @@ pub fn ___syscall197(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_in 0 } +// getuid pub fn ___syscall199(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { - debug!("emscripten::___syscall199 - stub"); - -1 + debug!("emscripten::___syscall199 (getuid)"); + let uid = unsafe { getuid() as _ }; + debug!(" => {}", uid); + uid } pub fn ___syscall209(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 {