Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Fix race condition with unix process listing #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions process_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package ps

import (
"fmt"
"io"
"os"
"strconv"
)
Expand Down Expand Up @@ -55,41 +54,36 @@ func processes() ([]Process, error) {
defer d.Close()

results := make([]Process, 0, 50)
for {
fis, err := d.Readdir(10)
if err == io.EOF {
break
fis, err := d.Readdir(-1)
if err != nil {
return nil, err
}

for _, fi := range fis {
// We only care about directories, since all pids are dirs
if !fi.IsDir() {
continue
}

// We only care if the name starts with a numeric
name := fi.Name()
if name[0] < '0' || name[0] > '9' {
continue
}

// From this point forward, any errors we just ignore, because
// it might simply be that the process doesn't exist anymore.
pid, err := strconv.ParseInt(name, 10, 0)
if err != nil {
return nil, err
continue
}

for _, fi := range fis {
// We only care about directories, since all pids are dirs
if !fi.IsDir() {
continue
}

// We only care if the name starts with a numeric
name := fi.Name()
if name[0] < '0' || name[0] > '9' {
continue
}

// From this point forward, any errors we just ignore, because
// it might simply be that the process doesn't exist anymore.
pid, err := strconv.ParseInt(name, 10, 0)
if err != nil {
continue
}

p, err := newUnixProcess(int(pid))
if err != nil {
continue
}

results = append(results, p)
p, err := newUnixProcess(int(pid))
if err != nil {
continue
}

results = append(results, p)
}

return results, nil
Expand Down