2017-02-01 16:36:24 +00:00
|
|
|
// +build linux freebsd openbsd darwin
|
|
|
|
|
|
|
|
package process
|
|
|
|
|
|
|
|
import (
|
2018-10-19 18:33:23 +00:00
|
|
|
"context"
|
2020-07-01 12:47:56 +00:00
|
|
|
"fmt"
|
2017-02-01 16:36:24 +00:00
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2018-10-19 18:33:23 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2017-02-01 16:36:24 +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)
|
2018-10-19 18:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-01 16:36:24 +00:00
|
|
|
for _, devname := range devnames {
|
|
|
|
if strings.HasPrefix(devname, "/dev/tty") {
|
|
|
|
termfiles = append(termfiles, "/dev/tty/"+devname)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var ptsnames []string
|
|
|
|
ptsd, err := os.Open("/dev/pts")
|
|
|
|
if err != nil {
|
|
|
|
ptsnames, _ = filepath.Glob("/dev/ttyp*")
|
|
|
|
if ptsnames == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
defer ptsd.Close()
|
|
|
|
|
2017-02-01 16:36:24 +00:00
|
|
|
if ptsnames == nil {
|
|
|
|
defer ptsd.Close()
|
|
|
|
ptsnames, err = ptsd.Readdirnames(-1)
|
2018-10-19 18:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-01 16:36:24 +00:00
|
|
|
for _, ptsname := range ptsnames {
|
|
|
|
termfiles = append(termfiles, "/dev/pts/"+ptsname)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
termfiles = ptsnames
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range termfiles {
|
2018-10-19 18:33:23 +00:00
|
|
|
stat := unix.Stat_t{}
|
|
|
|
if err = unix.Stat(name, &stat); err != nil {
|
2017-02-01 16:36:24 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rdev := uint64(stat.Rdev)
|
|
|
|
ret[rdev] = strings.Replace(name, "/dev", "", -1)
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) {
|
|
|
|
if pid <= 0 {
|
|
|
|
return false, fmt.Errorf("invalid pid %v", pid)
|
|
|
|
}
|
|
|
|
proc, err := os.FindProcess(int(pid))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(common.HostProc()); err == nil { //Means that proc filesystem exist
|
|
|
|
// Checking PID existence based on existence of /<HOST_PROC>/proc/<PID> folder
|
|
|
|
// This covers the case when running inside container with a different process namespace (by default)
|
|
|
|
|
|
|
|
_, err := os.Stat(common.HostProc(strconv.Itoa(int(pid))))
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return err == nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
//'/proc' filesystem is not exist, checking of PID existence is done via signalling the process
|
|
|
|
//Make sense only if we run in the same process namespace
|
|
|
|
err = proc.Signal(syscall.Signal(0))
|
|
|
|
if err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if err.Error() == "os: process already finished" {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
errno, ok := err.(syscall.Errno)
|
|
|
|
if !ok {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
switch errno {
|
|
|
|
case syscall.ESRCH:
|
|
|
|
return false, nil
|
|
|
|
case syscall.EPERM:
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
// SendSignal sends a unix.Signal to the process.
|
2017-02-01 16:36:24 +00:00
|
|
|
// Currently, SIGSTOP, SIGCONT, SIGTERM and SIGKILL are supported.
|
|
|
|
func (p *Process) SendSignal(sig syscall.Signal) error {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.SendSignalWithContext(context.Background(), sig)
|
|
|
|
}
|
2017-02-01 16:36:24 +00:00
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
func (p *Process) SendSignalWithContext(ctx context.Context, sig syscall.Signal) error {
|
|
|
|
process, err := os.FindProcess(int(p.Pid))
|
2017-02-01 16:36:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
|
|
|
|
err = process.Signal(sig)
|
2017-02-01 16:36:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Suspend sends SIGSTOP to the process.
|
|
|
|
func (p *Process) Suspend() error {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.SuspendWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) SuspendWithContext(ctx context.Context) error {
|
|
|
|
return p.SendSignal(unix.SIGSTOP)
|
2017-02-01 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resume sends SIGCONT to the process.
|
|
|
|
func (p *Process) Resume() error {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.ResumeWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) ResumeWithContext(ctx context.Context) error {
|
|
|
|
return p.SendSignal(unix.SIGCONT)
|
2017-02-01 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate sends SIGTERM to the process.
|
|
|
|
func (p *Process) Terminate() error {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.TerminateWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) TerminateWithContext(ctx context.Context) error {
|
|
|
|
return p.SendSignal(unix.SIGTERM)
|
2017-02-01 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Kill sends SIGKILL to the process.
|
|
|
|
func (p *Process) Kill() error {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.KillWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) KillWithContext(ctx context.Context) error {
|
|
|
|
return p.SendSignal(unix.SIGKILL)
|
2017-02-01 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Username returns a username of the process.
|
|
|
|
func (p *Process) Username() (string, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.UsernameWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) UsernameWithContext(ctx context.Context) (string, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
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
|
|
|
|
}
|