2022-04-21 20:44:59 +00:00
|
|
|
-- example of how to run the ls command
|
|
|
|
-- and pipe its output to the http user
|
|
|
|
local unix = require "unix"
|
|
|
|
function main()
|
|
|
|
syscall = 'commandv'
|
|
|
|
ls, errno = unix.commandv("ls")
|
|
|
|
if ls then
|
|
|
|
syscall = 'pipe'
|
|
|
|
reader, writer, errno = unix.pipe()
|
|
|
|
if reader then
|
2022-04-22 05:07:21 +00:00
|
|
|
oldint = unix.sigaction(unix.SIGINT, unix.SIG_IGN)
|
|
|
|
oldquit = unix.sigaction(unix.SIGQUIT, unix.SIG_IGN)
|
|
|
|
oldmask = unix.sigprocmask(unix.SIG_BLOCK, unix.SIGCHLD)
|
2022-04-21 20:44:59 +00:00
|
|
|
syscall = 'fork'
|
|
|
|
child, errno = unix.fork()
|
|
|
|
if child then
|
|
|
|
if child == 0 then
|
|
|
|
unix.close(1)
|
|
|
|
unix.dup(writer)
|
|
|
|
unix.close(writer)
|
|
|
|
unix.close(reader)
|
2022-04-22 05:07:21 +00:00
|
|
|
unix.sigaction(unix.SIGINT, oldint)
|
|
|
|
unix.sigaction(unix.SIGQUIT, oldquit)
|
|
|
|
unix.sigprocmask(unix.SIG_SETMASK, oldmask)
|
2022-04-21 20:44:59 +00:00
|
|
|
unix.execve(ls, {ls, "-Shal"})
|
|
|
|
unix.exit(127)
|
|
|
|
else
|
|
|
|
unix.close(writer)
|
|
|
|
SetStatus(200)
|
|
|
|
SetHeader('Content-Type', 'text/plain')
|
|
|
|
while true do
|
2022-04-22 05:07:21 +00:00
|
|
|
data, errno = unix.read(reader)
|
|
|
|
if data then
|
|
|
|
if data ~= "" then
|
|
|
|
Write(data)
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
elseif errno ~= unix.EINTR then
|
|
|
|
Log(kLogWarn, string.format('read() failed: %s', unix.strerror(errno)))
|
2022-04-21 20:44:59 +00:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
unix.close(reader)
|
|
|
|
unix.wait(-1)
|
2022-04-22 05:07:21 +00:00
|
|
|
unix.sigaction(unix.SIGINT, oldint)
|
|
|
|
unix.sigaction(unix.SIGQUIT, oldquit)
|
|
|
|
unix.sigprocmask(unix.SIG_SETMASK, oldmask)
|
2022-04-21 20:44:59 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
SetStatus(200)
|
|
|
|
SetHeader('Content-Type', 'text/plain')
|
|
|
|
Write(string.format('error %s calling %s()', unix.strerrno(errno), syscall))
|
|
|
|
end
|
|
|
|
main()
|