Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add exece #362

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

add exece #362

wants to merge 1 commit into from

Conversation

aabacchus
Copy link

A wrapper of execve to be able to specify the environment of the new process. I'm not convinced this is good code: should the Lua strings, used to fill the C array envp, be removed from the stack before (or after) execve? Surely when execv returns (in case of error) there will be more values on the stack than reported by the return value of the function?

Also, valgrind reports errors with my function but not with unistd.exec, I'm not sure why (both leave extra Lua strings on the stack).

Also, would it be better to change unistd.exec to have an optional env arg rather than a separate unistd.exece function?

@aabacchus
Copy link
Author

I suppose the same features could be implemented using stdlib.setenv and unistd.exec but that would be a) much more verbose, b) probably slower, and c) execve is in POSIX, after all.

@aabacchus
Copy link
Author

Indeed, I am currently using this in place of an exece:

-- path is a string, cmd is an array, env is a table
-- Despite the variable name, path doesn't have to be absolute because execp is used.
function run(path, cmd, env)
    io.stderr:write(path .. ' ' .. table.concat(cmd, ' ', 1) .. '\n')

    local pid = unistd.fork()

    if not pid then
        die("fork failed")
    elseif pid == 0 then
        if env then
            for k,v in pairs(env) do
                stdlib.setenv(k, v)
            end
        end

        unistd.execp(path, cmd)
    else
        local _, msg, code = sys_wait.wait(pid)
        if msg ~= "exited" then
            die("run failed: " .. msg)
        end
        return code == 0
    end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant