From 7ad9f21182afb16802397c955c5cb51052081d73 Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Sun, 23 May 2021 12:12:23 +1000 Subject: [PATCH] hopefully implement shutdown --- src/c/lnative.c | 16 +++++++++++++++- src/lua/core/computer.lua | 5 +++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/c/lnative.c b/src/c/lnative.c index ee4e7ca..875815b 100644 --- a/src/c/lnative.c +++ b/src/c/lnative.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -151,7 +152,7 @@ static int l_fs_spaceUsed (lua_State *L) { #ifndef _WIN32 struct statvfs s; if( statvfs(fname, &s) != -1 ) { - lua_pushnumber(L, s.f_bsize * s.f_bfree); + lua_pushnumber(L, s.f_bsize * (s.f_blocks - s.f_bfree)); } else { lua_pushnumber(L, -1); } @@ -377,6 +378,18 @@ static int l_uptime (lua_State *L) { /* Return ms */ return 1; } +static int l_shutdown(lua_State *L) { + int shutdownmode = lua_toboolean(L, 1); + sync(); + printf("%i", shutdownmode); + int rebootcmd = LINUX_REBOOT_CMD_POWER_OFF; + if (shutdownmode == 1) { + rebootcmd = LINUX_REBOOT_CMD_RESTART; + } + reboot(rebootcmd); + exit(0); +} + static int l_totalMemory (lua_State *L) { #if defined(_WIN32) && (defined(__CYGWIN__) || defined(__CYGWIN32__)) MEMORYSTATUS status; @@ -492,6 +505,7 @@ void luanative_start(lua_State *L) { {"towupper", l_towupper}, {"beep", l_beep}, {"uptime", l_uptime}, + {"shutdown", l_shutdown}, {"totalMemory", l_totalMemory}, {"freeMemory", l_freeMemory}, {"pull", l_pull}, diff --git a/src/lua/core/computer.lua b/src/lua/core/computer.lua index 99554bd..4fa33e7 100644 --- a/src/lua/core/computer.lua +++ b/src/lua/core/computer.lua @@ -166,7 +166,7 @@ function api.totalMemory() return native.totalMemory() end -function api.shutdown() +function api.shutdown(reboot) --TODO: Longjmp to init somehow? print("Running shutdown hooks") for k, hook in ipairs(deadhooks) do @@ -178,7 +178,8 @@ function api.shutdown() end print("Hooks executed: " .. #deadhooks) + native.shutdown(reboot) os.exit(0) end -return computer \ No newline at end of file +return computer