1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2024, Baptiste Daroussin <bapt@FreeBSD.org> 5 */ 6 7 #include <sys/param.h> 8 #include <sys/linker.h> 9 10 #include <errno.h> 11 #include <stdio.h> 12 #include <string.h> 13 14 #include <lua.h> 15 #include <lualib.h> 16 #include <lauxlib.h> 17 18 #include "bootstrap.h" 19 20 int luaopen_freebsd_sys_linker(lua_State *L); 21 22 static int 23 lua_kldload(lua_State *L) 24 { 25 const char *name; 26 int ret; 27 28 name = luaL_checkstring(L, 1); 29 ret = kldload(name); 30 if (ret == -1) { 31 lua_pushnil(L); 32 lua_pushstring(L, strerror(errno)); 33 lua_pushinteger(L, errno); 34 return (3); 35 } 36 lua_pushinteger(L, ret); 37 return (1); 38 } 39 40 static int 41 lua_kldunload(lua_State *L) 42 { 43 const char *name; 44 int ret, fileid; 45 46 if (lua_isinteger(L, 1)) { 47 fileid = lua_tointeger(L, 1); 48 } else { 49 name = luaL_checkstring(L, 1); 50 fileid = kldfind(name); 51 } 52 if (fileid == -1) { 53 lua_pushnil(L); 54 lua_pushstring(L, strerror(errno)); 55 lua_pushinteger(L, errno); 56 return (3); 57 } 58 ret = kldunload(fileid); 59 lua_pushinteger(L, ret); 60 if (ret == -1) { 61 lua_pushnil(L); 62 lua_pushstring(L, strerror(errno)); 63 lua_pushinteger(L, errno); 64 return (3); 65 } 66 lua_pushinteger(L, 0); 67 return (1); 68 } 69 70 #define REG_SIMPLE(n) { #n, lua_ ## n } 71 static const struct luaL_Reg freebsd_sys_linker[] = { 72 REG_SIMPLE(kldload), 73 REG_SIMPLE(kldunload), 74 { NULL, NULL }, 75 }; 76 #undef REG_SIMPLE 77 78 int 79 luaopen_freebsd_sys_linker(lua_State *L) 80 { 81 luaL_newlib(L, freebsd_sys_linker); 82 83 return (1); 84 } 85 86 FLUA_MODULE_NAMED(freebsd_sys_linker, "freebsd.sys.linker"); 87