Skip to content

Commit

Permalink
Merge pull request #46 from vrothberg/issue-28
Browse files Browse the repository at this point in the history
make psgo thread-safe
  • Loading branch information
rhatdan authored Mar 11, 2019
2 parents dc0bc9f + 31c99a9 commit ee081b6
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 113 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)
}
5 changes: 2 additions & 3 deletions internal/proc/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"os/exec"
"strings"

"github.com/containers/psgo/internal/types"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -207,11 +206,11 @@ func readStatusDefault(pid string) ([]string, error) {
}

// ParseStatus parses the /proc/$pid/status file and returns a *Status.
func ParseStatus(ctx *types.PsContext, pid string) (*Status, error) {
func ParseStatus(pid string, joinUserNS bool) (*Status, error) {
var lines []string
var err error

if ctx.JoinUserNS {
if joinUserNS {
lines, err = readStatusUserNS(pid)
} else {
lines, err = readStatusDefault(pid)
Expand Down
13 changes: 6 additions & 7 deletions internal/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"github.com/containers/psgo/internal/host"
"github.com/containers/psgo/internal/proc"
"github.com/containers/psgo/internal/types"
"github.com/opencontainers/runc/libcontainer/user"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -76,13 +75,13 @@ func LookupUID(uid string) (string, error) {

// New returns a new Process with the specified pid and parses the relevant
// data from /proc and /dev.
func New(ctx *types.PsContext, pid string) (*Process, error) {
func New(pid string, joinUserNS bool) (*Process, error) {
p := Process{Pid: pid}

if err := p.parseStat(); err != nil {
return nil, err
}
if err := p.parseStatus(ctx); err != nil {
if err := p.parseStatus(joinUserNS); err != nil {
return nil, err
}
if err := p.parseCmdLine(); err != nil {
Expand All @@ -103,10 +102,10 @@ func New(ctx *types.PsContext, pid string) (*Process, error) {
}

// FromPIDs creates a new Process for each pid.
func FromPIDs(ctx *types.PsContext, pids []string) ([]*Process, error) {
func FromPIDs(pids []string, joinUserNS bool) ([]*Process, error) {
processes := []*Process{}
for _, pid := range pids {
p, err := New(ctx, pid)
p, err := New(pid, joinUserNS)
if err != nil {
if os.IsNotExist(errors.Cause(err)) {
// proc parsing is racy
Expand All @@ -131,8 +130,8 @@ func (p *Process) parseStat() error {
}

// parseStatus parses /proc/$pid/status.
func (p *Process) parseStatus(ctx *types.PsContext) error {
s, err := proc.ParseStatus(ctx, p.Pid)
func (p *Process) parseStatus(joinUserNS bool) error {
s, err := proc.ParseStatus(p.Pid, joinUserNS)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions internal/process/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ package process
import (
"testing"

"github.com/containers/psgo/internal/types"
"github.com/stretchr/testify/assert"
)

func TestAll(t *testing.T) {
// no thorough test but it makes sure things are working
p, err := New(&types.PsContext{}, "self")
p, err := New("self", false)
assert.Nil(t, err)

assert.NotNil(t, p.Stat)
Expand Down
22 changes: 0 additions & 22 deletions internal/types/types.go

This file was deleted.

Loading

0 comments on commit ee081b6

Please sign in to comment.