jail(3lua): add jail.attach()/jail.remove() methods

These aren't a part of or use libjail(3), but rather are direct
syscalls.  Still, they seem like good additions, allowing us to attach
to already-running jails.

Reviewed by:	freqlabs
Differential Revision:	https://reviews.freebsd.org/D26927
This commit is contained in:
Kyle Evans 2020-10-23 12:52:31 -05:00
parent 6a7647eccd
commit a6499c56ab
2 changed files with 96 additions and 0 deletions

View file

@ -30,11 +30,13 @@
.Dt JAIL 3lua
.Os
.Sh NAME
.Nm attach ,
.Nm getid ,
.Nm getname ,
.Nm list ,
.Nm allparams ,
.Nm getparams ,
.Nm remove ,
.Nm setparams ,
.Nm CREATE ,
.Nm UPDATE ,
@ -48,11 +50,13 @@ local jail = require('jail')
.Ed
.Pp
.Bl -tag -width XXXX -compact
.It Dv ok, err = jail.attach(jid|name)
.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 ok, err = jail.remove(jid|name)
.It Dv jid, err = jail.setparams(jid|name, params, flags )
.It Dv jail.CREATE
.It Dv jail.UPDATE
@ -71,6 +75,11 @@ and
.Xr jail_set 2
system calls.
.Bl -tag -width XXXX
.It Dv ok, err = jail.attach(jid|name)
Attach to the given jail, identified by an integer
.Fa jid
or the
.Fa name .
.It Dv jid, err = jail.getid(name)
Get the jail identifier
.Pq jid
@ -114,6 +123,11 @@ See the list of flags below.
Only the
.Dv DYING
flag is valid to set.
.It Dv ok, err = jail.remove(jid|name)
Remove the given jail, identified by an integer
.Fa jid
or the
.Fa name .
.It Dv jid, err = jail.setparams(jid|name, params [, flags ] )
Set parameters for a given jail.
This is used to create, update, attach to, or destroy a jail.
@ -188,6 +202,14 @@ and an error message string if an error occurred.
The
.Fn list
function returns an iterator over the list of running jails.
.Pp
The
.Fn attach
and
.Fn remove
functions return true on success, or
.Dv nil
and an error message string if an error occurred.
.Sh EXAMPLES
Set the hostname of jail
.Dq foo

View file

@ -575,6 +575,68 @@ l_setparams(lua_State *L)
return (1);
}
static int
l_attach(lua_State *L)
{
int jid, type;
type = lua_type(L, 1);
luaL_argcheck(L, type == LUA_TSTRING || type == LUA_TNUMBER, 1,
"expected a jail name (string) or id (integer)");
if (lua_isstring(L, 1)) {
/* Resolve it to a jid. */
jid = jail_getid(lua_tostring(L, 1));
if (jid == -1) {
lua_pushnil(L);
lua_pushstring(L, jail_errmsg);
return (2);
}
} else {
jid = lua_tointeger(L, 1);
}
if (jail_attach(jid) == -1) {
lua_pushnil(L);
lua_pushstring(L, strerror(errno));
return (2);
}
lua_pushboolean(L, 1);
return (1);
}
static int
l_remove(lua_State *L)
{
int jid, type;
type = lua_type(L, 1);
luaL_argcheck(L, type == LUA_TSTRING || type == LUA_TNUMBER, 1,
"expected a jail name (string) or id (integer)");
if (lua_isstring(L, 1)) {
/* Resolve it to a jid. */
jid = jail_getid(lua_tostring(L, 1));
if (jid == -1) {
lua_pushnil(L);
lua_pushstring(L, jail_errmsg);
return (2);
}
} else {
jid = lua_tointeger(L, 1);
}
if (jail_remove(jid) == -1) {
lua_pushnil(L);
lua_pushstring(L, strerror(errno));
return (2);
}
lua_pushboolean(L, 1);
return (1);
}
static const struct luaL_Reg l_jail[] = {
/** Get id of a jail by name.
* @param name jail name (string)
@ -616,6 +678,18 @@ static const struct luaL_Reg l_jail[] = {
* close methods
*/
{"list", l_list},
/** Attach to a running jail.
* @param jail jail name (string) or id (integer)
* @return true (boolean)
* or nil, error (string) on error
*/
{"attach", l_attach},
/** Remove a running jail.
* @param jail jail name (string) or id (integer)
* @return true (boolean)
* or nil, error (string) on error
*/
{"remove", l_remove},
{NULL, NULL}
};