fix(agent): recover OpenResty runtime after invalid PID (#45)

* fix(agent): recover OpenResty runtime after invalid PID

* fix(agent): scope OpenResty shutdown to instance processes

---------

Co-authored-by: Noru Wyrms <wyrmsnoru@gmail.com>
This commit is contained in:
Ryan
2026-10-08 08:39:40 +08:00
committed by GitHub
parent fec9bd18dd
commit 96a17ae35d
15 changed files with 1000 additions and 91 deletions
@@ -0,0 +1,49 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package nginx
import (
"fmt"
"strconv"
"strings"
)
//nolint:unused // Used by the non-Linux Unix inspector.
const (
psCommandIndex = 8
psMasterBinaryIndex = 11
)
//nolint:unused // Used by the non-Linux Unix inspector; portable for fixture tests.
func parsePSRuntime(output, binary, configPath string, owner int) ([]runtimeProcess, error) {
var processes []runtimeProcess
for _, line := range strings.Split(output, "\n") {
fields := strings.Fields(line)
if len(fields) <= psCommandIndex {
continue
}
uid, err := strconv.Atoi(fields[2])
if err != nil {
continue
}
title := strings.Join(fields[psCommandIndex:], " ")
if !strings.HasPrefix(title, "nginx: ") && !strings.HasPrefix(title, "openresty: ") {
continue
}
master := uid == owner && matchesRuntimeMaster(title, configPath)
if master && (len(fields) <= psMasterBinaryIndex || fields[psMasterBinaryIndex] != binary) {
return nil, fmt.Errorf("cannot verify openresty master invocation %s", fields[0])
}
pid, err := strconv.Atoi(fields[0])
if err != nil {
return nil, err
}
parentPID, err := strconv.Atoi(fields[1])
if err != nil {
return nil, err
}
processes = append(processes, runtimeProcess{PID: pid, ParentPID: parentPID, Master: master, StartTime: strings.Join(fields[3:psCommandIndex], " ")})
}
return processes, nil
}