shithub: lu9-p9

Download patch

ref: 4833b8d20e847f59dfdb138babd7265509c0c192
parent: cc882f7cb96fb0e481aba49bfcad199bb56392db
author: kvik <[email protected]>
date: Mon Apr 19 13:50:34 EDT 2021

proc: implement p9.wait()

--- a/p9.c
+++ b/p9.c
@@ -176,6 +176,7 @@
 	{"sleep", p9_sleep},
 	{"alarm", p9_alarm},
 	{"rfork", p9_rfork},
+	{"wait", p9_wait},
 	{"exec", p9_exec},
 	{"wdir", p9_wdir},
 	{"pid", p9_pid},
--- a/proc.c
+++ b/proc.c
@@ -165,3 +165,29 @@
 	free(argv);
 	return error(L, "exec: %r");
 }
+
+static int
+p9_wait(lua_State *L)
+{
+	Waitmsg *w;
+	
+	w = wait();
+	if(w == nil)
+		return error(L, "wait: %r");
+	lua_pushboolean(L, w->msg[0] == 0 ? 1 : 0);
+	lua_createtable(L, 0, 3);
+		lua_pushinteger(L, w->pid);
+			lua_setfield(L, -2, "pid");
+		lua_pushstring(L, w->msg);
+			lua_setfield(L, -2, "status");
+		lua_createtable(L, 3, 0);
+			lua_pushinteger(L, w->time[0]);
+				lua_setfield(L, -2, "user");
+			lua_pushinteger(L, w->time[1]);
+				lua_setfield(L, -2, "system");
+			lua_pushinteger(L, w->time[2]);
+				lua_setfield(L, -2, "real");
+		lua_setfield(L, -2, "time");
+	free(w);
+	return 2;
+}
--- a/test.lua
+++ b/test.lua
@@ -260,6 +260,26 @@
 	
 	local ok = p9.exec("/dev/mordor")
 	assert(not ok)
+	
+	-- Wait
+	local N = 9
+	for i = 1, N do
+		if p9.rfork("proc") == 0 then
+			p9.exec("sleep", "0")
+		end
+	end
+	for i = 1, N do
+		local ok, w = p9.wait()
+	end
+	local ok = p9.wait()
+	assert(not ok)
+	-- Wait (status)
+	if p9.rfork("proc") == 0 then
+		p9.sleep(0)
+		p9.exits("i failed you")
+	end
+	local ok, w = p9.wait()
+	assert(not ok and w.status:match("i failed you"))
 end