proc: make go-ps a dependency instead of using in-tree copy

This commit is contained in:
Jared Watts committed 2017-07-13 18:14:16 -07:00
1 parent 4c5466eda5
commit 299ce8f62f
4 files changed
+12 -197

No files matched your search

Generated
+4 -2
View File
@@ -1,5 +1,5 @@
hash: eb93da0e16c0bbaba8400e87ec9f164db2da6762533a492dd74ba7acc4f85f10
updated: 2017-07-13T12:28:26.349758067-07:00
hash: fe1ad353107dd7cafbb209b1a47dbc8f133c33e903c624dbb0ca6b8e5dd96b90
updated: 2017-07-13T16:41:53.64302729-07:00
imports:
- name: github.com/aws/aws-sdk-go
version: 1fd5dd46e2bf3e0be156701a559098dc823053d5
@@ -177,6 +177,8 @@ imports:
version: 54f5c7e1dea7e1c582ff563308228c592252fff9
- name: github.com/inconshreveable/mousetrap
version: 76626ae9c91c4f2a10f34cad8ce83ea42c93bb75
- name: github.com/jbw976/go-ps
version: 82859aed1b5dafbbe6ad4f3dac5404d2a742cf18
- name: github.com/jmespath/go-jmespath
version: 3433f3ea46d9f8019119e7dd41274e112a2359a9
- name: github.com/jmoiron/jsonq
+2
View File
@@ -36,6 +36,8 @@ import:
subpackages:
- prometheus
- prometheus/promhttp
- package: github.com/jbw976/go-ps
version: 82859aed1b5dafbbe6ad4f3dac5404d2a742cf18
testImport:
- package: github.com/ghodss/yaml
version: 73d445a93680fa1a78ae23a5839bad48f32ba1ee
-190
View File
@@ -1,190 +0,0 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Mitchell Hashimoto
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package proc
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
// UnixProcess is an implementation of Process that contains Unix-specific
// fields and information.
type UnixProcess struct {
pid int
ppid int
state rune
pgrp int
sid int
binary string
cmdline string
}
func (p *UnixProcess) Pid() int {
return p.pid
}
func (p *UnixProcess) PPid() int {
return p.ppid
}
func (p *UnixProcess) Executable() string {
return p.binary
}
// Refresh reloads all the data associated with this process.
func (p *UnixProcess) Refresh() error {
statPath := fmt.Sprintf("/proc/%d/stat", p.pid)
dataBytes, err := ioutil.ReadFile(statPath)
if err != nil {
return err
}
// First, parse out the image name
data := string(dataBytes)
binStart := strings.IndexRune(data, '(') + 1
binEnd := strings.IndexRune(data[binStart:], ')')
p.binary = data[binStart : binStart+binEnd]
// Move past the image name and start parsing the rest
data = data[binStart+binEnd+2:]
_, err = fmt.Sscanf(data,
"%c %d %d %d",
&p.state,
&p.ppid,
&p.pgrp,
&p.sid)
// retrieve the cmdline for the process as well
cmdlineFilePath := fmt.Sprintf("/proc/%d/cmdline", p.pid)
dataBytes, err = ioutil.ReadFile(cmdlineFilePath)
if err != nil {
return err
}
p.cmdline = strings.TrimSpace(string(dataBytes))
return err
}
func findProcess(pid int) (*UnixProcess, error) {
dir := fmt.Sprintf("/proc/%d", pid)
_, err := os.Stat(dir)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
return newUnixProcess(pid)
}
func findProcessSearch(binary, cmdlinePattern string) (*UnixProcess, error) {
if cmdlinePattern == "" {
return nil, fmt.Errorf("no cmdline search pattern provided")
}
procs, err := processes()
if err != nil {
return nil, err
}
b := filepath.Base(binary)
for i := range procs {
if procs[i].binary == b {
if matched, _ := regexp.MatchString(cmdlinePattern, procs[i].cmdline); matched {
return procs[i], nil
}
}
}
return nil, nil
}
func processes() ([]*UnixProcess, error) {
d, err := os.Open("/proc")
if err != nil {
if os.IsNotExist(err) {
logger.Warningf("cannot get processes. /proc not found")
return nil, nil
}
return nil, err
}
defer d.Close()
results := make([]*UnixProcess, 0, 50)
for {
fis, err := d.Readdir(10)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
for _, fi := range fis {
// We only care about directories, since all pids are dirs
if !fi.IsDir() {
continue
}
// We only care if the name starts with a numeric
name := fi.Name()
if name[0] < '0' || name[0] > '9' {
continue
}
// From this point forward, any errors we just ignore, because
// it might simply be that the process doesn't exist anymore.
pid, err := strconv.ParseInt(name, 10, 0)
if err != nil {
continue
}
p, err := newUnixProcess(int(pid))
if err != nil {
continue
}
results = append(results, p)
}
}
return results, nil
}
func newUnixProcess(pid int) (*UnixProcess, error) {
p := &UnixProcess{pid: pid}
return p, p.Refresh()
}
+6 -5
View File
@@ -23,6 +23,7 @@ import (
"sync"
"syscall"
"github.com/jbw976/go-ps"
"github.com/rook/rook/pkg/util/exec"
)
@@ -149,7 +150,7 @@ func (p *ProcManager) checkProcessExists(binary, procSearchPattern string, polic
}
// check if this process is currently running even though not being managed
existingProc, err := findProcessSearch(binary, procSearchPattern)
existingProc, err := ps.FindProcessByCmdline(binary, procSearchPattern)
if err != nil {
return false, fmt.Errorf("failed to search for process %s with pattern '%s': %+v", binary, procSearchPattern, err)
}
@@ -159,7 +160,7 @@ func (p *ProcManager) checkProcessExists(binary, procSearchPattern string, polic
return true, nil
}
logger.Infof("existing process found for binary %s with pid %d, cmdline '%s'.", binary, existingProc.pid, existingProc.cmdline)
logger.Infof("existing process found for binary %s with pid %d, cmdline '%s'.", binary, existingProc.Pid(), existingProc.Cmdline())
if policy == ReuseExisting {
logger.Infof("Policy is 'reuse', reusing existing process.")
return false, nil
@@ -168,7 +169,7 @@ func (p *ProcManager) checkProcessExists(binary, procSearchPattern string, polic
logger.Infof("Policy is 'restart', restarting existing process.")
// double check if the process is managed and stop it
index, proc := p.findMonitoredProcByPID(existingProc.pid)
index, proc := p.findMonitoredProcByPID(existingProc.Pid())
if proc != nil {
p.purgeManagedProc(index, proc)
return true, nil
@@ -176,8 +177,8 @@ func (p *ProcManager) checkProcessExists(binary, procSearchPattern string, polic
// we could't stop the existing process through our own managed proces set, try to stop the process
// via a direct signal to its PID
if err := syscall.Kill(existingProc.pid, syscall.SIGKILL); err != nil {
return false, fmt.Errorf("failed to stop child process %d: %v", existingProc.pid, err)
if err := syscall.Kill(existingProc.Pid(), syscall.SIGKILL); err != nil {
return false, fmt.Errorf("failed to stop child process %d: %v", existingProc.Pid(), err)
}
return true, nil