justincormack / ljsyscall

LuaJIT Unix syscall FFI
http://www.myriabit.com/ljsyscall/
Other
440 stars 54 forks source link

execv() #204

Closed lukego closed 7 years ago

lukego commented 7 years ago

Seems that ljsyscall provides execve() but not execv(). Is there a simple way to emulate the latter? (I want the new process image to inherit the environment.)

My first attempt did not work:

S.execve("/bin/ls", "", S.environ())
Error in syscall/syscalls.lua:568: cannot convert 'string' to 'const char *[?]'
justincormack commented 7 years ago

Yes, we should provide a conversion function here for this use case, makes sense.

On 11 Nov 2016 1:07 p.m., "Luke Gorrie" notifications@github.com wrote:

Seems that ljsyscall provides execve() but not execv(). Is there a simple way to emulate the latter? (I want the new process image to inherit the environment.)

My first attempt did not work:

S.execve("/bin/ls", "", S.environ()) Error in syscall/syscalls.lua:568: cannot convert 'string' to 'const char *[?]'

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/justincormack/ljsyscall/issues/204, or mute the thread https://github.com/notifications/unsubscribe-auth/AAdcPJ_T0EncLG4zIoGtfcQ3eYvst-Lyks5q9FqMgaJpZM4KvsFM .

lukego commented 7 years ago

Could also be that I misdiagnosed my problem here. The error seems to be because I am passing a bad argv ("" instead of {}). Maybe S.environ() actually does work for passing the existing environment?

lukego commented 7 years ago

@justincormack JFYI it took me a while to understand why I could not pass my current environment to a new process like this:

S.execve(cmd, argv, S.environ())

and eventually realized while looking at strace that no environment is passed in this case. I suppose that S.environ() is returning a table with string keys while S.execve() is expecting an array of key=value strings. I suppose this is a Lua-ism where ljsyscall will iterate through the array part of the table with pairs() and think it is empty i.e. silently ignore the hash part.

This is what I am doing instead right now:

local env = {}
for key, value in pairs(S.environ()) do
   table.insert(env, key.."="..value)
end
S.execve(cmd, argv, env)
lukego commented 7 years ago

(Closing since this seems to be working fine and I just needed to work out the API.)