authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2022-10-27 22:25:48+11:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-10-27 20:39:35-04:00
logc0c8ee5ae9a3ddd78d94166caaa75d88d0fe8c15
treeac39dcf923803766feaeff35e57113504db96b31
parent0b99e5e4c42f3c9711f3a119711d584fa32a92a1

Add reboot syscall

Only linux for now

2 files changed, 107 insertions(+), 0 deletions(-)

lib/std/os.zig+50
...@@ -366,6 +366,56 @@ pub fn fchown(fd: fd_t, owner: ?uid_t, group: ?gid_t) FChownError!void {...@@ -366,6 +366,56 @@ pub fn fchown(fd: fd_t, owner: ?uid_t, group: ?gid_t) FChownError!void {
366 }366 }
367}367}
368368
369pub const RebootError = error{
370 PermissionDenied,
371} || UnexpectedError;
372
373pub const RebootCommand = switch (builtin.os.tag) {
374 .linux => union(linux.LINUX_REBOOT.CMD) {
375 RESTART: void,
376 HALT: void,
377 CAD_ON: void,
378 CAD_OFF: void,
379 POWER_OFF: void,
380 RESTART2: [*:0]const u8,
381 SW_SUSPEND: void,
382 KEXEC: void,
383 },
384 else => @compileError("Unsupported OS"),
385};
386
387pub fn reboot(cmd: RebootCommand) RebootError!void {
388 switch (builtin.os.tag) {
389 .linux => {
390 switch (system.getErrno(linux.reboot(
391 .MAGIC1,
392 .MAGIC2,
393 @as(linux.LINUX_REBOOT.CMD, cmd),
394 switch (cmd) {
395 .RESTART2 => |s| s,
396 else => null,
397 },
398 ))) {
399 .SUCCESS => {},
400 .PERM => return error.PermissionDenied,
401 else => |err| return std.os.unexpectedErrno(err),
402 }
403 switch (cmd) {
404 .CAD_OFF => {},
405 .CAD_ON => {},
406 .SW_SUSPEND => {},
407
408 .HALT => unreachable,
409 .KEXEC => unreachable,
410 .POWER_OFF => unreachable,
411 .RESTART => unreachable,
412 .RESTART2 => unreachable,
413 }
414 },
415 else => @compileError("Unsupported OS"),
416 }
417}
418
369pub const GetRandomError = OpenError;419pub const GetRandomError = OpenError;
370420
371/// Obtain a series of random bytes. These bytes can be used to seed user-space421/// Obtain a series of random bytes. These bytes can be used to seed user-space
lib/std/os/linux.zig+57
...@@ -804,6 +804,63 @@ pub fn exit_group(status: i32) noreturn {...@@ -804,6 +804,63 @@ pub fn exit_group(status: i32) noreturn {
804 unreachable;804 unreachable;
805}805}
806806
807/// flags for the `reboot' system call.
808pub const LINUX_REBOOT = struct {
809 /// First magic value required to use _reboot() system call.
810 pub const MAGIC1 = enum(u32) {
811 MAGIC1 = 0xfee1dead,
812 _,
813 };
814
815 /// Second magic value required to use _reboot() system call.
816 pub const MAGIC2 = enum(u32) {
817 MAGIC2 = 672274793,
818 MAGIC2A = 85072278,
819 MAGIC2B = 369367448,
820 MAGIC2C = 537993216,
821 _,
822 };
823
824 /// Commands accepted by the _reboot() system call.
825 pub const CMD = enum(u32) {
826 /// Restart system using default command and mode.
827 RESTART = 0x01234567,
828
829 /// Stop OS and give system control to ROM monitor, if any.
830 HALT = 0xCDEF0123,
831
832 /// Ctrl-Alt-Del sequence causes RESTART command.
833 CAD_ON = 0x89ABCDEF,
834
835 /// Ctrl-Alt-Del sequence sends SIGINT to init task.
836 CAD_OFF = 0x00000000,
837
838 /// Stop OS and remove all power from system, if possible.
839 POWER_OFF = 0x4321FEDC,
840
841 /// Restart system using given command string.
842 RESTART2 = 0xA1B2C3D4,
843
844 /// Suspend system using software suspend if compiled in.
845 SW_SUSPEND = 0xD000FCE2,
846
847 /// Restart system using a previously loaded Linux kernel
848 KEXEC = 0x45584543,
849
850 _,
851 };
852};
853
854pub fn reboot(magic: LINUX_REBOOT.MAGIC1, magic2: LINUX_REBOOT.MAGIC2, cmd: LINUX_REBOOT.CMD, arg: ?*const anyopaque) usize {
855 return std.os.linux.syscall4(
856 .reboot,
857 @enumToInt(magic),
858 @enumToInt(magic2),
859 @enumToInt(cmd),
860 @ptrToInt(arg),
861 );
862}
863
807pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize {864pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize {
808 return syscall3(.getrandom, @ptrToInt(buf), count, flags);865 return syscall3(.getrandom, @ptrToInt(buf), count, flags);
809}866}