File: cmdline_linux.go

package info (click to toggle)
witr 0.2.4%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 736 kB
  • sloc: sh: 79; makefile: 10
file content (23 lines) | stat: -rw-r--r-- 429 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//go:build linux

package proc

import (
	"fmt"
	"os"
	"strings"
)

// GetCmdline returns the command line for a given PID
func GetCmdline(pid int) string {
	cmdlineBytes, err := os.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
	if err != nil {
		return "(unknown)"
	}
	cmd := strings.ReplaceAll(string(cmdlineBytes), "\x00", " ")
	cmdline := strings.TrimSpace(cmd)
	if cmdline == "" {
		return "(unknown)"
	}
	return cmdline
}