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
6 // http://www.apache.org/licenses/LICENSE-2.0
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.
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.
29 // Namespaces contains all of the namespaces that the process is contained in.
30 type Namespaces map[string]Namespace
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"))
41 names, err := d.Readdirnames(-1)
43 return nil, fmt.Errorf("failed to read contents of ns dir: %v", err)
46 ns := make(Namespaces, len(names))
47 for _, name := range names {
48 target, err := os.Readlink(p.path("ns", name))
53 fields := strings.SplitN(target, ":", 2)
55 return nil, fmt.Errorf("failed to parse namespace type and inode from '%v'", target)
59 inode, err := strconv.ParseUint(strings.Trim(fields[1], "[]"), 10, 32)
61 return nil, fmt.Errorf("failed to parse inode from '%v': %v", fields[1], err)
64 ns[name] = Namespace{typ, uint32(inode)}