2017-08-29 04:32:14 +02:00
|
|
|
// Copyright 2017 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package goprocess reports the Go processes running on a host.
|
|
|
|
package goprocess
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/google/gops/internal"
|
2022-11-25 15:16:13 +02:00
|
|
|
"github.com/shirou/gopsutil/v3/process"
|
2017-08-29 04:32:14 +02:00
|
|
|
)
|
|
|
|
|
2017-09-04 20:45:49 +02:00
|
|
|
// P represents a Go process.
|
|
|
|
type P struct {
|
2017-08-29 04:32:14 +02:00
|
|
|
PID int
|
|
|
|
PPID int
|
|
|
|
Exec string
|
|
|
|
Path string
|
|
|
|
BuildVersion string
|
|
|
|
Agent bool
|
|
|
|
}
|
|
|
|
|
2017-09-04 20:45:49 +02:00
|
|
|
// FindAll returns all the Go processes currently running on this host.
|
|
|
|
func FindAll() []P {
|
2020-09-07 23:23:34 +02:00
|
|
|
const concurrencyLimit = 10 // max number of concurrent workers
|
2022-11-25 15:16:13 +02:00
|
|
|
pss, err := process.Processes()
|
2017-08-29 04:32:14 +02:00
|
|
|
if err != nil {
|
2018-03-11 07:24:15 +02:00
|
|
|
return nil
|
2017-08-29 04:32:14 +02:00
|
|
|
}
|
2020-09-10 10:12:57 +02:00
|
|
|
return findAll(pss, isGo, concurrencyLimit)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allows to inject isGo for testing.
|
2022-11-25 15:16:13 +02:00
|
|
|
type isGoFunc func(*process.Process) (path, version string, agent, ok bool, err error)
|
2017-08-29 04:32:14 +02:00
|
|
|
|
2022-11-25 15:16:13 +02:00
|
|
|
func findAll(pss []*process.Process, isGo isGoFunc, concurrencyLimit int) []P {
|
|
|
|
input := make(chan *process.Process, len(pss))
|
2020-09-07 23:23:34 +02:00
|
|
|
output := make(chan P, len(pss))
|
|
|
|
|
|
|
|
for _, ps := range pss {
|
|
|
|
input <- ps
|
|
|
|
}
|
|
|
|
close(input)
|
|
|
|
|
2017-08-29 04:32:14 +02:00
|
|
|
var wg sync.WaitGroup
|
2020-09-07 23:23:34 +02:00
|
|
|
wg.Add(concurrencyLimit) // used to wait for workers to be finished
|
2017-08-29 04:32:14 +02:00
|
|
|
|
2020-09-07 23:23:34 +02:00
|
|
|
// Run concurrencyLimit of workers until there
|
|
|
|
// is no more processes to be checked in the input channel.
|
|
|
|
for i := 0; i < concurrencyLimit; i++ {
|
2017-08-29 04:32:14 +02:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
2020-09-07 23:23:34 +02:00
|
|
|
for pr := range input {
|
|
|
|
path, version, agent, ok, err := isGo(pr)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(jbd): Return a list of errors.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-11-25 15:16:13 +02:00
|
|
|
ppid, err := pr.Ppid()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name, err := pr.Name()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-09-07 23:23:34 +02:00
|
|
|
output <- P{
|
2022-11-25 15:16:13 +02:00
|
|
|
PID: int(pr.Pid),
|
|
|
|
PPID: int(ppid),
|
|
|
|
Exec: name,
|
2020-09-07 23:23:34 +02:00
|
|
|
Path: path,
|
|
|
|
BuildVersion: version,
|
|
|
|
Agent: agent,
|
|
|
|
}
|
2018-03-11 07:24:15 +02:00
|
|
|
}
|
2017-08-29 04:32:14 +02:00
|
|
|
}()
|
|
|
|
}
|
2020-09-07 23:23:34 +02:00
|
|
|
wg.Wait() // wait until all workers are finished
|
|
|
|
close(output) // no more results to be waited for
|
|
|
|
|
2018-03-11 07:24:15 +02:00
|
|
|
var results []P
|
2020-09-07 23:23:34 +02:00
|
|
|
for p := range output {
|
2018-03-11 07:24:15 +02:00
|
|
|
results = append(results, p)
|
|
|
|
}
|
2017-08-29 04:32:14 +02:00
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2017-09-04 20:45:49 +02:00
|
|
|
// Find finds info about the process identified with the given PID.
|
|
|
|
func Find(pid int) (p P, ok bool, err error) {
|
2022-11-25 15:16:13 +02:00
|
|
|
pr, err := process.NewProcess(int32(pid))
|
2017-09-04 20:45:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return P{}, false, err
|
|
|
|
}
|
|
|
|
path, version, agent, ok, err := isGo(pr)
|
2022-11-25 15:16:13 +02:00
|
|
|
if !ok || err != nil {
|
2017-09-04 20:45:49 +02:00
|
|
|
return P{}, false, nil
|
|
|
|
}
|
2022-11-25 15:16:13 +02:00
|
|
|
ppid, err := pr.Ppid()
|
|
|
|
if err != nil {
|
|
|
|
return P{}, false, err
|
|
|
|
}
|
|
|
|
name, err := pr.Name()
|
|
|
|
if err != nil {
|
|
|
|
return P{}, false, err
|
|
|
|
}
|
2017-09-04 20:45:49 +02:00
|
|
|
return P{
|
2022-11-25 15:16:13 +02:00
|
|
|
PID: int(pr.Pid),
|
|
|
|
PPID: int(ppid),
|
|
|
|
Exec: name,
|
2017-09-04 20:45:49 +02:00
|
|
|
Path: path,
|
|
|
|
BuildVersion: version,
|
|
|
|
Agent: agent,
|
|
|
|
}, true, nil
|
|
|
|
}
|
|
|
|
|
2017-08-29 04:32:14 +02:00
|
|
|
// isGo looks up the runtime.buildVersion symbol
|
|
|
|
// in the process' binary and determines if the process
|
|
|
|
// if a Go process or not. If the process is a Go process,
|
|
|
|
// it reports PID, binary name and full path of the binary.
|
2022-11-25 15:16:13 +02:00
|
|
|
func isGo(pr *process.Process) (path, version string, agent, ok bool, err error) {
|
|
|
|
if pr.Pid == 0 {
|
2017-08-29 04:32:14 +02:00
|
|
|
// ignore system process
|
|
|
|
return
|
|
|
|
}
|
2022-11-25 15:16:13 +02:00
|
|
|
path, err = pr.Exe()
|
2017-08-29 04:32:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-05-04 22:41:16 +02:00
|
|
|
version, err = goVersion(path)
|
2017-08-29 04:32:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-09-04 20:53:45 +02:00
|
|
|
ok = true
|
2022-11-25 15:16:13 +02:00
|
|
|
pidfile, err := internal.PIDFile(int(pr.Pid))
|
2017-08-29 04:32:14 +02:00
|
|
|
if err == nil {
|
|
|
|
_, err := os.Stat(pidfile)
|
|
|
|
agent = err == nil
|
|
|
|
}
|
|
|
|
return path, version, agent, ok, nil
|
|
|
|
}
|