2014-04-24 17:28:45 +08:00
|
|
|
// +build linux freebsd
|
|
|
|
|
|
|
|
package gopsutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2014-04-25 17:05:02 +08:00
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
2014-04-24 17:28:45 +08:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
// POSIX
|
|
|
|
func getTerminalMap() (map[uint64]string, error) {
|
|
|
|
ret := make(map[uint64]string)
|
2014-04-30 15:16:07 +08:00
|
|
|
var termfiles []string
|
2014-04-24 17:28:45 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ptsd, err := os.Open("/dev/pts")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer ptsd.Close()
|
|
|
|
|
|
|
|
ptsnames, err := ptsd.Readdirnames(-1)
|
|
|
|
for _, ptsname := range ptsnames {
|
|
|
|
termfiles = append(termfiles, "/dev/pts/"+ptsname)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range termfiles {
|
|
|
|
stat := syscall.Stat_t{}
|
|
|
|
syscall.Stat(name, &stat)
|
2014-04-24 18:14:00 +08:00
|
|
|
rdev := uint64(stat.Rdev)
|
2014-04-24 17:28:45 +08:00
|
|
|
ret[rdev] = strings.Replace(name, "/dev", "", -1)
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
2014-04-25 17:05:02 +08:00
|
|
|
|
2014-04-30 15:16:07 +08:00
|
|
|
func (p *Process) SendSignal(sig syscall.Signal) error {
|
|
|
|
sigAsStr := "INT"
|
2014-04-25 17:05:02 +08:00
|
|
|
switch sig {
|
|
|
|
case syscall.SIGSTOP:
|
2014-04-30 15:16:07 +08:00
|
|
|
sigAsStr = "STOP"
|
2014-04-25 17:05:02 +08:00
|
|
|
case syscall.SIGCONT:
|
2014-04-30 15:16:07 +08:00
|
|
|
sigAsStr = "CONT"
|
2014-04-25 17:05:02 +08:00
|
|
|
case syscall.SIGTERM:
|
2014-04-30 15:16:07 +08:00
|
|
|
sigAsStr = "TERM"
|
2014-04-25 17:05:02 +08:00
|
|
|
case syscall.SIGKILL:
|
2014-04-30 15:16:07 +08:00
|
|
|
sigAsStr = "KILL"
|
2014-04-25 17:05:02 +08:00
|
|
|
}
|
|
|
|
|
2014-04-30 15:16:07 +08:00
|
|
|
cmd := exec.Command("kill", "-s", sigAsStr, strconv.Itoa(int(p.Pid)))
|
2014-04-25 17:05:02 +08:00
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) Suspend() error {
|
2014-04-30 15:16:07 +08:00
|
|
|
return p.SendSignal(syscall.SIGSTOP)
|
2014-04-25 17:05:02 +08:00
|
|
|
}
|
|
|
|
func (p *Process) Resume() error {
|
2014-04-30 15:16:07 +08:00
|
|
|
return p.SendSignal(syscall.SIGCONT)
|
2014-04-25 17:05:02 +08:00
|
|
|
}
|
|
|
|
func (p *Process) Terminate() error {
|
2014-04-30 15:16:07 +08:00
|
|
|
return p.SendSignal(syscall.SIGTERM)
|
2014-04-25 17:05:02 +08:00
|
|
|
}
|
|
|
|
func (p *Process) Kill() error {
|
2014-04-30 15:16:07 +08:00
|
|
|
return p.SendSignal(syscall.SIGKILL)
|
2014-04-25 17:05:02 +08:00
|
|
|
}
|