Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Canread
Gopsutil
提交
61f064fe
G
Gopsutil
项目概览
Canread
/
Gopsutil
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
Gopsutil
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
61f064fe
编写于
10月 30, 2021
作者:
S
shirou
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'rschmied-dev'
上级
e032a1c8
45a12871
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
145 addition
and
26 deletion
+145
-26
v3/process/process.go
v3/process/process.go
+1
-0
v3/process/process_bsd.go
v3/process/process_bsd.go
+0
-5
v3/process/process_darwin.go
v3/process/process_darwin.go
+0
-21
v3/process/process_darwin_cgo.go
v3/process/process_darwin_cgo.go
+109
-0
v3/process/process_darwin_nocgo.go
v3/process/process_darwin_nocgo.go
+27
-0
v3/process/process_freebsd.go
v3/process/process_freebsd.go
+4
-0
v3/process/process_openbsd.go
v3/process/process_openbsd.go
+4
-0
未找到文件。
v3/process/process.go
浏览文件 @
61f064fe
...
...
@@ -19,6 +19,7 @@ var (
invoke
common
.
Invoker
=
common
.
Invoke
{}
ErrorNoChildren
=
errors
.
New
(
"process does not have children"
)
ErrorProcessNotRunning
=
errors
.
New
(
"process does not exist"
)
ErrorNotPermitted
=
errors
.
New
(
"operation not permitted"
)
)
type
Process
struct
{
...
...
v3/process/process_bsd.go
浏览文件 @
61f064fe
...
...
@@ -19,10 +19,6 @@ func (p *Process) TgidWithContext(ctx context.Context) (int32, error) {
return
0
,
common
.
ErrNotImplementedError
}
func
(
p
*
Process
)
CwdWithContext
(
ctx
context
.
Context
)
(
string
,
error
)
{
return
""
,
common
.
ErrNotImplementedError
}
func
(
p
*
Process
)
IOniceWithContext
(
ctx
context
.
Context
)
(
int32
,
error
)
{
return
0
,
common
.
ErrNotImplementedError
}
...
...
@@ -77,4 +73,3 @@ func parseKinfoProc(buf []byte) (KinfoProc, error) {
err
:=
common
.
Read
(
br
,
binary
.
LittleEndian
,
&
k
)
return
k
,
err
}
v3/process/process_darwin.go
浏览文件 @
61f064fe
...
...
@@ -100,14 +100,6 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
return
name
,
nil
}
func
(
p
*
Process
)
CmdlineWithContext
(
ctx
context
.
Context
)
(
string
,
error
)
{
r
,
err
:=
callPsWithContext
(
ctx
,
"command"
,
p
.
Pid
,
false
,
false
)
if
err
!=
nil
{
return
""
,
err
}
return
strings
.
Join
(
r
[
0
],
" "
),
err
}
// cmdNameWithContext returns the command name (including spaces) without any arguments
func
(
p
*
Process
)
cmdNameWithContext
(
ctx
context
.
Context
)
([]
string
,
error
)
{
r
,
err
:=
callPsWithContext
(
ctx
,
"command"
,
p
.
Pid
,
false
,
true
)
...
...
@@ -117,19 +109,6 @@ func (p *Process) cmdNameWithContext(ctx context.Context) ([]string, error) {
return
r
[
0
],
err
}
// CmdlineSliceWithContext returns the command line arguments of the process as a slice with each
// element being an argument. Because of current deficiencies in the way that the command
// line arguments are found, single arguments that have spaces in the will actually be
// reported as two separate items. In order to do something better CGO would be needed
// to use the native darwin functions.
func
(
p
*
Process
)
CmdlineSliceWithContext
(
ctx
context
.
Context
)
([]
string
,
error
)
{
r
,
err
:=
callPsWithContext
(
ctx
,
"command"
,
p
.
Pid
,
false
,
false
)
if
err
!=
nil
{
return
nil
,
err
}
return
r
[
0
],
err
}
func
(
p
*
Process
)
createTimeWithContext
(
ctx
context
.
Context
)
(
int64
,
error
)
{
r
,
err
:=
callPsWithContext
(
ctx
,
"etime"
,
p
.
Pid
,
false
,
false
)
if
err
!=
nil
{
...
...
v3/process/process_darwin_cgo.go
浏览文件 @
61f064fe
...
...
@@ -5,13 +5,39 @@ package process
// #include <stdlib.h>
// #include <libproc.h>
// #include <string.h>
// #include <sys/errno.h>
// #include <sys/proc_info.h>
// #include <sys/sysctl.h>
import
"C"
import
(
"bytes"
"context"
"fmt"
"strings"
"syscall"
"unsafe"
)
var
argMax
int
func
init
()
{
argMax
=
getArgMax
()
}
func
getArgMax
()
int
{
var
(
mib
=
[
...
]
C
.
int
{
C
.
CTL_KERN
,
C
.
KERN_ARGMAX
}
argmax
C
.
int
size
C
.
size_t
=
C
.
ulong
(
unsafe
.
Sizeof
(
argmax
))
)
retval
:=
C
.
sysctl
(
&
mib
[
0
],
2
,
unsafe
.
Pointer
(
&
argmax
),
&
size
,
C
.
NULL
,
0
)
if
retval
==
0
{
return
int
(
argmax
)
}
return
0
}
func
(
p
*
Process
)
ExeWithContext
(
ctx
context
.
Context
)
(
string
,
error
)
{
var
c
C
.
char
// need a var for unsafe.Sizeof need a var
const
bufsize
=
C
.
PROC_PIDPATHINFO_MAXSIZE
*
unsafe
.
Sizeof
(
c
)
...
...
@@ -28,3 +54,86 @@ func (p *Process) ExeWithContext(ctx context.Context) (string, error) {
return
C
.
GoString
(
buffer
),
nil
}
// CwdWithContext retrieves the Current Working Directory for the given process.
// It uses the proc_pidinfo from libproc and will only work for processes the
// EUID can access. Otherwise "operation not permitted" will be returned as the
// error.
// Note: This might also work for other *BSD OSs.
func
(
p
*
Process
)
CwdWithContext
(
ctx
context
.
Context
)
(
string
,
error
)
{
const
vpiSize
=
C
.
sizeof_struct_proc_vnodepathinfo
vpi
:=
(
*
C
.
struct_proc_vnodepathinfo
)(
C
.
malloc
(
vpiSize
))
defer
C
.
free
(
unsafe
.
Pointer
(
vpi
))
ret
,
err
:=
C
.
proc_pidinfo
(
C
.
int
(
p
.
Pid
),
C
.
PROC_PIDVNODEPATHINFO
,
0
,
unsafe
.
Pointer
(
vpi
),
vpiSize
)
if
err
!=
nil
{
// fmt.Printf("ret: %d %T\n", ret, err)
if
err
==
syscall
.
EPERM
{
return
""
,
ErrorNotPermitted
}
return
""
,
err
}
if
ret
<=
0
{
return
""
,
fmt
.
Errorf
(
"unknown error: proc_pidinfo returned %d"
,
ret
)
}
if
ret
!=
C
.
sizeof_struct_proc_vnodepathinfo
{
return
""
,
fmt
.
Errorf
(
"too few bytes; expected %d, got %d"
,
vpiSize
,
ret
)
}
return
C
.
GoString
(
&
vpi
.
pvi_cdir
.
vip_path
[
0
]),
err
}
func
procArgs
(
pid
int32
)
(
*
[]
byte
,
int
,
error
)
{
var
(
mib
=
[
...
]
C
.
int
{
C
.
CTL_KERN
,
C
.
KERN_PROCARGS2
,
C
.
int
(
pid
)}
size
C
.
size_t
=
C
.
ulong
(
argMax
)
nargs
C
.
int
result
[]
byte
)
procargs
:=
(
*
C
.
char
)(
C
.
malloc
(
C
.
ulong
(
argMax
)))
defer
C
.
free
(
unsafe
.
Pointer
(
procargs
))
retval
:=
C
.
sysctl
(
&
mib
[
0
],
3
,
unsafe
.
Pointer
(
procargs
),
&
size
,
C
.
NULL
,
0
)
if
retval
==
0
{
C
.
memcpy
(
unsafe
.
Pointer
(
&
nargs
),
unsafe
.
Pointer
(
procargs
),
C
.
sizeof_int
)
result
=
C
.
GoBytes
(
unsafe
.
Pointer
(
procargs
),
C
.
int
(
size
))
// fmt.Printf("size: %d %d\n%s\n", size, nargs, hex.Dump(result))
return
&
result
,
int
(
nargs
),
nil
}
return
nil
,
0
,
fmt
.
Errorf
(
"error: %d"
,
retval
)
}
func
(
p
*
Process
)
CmdlineSliceWithContext
(
ctx
context
.
Context
)
([]
string
,
error
)
{
pargs
,
nargs
,
err
:=
procArgs
(
p
.
Pid
)
if
err
!=
nil
{
return
nil
,
err
}
// The first bytes hold the nargs int, skip it.
args
:=
bytes
.
Split
((
*
pargs
)[
C
.
sizeof_int
:
],
[]
byte
{
0
})
var
argStr
string
// The first element is the actual binary/command path.
// command := args[0]
var
argSlice
[]
string
// var envSlice []string
// All other, non-zero elements are arguments. The first "nargs" elements
// are the arguments. Everything else in the slice is then the environment
// of the process.
for
_
,
arg
:=
range
args
[
1
:
]
{
argStr
=
string
(
arg
[
:
])
if
len
(
argStr
)
>
0
{
if
nargs
>
0
{
argSlice
=
append
(
argSlice
,
argStr
)
nargs
--
continue
}
break
// envSlice = append(envSlice, argStr)
}
}
return
argSlice
,
err
}
func
(
p
*
Process
)
CmdlineWithContext
(
ctx
context
.
Context
)
(
string
,
error
)
{
r
,
err
:=
p
.
CmdlineSliceWithContext
(
ctx
)
if
err
!=
nil
{
return
""
,
err
}
return
strings
.
Join
(
r
,
" "
),
err
}
v3/process/process_darwin_nocgo.go
浏览文件 @
61f064fe
...
...
@@ -9,8 +9,14 @@ import (
"os/exec"
"strconv"
"strings"
"github.com/shirou/gopsutil/v3/internal/common"
)
func
(
p
*
Process
)
CwdWithContext
(
ctx
context
.
Context
)
(
string
,
error
)
{
return
""
,
common
.
ErrNotImplementedError
}
func
(
p
*
Process
)
ExeWithContext
(
ctx
context
.
Context
)
(
string
,
error
)
{
lsof_bin
,
err
:=
exec
.
LookPath
(
"lsof"
)
if
err
!=
nil
{
...
...
@@ -32,3 +38,24 @@ func (p *Process) ExeWithContext(ctx context.Context) (string, error) {
}
return
""
,
fmt
.
Errorf
(
"missing txt data returned by lsof"
)
}
func
(
p
*
Process
)
CmdlineWithContext
(
ctx
context
.
Context
)
(
string
,
error
)
{
r
,
err
:=
callPsWithContext
(
ctx
,
"command"
,
p
.
Pid
,
false
,
false
)
if
err
!=
nil
{
return
""
,
err
}
return
strings
.
Join
(
r
[
0
],
" "
),
err
}
// CmdlineSliceWithContext returns the command line arguments of the process as a slice with each
// element being an argument. Because of current deficiencies in the way that the command
// line arguments are found, single arguments that have spaces in the will actually be
// reported as two separate items. In order to do something better CGO would be needed
// to use the native darwin functions.
func
(
p
*
Process
)
CmdlineSliceWithContext
(
ctx
context
.
Context
)
([]
string
,
error
)
{
r
,
err
:=
callPsWithContext
(
ctx
,
"command"
,
p
.
Pid
,
false
,
false
)
if
err
!=
nil
{
return
nil
,
err
}
return
r
[
0
],
err
}
v3/process/process_freebsd.go
浏览文件 @
61f064fe
...
...
@@ -64,6 +64,10 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
return
name
,
nil
}
func
(
p
*
Process
)
CwdWithContext
(
ctx
context
.
Context
)
(
string
,
error
)
{
return
""
,
common
.
ErrNotImplementedError
}
func
(
p
*
Process
)
ExeWithContext
(
ctx
context
.
Context
)
(
string
,
error
)
{
return
""
,
common
.
ErrNotImplementedError
}
...
...
v3/process/process_openbsd.go
浏览文件 @
61f064fe
...
...
@@ -69,6 +69,10 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
return
name
,
nil
}
func
(
p
*
Process
)
CwdWithContext
(
ctx
context
.
Context
)
(
string
,
error
)
{
return
""
,
common
.
ErrNotImplementedError
}
func
(
p
*
Process
)
ExeWithContext
(
ctx
context
.
Context
)
(
string
,
error
)
{
return
""
,
common
.
ErrNotImplementedError
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录