[luci] [luci+nixio libs] implementing popen2 with nixio and execute with timeout

Jo-Philipp Wich xm at subsignal.org
Wed Mar 7 01:46:49 CET 2012


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

something like this should work:

- -- >8 --

function open3(command)

	local cldin  = { nixio.pipe() }
	local cldout = { nixio.pipe() }
	local clderr = { nixio.pipe() }

	local pid = nixio.fork()

	if pid > 0 then

		cldin[1]:close()
		cldout[2]:close()
		clderr[2]:close()

		return pid, cldin[2], cldout[1], clderr[1]

	elseif pid == 0 then

		cldin[2]:close()
		cldout[1]:close()
		clderr[1]:close()

		nixio.dup(cldin[1],  nixio.stdin)
		nixio.dup(cldout[2], nixio.stdout)
		nixio.dup(clderr[2], nixio.stderr)

		nixio.exec("/bin/sh", "-c", command)

	end
end

- -- 8< --


Call it like:

  local pid, cin, cout, cerr = open3("some command")

  cin:write("Write to child stdin")
  cout:read(1024) -- Read from child stdout
  cerr:read(1024) -- Read from child stderr


~ Jow
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk9Wr/kACgkQdputYINPTPO1qACcCM0IiK2rXLt9sVUWcJOd1FKl
eigAnAwo6UfRloPQnEaxU98xfMWi6ru8
=RAoL
-----END PGP SIGNATURE-----


More information about the luci mailing list