aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/sys/hsu.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/sys/hsu.go')
-rw-r--r--internal/sys/hsu.go23
1 files changed, 23 insertions, 0 deletions
diff --git a/internal/sys/hsu.go b/internal/sys/hsu.go
index 84bb0731..2be12d1e 100644
--- a/internal/sys/hsu.go
+++ b/internal/sys/hsu.go
@@ -3,6 +3,7 @@ package sys
import (
"errors"
"fmt"
+ "log"
"os"
"os/exec"
"strconv"
@@ -11,6 +12,7 @@ import (
"hakurei.app/container"
"hakurei.app/hst"
"hakurei.app/internal"
+ "hakurei.app/internal/hlog"
)
// Hsu caches responses from cmd/hsu.
@@ -79,3 +81,24 @@ func (h *Hsu) Uid(identity int) (int, error) {
}
return u.uid, u.err
}
+
+// MustUid calls [State.Uid] and terminates on error.
+func MustUid(s State, identity int) int {
+ uid, err := s.Uid(identity)
+ if err == nil {
+ return uid
+ }
+
+ const fallback = "cannot obtain uid from setuid wrapper:"
+ if errors.Is(err, ErrHsuAccess) {
+ hlog.Verbose("*"+fallback, err)
+ os.Exit(1)
+ return -0xdeadbeef
+ } else if m, ok := container.GetErrorMessage(err); ok {
+ log.Fatal(m)
+ return -0xdeadbeef
+ } else {
+ log.Fatalln(fallback, err)
+ return -0xdeadbeef
+ }
+}