From 8b72cf0f910f2893df421e4ec738187750281bd4 Mon Sep 17 00:00:00 2001 From: Jonathan Boulle Date: Fri, 23 Jan 2015 21:44:53 -0800 Subject: [PATCH] pkg: remove unused code --- pkg/io/writer.go | 37 --------- pkg/proc/proc.go | 210 ----------------------------------------------- test | 2 +- 3 files changed, 1 insertion(+), 248 deletions(-) delete mode 100644 pkg/io/writer.go delete mode 100644 pkg/proc/proc.go diff --git a/pkg/io/writer.go b/pkg/io/writer.go deleted file mode 100644 index 187a7f1..0000000 --- a/pkg/io/writer.go +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2014 CoreOS, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package io - -import "io" - -// LimitedWriter is similar to io.LimitedReader; it writes to W but -// limits the amount of data written to just N bytes. Each subsequent call to -// Write() will return a nil error and a count of 0. -type LimitedWriter struct { - W io.Writer - N int64 -} - -func (l *LimitedWriter) Write(data []byte) (n int, err error) { - if l.N <= 0 { - return 0, nil - } - if int64(len(data)) > l.N { - data = data[0:l.N] - } - n, err = l.W.Write(data) - l.N -= int64(n) - return -} diff --git a/pkg/proc/proc.go b/pkg/proc/proc.go deleted file mode 100644 index 3181f58..0000000 --- a/pkg/proc/proc.go +++ /dev/null @@ -1,210 +0,0 @@ -// Copyright 2014 CoreOS, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package proc - -import ( - "bufio" - "errors" - "io" - "io/ioutil" - "os" - "path" - "path/filepath" - "regexp" - "strconv" - "strings" -) - -var ( - ErrNotRoot = errors.New("must be root to access all process information") - - // socket symlinks in /proc//fds take the form: - // type:[inode] - sre = regexp.MustCompile(`^([[:alpha:]]+):\[([[:digit:]]+)\]$`) - // we hardcode the group indices because golang doesn't make named groups any easier :< - sreTypei = 1 - sreInodei = 2 -) - -const ( - procfs = "/proc/" - unixSocks = "/proc/net/unix" -) - -// LiveProcs is similar to `man 1 fuser`; it takes a prefix and returns a map -// of PIDs of any processes accessing files with the prefix. -// A process is considered to be accessing a file if it has an open file -// descriptor directly referencing the file, has an open Unix socket -// referencing a file, or has a file mapped into memory. -// This operation is inherently racy (both false positives and false negatives -// are possible) and hence this should be considered an approximation only. -// TODO(jonboulle): map filename(string) -> []int(pids) instead -func LiveProcs(prefix string) (map[int][]string, error) { - if os.Getegid() != 0 { - return nil, ErrNotRoot - } - skts, err := unixSocketsWithPrefix(prefix) - if err != nil { - return nil, err - } - ps, err := ioutil.ReadDir(procfs) - if err != nil { - return nil, err - } - pids := make(map[int][]string) - self := os.Getpid() - for _, p := range ps { - pid, err := strconv.Atoi(p.Name()) - if err != nil { - continue - } - if pid == self { - continue - } - - // Parse file descriptors - pdir := filepath.Join(procfs, p.Name(), "fd") - fds, err := ioutil.ReadDir(pdir) - switch { - case err == nil: - case os.IsNotExist(err): - // assume we're too late - continue - default: - return nil, err - } - links := make([]string, len(fds)) - for i, fd := range fds { - links[i] = path.Join(pdir, fd.Name()) - } - - for _, path := range fdsWithPrefix(links, prefix, skts) { - if _, ok := pids[pid]; ok { - pids[pid] = make([]string, 0) - } - pids[pid] = append(pids[pid], path) - } - - // Parse maps - mfile := filepath.Join(procfs, p.Name(), "maps") - mfh, err := os.Open(mfile) - switch { - case err == nil: - case os.IsNotExist(err): - // assume we're too late - continue - default: - return nil, err - } - paths, err := mmapsWithPrefix(mfh, prefix) - mfh.Close() - if err != nil { - return nil, err - } - for _, path := range paths { - if _, ok := pids[pid]; ok { - pids[pid] = make([]string, 0) - } - pids[pid] = append(pids[pid], path) - } - - } - return pids, nil -} - -// unixSocketsWithPrefix returns a map (of inode->path) describing the Unix -// sockets open on the system accessing any paths with the given prefix -func unixSocketsWithPrefix(pre string) (map[string]string, error) { - fh, err := os.Open(unixSocks) - if err != nil { - return nil, err - } - defer fh.Close() - socks := make(map[string]string) - scanner := bufio.NewScanner(fh) - for scanner.Scan() { - if err := scanner.Err(); err != nil { - return nil, err - } - line := scanner.Text() - parts := strings.Fields(line) - if len(parts) != 8 { - continue - } - // lines are of the form: - // Num RefCount Protocol Flags Type St Inode Path - inode := parts[6] - pathname := parts[7] - pathname = strings.TrimPrefix(pathname, "@") - if strings.HasPrefix(pathname, pre) { - socks[inode] = pathname - } - } - return socks, nil -} - -// fdsWithPrefix takes a list of fds (which should represent absolute paths to -// file descriptor links, i.e. as in /proc//fd/), determines which files -// the fds represent, and returns a subset of those files that have the given -// prefix. -func fdsWithPrefix(fds []string, pre string, socks map[string]string) []string { - var paths []string - for _, fd := range fds { - dest, err := os.Readlink(fd) - if err != nil { - continue - } - // Simple case: fd directly references a file we're interested in - if strings.HasPrefix(dest, pre) { - paths = append(paths, dest) - } - // Check for Unix sockets - if m := sre.FindStringSubmatch(dest); m != nil { - if m[sreTypei] != "socket" { - continue - } - if p, ok := socks[m[sreInodei]]; ok { - paths = append(paths, p) - } - } - } - return paths -} - -// mmapsWithPrefix takes a Reader which should represent a file in the -// /proc/*/maps format (`man 5 proc`), and returns a list of paths in the map -// that have the given prefix -func mmapsWithPrefix(r io.Reader, pre string) ([]string, error) { - var paths []string - scanner := bufio.NewScanner(r) - for scanner.Scan() { - if err := scanner.Err(); err != nil { - return nil, err - } - line := scanner.Text() - parts := strings.Fields(line) - // Lines are of the form: - // address perms offset dev inode - // address perms offset dev inode pathname - if len(parts) < 6 { - continue - } - pathname := parts[5] - if strings.HasPrefix(pathname, pre) { - paths = append(paths, pathname) - } - } - return paths, nil -} diff --git a/test b/test index f9d1ec1..8476ad7 100755 --- a/test +++ b/test @@ -15,7 +15,7 @@ COVER=${COVER:-"-cover"} source ./build TESTABLE_AND_FORMATTABLE="cas pkg/keystore pkg/lock pkg/tar rkt stage1/init" -FORMATTABLE="$TESTABLE_AND_FORMATTABLE networking path pkg/io pkg/proc stage0/run.go version" +FORMATTABLE="$TESTABLE_AND_FORMATTABLE networking path stage0/run.go version" # user has not provided PKG override if [ -z "$PKG" ]; then