Skip to content

Commit

Permalink
psContext: store TTYs to allow concurrent accesses
Browse files Browse the repository at this point in the history
Signed-off-by: Valentin Rothberg <[email protected]>
  • Loading branch information
vrothberg committed Mar 11, 2019
1 parent ea66d26 commit 31c99a9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
11 changes: 4 additions & 7 deletions internal/dev/tty.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,17 @@ 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.
maj := (ttyNr >> 8) & 0xFF
min := (ttyNr & 0xFF) | ((ttyNr >> 20) & 0xFFF)

if devices == nil {
devs, err := getTTYs()
devs, err := TTYs()
if err != nil {
return nil, err
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/dev/tty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
9 changes: 7 additions & 2 deletions psgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 31c99a9

Please sign in to comment.