Skip to content

Commit

Permalink
Make PATH altering behavior configurable
Browse files Browse the repository at this point in the history
Note that we don't currently do anything to try and restore PATH; we'll just
keep adding paths if run_script() is called multiple times.  The orch(1)
driver will alter PATH unconditionally, but lua scripts using it are
expected to be more explicit if they want that behavior.

Fixes #5

Signed-off-by: Kyle Evans <[email protected]>
  • Loading branch information
kevans91 committed Jan 22, 2024
1 parent a46ded4 commit 24022fd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
11 changes: 8 additions & 3 deletions orch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ execute = function(func, freshctx)
end
end

local function include_file(file)
local f = assert(impl.open(file))
local function include_file(file, alter_path)
local f = assert(impl.open(file, alter_path))
local chunk = f:read("*l") -- * for compatibility with Lua 5.2...

if not chunk then
Expand Down Expand Up @@ -713,10 +713,15 @@ function orch_env.write(str)
return true
end

-- Valid config options:
-- * alter_path: boolean, add script's directory to $PATH (default: false)
-- * command: argv table to pass to spawn
function orch.run_script(scriptfile, config)
local done

include_file(scriptfile)
-- Note that the orch(1) driver will setup alter_path == true; scripts
-- importing orch.lua are expected to be more explicit.
include_file(scriptfile, config and config.alter_path)
--match_ctx_stack:dump()

if config and config.command then
Expand Down
16 changes: 9 additions & 7 deletions orch_interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,21 @@ orch_interp(const char *scriptf, const char *orch_invoke_path,
if (luaL_dofile(L, orch_interp_script(orch_invoke_path)) != LUA_OK) {
status = orch_interp_error(L);
} else {
int nargs = 1;

/*
* orch table is now at the top of stack, fetch run_script()
* and call it. run_script(scriptf[, config])
*/
lua_getfield(L, -1, "run_script");
lua_pushstring(L, scriptf);
if (argc > 0) {
/* config */
lua_createtable(L, 0, 1);
nargs++;

/* config */
lua_createtable(L, 0, 1);

/* config.alter_path */
lua_pushboolean(L, 1);
lua_setfield(L, -2, "alter_path");

if (argc > 0) {
/* config.command */
lua_createtable(L, argc, 0);
for (int i = 0; i < argc; i++) {
Expand All @@ -125,7 +127,7 @@ orch_interp(const char *scriptf, const char *orch_invoke_path,
lua_setfield(L, -2, "command");
}

if (lua_pcall(L, nargs, 1, 0) == LUA_OK)
if (lua_pcall(L, 2, 1, 0) == LUA_OK)
status = lua_toboolean(L, -1) ? 0 : 1;
else
status = orch_interp_error(L);
Expand Down
11 changes: 7 additions & 4 deletions orch_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ orchlua_close(lua_State *L)
}

static int
orchlua_open_init(const char *filename, const char **script)
orchlua_open_init(const char *filename, const char **script, bool alter_path)
{

assert(!orchlua_cfg.initialized);
Expand Down Expand Up @@ -106,8 +106,8 @@ orchlua_open_init(const char *filename, const char **script)
if (*script == NULL)
return (ENOMEM);

/* XXX Should be configurable */
orchlua_add_execpath(scriptroot);
if (alter_path)
orchlua_add_execpath(scriptroot);

orchlua_cfg.dirfd = open(scriptroot,
O_DIRECTORY | O_PATH | O_CLOEXEC);
Expand All @@ -125,16 +125,19 @@ orchlua_open(lua_State *L)
luaL_Stream *p;
const char *filename, *script;
int fd, rvals;
bool alter_path;

rvals = 1;
filename = luaL_checkstring(L, 1);
alter_path = lua_toboolean(L, 2);
script = NULL;

/* First open() sets up the sandbox state. */
if (!orchlua_cfg.initialized) {
int error;

if ((error = orchlua_open_init(filename, &script)) != 0) {
error = orchlua_open_init(filename, &script, alter_path);
if (error != 0) {
luaL_pushfail(L);
lua_pushstring(L, strerror(error));
return (true);
Expand Down

0 comments on commit 24022fd

Please sign in to comment.