mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-08 16:46:37 +08:00
96a17ae35d
* fix(agent): recover OpenResty runtime after invalid PID * fix(agent): scope OpenResty shutdown to instance processes --------- Co-authored-by: Noru Wyrms <wyrmsnoru@gmail.com>
50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
// 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
|
|
}
|