diff --git a/internal/dev/tty.go b/internal/dev/tty.go index cc7d0a4..b7d6f28 100644 --- a/internal/dev/tty.go +++ b/internal/dev/tty.go @@ -31,12 +31,9 @@ type TTY struct { Path string } -// cache TTYs to avoid redundant lookups -var devices *[]TTY - // FindTTY return the corresponding TTY to the ttyNr or nil of non could be // found. -func FindTTY(ttyNr uint64) (*TTY, error) { +func FindTTY(ttyNr uint64, devices *[]TTY) (*TTY, error) { // (man 5 proc) The minor device number is contained in the combination // of bits 31 to 20 and 7 to 0; the major device number is in bits 15 // to 8. @@ -44,7 +41,7 @@ func FindTTY(ttyNr uint64) (*TTY, error) { min := (ttyNr & 0xFF) | ((ttyNr >> 20) & 0xFFF) if devices == nil { - devs, err := getTTYs() + devs, err := TTYs() if err != nil { return nil, err } @@ -70,8 +67,8 @@ func minDevNum(rdev uint64) uint64 { return (rdev & 0xff) | ((rdev >> 12) & 0xfff00) } -// getTTYs parses /dev for tty and pts devices. -func getTTYs() (*[]TTY, error) { +// TTYs parses /dev for tty and pts devices. +func TTYs() (*[]TTY, error) { devDir, err := os.Open("/dev/") if err != nil { return nil, err diff --git a/internal/dev/tty_test.go b/internal/dev/tty_test.go index 1e29ee3..f0a87f5 100644 --- a/internal/dev/tty_test.go +++ b/internal/dev/tty_test.go @@ -22,7 +22,7 @@ import ( func TestGetTTYs(t *testing.T) { // no thorough test but it makes sure things are working - devs, err := getTTYs() + devs, err := TTYs() assert.Nil(t, err) assert.NotNil(t, devs) } diff --git a/psgo.go b/psgo.go index 188dcb1..e0f1027 100644 --- a/psgo.go +++ b/psgo.go @@ -48,6 +48,8 @@ type psContext struct { containersProcesses []*process.Process // Processes on the host. Used to map those to the ones running in the container. hostProcesses []*process.Process + // tty and pty devices. + ttys *[]dev.TTY } // processFunc is used to map a given aixFormatDescriptor to a corresponding @@ -287,7 +289,10 @@ func JoinNamespaceAndProcessInfo(pid string, descriptors []string) ([][]string, // of the specified descriptors requires host data for _, d := range aixDescriptors { if d.onHost { - ctx.hostProcesses, _ = hostProcesses(pid) + ctx.hostProcesses, err = hostProcesses(pid) + if err != nil { + return nil, err + } break } } @@ -607,7 +612,7 @@ func processTTY(p *process.Process, ctx *psContext) (string, error) { return "", nil } - tty, err := dev.FindTTY(ttyNr) + tty, err := dev.FindTTY(ttyNr, ctx.ttys) if err != nil { return "", nil }