💾 Archived View for tilde.club › ~kitzman › kitztech › resources › pls_reboot.v captured on 2023-07-10 at 14:24:47.

View Raw

More Information

⬅️ Previous capture (2022-03-01)

-=-=-=-=-=-=-

module main

import time
import os

#include "unistd.h"
#include "sys/syscall.h"
#include "linux/reboot.h"
#include "errno.h"

fn C.syscall(arg0 i64, arg1 i64, arg2 i64, arg3 i64, arg4 i64) i64

const (
	program_usage = '
./pls_reboot <mode>

mode can be any of: recovery, bootloader, none
'

	possible_modes = ['bootloader', 'recovery', 'none']
)

fn sys_reboot(mode string) i64 {
	mut mode_ptr := voidptr(''.str)

	if mode != 'none' {
		mode_ptr = voidptr(mode.str)
	}

	return C.syscall(C.SYS_reboot, C.LINUX_REBOOT_MAGIC1, C.LINUX_REBOOT_MAGIC2, C.LINUX_REBOOT_CMD_RESTART2,
		mode_ptr)
}

fn main() {
	// command initialization

	if os.args.len != 2 {
		println(program_usage)
		exit(1)
	}

	reboot_mode := os.args[1]

	if !possible_modes.contains(reboot_mode) {
		println('could not find mode')
		println(program_usage)
		exit(1)
	}

	// starting reboot

	println('rebooting in:')

	for i in 0 .. 5 {
		time.sleep(1 * time.second)
		print('${5 - i}...')
	}

	println('\nrebooting!')

	// reboot and handling

	rc := sys_reboot(reboot_mode)

	if rc != 0 {
		print('error: ')
		println(c_error_number_str(C.errno))
		exit(1)
	}

	exit(0)
}