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

Commit

Permalink
Extract function to return all current process ids
Browse files Browse the repository at this point in the history
This implementation should be more efficient because it only uses the
names under /proc, whereas the previous implementation fetched the
full directory entry for each one, and sorted them too.
  • Loading branch information
bboreham committed Sep 29, 2016
1 parent 0c2774f commit 7149113
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
27 changes: 27 additions & 0 deletions common/process.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package common

import (
"os"
"strconv"
)

func AllPids(procRoot string) ([]int, error) {
fh, err := os.Open(procRoot)
if err != nil {
return nil, err
}
defer fh.Close()
dirNames, err := fh.Readdirnames(-1)
if err != nil {
return nil, err
}
pids := make([]int, len(dirNames))
for _, dirName := range dirNames {
pid, err := strconv.Atoi(dirName)
if err != nil { // Only interested in numeric entries - skip /proc/stat, etc.
continue
}
pids = append(pids, pid)
}
return pids, err
}
12 changes: 5 additions & 7 deletions prog/weaveutil/docker_tls_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,23 @@ import (
"path/filepath"
"strconv"
"strings"

"github.com/weaveworks/weave/common"
)

func dockerTLSArgs(args []string) error {
if len(args) > 0 {
cmdUsage("docker-tls-args", "")
}
procRoot := "/proc"
dirEntries, err := ioutil.ReadDir(procRoot)
pids, err := common.AllPids(procRoot)
if err != nil {
return err
}

for _, dirEntry := range dirEntries {
dirName := dirEntry.Name()
if _, err := strconv.Atoi(dirName); err != nil {
continue
}

for _, pid := range pids {
isDaemon := false
dirName := strconv.Itoa(pid)
if comm, err := ioutil.ReadFile(filepath.Join(procRoot, dirName, "comm")); err != nil {
continue
} else if string(comm) == "dockerd\n" {
Expand Down

0 comments on commit 7149113

Please sign in to comment.