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