-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess_procfs.go
49 lines (41 loc) · 1004 Bytes
/
process_procfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2021 Tobias Klauser. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build linux || netbsd || solaris
package ps
import (
"fmt"
"os"
"strconv"
"golang.org/x/sys/unix"
)
func processes() ([]Process, error) {
fd, err := unix.Open("/proc", unix.O_DIRECTORY|unix.O_RDONLY, 0)
if err != nil {
return nil, fmt.Errorf("failed to open /proc: %w", err)
}
f := os.NewFile(uintptr(fd), "proc-dir")
defer f.Close()
// Obtain a list of all processes that are currently running.
names, err := f.Readdirnames(-1)
if err != nil {
return nil, err
}
var procs []Process
for _, name := range names {
// Filter out non-process entries
pid, err := strconv.Atoi(name)
if err != nil {
continue
}
proc, err := newUnixProcess(pid)
if err != nil {
continue
}
procs = append(procs, proc)
}
return procs, nil
}
func findProcess(pid int) (Process, error) {
return newUnixProcess(pid)
}