hopefully implement shutdown

This commit is contained in:
Izaya 2021-05-23 12:12:23 +10:00
parent 2d386f1276
commit 7ad9f21182
2 changed files with 18 additions and 3 deletions

View File

@ -13,6 +13,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h> #include <sys/time.h>
#include <linux/reboot.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
@ -151,7 +152,7 @@ static int l_fs_spaceUsed (lua_State *L) {
#ifndef _WIN32 #ifndef _WIN32
struct statvfs s; struct statvfs s;
if( statvfs(fname, &s) != -1 ) { 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 { } else {
lua_pushnumber(L, -1); lua_pushnumber(L, -1);
} }
@ -377,6 +378,18 @@ static int l_uptime (lua_State *L) { /* Return ms */
return 1; 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) { static int l_totalMemory (lua_State *L) {
#if defined(_WIN32) && (defined(__CYGWIN__) || defined(__CYGWIN32__)) #if defined(_WIN32) && (defined(__CYGWIN__) || defined(__CYGWIN32__))
MEMORYSTATUS status; MEMORYSTATUS status;
@ -492,6 +505,7 @@ void luanative_start(lua_State *L) {
{"towupper", l_towupper}, {"towupper", l_towupper},
{"beep", l_beep}, {"beep", l_beep},
{"uptime", l_uptime}, {"uptime", l_uptime},
{"shutdown", l_shutdown},
{"totalMemory", l_totalMemory}, {"totalMemory", l_totalMemory},
{"freeMemory", l_freeMemory}, {"freeMemory", l_freeMemory},
{"pull", l_pull}, {"pull", l_pull},

View File

@ -166,7 +166,7 @@ function api.totalMemory()
return native.totalMemory() return native.totalMemory()
end end
function api.shutdown() function api.shutdown(reboot)
--TODO: Longjmp to init somehow? --TODO: Longjmp to init somehow?
print("Running shutdown hooks") print("Running shutdown hooks")
for k, hook in ipairs(deadhooks) do for k, hook in ipairs(deadhooks) do
@ -178,7 +178,8 @@ function api.shutdown()
end end
print("Hooks executed: " .. #deadhooks) print("Hooks executed: " .. #deadhooks)
native.shutdown(reboot)
os.exit(0) os.exit(0)
end end
return computer return computer