Debian Codegraph @

internal/proc/ns.go

raw symbols SCIP
1 // Copyright 2018 psgo authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package proc
16
17 import (
18 "bufio"
19 "fmt"
20 "io"
21 "os"
22
23 "go.podman.io/storage/pkg/idtools"
24 )
25
26 // ParsePIDNamespace returns the content of /proc/$pid/ns/pid.
27 func ParsePIDNamespace(pid string) (string, error) {
28 pidNS, err := os.Readlink(fmt.Sprintf("/proc/%s/ns/pid", pid))
29 if err != nil {
30 return "", err
31 }
32 return pidNS, nil
33 }
34
35 // ParseUserNamespace returns the content of /proc/$pid/ns/user.
36 func ParseUserNamespace(pid string) (string, error) {
37 userNS, err := os.Readlink(fmt.Sprintf("/proc/%s/ns/user", pid))
38 if err != nil {
39 return "", err
40 }
41 return userNS, nil
42 }
43
44 // ReadMappings reads the user namespace mappings at the specified path
45 func ReadMappings(path string) ([]idtools.IDMap, error) {
46 file, err := os.Open(path)
47 if err != nil {
48 return nil, err
49 }
50 defer file.Close() //nolint:errcheck
51
52 var mappings []idtools.IDMap
53
54 buf := bufio.NewReader(file)
55 for {
56 line, _, err := buf.ReadLine()
57 if err != nil {
58 if err == io.EOF { //nolint:errorlint // False positive, see https://github.com/polyfloyd/go-errorlint/pull/12
59 return mappings, nil
60 }
61 return nil, fmt.Errorf("cannot read line from %s: %w", path, err)
62 }
63 if line == nil {
64 return mappings, nil
65 }
66
67 var containerID, hostID, size int
68 if _, err := fmt.Sscanf(string(line), "%d %d %d", &containerID, &hostID, &size); err != nil {
69 return nil, fmt.Errorf("cannot parse %s: %w", string(line), err)
70 }
71 mappings = append(mappings, idtools.IDMap{ContainerID: containerID, HostID: hostID, Size: size})
72 }
73 }