ref: cc882f7cb96fb0e481aba49bfcad199bb56392db
parent: afb18ac80f88ac730d95fb6f6e442c77b90f47ea
author: kvik <[email protected]>
date: Mon Apr 19 12:54:07 EDT 2021
proc: implement p9.exec()
--- a/p9.c
+++ b/p9.c
@@ -176,6 +176,7 @@
{"sleep", p9_sleep},
{"alarm", p9_alarm},
{"rfork", p9_rfork},
+ {"exec", p9_exec},
{"wdir", p9_wdir},
{"pid", p9_pid},
{"ppid", p9_ppid},
--- a/proc.c
+++ b/proc.c
@@ -141,3 +141,27 @@
lua_pushinteger(L, r);
return 1;
}
+
+static int
+p9_exec(lua_State *L)
+{
+ int argc, i;
+ const char **argv, *p;
+ char buf[Smallbuf];
+
+ argc = lua_gettop(L);
+ if(argc < 1)
+ luaL_argerror(L, 1, "string arguments expected");
+ argv = lalloc(L, nil, (argc+1) * sizeof(char*));
+ for(i = 1; i <= argc; i++)
+ argv[i-1] = luaL_checkstring(L, i);
+ argv[argc] = nil;
+ p = argv[0];
+ if(p[0] != '/' && (p[0] != '.' && p[1] != '/')){
+ snprint(buf, sizeof buf, "/bin/%s", argv[0]);
+ argv[0] = buf;
+ }
+ exec(argv[0], argv);
+ free(argv);
+ return error(L, "exec: %r");
+}
--- a/test.lua
+++ b/test.lua
@@ -242,6 +242,29 @@
assert(p9.pid() and p9.ppid())
end
+-- Fork & Exec
+do
+ local us, them = p9.pipe()
+ if p9.rfork("proc nowait fd") == 0 then
+ them:dup(p9.file(0))
+ them:dup(p9.file(1))
+ them:close()
+ p9.exec("cat")
+ else
+ them:close()
+ us:write("HELLO CAT")
+ us:write("")
+ assert(us:slurp() == "HELLO CAT")
+ us:close()
+ end
+
+ local ok = p9.exec("/dev/mordor")
+ assert(not ok)
+end
+
+
+
+
-- Environment variables
do
local e