Skip to content

Commit b4f04bd

Browse files
committed
WIP
1 parent 6d06bb2 commit b4f04bd

File tree

6 files changed

+69
-11
lines changed

6 files changed

+69
-11
lines changed

test/tarantool-c-tests/fix-argv-handling.test.c

Whitespace-only changes.

test/tarantool-tests/fix-argv-handling.test.lua

+14-7
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@ local test = tap.test('fix-argv-handling'):skipcond({
55

66
test:plan(1)
77

8-
local script = require('utils').exec.makecmd(arg, {
9-
env = { LD_PRELOAD = 'mynewstate.so', DYLD_PRELOAD = 'mynewstate.so' },
10-
redirect = '2>&1',
11-
})
12-
local output = script()
8+
local ffi = require('ffi')
9+
ffi.cdef[[
10+
const char *empty_argv_exec(const char *path);
11+
void free(void *ptr);
12+
]]
13+
local execlib = ffi.load('emptyargvexec')
14+
15+
local cmd = require('utils').exec.luabin(arg)
16+
print(cmd)
17+
local output = execlib.empty_argv_exec(cmd)
18+
ffi.gc(output, ffi.C.free)
1319

20+
output_str = ffi.string(output)
1421
-- Skip test for static build.
15-
if output:match('cannot be preloaded') then
22+
if output_str:match('cannot be preloaded') then
1623
test:ok(true, 'static build -- can not perform test')
1724
else
18-
test:like(output, 'cannot create state', 'correct argv handling')
25+
test:like(output_str, 'cannot create state', 'correct argv handling')
1926
end
2027

2128
test:done(true)
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
BuildTestCLib(mynewstate mynewstate.c)
2+
BuildTestCLib(libemptyargvexec empty_argv_exec.c)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#define _GNU_SOURCE
2+
#include <fcntl.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <sys/wait.h>
6+
#include <unistd.h>
7+
8+
#define BUF_SIZE 1024
9+
#define CHECKED(call) \
10+
do { \
11+
int status = 0; \
12+
status = (call); \
13+
if (status == -1) { \
14+
perror(#call); \
15+
exit(1); \
16+
} \
17+
} while(0)
18+
19+
const char *empty_argv_exec(const char *path, char *const env[]) {
20+
int pipefds[2] = {};
21+
CHECKED(pipe2(pipefds, O_CLOEXEC));
22+
23+
pid_t pid = fork();
24+
if (pid == 0) {
25+
CHECKED(dup2(pipefds[1], 1));
26+
CHECKED(dup2(pipefds[1], 2));
27+
CHECKED(execl(path, NULL));
28+
}
29+
30+
close(pipefds[1]);
31+
waitpid(pid, NULL, 0);
32+
/* Kilobyte should be enough. */
33+
char *buf = calloc(BUF_SIZE, sizeof(char));
34+
if (buf == NULL) {
35+
perror("calloc");
36+
exit(1);
37+
}
38+
CHECKED(read(pipefds[0], buf, BUF_SIZE));
39+
return buf;
40+
}
41+

test/tarantool-tests/tap.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
--- tap.lua internal file.
1+
---tap.lua internal file.
22
---
33
--- The Test Anything Protocol version 13 producer.
44
---

test/tarantool-tests/utils/exec.lua

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
local M = {}
22

3-
function M.luacmd(args)
3+
local function executable_idx(args)
44
-- arg[-1] is guaranteed to be not nil.
55
local idx = -2
66
while args[idx] do
77
assert(type(args[idx]) == 'string', 'Command part have to be a string')
88
idx = idx - 1
99
end
10-
-- return the full command with flags.
11-
return table.concat(args, ' ', idx + 1, -1)
10+
return idx + 1
11+
end
12+
13+
function M.luabin(args)
14+
-- Return only the executable.
15+
return args[executable_idx(args)]
16+
end
17+
18+
function M.luacmd(args)
19+
-- Return the full command with flags.
20+
return table.concat(args, ' ', executable_idx(args), -1)
1221
end
1322

1423
local function makeenv(tabenv)

0 commit comments

Comments
 (0)