@@ -38,7 +38,7 @@ type _Ctype_struct___0 struct {
38
38
func pidsWithContext (ctx context.Context ) ([]int32 , error ) {
39
39
var ret []int32
40
40
41
- pids , err := callPsWithContext (ctx , "pid" , 0 , false )
41
+ pids , err := callPsWithContext (ctx , "pid" , 0 , false , false )
42
42
if err != nil {
43
43
return ret , err
44
44
}
@@ -55,7 +55,7 @@ func pidsWithContext(ctx context.Context) ([]int32, error) {
55
55
}
56
56
57
57
func (p * Process ) PpidWithContext (ctx context.Context ) (int32 , error ) {
58
- r , err := callPsWithContext (ctx , "ppid" , p .Pid , false )
58
+ r , err := callPsWithContext (ctx , "ppid" , p .Pid , false , false )
59
59
if err != nil {
60
60
return 0 , err
61
61
}
@@ -76,16 +76,16 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
76
76
name := common .IntToString (k .Proc .P_comm [:])
77
77
78
78
if len (name ) >= 15 {
79
- cmdlineSlice , err := p .CmdlineSliceWithContext (ctx )
79
+ cmdName , err := p .cmdNameWithContext (ctx )
80
80
if err != nil {
81
81
return "" , err
82
82
}
83
- if len (cmdlineSlice ) > 0 {
84
- extendedName := filepath .Base (cmdlineSlice [0 ])
83
+ if len (cmdName ) > 0 {
84
+ extendedName := filepath .Base (cmdName [0 ])
85
85
if strings .HasPrefix (extendedName , p .name ) {
86
86
name = extendedName
87
87
} else {
88
- name = cmdlineSlice [0 ]
88
+ name = cmdName [0 ]
89
89
}
90
90
}
91
91
}
@@ -94,28 +94,37 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
94
94
}
95
95
96
96
func (p * Process ) CmdlineWithContext (ctx context.Context ) (string , error ) {
97
- r , err := callPsWithContext (ctx , "command" , p .Pid , false )
97
+ r , err := callPsWithContext (ctx , "command" , p .Pid , false , false )
98
98
if err != nil {
99
99
return "" , err
100
100
}
101
101
return strings .Join (r [0 ], " " ), err
102
102
}
103
103
104
+ // cmdNameWithContext returns the command name (including spaces) without any arguments
105
+ func (p * Process ) cmdNameWithContext (ctx context.Context ) ([]string , error ) {
106
+ r , err := callPsWithContext (ctx , "command" , p .Pid , false , true )
107
+ if err != nil {
108
+ return nil , err
109
+ }
110
+ return r [0 ], err
111
+ }
112
+
104
113
// CmdlineSliceWithContext returns the command line arguments of the process as a slice with each
105
114
// element being an argument. Because of current deficiencies in the way that the command
106
115
// line arguments are found, single arguments that have spaces in the will actually be
107
116
// reported as two separate items. In order to do something better CGO would be needed
108
117
// to use the native darwin functions.
109
118
func (p * Process ) CmdlineSliceWithContext (ctx context.Context ) ([]string , error ) {
110
- r , err := callPsWithContext (ctx , "command" , p .Pid , false )
119
+ r , err := callPsWithContext (ctx , "command" , p .Pid , false , false )
111
120
if err != nil {
112
121
return nil , err
113
122
}
114
123
return r [0 ], err
115
124
}
116
125
117
126
func (p * Process ) createTimeWithContext (ctx context.Context ) (int64 , error ) {
118
- r , err := callPsWithContext (ctx , "etime" , p .Pid , false )
127
+ r , err := callPsWithContext (ctx , "etime" , p .Pid , false , false )
119
128
if err != nil {
120
129
return 0 , err
121
130
}
@@ -163,7 +172,7 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
163
172
}
164
173
165
174
func (p * Process ) StatusWithContext (ctx context.Context ) (string , error ) {
166
- r , err := callPsWithContext (ctx , "state" , p .Pid , false )
175
+ r , err := callPsWithContext (ctx , "state" , p .Pid , false , false )
167
176
if err != nil {
168
177
return "" , err
169
178
}
@@ -255,7 +264,7 @@ func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, e
255
264
}
256
265
257
266
func (p * Process ) NumThreadsWithContext (ctx context.Context ) (int32 , error ) {
258
- r , err := callPsWithContext (ctx , "utime,stime" , p .Pid , true )
267
+ r , err := callPsWithContext (ctx , "utime,stime" , p .Pid , true , false )
259
268
if err != nil {
260
269
return 0 , err
261
270
}
@@ -309,7 +318,7 @@ func convertCPUTimes(s string) (ret float64, err error) {
309
318
}
310
319
311
320
func (p * Process ) TimesWithContext (ctx context.Context ) (* cpu.TimesStat , error ) {
312
- r , err := callPsWithContext (ctx , "utime,stime" , p .Pid , false )
321
+ r , err := callPsWithContext (ctx , "utime,stime" , p .Pid , false , false )
313
322
314
323
if err != nil {
315
324
return nil , err
@@ -333,7 +342,7 @@ func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error)
333
342
}
334
343
335
344
func (p * Process ) MemoryInfoWithContext (ctx context.Context ) (* MemoryInfoStat , error ) {
336
- r , err := callPsWithContext (ctx , "rss,vsize,pagein" , p .Pid , false )
345
+ r , err := callPsWithContext (ctx , "rss,vsize,pagein" , p .Pid , false , false )
337
346
if err != nil {
338
347
return nil , err
339
348
}
@@ -419,9 +428,9 @@ func (p *Process) getKProc() (*KinfoProc, error) {
419
428
420
429
// call ps command.
421
430
// Return value deletes Header line(you must not input wrong arg).
422
- // And splited by Space . Caller have responsibility to manage.
431
+ // And split by space . Caller have responsibility to manage.
423
432
// If passed arg pid is 0, get information from all process.
424
- func callPsWithContext (ctx context.Context , arg string , pid int32 , threadOption bool ) ([][]string , error ) {
433
+ func callPsWithContext (ctx context.Context , arg string , pid int32 , threadOption bool , nameOption bool ) ([][]string , error ) {
425
434
bin , err := exec .LookPath ("ps" )
426
435
if err != nil {
427
436
return [][]string {}, err
@@ -435,6 +444,10 @@ func callPsWithContext(ctx context.Context, arg string, pid int32, threadOption
435
444
} else {
436
445
cmd = []string {"-x" , "-o" , arg , "-p" , strconv .Itoa (int (pid ))}
437
446
}
447
+
448
+ if nameOption {
449
+ cmd = append (cmd , "-c" )
450
+ }
438
451
out , err := invoke .CommandWithContext (ctx , bin , cmd ... )
439
452
if err != nil {
440
453
return [][]string {}, err
@@ -443,13 +456,19 @@ func callPsWithContext(ctx context.Context, arg string, pid int32, threadOption
443
456
444
457
var ret [][]string
445
458
for _ , l := range lines [1 :] {
459
+
446
460
var lr []string
447
- for _ , r := range strings .Split (l , " " ) {
448
- if r == "" {
449
- continue
461
+ if nameOption {
462
+ lr = append (lr , l )
463
+ } else {
464
+ for _ , r := range strings .Split (l , " " ) {
465
+ if r == "" {
466
+ continue
467
+ }
468
+ lr = append (lr , strings .TrimSpace (r ))
450
469
}
451
- lr = append (lr , strings .TrimSpace (r ))
452
470
}
471
+
453
472
if len (lr ) != 0 {
454
473
ret = append (ret , lr )
455
474
}
0 commit comments