Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / github.com / prometheus / procfs / proc_ns.go
1 // Copyright 2018 The Prometheus Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package procfs
15
16 import (
17         "fmt"
18         "os"
19         "strconv"
20         "strings"
21 )
22
23 // Namespace represents a single namespace of a process.
24 type Namespace struct {
25         Type  string // Namespace type.
26         Inode uint32 // Inode number of the namespace. If two processes are in the same namespace their inodes will match.
27 }
28
29 // Namespaces contains all of the namespaces that the process is contained in.
30 type Namespaces map[string]Namespace
31
32 // NewNamespaces reads from /proc/[pid/ns/* to get the namespaces of which the
33 // process is a member.
34 func (p Proc) NewNamespaces() (Namespaces, error) {
35         d, err := os.Open(p.path("ns"))
36         if err != nil {
37                 return nil, err
38         }
39         defer d.Close()
40
41         names, err := d.Readdirnames(-1)
42         if err != nil {
43                 return nil, fmt.Errorf("failed to read contents of ns dir: %v", err)
44         }
45
46         ns := make(Namespaces, len(names))
47         for _, name := range names {
48                 target, err := os.Readlink(p.path("ns", name))
49                 if err != nil {
50                         return nil, err
51                 }
52
53                 fields := strings.SplitN(target, ":", 2)
54                 if len(fields) != 2 {
55                         return nil, fmt.Errorf("failed to parse namespace type and inode from '%v'", target)
56                 }
57
58                 typ := fields[0]
59                 inode, err := strconv.ParseUint(strings.Trim(fields[1], "[]"), 10, 32)
60                 if err != nil {
61                         return nil, fmt.Errorf("failed to parse inode from '%v': %v", fields[1], err)
62                 }
63
64                 ns[name] = Namespace{typ, uint32(inode)}
65         }
66
67         return ns, nil
68 }