Skip to content

Commit

Permalink
jail(3lua): add a jail.list() method
Browse files Browse the repository at this point in the history
This is implemented as an iterator, reusing parts of the earlier logic
to populate jailparams from a passed in table.

The user may request any number of parameters to pull in while we're
searching, but we'll force jid and name to appear at a minimum.

(cherry picked from commit 6a7647e)
  • Loading branch information
kevans91 committed Oct 6, 2021
1 parent bd4ee4e commit 44175ec
Show file tree
Hide file tree
Showing 3 changed files with 385 additions and 39 deletions.
47 changes: 47 additions & 0 deletions lib/flua/libjail/jail.3lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
.Sh NAME
.Nm getid ,
.Nm getname ,
.Nm list ,
.Nm allparams ,
.Nm getparams ,
.Nm setparams ,
Expand All @@ -50,6 +51,7 @@ local jail = require('jail')
.It Dv jid, err = jail.getid(name)
.It Dv name, err = jail.getname(jid)
.It Dv params, err = jail.allparams()
.It Dv iter, jail_obj = jail.list([params])
.It Dv jid, res = jail.getparams(jid|name, params [, flags ] )
.It Dv jid, err = jail.setparams(jid|name, params, flags )
.It Dv jail.CREATE
Expand Down Expand Up @@ -79,6 +81,21 @@ is the name of a jail or a jid in the form of a string.
Get the name of a jail as a string for the given
.Fa jid
.Pq an integer .
.It Dv iter, jail_obj = jail.list([params])
Returns an iterator over running jails on the system.
.Dv params
is a list of parameters to fetch for each jail as we iterate.
.Dv jid
and
.Dv name
will always be returned, and may be omitted from
.Dv params .
Additionally,
.Dv params
may be omitted or an empty table, but not nil.
.Pp
See
.Sx EXAMPLES .
.It Dv params, err = jail.allparams()
Get a list of all supported parameter names
.Pq as strings .
Expand Down Expand Up @@ -167,6 +184,10 @@ function returns a jail identifier integer and a table of jail parameters
with parameter name strings as keys and strings for values on success, or
.Dv nil
and an error message string if an error occurred.
.Pp
The
.Fn list
function returns an iterator over the list of running jails.
.Sh EXAMPLES
Set the hostname of jail
.Dq foo
Expand All @@ -193,6 +214,32 @@ if not jid then
end
print(res["host.hostname"])
.Ed
.Pp
Iterate over jails on the system:
.Bd -literal -offset indent
local jail = require('jail')

-- Recommended: just loop over it
for jparams in jail.list() do
print(jparams["jid"] .. " = " .. jparams["name"])
end

-- Request path and hostname, too
for jparams in jail.list({"path", "host.hostname"}) do
print(jparams["host.hostname"] .. " mounted at " .. jparams["path"])
end

-- Raw iteration protocol
local iter, jail_obj = jail.list()

-- Request the first params
local jparams = jail_obj:next()
while jparams do
print(jparams["jid"] .. " = " .. jparams["name"])
-- Subsequent calls may return nil
jparams = jail_obj:next()
end
.Ed
.Sh SEE ALSO
.Xr jail 2 ,
.Xr jail 3 ,
Expand Down
Loading

0 comments on commit 44175ec

Please sign in to comment.