From b6ed20ff87b78779c850f483737c5b589da9f764 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Mon, 2 Oct 2023 23:08:55 -0700 Subject: [PATCH] initial import --- cmd/sysreboot/main.go | 7 +++++++ go.mod | 3 +++ sys.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 cmd/sysreboot/main.go create mode 100644 go.mod create mode 100644 sys.go diff --git a/cmd/sysreboot/main.go b/cmd/sysreboot/main.go new file mode 100644 index 0000000..13e7e12 --- /dev/null +++ b/cmd/sysreboot/main.go @@ -0,0 +1,7 @@ +package main + +import "git.wntrmute.dev/kyle/gokpkg" + +func main() { + gokpkg.Reboot() +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c33c8e6 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.wntrmute.dev/kyle/gokpkg + +go 1.20 diff --git a/sys.go b/sys.go new file mode 100644 index 0000000..0021a7f --- /dev/null +++ b/sys.go @@ -0,0 +1,31 @@ +package gokpkg + +import ( + "log" + "os" + "os/exec" + "syscall" +) + +func MustDropPrivileges() { + if os.Getenv("NTP_PRIVILEGES_DROPPED") == "1" { + return + } + + cmd := exec.Command(os.Args[0]) + cmd.Env = append(os.Environ(), "NTP_PRIVILEGES_DROPPED=1") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.SysProcAttr = &syscall.SysProcAttr{ + Credential: &syscall.Credential{ + Uid: 65534, + Gid: 65534, + }, + } + log.Fatal(cmd.Run()) +} + +func Reboot() { + cmd := exec.Command("reboot") + log.Fatal(cmd.Run()) +}