Lightweight disk identity, mount, and S.M.A.R.T. extraction toolkit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

186 lines
6.3 KiB

/*-
* Copyright (c) 2026 Sergey Kiselev <root@dc365.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <string.h>
#include "../../include/bsdiskinfo.h"
/*
* Compatibility layer for safe Lua library registration across versions.
* Supports Lua 5.1, LuaJIT, and modern Lua 5.2+ environments seamlessly.
*/
#if LUA_VERSION_NUM == 501
#define luaL_newlib(L, l) (lua_newtable(L), luaL_register(L, NULL, l))
#endif
/*
* Lua binding: bsdiskinfo.exists("ada0")
* Returns boolean true if character device node exists, false otherwise.
*/
static int l_diskinfo_exists(lua_State *L) {
const char *disk_name = luaL_checkstring(L, 1);
lua_pushboolean(L, diskinfo_exists(disk_name));
return 1;
}
/*
* Lua binding: bsdiskinfo.get_properties("ada0")
* Returns associative table containing raw hardware identification properties or nil.
*/
static int l_diskinfo_get_properties(lua_State *L) {
const char *disk_name = luaL_checkstring(L, 1);
struct disk_properties props;
if (diskinfo_get_properties(disk_name, &props) != 0) {
lua_pushnil(L);
return 1;
}
lua_newtable(L);
lua_pushstring(L, props.model); lua_setfield(L, -2, "model");
lua_pushstring(L, props.serial); lua_setfield(L, -2, "serial");
lua_pushstring(L, props.fw_version); lua_setfield(L, -2, "fw_version");
lua_pushstring(L, props.wwn); lua_setfield(L, -2, "wwn");
lua_pushstring(L, props.type_str); lua_setfield(L, -2, "type");
/* Export raw media capacities as numbers for granular high-level math formatting */
lua_pushnumber(L, (lua_Number)props.mediasize);
lua_setfield(L, -2, "mediasize");
lua_pushinteger(L, props.logical_sector_size);
lua_setfield(L, -2, "logical_sector_size");
lua_pushinteger(L, props.physical_sector_size);
lua_setfield(L, -2, "physical_sector_size");
if (props.rotation_rate >= 0) {
lua_pushinteger(L, props.rotation_rate);
lua_setfield(L, -2, "rotation_rate");
}
if (props.smart_supported >= 0) {
lua_pushboolean(L, props.smart_supported == 1);
lua_setfield(L, -2, "smart_supported");
lua_pushboolean(L, props.smart_enabled == 1);
lua_setfield(L, -2, "smart_enabled");
}
return 1;
}
/*
* Lua binding: bsdiskinfo.get_mounts("ada0")
* Returns a sequentially indexed array table containing active partitions and mount targets.
*/
static int l_diskinfo_get_mounts(lua_State *L) {
const char *disk_name = luaL_checkstring(L, 1);
struct disk_mount mounts[MAX_MOUNTS];
int count = diskinfo_get_mounts(disk_name, mounts, MAX_MOUNTS);
if (count < 0) {
lua_pushnil(L);
return 1;
}
lua_newtable(L); /* Root array table */
for (int i = 0; i < count; i++) {
lua_newtable(L); /* Individual record sub-table */
lua_pushstring(L, mounts[i].device); lua_setfield(L, -2, "device");
lua_pushstring(L, mounts[i].path); lua_setfield(L, -2, "path");
lua_pushstring(L, mounts[i].fstype); lua_setfield(L, -2, "fstype");
lua_rawseti(L, -2, i + 1); /* Inject into sequential array slot */
}
return 1;
}
/*
* Lua binding: bsdiskinfo.get_smart_counters("ada0")
* Returns a sequentially indexed array table storing extracted raw S.M.A.R.T. structural entries.
*/
static int l_diskinfo_get_smart_counters(lua_State *L) {
const char *disk_name = luaL_checkstring(L, 1);
struct smart_counter counters[30];
int count = diskinfo_get_smart_counters(disk_name, counters, 30);
if (count < 0) {
lua_pushnil(L);
return 1;
}
lua_newtable(L); /* Root array table */
for (int i = 0; i < count; i++) {
lua_newtable(L); /* Individual attribute sub-table */
lua_pushinteger(L, counters[i].id);
lua_setfield(L, -2, "id");
lua_pushinteger(L, counters[i].value);
lua_setfield(L, -2, "value");
lua_pushinteger(L, counters[i].worst);
lua_setfield(L, -2, "worst");
/* Export raw values as Lua numbers to handle massive 48-bit storage tracking values safely */
lua_pushnumber(L, (lua_Number)counters[i].raw_val);
lua_setfield(L, -2, "raw_val");
lua_rawseti(L, -2, i + 1);
}
return 1;
}
/*
* Exported library methods routing dictionary map block compliant with Lua module initialization standards
*/
static const struct luaL_Reg bsdiskinfo_methods[] = {
{ "exists", l_diskinfo_exists },
{ "get_properties", l_diskinfo_get_properties },
{ "get_mounts", l_diskinfo_get_mounts },
{ "get_smart_counters", l_diskinfo_get_smart_counters },
{ NULL, NULL }
};
/*
* Official library runtime shared object entry-point.
* Executed automatically by kernel dynamic loaders when require("bsdiskinfo") fires inside user space scripts.
*/
int luaopen_bsdiskinfo(lua_State *L) {
#if LUA_VERSION_NUM >= 502
luaL_newlib(L, bsdiskinfo_methods);
#else
lua_newtable(L);
luaL_register(L, NULL, bsdiskinfo_methods);
#endif
return 1;
}