Skip to content

Commit 5e7cce4

Browse files
committed
Allow to use the zero value for pid to operate with the current task
In this case we can use /proc/self/, which is correct even if a task live in another pid namespace. Signed-off-by: Andrey Vagin <avagin@openvz.org>
1 parent 3c85049 commit 5e7cce4

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

capability/capability.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ type Capabilities interface {
6060
Apply(kind CapType) error
6161
}
6262

63-
// NewPid create new initialized Capabilities object for given pid.
63+
// NewPid create new initialized Capabilities object for given pid when it
64+
// is nonzero, or for the current pid if pid is 0
6465
func NewPid(pid int) (Capabilities, error) {
6566
return newPid(pid)
6667
}

capability/capability_linux.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,15 @@ func (c *capsV3) Load() (err error) {
351351
return
352352
}
353353

354-
f, err := os.Open(fmt.Sprintf("/proc/%d/status", c.hdr.pid))
354+
var status_path string
355+
356+
if c.hdr.pid == 0 {
357+
status_path = fmt.Sprintf("/proc/self/status")
358+
} else {
359+
status_path = fmt.Sprintf("/proc/%d/status", c.hdr.pid)
360+
}
361+
362+
f, err := os.Open(status_path)
355363
if err != nil {
356364
return
357365
}

0 commit comments

Comments
 (0)