2017-02-02 06:41:29 +00:00
|
|
|
// +build linux freebsd openbsd darwin
|
2016-02-12 18:02:16 +00:00
|
|
|
|
|
|
|
package process
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/user"
|
2017-02-02 06:41:29 +00:00
|
|
|
"path/filepath"
|
2016-02-12 18:02:16 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
2016-06-17 14:31:00 +00:00
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2016-02-12 18:02:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// POSIX
|
|
|
|
func getTerminalMap() (map[uint64]string, error) {
|
|
|
|
ret := make(map[uint64]string)
|
|
|
|
var termfiles []string
|
|
|
|
|
|
|
|
d, err := os.Open("/dev")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer d.Close()
|
|
|
|
|
|
|
|
devnames, err := d.Readdirnames(-1)
|
|
|
|
for _, devname := range devnames {
|
|
|
|
if strings.HasPrefix(devname, "/dev/tty") {
|
|
|
|
termfiles = append(termfiles, "/dev/tty/"+devname)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 06:41:29 +00:00
|
|
|
var ptsnames []string
|
2016-02-12 18:02:16 +00:00
|
|
|
ptsd, err := os.Open("/dev/pts")
|
|
|
|
if err != nil {
|
2017-02-02 06:41:29 +00:00
|
|
|
ptsnames, _ = filepath.Glob("/dev/ttyp*")
|
|
|
|
if ptsnames == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
2017-03-13 17:50:09 +00:00
|
|
|
defer ptsd.Close()
|
|
|
|
|
2017-02-02 06:41:29 +00:00
|
|
|
if ptsnames == nil {
|
|
|
|
defer ptsd.Close()
|
|
|
|
ptsnames, err = ptsd.Readdirnames(-1)
|
|
|
|
for _, ptsname := range ptsnames {
|
|
|
|
termfiles = append(termfiles, "/dev/pts/"+ptsname)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
termfiles = ptsnames
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range termfiles {
|
2017-07-28 16:46:44 +00:00
|
|
|
stat := unix.Stat_t{}
|
|
|
|
if err = unix.Stat(name, &stat); err != nil {
|
2016-02-12 18:02:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rdev := uint64(stat.Rdev)
|
|
|
|
ret[rdev] = strings.Replace(name, "/dev", "", -1)
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
// SendSignal sends a unix.Signal to the process.
|
2016-05-09 15:19:04 +00:00
|
|
|
// Currently, SIGSTOP, SIGCONT, SIGTERM and SIGKILL are supported.
|
2016-02-12 18:02:16 +00:00
|
|
|
func (p *Process) SendSignal(sig syscall.Signal) error {
|
2017-07-28 16:46:44 +00:00
|
|
|
process, err := os.FindProcess(int(p.Pid))
|
2016-05-09 15:19:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-28 16:46:44 +00:00
|
|
|
|
|
|
|
err = process.Signal(sig)
|
2016-02-12 18:02:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
// Suspend sends SIGSTOP to the process.
|
2016-02-12 18:02:16 +00:00
|
|
|
func (p *Process) Suspend() error {
|
2017-07-28 16:46:44 +00:00
|
|
|
return p.SendSignal(unix.SIGSTOP)
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
2016-05-09 15:19:04 +00:00
|
|
|
|
|
|
|
// Resume sends SIGCONT to the process.
|
2016-02-12 18:02:16 +00:00
|
|
|
func (p *Process) Resume() error {
|
2017-07-28 16:46:44 +00:00
|
|
|
return p.SendSignal(unix.SIGCONT)
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
2016-05-09 15:19:04 +00:00
|
|
|
|
|
|
|
// Terminate sends SIGTERM to the process.
|
2016-02-12 18:02:16 +00:00
|
|
|
func (p *Process) Terminate() error {
|
2017-07-28 16:46:44 +00:00
|
|
|
return p.SendSignal(unix.SIGTERM)
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
2016-05-09 15:19:04 +00:00
|
|
|
|
|
|
|
// Kill sends SIGKILL to the process.
|
2016-02-12 18:02:16 +00:00
|
|
|
func (p *Process) Kill() error {
|
2017-07-28 16:46:44 +00:00
|
|
|
return p.SendSignal(unix.SIGKILL)
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
2016-05-09 15:19:04 +00:00
|
|
|
|
|
|
|
// Username returns a username of the process.
|
2016-02-12 18:02:16 +00:00
|
|
|
func (p *Process) Username() (string, error) {
|
|
|
|
uids, err := p.Uids()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(uids) > 0 {
|
|
|
|
u, err := user.LookupId(strconv.Itoa(int(uids[0])))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return u.Username, nil
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|