From nobody Wed Oct 06 07:13:39 2021 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 83FD812D9918; Wed, 6 Oct 2021 07:13:40 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4HPQfW6Xjrz3mLt; Wed, 6 Oct 2021 07:13:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id BD4851BC7B; Wed, 6 Oct 2021 07:13:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 1967Ddi4038321; Wed, 6 Oct 2021 07:13:39 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1967DdKa038320; Wed, 6 Oct 2021 07:13:39 GMT (envelope-from git) Date: Wed, 6 Oct 2021 07:13:39 GMT Message-Id: <202110060713.1967DdKa038320@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kyle Evans Subject: git: 188c088fe351 - stable/13 - jail(3lua): add jail.attach()/jail.remove() methods List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 188c088fe3510eea080f69a8db447fb9d21b2b73 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=188c088fe3510eea080f69a8db447fb9d21b2b73 commit 188c088fe3510eea080f69a8db447fb9d21b2b73 Author: Kyle Evans AuthorDate: 2020-10-23 17:52:31 +0000 Commit: Kyle Evans CommitDate: 2021-10-06 07:13:27 +0000 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. (cherry picked from commit a6499c56ab6ca54f01dca44b7e34a0fc6a680e90) --- lib/flua/libjail/jail.3lua | 22 ++++++++++++++ lib/flua/libjail/lua_jail.c | 74 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/lib/flua/libjail/jail.3lua b/lib/flua/libjail/jail.3lua index aa1e0ec49616..a0cb7ae1381e 100644 --- a/lib/flua/libjail/jail.3lua +++ b/lib/flua/libjail/jail.3lua @@ -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 diff --git a/lib/flua/libjail/lua_jail.c b/lib/flua/libjail/lua_jail.c index 7bb0e13cceea..025694bf1181 100644 --- a/lib/flua/libjail/lua_jail.c +++ b/lib/flua/libjail/lua_jail.c @@ -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} };