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 int luaopen_freebsd_sys_linker(lua_State *L); 19 20 static int 21 lua_kldload(lua_State *L) 22 { 23 const char *name; 24 int ret; 25 26 name = luaL_checkstring(L, 1); 27 ret = kldload(name); 28 if (ret == -1) { 29 lua_pushnil(L); 30 lua_pushstring(L, strerror(errno)); 31 lua_pushinteger(L, errno); 32 return (3); 33 } 34 lua_pushinteger(L, ret); 35 return (1); 36 } 37 38 static int 39 lua_kldunload(lua_State *L) 40 { 41 const char *name; 42 int ret, fileid; 43 44 if (lua_isinteger(L, 1)) { 45 fileid = lua_tointeger(L, 1); 46 } else { 47 name = luaL_checkstring(L, 1); 48 fileid = kldfind(name); 49 } 50 if (fileid == -1) { 51 lua_pushnil(L); 52 lua_pushstring(L, strerror(errno)); 53 lua_pushinteger(L, errno); 54 return (3); 55 } 56 ret = kldunload(fileid); 57 lua_pushinteger(L, ret); 58 if (ret == -1) { 59 lua_pushnil(L); 60 lua_pushstring(L, strerror(errno)); 61 lua_pushinteger(L, errno); 62 return (3); 63 } 64 lua_pushinteger(L, 0); 65 return (1); 66 } 67 68 #define REG_SIMPLE(n) { #n, lua_ ## n } 69 static const struct luaL_Reg freebsd_sys_linker[] = { 70 REG_SIMPLE(kldload), 71 REG_SIMPLE(kldunload), 72 { NULL, NULL }, 73 }; 74 #undef REG_SIMPLE 75 76 int 77 luaopen_freebsd_sys_linker(lua_State *L) 78 { 79 luaL_newlib(L, freebsd_sys_linker); 80 81 return (1); 82 } 83