MCPcopy Index your code
hub / github.com/coder/envbox / ReadCGroupSelf

Function ReadCGroupSelf

xunix/sys.go:129–149  ·  view source on GitHub ↗

readCGroup attempts to determine the cgroup for the container by reading the fields of /proc/self/cgroup (third field) We currently only check the first line of /proc/self/cgroup.

(ctx context.Context)

Source from the content-addressed store, hash-verified

127// reading the fields of /proc/self/cgroup (third field)
128// We currently only check the first line of /proc/self/cgroup.
129func ReadCGroupSelf(ctx context.Context) (string, error) {
130 fs := GetFS(ctx)
131 raw, err := afero.ReadFile(fs, "/proc/self/cgroup")
132 if err != nil {
133 return "", xerrors.Errorf("read /proc/self/cgroup: %w", err)
134 }
135
136 lines := bytes.Split(raw, []byte("\n"))
137 if len(lines) == 0 {
138 return "", xerrors.Errorf("unexpected content of /proc/self/cgroup: %s", string(raw))
139 }
140
141 // Just pick the first line.
142 line := lines[0]
143 fields := bytes.Split(line, []byte(":"))
144 if len(fields) != 3 {
145 return "", xerrors.Errorf("expected 3 fields in last line of /proc/self/cgroup: %s", string(raw))
146 }
147
148 return string(fields[2]), nil
149}

Callers 1

readCPUQuotaCGroupV2Function · 0.85

Calls 2

GetFSFunction · 0.85
ErrorfMethod · 0.65

Tested by

no test coverage detected