2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2020, Ryan Moeller <freqlabs@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
44 int luaopen_jail(lua_State *);
52 name = luaL_checkstring(L, 1);
53 jid = jail_getid(name);
56 lua_pushstring(L, jail_errmsg);
59 lua_pushinteger(L, jid);
64 l_getname(lua_State *L)
69 jid = luaL_checkinteger(L, 1);
70 name = jail_getname(jid);
73 lua_pushstring(L, jail_errmsg);
76 lua_pushstring(L, name);
82 l_allparams(lua_State *L)
84 struct jailparam *params;
87 params_count = jailparam_all(¶ms);
88 if (params_count == -1) {
90 lua_pushstring(L, jail_errmsg);
94 for (int i = 0; i < params_count; ++i) {
95 lua_pushstring(L, params[i].jp_name);
96 lua_rawseti(L, -2, i + 1);
98 jailparam_free(params, params_count);
104 l_getparams(lua_State *L)
107 struct jailparam *params;
108 size_t params_count, skipped;
109 int flags, jid, type;
111 type = lua_type(L, 1);
112 luaL_argcheck(L, type == LUA_TSTRING || type == LUA_TNUMBER, 1,
113 "expected a jail name (string) or id (integer)");
114 luaL_checktype(L, 2, LUA_TTABLE);
115 params_count = 1 + lua_rawlen(L, 2);
116 flags = luaL_optinteger(L, 3, 0);
118 params = malloc(params_count * sizeof(struct jailparam));
120 return (luaL_error(L, "malloc: %s", strerror(errno)));
123 * Set the jail name or id param as determined by the first arg.
126 if (type == LUA_TSTRING) {
127 if (jailparam_init(¶ms[0], "name") == -1) {
129 return (luaL_error(L, "jailparam_init: %s",
132 name = lua_tostring(L, 1);
133 if (jailparam_import(¶ms[0], name) == -1) {
134 jailparam_free(params, 1);
136 return (luaL_error(L, "jailparam_import: %s",
139 } else /* type == LUA_TNUMBER */ {
140 if (jailparam_init(¶ms[0], "jid") == -1) {
142 return (luaL_error(L, "jailparam_init: %s",
145 jid = lua_tointeger(L, 1);
146 if (jailparam_import_raw(¶ms[0], &jid, sizeof(jid)) == -1) {
147 jailparam_free(params, 1);
149 return (luaL_error(L, "jailparam_import_raw: %s",
155 * Set the remaining param names being requested.
159 for (size_t i = 1; i < params_count; ++i) {
160 const char *param_name;
162 lua_rawgeti(L, -1, i);
163 param_name = lua_tostring(L, -1);
164 if (param_name == NULL) {
165 jailparam_free(params, i - skipped);
167 return (luaL_argerror(L, 2,
168 "param names must be strings"));
171 /* Skip name or jid, whichever was given. */
172 if (type == LUA_TSTRING) {
173 if (strcmp(param_name, "name") == 0) {
177 } else /* type == LUA_TNUMBER */ {
178 if (strcmp(param_name, "jid") == 0) {
183 if (jailparam_init(¶ms[i - skipped], param_name) == -1) {
184 jailparam_free(params, i - skipped);
186 return (luaL_error(L, "jailparam_init: %s",
190 params_count -= skipped;
193 * Get the values and convert to a table.
196 jid = jailparam_get(params, params_count, flags);
198 jailparam_free(params, params_count);
201 lua_pushstring(L, jail_errmsg);
204 lua_pushinteger(L, jid);
207 for (size_t i = 0; i < params_count; ++i) {
210 value = jailparam_export(¶ms[i]);
211 lua_pushstring(L, value);
213 lua_setfield(L, -2, params[i].jp_name);
216 jailparam_free(params, params_count);
223 l_setparams(lua_State *L)
226 struct jailparam *params;
228 int flags, jid, type;
230 type = lua_type(L, 1);
231 luaL_argcheck(L, type == LUA_TSTRING || type == LUA_TNUMBER, 1,
232 "expected a jail name (string) or id (integer)");
233 luaL_checktype(L, 2, LUA_TTABLE);
236 for (params_count = 1; lua_next(L, 2) != 0; ++params_count)
239 flags = luaL_optinteger(L, 3, 0);
241 params = malloc(params_count * sizeof(struct jailparam));
243 return (luaL_error(L, "malloc: %s", strerror(errno)));
246 * Set the jail name or id param as determined by the first arg.
249 if (type == LUA_TSTRING) {
250 if (jailparam_init(¶ms[0], "name") == -1) {
252 return (luaL_error(L, "jailparam_init: %s",
255 name = lua_tostring(L, 1);
256 if (jailparam_import(¶ms[0], name) == -1) {
257 jailparam_free(params, 1);
259 return (luaL_error(L, "jailparam_import: %s",
262 } else /* type == LUA_TNUMBER */ {
263 if (jailparam_init(¶ms[0], "jid") == -1) {
265 return (luaL_error(L, "jailparam_init: %s",
268 jid = lua_tointeger(L, 1);
269 if (jailparam_import_raw(¶ms[0], &jid, sizeof(jid)) == -1) {
270 jailparam_free(params, 1);
272 return (luaL_error(L, "jailparam_import_raw: %s",
278 * Set the rest of the provided params.
282 for (size_t i = 1; i < params_count && lua_next(L, 2) != 0; ++i) {
285 name = lua_tostring(L, -2);
287 jailparam_free(params, i);
289 return (luaL_argerror(L, 2,
290 "param names must be strings"));
292 if (jailparam_init(¶ms[i], name) == -1) {
293 jailparam_free(params, i);
295 return (luaL_error(L, "jailparam_init: %s",
299 value = lua_tostring(L, -1);
301 jailparam_free(params, i + 1);
303 return (luaL_argerror(L, 2,
304 "param values must be strings"));
306 if (jailparam_import(¶ms[i], value) == -1) {
307 jailparam_free(params, i + 1);
309 return (luaL_error(L, "jailparam_import: %s",
317 * Attempt to set the params.
320 jid = jailparam_set(params, params_count, flags);
322 jailparam_free(params, params_count);
325 lua_pushstring(L, jail_errmsg);
328 lua_pushinteger(L, jid);
330 jailparam_free(params, params_count);
335 static const struct luaL_Reg l_jail[] = {
336 /** Get id of a jail by name.
337 * @param name jail name (string)
338 * @return jail id (integer)
339 * or nil, error (string) on error
342 /** Get name of a jail by id.
343 * @param jid jail id (integer)
344 * @return jail name (string)
345 * or nil, error (string) on error
347 {"getname", l_getname},
348 /** Get a list of all known jail parameters.
349 * @return list of jail parameter names (table of strings)
350 * or nil, error (string) on error
352 {"allparams", l_allparams},
353 /** Get the listed params for a given jail.
354 * @param jail jail name (string) or id (integer)
355 * @param params list of parameter names (table of strings)
356 * @param flags optional flags (integer)
357 * @return jid (integer), params (table of [string] = string)
358 * or nil, error (string) on error
360 {"getparams", l_getparams},
361 /** Set params for a given jail.
362 * @param jail jail name (string) or id (integer)
363 * @param params params and values (table of [string] = string)
364 * @param flags optional flags (integer)
365 * @return jid (integer)
366 * or nil, error (string) on error
368 {"setparams", l_setparams},
373 luaopen_jail(lua_State *L)
377 luaL_setfuncs(L, l_jail, 0);
379 lua_pushinteger(L, JAIL_CREATE);
380 lua_setfield(L, -2, "CREATE");
381 lua_pushinteger(L, JAIL_UPDATE);
382 lua_setfield(L, -2, "UPDATE");
383 lua_pushinteger(L, JAIL_ATTACH);
384 lua_setfield(L, -2, "ATTACH");
385 lua_pushinteger(L, JAIL_DYING);
386 lua_setfield(L, -2, "DYING");