57 lines
1.4 KiB
Go
57 lines
1.4 KiB
Go
package windows
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// processSnapshot is deliberately small so the matching invariant can be
|
|
// tested without a Windows host. NextImagePath returns more=false only after a
|
|
// successful end-of-snapshot; any enumeration or image-path failure is an
|
|
// error, rather than evidence that the target is not running.
|
|
type processSnapshot interface {
|
|
NextImagePath() (path string, more bool, err error)
|
|
Close() error
|
|
}
|
|
|
|
type processSnapshotFactory func() (processSnapshot, error)
|
|
|
|
func entrypointIsRunning(entrypoint string, newSnapshot processSnapshotFactory) (bool, error) {
|
|
target, err := canonicalProcessPath(entrypoint)
|
|
if err != nil {
|
|
return false, fmt.Errorf("canonicalize target entrypoint: %w", err)
|
|
}
|
|
|
|
snapshot, err := newSnapshot()
|
|
if err != nil {
|
|
return false, fmt.Errorf("create process snapshot: %w", err)
|
|
}
|
|
defer snapshot.Close()
|
|
|
|
for {
|
|
path, more, err := snapshot.NextImagePath()
|
|
if err != nil {
|
|
return false, fmt.Errorf("enumerate process image path: %w", err)
|
|
}
|
|
if !more {
|
|
return false, nil
|
|
}
|
|
candidate, err := canonicalProcessPath(path)
|
|
if err != nil {
|
|
return false, fmt.Errorf("canonicalize process image path: %w", err)
|
|
}
|
|
if strings.EqualFold(target, candidate) {
|
|
return true, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
func canonicalProcessPath(path string) (string, error) {
|
|
abs, err := filepath.Abs(path)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Clean(abs), nil
|
|
}
|