master v1.1.0
兔子 4 years ago
parent d981a03b14
commit 3501166592

2
.gitignore vendored

@ -0,0 +1,2 @@
.idea
.vscode

@ -28,7 +28,6 @@ type StarCmd struct {
errout []byte errout []byte
runerr error runerr error
exitcode int exitcode int
customCtx context.Context
stdoutBuf *bytes.Buffer stdoutBuf *bytes.Buffer
stderrBuf *bytes.Buffer stderrBuf *bytes.Buffer
stdoutpoint int stdoutpoint int
@ -314,9 +313,13 @@ func (starcli *StarCmd) ExitCode() int {
return starcli.exitcode return starcli.exitcode
} }
func (starcli *StarCmd) Kill() { func (starcli *StarCmd) Kill() error{
starcli.CMD.Process.Kill() err:=starcli.CMD.Process.Kill()
if err!=nil{
return err
}
starcli.running = false starcli.running = false
return nil
} }
func (starcli *StarCmd) GetPid() int { func (starcli *StarCmd) GetPid() int {

@ -283,25 +283,49 @@ func Daemon(path string, args ...string) (int, error) {
return pid, err return pid, err
} }
func (starcli *StarCmd) SetRunUser(uid, gid uint32) { func DaemonWithUser(uid, gid uint32,groups []uint32,path string, args ...string) (int, error) {
cmd := exec.Command(path, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Credential: &syscall.Credential{
Uid: uid,
Gid: gid,
Groups: groups,
},
Setsid: true,
}
if err := cmd.Start(); err != nil {
return -1, err
}
pid := cmd.Process.Pid
err := cmd.Process.Release()
return pid, err
}
func (starcli *StarCmd) SetRunUser(uid, gid uint32,groups []uint32) {
starcli.CMD.SysProcAttr = &syscall.SysProcAttr{ starcli.CMD.SysProcAttr = &syscall.SysProcAttr{
Credential: &syscall.Credential{ Credential: &syscall.Credential{
Uid: uid, Uid: uid,
Gid: gid, Gid: gid,
Groups: groups,
}, },
Setsid: true, Setsid: true,
} }
} }
func (starcli *StarCmd) Release() error { func (starcli *StarCmd) Release() error {
if err := starcli.CMD.Start(); err != nil { if starcli.CMD.SysProcAttr == nil {
return err starcli.CMD.SysProcAttr = &syscall.SysProcAttr{
Setsid: true,
}
} else {
starcli.CMD.SysProcAttr.Setsid = true
} }
starcli.CMD.SysProcAttr = &syscall.SysProcAttr{ if !starcli.IsRunning() {
Setsid: true, if err := starcli.CMD.Start(); err != nil {
return err
}
} }
starcli.CMD.Process.Release() return starcli.CMD.Process.Release()
return nil
} }
func (starcli *StarCmd) SetKeepCaps() error { func (starcli *StarCmd) SetKeepCaps() error {
@ -311,3 +335,11 @@ func (starcli *StarCmd) SetKeepCaps() error {
} }
return nil return nil
} }
func SetKeepCaps() error {
_, _, err := syscall.RawSyscall(157 /*SYS PRCTL */, 0x8 /*PR SET KEEPCAPS*/, 1, 0)
if 0 != err {
return err
}
return nil
}

Loading…
Cancel
Save