Commit 8ebc0ab6 authored by Karel Koci's avatar Karel Koci 🤘

Add possibility to specify environment variables to subprocess

parent 7a6a2d78
......@@ -42,6 +42,7 @@
#include <unistd.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
#include <openssl/md5.h>
......@@ -458,18 +459,38 @@ static int lua_subprocess(lua_State *L) {
// TODO verify type?
const char *message = luaL_checkstring(L, 2);
int timeout = luaL_checkinteger(L, 3);
const char *command = luaL_checkstring(L, 4);
const char *command = luaL_checkstring(L, 5);
// Environment
luaL_checktype(L, 4, LUA_TTABLE);
int env_size = 1; // For terminating NULL
lua_pushnil(L);
while (lua_next(L, 4) != 0) {
env_size++;
lua_pop(L, 1); // pop pushed key
}
struct env_change env[env_size];
lua_pushnil(L);
int i = 0;
while (lua_next(L, 4) != 0) {
env[i].name = strdup(luaL_checkstring(L, -2));
env[i].value = strdup(luaL_checkstring(L, -1));
lua_pop(L, 1); // pop pushed key
i++;
}
env[env_size - 1] = (struct env_change){NULL};
int ec;
char *output;
if (lua_gettop(L) > 4) {
if (lua_gettop(L) > 5) {
const char *args[lua_gettop(L) - 4];
for (int i = 5; i <= lua_gettop(L); i++)
args[i - 5] = luaL_checkstring(L, i);
args[lua_gettop(L) - 4] = NULL;
ec = lsubprocl(type, message, &output, timeout, command, args);
for (int i = 6; i <= lua_gettop(L); i++) {
args[i - 6] = luaL_checkstring(L, i);
}
args[lua_gettop(L) - 5] = NULL;
ec = lsubprocle(type, message, &output, timeout, env, command, args);
} else {
ec = lsubprocv(type, message, &output, timeout, command, NULL);
ec = lsubprocve(type, message, &output, timeout, env, command, NULL);
}
lua_pushinteger(L, ec);
......
......@@ -90,10 +90,10 @@ Subprocesses
------------
Family of functions ``subproc*`` defined in ``subprocess.h`` are exported to lua
in form of function `subproc`.
in form of functions `subprocess`.
Function `subprocess` is defined as follows:
`suboprocess(type, message, timeout, command ...)`
`subprocess(type, message, timeout, environment, command ...)`
`type` is identification used to specify what type of subprocess it's. Allowed
predefined constants are as follows:
......@@ -106,6 +106,10 @@ Function `subprocess` is defined as follows:
`timeout` is time in seconds after which subprocess will be automatically
killed.
`environment` is (hash) table containing changes to be done to environment
variables. Key has to be only strings but values can be anything that can be
later converted to string using `tostring` function.
`command` is any arbitrary number of string arguments that are passed as command
and its additional arguments.
......
......@@ -22,27 +22,33 @@ require 'lunit'
module("subproc", package.seeall, lunit.testcase)
function test_exit_code()
local ok, out = subprocess(LST_HOOK, "Test: true", 1, "true")
local ok, out = subprocess(LST_HOOK, "Test: true", 1, {}, "true")
assert_equal(0, ok)
assert_equal("", out)
local ok, out = subprocess(LST_HOOK, "Test: false", 1, "false")
local ok, out = subprocess(LST_HOOK, "Test: false", 1, {}, "false")
assert_not_equal(0, ok)
assert_equal("", out)
end
function test_output()
local ok, out = subprocess(LST_HOOK, "Test: echo", 1, "echo", "hello")
local ok, out = subprocess(LST_HOOK, "Test: echo", 1, {}, "echo", "hello")
assert_equal(0, ok)
assert_equal("hello\n", out)
local ok, out = subprocess(LST_HOOK, "Test: echo stderr", 1, "sh", "-c", "echo hello >&2")
local ok, out = subprocess(LST_HOOK, "Test: echo stderr", 1, {}, "sh", "-c", "echo hello >&2")
assert_equal(0, ok)
assert_equal("hello\n", out)
end
function test_timeout()
local ok, out = subprocess(LST_HOOK, "Test: sleep", 1, "sleep", "2")
local ok, out = subprocess(LST_HOOK, "Test: sleep", 1, {}, "sleep", "2")
assert_not_equal(0, ok)
assert_equal("", out)
end
function test_env()
local ok, out = subprocess(LST_HOOK, "Test: env", 1, {['TESTMSG'] = 'hello'}, "sh", "-c", "echo $TESTMSG")
assert_equal(0, ok)
assert_equal("hello\n", out)
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment