Skip to content

Commit

Permalink
set subprocess group ID for common.External
Browse files Browse the repository at this point in the history
  • Loading branch information
whalechoi committed May 27, 2024
1 parent dd28134 commit 7f7e6ed
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 32 deletions.
4 changes: 1 addition & 3 deletions main/commands/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ func startService() error {
return err
}
}
if err := service.SetUidGid("0", common.CoreGid); err != nil {
return err
}
service.SetUidGid("0", common.CoreGid)
ignoreSignals()
service.Start()
if service.Err() != nil {
Expand Down
2 changes: 1 addition & 1 deletion main/commands/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (this *UpdateCommand) Execute(args []string) error {
log.HandleDebug("CurrentGid: " + strconv.Itoa(os.Getgid()))
if *builds.BypassSelf && strconv.Itoa(os.Getgid()) != common.CoreGid {
self := common.NewExternal(0, os.Stdout, os.Stderr, os.Args[0], os.Args[1:]...)
_ = self.SetUidGid("0", common.CoreGid)
self.SetUidGid("0", common.CoreGid)
log.HandleDebug("will exec update command in new xrayhelper process, waiting")
self.Run()
var exitError *exec.ExitError
Expand Down
13 changes: 12 additions & 1 deletion main/common/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"errors"
"io"
"os/exec"
"strconv"
"syscall"
"time"
)

type External interface {
Err() error
SetUidGid(uid string, gid string) error
SetUidGid(uid string, gid string)
AppendEnv(env string)
Run()
Start()
Expand Down Expand Up @@ -44,9 +46,18 @@ func NewExternal(timeout time.Duration, out io.Writer, err io.Writer, name strin
ex.cmd.Stdout = out
ex.cmd.Stderr = err
}
ex.cmd.SysProcAttr = &syscall.SysProcAttr{}
ex.cmd.SysProcAttr.Setpgid = true
return &ex
}

// SetUidGid implement in linux
func (this *external) SetUidGid(uid string, gid string) {
uidInt, _ := strconv.Atoi(uid)
gidInt, _ := strconv.Atoi(gid)
this.cmd.SysProcAttr.Credential = &syscall.Credential{Uid: uint32(uidInt), Gid: uint32(gidInt)}
}

// AppendEnv add env variable, eg: JAVA_HOME=/usr/local/java/
func (this *external) AppendEnv(env string) {
this.cmd.Env = append(this.cmd.Env, env)
Expand Down
17 changes: 0 additions & 17 deletions main/common/external_linux.go

This file was deleted.

10 changes: 0 additions & 10 deletions main/common/external_other.go

This file was deleted.

0 comments on commit 7f7e6ed

Please sign in to comment.