From a8ee41715a6973497279b83715ff79276661f300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Mon, 22 Jan 2024 18:27:06 +0800 Subject: [PATCH] platform: Add service error wrapper for macOS system extension --- experimental/libbox/command_server.go | 12 +++++----- experimental/libbox/log.go | 12 +++++----- experimental/libbox/service_error.go | 32 +++++++++++++++++++++++++++ experimental/libbox/setup.go | 6 +++++ 4 files changed, 48 insertions(+), 14 deletions(-) create mode 100644 experimental/libbox/service_error.go diff --git a/experimental/libbox/command_server.go b/experimental/libbox/command_server.go index f5aabb4a..2bd99b8c 100644 --- a/experimental/libbox/command_server.go +++ b/experimental/libbox/command_server.go @@ -93,13 +93,11 @@ func (s *CommandServer) listenUNIX() error { if err != nil { return E.Cause(err, "listen ", sockPath) } - if sUserID > 0 { - err = os.Chown(sockPath, sUserID, sGroupID) - if err != nil { - listener.Close() - os.Remove(sockPath) - return E.Cause(err, "chown") - } + err = os.Chown(sockPath, sUserID, sGroupID) + if err != nil { + listener.Close() + os.Remove(sockPath) + return E.Cause(err, "chown") } s.listener = listener go s.loopConnection(listener) diff --git a/experimental/libbox/log.go b/experimental/libbox/log.go index 3e442b92..d9c4c712 100644 --- a/experimental/libbox/log.go +++ b/experimental/libbox/log.go @@ -20,13 +20,11 @@ func RedirectStderr(path string) error { return err } if runtime.GOOS != "android" { - if sUserID > 0 { - err = outputFile.Chown(sUserID, sGroupID) - if err != nil { - outputFile.Close() - os.Remove(outputFile.Name()) - return err - } + err = outputFile.Chown(sUserID, sGroupID) + if err != nil { + outputFile.Close() + os.Remove(outputFile.Name()) + return err } } err = unix.Dup2(int(outputFile.Fd()), int(os.Stderr.Fd())) diff --git a/experimental/libbox/service_error.go b/experimental/libbox/service_error.go new file mode 100644 index 00000000..2f22574a --- /dev/null +++ b/experimental/libbox/service_error.go @@ -0,0 +1,32 @@ +package libbox + +import ( + "os" + "path/filepath" +) + +func serviceErrorPath() string { + return filepath.Join(sWorkingPath, "network_extension_error") +} + +func ClearServiceError() { + os.Remove(serviceErrorPath()) +} + +func ReadServiceError() (string, error) { + data, err := os.ReadFile(serviceErrorPath()) + if err == nil { + os.Remove(serviceErrorPath()) + } + return string(data), err +} + +func WriteServiceError(message string) error { + errorFile, err := os.Create(serviceErrorPath()) + if err != nil { + return err + } + errorFile.WriteString(message) + errorFile.Chown(sUserID, sGroupID) + return errorFile.Close() +} diff --git a/experimental/libbox/setup.go b/experimental/libbox/setup.go index 8204a968..a4514dfe 100644 --- a/experimental/libbox/setup.go +++ b/experimental/libbox/setup.go @@ -25,6 +25,8 @@ func Setup(basePath string, workingPath string, tempPath string, isTVOS bool) { sUserID = os.Getuid() sGroupID = os.Getgid() sTVOS = isTVOS + os.MkdirAll(sWorkingPath, 0o777) + os.MkdirAll(sTempPath, 0o777) } func SetupWithUsername(basePath string, workingPath string, tempPath string, username string) error { @@ -37,6 +39,10 @@ func SetupWithUsername(basePath string, workingPath string, tempPath string, use } sUserID, _ = strconv.Atoi(sUser.Uid) sGroupID, _ = strconv.Atoi(sUser.Gid) + os.MkdirAll(sWorkingPath, 0o777) + os.MkdirAll(sTempPath, 0o777) + os.Chown(sWorkingPath, sUserID, sGroupID) + os.Chown(sTempPath, sUserID, sGroupID) return nil }