platform: Add service error wrapper for macOS system extension

This commit is contained in:
世界 2024-01-22 18:27:06 +08:00
parent 94f76d6671
commit a8ee41715a
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
4 changed files with 48 additions and 14 deletions

View file

@ -93,13 +93,11 @@ func (s *CommandServer) listenUNIX() error {
if err != nil { if err != nil {
return E.Cause(err, "listen ", sockPath) return E.Cause(err, "listen ", sockPath)
} }
if sUserID > 0 { err = os.Chown(sockPath, sUserID, sGroupID)
err = os.Chown(sockPath, sUserID, sGroupID) if err != nil {
if err != nil { listener.Close()
listener.Close() os.Remove(sockPath)
os.Remove(sockPath) return E.Cause(err, "chown")
return E.Cause(err, "chown")
}
} }
s.listener = listener s.listener = listener
go s.loopConnection(listener) go s.loopConnection(listener)

View file

@ -20,13 +20,11 @@ func RedirectStderr(path string) error {
return err return err
} }
if runtime.GOOS != "android" { if runtime.GOOS != "android" {
if sUserID > 0 { err = outputFile.Chown(sUserID, sGroupID)
err = outputFile.Chown(sUserID, sGroupID) if err != nil {
if err != nil { outputFile.Close()
outputFile.Close() os.Remove(outputFile.Name())
os.Remove(outputFile.Name()) return err
return err
}
} }
} }
err = unix.Dup2(int(outputFile.Fd()), int(os.Stderr.Fd())) err = unix.Dup2(int(outputFile.Fd()), int(os.Stderr.Fd()))

View file

@ -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()
}

View file

@ -25,6 +25,8 @@ func Setup(basePath string, workingPath string, tempPath string, isTVOS bool) {
sUserID = os.Getuid() sUserID = os.Getuid()
sGroupID = os.Getgid() sGroupID = os.Getgid()
sTVOS = isTVOS sTVOS = isTVOS
os.MkdirAll(sWorkingPath, 0o777)
os.MkdirAll(sTempPath, 0o777)
} }
func SetupWithUsername(basePath string, workingPath string, tempPath string, username string) error { 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) sUserID, _ = strconv.Atoi(sUser.Uid)
sGroupID, _ = strconv.Atoi(sUser.Gid) 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 return nil
} }