36b3af2e3d
PR Check / Code Quality: Format (push) Failing after 1s
PR Check / Code Quality: Lint (darwin) (push) Failing after 0s
PR Check / Code Quality: Lint (freebsd) (push) Failing after 1s
PR Check / Code Quality: Lint (windows) (push) Failing after 1s
PR Check / Code Quality: Lint (linux) (push) Failing after 1s
PR Check / Security: Vulnerability Scan (push) Failing after 0s
Update Documentation / update-docs (push) Failing after 2s
PR Check / Code Quality: Vendor (push) Failing after 1s
PR Check / Code Quality: Coverage (push) Failing after 0s
PR Check / Tests: Unit (macos-latest) (push) Has been cancelled
PR Check / Tests: Unit (ubuntu-24.04) (push) Has been cancelled
PR Check / Tests: Unit (ubuntu-24.04-arm) (push) Has been cancelled
PR Check / Tests: Unit (windows-latest) (push) Has been cancelled
42 lines
973 B
Go
42 lines
973 B
Go
//go:build linux
|
|
|
|
package proc
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
// ResolveSystemdService attempts to find the systemd service name associated with a port.
|
|
// It uses `systemctl list-sockets` to find the socket unit and then maps it to the service unit.
|
|
func ResolveSystemdService(port int) (string, error) {
|
|
// check if systemctl is available
|
|
if _, err := exec.LookPath("systemctl"); err != nil {
|
|
return "", fmt.Errorf("systemctl not found")
|
|
}
|
|
|
|
cmd := exec.Command("systemctl", "list-sockets", "--no-legend", "--full")
|
|
var out bytes.Buffer
|
|
cmd.Stdout = &out
|
|
if err := cmd.Run(); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
portStr := fmt.Sprintf(":%d", port)
|
|
lines := strings.Split(out.String(), "\n")
|
|
|
|
for _, line := range lines {
|
|
fields := strings.Fields(line)
|
|
if len(fields) < 3 {
|
|
continue
|
|
}
|
|
if strings.HasSuffix(fields[0], portStr) {
|
|
return fields[2], nil
|
|
}
|
|
}
|
|
|
|
return "", fmt.Errorf("no systemd service found for port %d", port)
|
|
}
|