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.
25 // Proc provides information about a running process.
33 // Procs represents a list of Proc structs.
36 func (p Procs) Len() int { return len(p) }
37 func (p Procs) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
38 func (p Procs) Less(i, j int) bool { return p[i].PID < p[j].PID }
40 // Self returns a process for the current process read via /proc/self.
41 func Self() (Proc, error) {
42 fs, err := NewFS(DefaultMountPoint)
49 // NewProc returns a process for the given pid under /proc.
50 func NewProc(pid int) (Proc, error) {
51 fs, err := NewFS(DefaultMountPoint)
55 return fs.NewProc(pid)
58 // AllProcs returns a list of all currently available processes under /proc.
59 func AllProcs() (Procs, error) {
60 fs, err := NewFS(DefaultMountPoint)
67 // Self returns a process for the current process.
68 func (fs FS) Self() (Proc, error) {
69 p, err := os.Readlink(fs.Path("self"))
73 pid, err := strconv.Atoi(strings.Replace(p, string(fs), "", -1))
77 return fs.NewProc(pid)
80 // NewProc returns a process for the given pid.
81 func (fs FS) NewProc(pid int) (Proc, error) {
82 if _, err := os.Stat(fs.Path(strconv.Itoa(pid))); err != nil {
85 return Proc{PID: pid, fs: fs}, nil
88 // AllProcs returns a list of all currently available processes.
89 func (fs FS) AllProcs() (Procs, error) {
90 d, err := os.Open(fs.Path())
96 names, err := d.Readdirnames(-1)
98 return Procs{}, fmt.Errorf("could not read %s: %s", d.Name(), err)
102 for _, n := range names {
103 pid, err := strconv.ParseInt(n, 10, 64)
107 p = append(p, Proc{PID: int(pid), fs: fs})
113 // CmdLine returns the command line of a process.
114 func (p Proc) CmdLine() ([]string, error) {
115 f, err := os.Open(p.path("cmdline"))
121 data, err := ioutil.ReadAll(f)
127 return []string{}, nil
130 return strings.Split(string(bytes.TrimRight(data, string("\x00"))), string(byte(0))), nil
133 // Comm returns the command name of a process.
134 func (p Proc) Comm() (string, error) {
135 f, err := os.Open(p.path("comm"))
141 data, err := ioutil.ReadAll(f)
146 return strings.TrimSpace(string(data)), nil
149 // Executable returns the absolute path of the executable command of a process.
150 func (p Proc) Executable() (string, error) {
151 exe, err := os.Readlink(p.path("exe"))
152 if os.IsNotExist(err) {
159 // Cwd returns the absolute path to the current working directory of the process.
160 func (p Proc) Cwd() (string, error) {
161 wd, err := os.Readlink(p.path("cwd"))
162 if os.IsNotExist(err) {
169 // RootDir returns the absolute path to the process's root directory (as set by chroot)
170 func (p Proc) RootDir() (string, error) {
171 rdir, err := os.Readlink(p.path("root"))
172 if os.IsNotExist(err) {
179 // FileDescriptors returns the currently open file descriptors of a process.
180 func (p Proc) FileDescriptors() ([]uintptr, error) {
181 names, err := p.fileDescriptors()
186 fds := make([]uintptr, len(names))
187 for i, n := range names {
188 fd, err := strconv.ParseInt(n, 10, 32)
190 return nil, fmt.Errorf("could not parse fd %s: %s", n, err)
198 // FileDescriptorTargets returns the targets of all file descriptors of a process.
199 // If a file descriptor is not a symlink to a file (like a socket), that value will be the empty string.
200 func (p Proc) FileDescriptorTargets() ([]string, error) {
201 names, err := p.fileDescriptors()
206 targets := make([]string, len(names))
208 for i, name := range names {
209 target, err := os.Readlink(p.path("fd", name))
218 // FileDescriptorsLen returns the number of currently open file descriptors of
220 func (p Proc) FileDescriptorsLen() (int, error) {
221 fds, err := p.fileDescriptors()
229 // MountStats retrieves statistics and configuration for mount points in a
230 // process's namespace.
231 func (p Proc) MountStats() ([]*Mount, error) {
232 f, err := os.Open(p.path("mountstats"))
238 return parseMountStats(f)
241 func (p Proc) fileDescriptors() ([]string, error) {
242 d, err := os.Open(p.path("fd"))
248 names, err := d.Readdirnames(-1)
250 return nil, fmt.Errorf("could not read %s: %s", d.Name(), err)
256 func (p Proc) path(pa ...string) string {
257 return p.fs.Path(append([]string{strconv.Itoa(p.PID)}, pa...)...)