blob: 391b4cbd11b999b435db34ae25b495037029c676 [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001// 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
14package procfs
15
16import (
17 "fmt"
18 "os"
19 "strconv"
20 "strings"
21)
22
23// Namespace represents a single namespace of a process.
24type 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.
30type Namespaces map[string]Namespace
31
32// Namespaces reads from /proc/<pid>/ns/* to get the namespaces of which the
33// process is a member.
34func (p Proc) Namespaces() (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 {
khenaidood948f772021-08-11 17:49:24 -040043 return nil, fmt.Errorf("failed to read contents of ns dir: %w", err)
khenaidooab1f7bd2019-11-14 14:00:27 -050044 }
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 {
khenaidood948f772021-08-11 17:49:24 -040055 return nil, fmt.Errorf("failed to parse namespace type and inode from %q", target)
khenaidooab1f7bd2019-11-14 14:00:27 -050056 }
57
58 typ := fields[0]
59 inode, err := strconv.ParseUint(strings.Trim(fields[1], "[]"), 10, 32)
60 if err != nil {
khenaidood948f772021-08-11 17:49:24 -040061 return nil, fmt.Errorf("failed to parse inode from %q: %w", fields[1], err)
khenaidooab1f7bd2019-11-14 14:00:27 -050062 }
63
64 ns[name] = Namespace{typ, uint32(inode)}
65 }
66
67 return ns, nil
68}