fix(pidproxy): handle new line

example: sshd
This commit is contained in:
2023-12-26 09:20:42 +11:00
parent e2275ef05e
commit b15066b513
4 changed files with 34 additions and 13 deletions

View File

@@ -1,6 +1,8 @@
package main
import (
"bufio"
"errors"
"log"
"os"
"os/exec"
@@ -130,8 +132,6 @@ func readPid(pidFile string) (int, error) {
var (
file *os.File
err error
buf []byte
n int
pid64 int64
)
@@ -142,18 +142,15 @@ func readPid(pidFile string) (int, error) {
_ = file.Close()
}()
buf = make([]byte, 1024)
n, err = file.Read(buf)
if err != nil {
return 0, err
scanner := bufio.NewScanner(file)
if scanner.Scan() {
if pid64, err = strconv.ParseInt(scanner.Text(), 10, 64); err != nil {
return 0, err
}
return int(pid64), nil
} else {
return 0, errors.New("invalid scanner")
}
pid64, err = strconv.ParseInt(string(buf[:n]), 10, 64)
if err != nil {
return 0, err
}
return int(pid64), nil
}
func startProcess(arg0 string, args ...string) {