Make /proc/[pid] offset start at TGID_OFFSET

Updates #1195

PiperOrigin-RevId: 288725745
This commit is contained in:
Fabricio Voznika
2020-01-08 10:45:12 -08:00
committed by gVisor bot
parent 9df018767c
commit db376e1392
2 changed files with 231 additions and 34 deletions
+87 -31
View File
@@ -27,7 +27,11 @@ import (
"gvisor.dev/gvisor/pkg/syserror"
)
const defaultPermission = 0444
const (
defaultPermission = 0444
selfName = "self"
threadSelfName = "thread-self"
)
// InoGenerator generates unique inode numbers for a given filesystem.
type InoGenerator interface {
@@ -45,6 +49,11 @@ type tasksInode struct {
inoGen InoGenerator
pidns *kernel.PIDNamespace
// '/proc/self' and '/proc/thread-self' have custom directory offsets in
// Linux. So handle them outside of OrderedChildren.
selfSymlink *vfs.Dentry
threadSelfSymlink *vfs.Dentry
}
var _ kernfs.Inode = (*tasksInode)(nil)
@@ -54,20 +63,20 @@ func newTasksInode(inoGen InoGenerator, k *kernel.Kernel, pidns *kernel.PIDNames
contents := map[string]*kernfs.Dentry{
//"cpuinfo": newCPUInfo(ctx, msrc),
//"filesystems": seqfile.NewSeqFileInode(ctx, &filesystemsData{}, msrc),
"loadavg": newDentry(root, inoGen.NextIno(), defaultPermission, &loadavgData{}),
"meminfo": newDentry(root, inoGen.NextIno(), defaultPermission, &meminfoData{k: k}),
"mounts": kernfs.NewStaticSymlink(root, inoGen.NextIno(), defaultPermission, "self/mounts"),
"self": newSelfSymlink(root, inoGen.NextIno(), defaultPermission, pidns),
"stat": newDentry(root, inoGen.NextIno(), defaultPermission, &statData{k: k}),
"thread-self": newThreadSelfSymlink(root, inoGen.NextIno(), defaultPermission, pidns),
"loadavg": newDentry(root, inoGen.NextIno(), defaultPermission, &loadavgData{}),
"meminfo": newDentry(root, inoGen.NextIno(), defaultPermission, &meminfoData{k: k}),
"mounts": kernfs.NewStaticSymlink(root, inoGen.NextIno(), defaultPermission, "self/mounts"),
"stat": newDentry(root, inoGen.NextIno(), defaultPermission, &statData{k: k}),
//"uptime": newUptime(ctx, msrc),
//"version": newVersionData(root, inoGen.NextIno(), k),
"version": newDentry(root, inoGen.NextIno(), defaultPermission, &versionData{k: k}),
}
inode := &tasksInode{
pidns: pidns,
inoGen: inoGen,
pidns: pidns,
inoGen: inoGen,
selfSymlink: newSelfSymlink(root, inoGen.NextIno(), 0444, pidns).VFSDentry(),
threadSelfSymlink: newThreadSelfSymlink(root, inoGen.NextIno(), 0444, pidns).VFSDentry(),
}
inode.InodeAttrs.Init(root, inoGen.NextIno(), linux.ModeDirectory|0555)
@@ -86,6 +95,13 @@ func (i *tasksInode) Lookup(ctx context.Context, name string) (*vfs.Dentry, erro
// Try to lookup a corresponding task.
tid, err := strconv.ParseUint(name, 10, 64)
if err != nil {
// If it failed to parse, check if it's one of the special handled files.
switch name {
case selfName:
return i.selfSymlink, nil
case threadSelfName:
return i.threadSelfSymlink, nil
}
return nil, syserror.ENOENT
}
@@ -104,32 +120,27 @@ func (i *tasksInode) Valid(ctx context.Context) bool {
}
// IterDirents implements kernfs.inodeDynamicLookup.
//
// TODO(gvisor.dev/issue/1195): Use tgid N offset = TGID_OFFSET + N.
func (i *tasksInode) IterDirents(ctx context.Context, cb vfs.IterDirentsCallback, offset, relOffset int64) (int64, error) {
var tids []int
func (i *tasksInode) IterDirents(ctx context.Context, cb vfs.IterDirentsCallback, offset, _ int64) (int64, error) {
// fs/proc/internal.h: #define FIRST_PROCESS_ENTRY 256
const FIRST_PROCESS_ENTRY = 256
// Collect all tasks. Per linux we only include it in directory listings if
// it's the leader. But for whatever crazy reason, you can still walk to the
// given node.
for _, tg := range i.pidns.ThreadGroups() {
if leader := tg.Leader(); leader != nil {
tids = append(tids, int(i.pidns.IDOfThreadGroup(tg)))
}
}
if len(tids) == 0 {
return offset, nil
}
if relOffset >= int64(len(tids)) {
// Use maxTaskID to shortcut searches that will result in 0 entries.
const maxTaskID = kernel.TasksLimit + 1
if offset >= maxTaskID {
return offset, nil
}
sort.Ints(tids)
for _, tid := range tids[relOffset:] {
// According to Linux (fs/proc/base.c:proc_pid_readdir()), process directories
// start at offset FIRST_PROCESS_ENTRY with '/proc/self', followed by
// '/proc/thread-self' and then '/proc/[pid]'.
if offset < FIRST_PROCESS_ENTRY {
offset = FIRST_PROCESS_ENTRY
}
if offset == FIRST_PROCESS_ENTRY {
dirent := vfs.Dirent{
Name: strconv.FormatUint(uint64(tid), 10),
Type: linux.DT_DIR,
Name: selfName,
Type: linux.DT_LNK,
Ino: i.inoGen.NextIno(),
NextOff: offset + 1,
}
@@ -138,7 +149,52 @@ func (i *tasksInode) IterDirents(ctx context.Context, cb vfs.IterDirentsCallback
}
offset++
}
return offset, nil
if offset == FIRST_PROCESS_ENTRY+1 {
dirent := vfs.Dirent{
Name: threadSelfName,
Type: linux.DT_LNK,
Ino: i.inoGen.NextIno(),
NextOff: offset + 1,
}
if !cb.Handle(dirent) {
return offset, nil
}
offset++
}
// Collect all tasks that TGIDs are greater than the offset specified. Per
// Linux we only include in directory listings if it's the leader. But for
// whatever crazy reason, you can still walk to the given node.
var tids []int
startTid := offset - FIRST_PROCESS_ENTRY - 2
for _, tg := range i.pidns.ThreadGroups() {
tid := i.pidns.IDOfThreadGroup(tg)
if int64(tid) < startTid {
continue
}
if leader := tg.Leader(); leader != nil {
tids = append(tids, int(tid))
}
}
if len(tids) == 0 {
return offset, nil
}
sort.Ints(tids)
for _, tid := range tids {
dirent := vfs.Dirent{
Name: strconv.FormatUint(uint64(tid), 10),
Type: linux.DT_DIR,
Ino: i.inoGen.NextIno(),
NextOff: FIRST_PROCESS_ENTRY + 2 + int64(tid) + 1,
}
if !cb.Handle(dirent) {
return offset, nil
}
offset++
}
return maxTaskID, nil
}
// Open implements kernfs.Inode.
+144 -3
View File
@@ -16,6 +16,7 @@ package proc
import (
"fmt"
"math"
"path"
"strconv"
"testing"
@@ -30,6 +31,18 @@ import (
"gvisor.dev/gvisor/pkg/syserror"
)
var (
// Next offset 256 by convention. Adds 1 for the next offset.
selfLink = vfs.Dirent{Type: linux.DT_LNK, NextOff: 256 + 0 + 1}
threadSelfLink = vfs.Dirent{Type: linux.DT_LNK, NextOff: 256 + 1 + 1}
// /proc/[pid] next offset starts at 256+2 (files above), then adds the
// PID, and adds 1 for the next offset.
proc1 = vfs.Dirent{Type: linux.DT_DIR, NextOff: 258 + 1 + 1}
proc2 = vfs.Dirent{Type: linux.DT_DIR, NextOff: 258 + 2 + 1}
proc3 = vfs.Dirent{Type: linux.DT_DIR, NextOff: 258 + 3 + 1}
)
type testIterDirentsCallback struct {
dirents []vfs.Dirent
}
@@ -59,9 +72,9 @@ func checkTasksStaticFiles(gots []vfs.Dirent) ([]vfs.Dirent, error) {
"loadavg": {Type: linux.DT_REG},
"meminfo": {Type: linux.DT_REG},
"mounts": {Type: linux.DT_LNK},
"self": {Type: linux.DT_LNK},
"self": selfLink,
"stat": {Type: linux.DT_REG},
"thread-self": {Type: linux.DT_LNK},
"thread-self": threadSelfLink,
"version": {Type: linux.DT_REG},
}
return checkFiles(gots, wants)
@@ -93,6 +106,9 @@ func checkFiles(gots []vfs.Dirent, wants map[string]vfs.Dirent) ([]vfs.Dirent, e
if want.Type != got.Type {
return gots, fmt.Errorf("wrong file type, want: %v, got: %v: %+v", want.Type, got.Type, got)
}
if want.NextOff != 0 && want.NextOff != got.NextOff {
return gots, fmt.Errorf("wrong dirent offset, want: %v, got: %v: %+v", want.NextOff, got.NextOff, got)
}
delete(wants, got.Name)
gots = append(gots[0:i], gots[i+1:]...)
@@ -154,7 +170,7 @@ func TestTasksEmpty(t *testing.T) {
t.Error(err.Error())
}
if len(cb.dirents) != 0 {
t.Error("found more files than expected: %+v", cb.dirents)
t.Errorf("found more files than expected: %+v", cb.dirents)
}
}
@@ -216,6 +232,11 @@ func TestTasks(t *testing.T) {
if !found {
t.Errorf("Additional task ID %d listed: %v", pid, tasks)
}
// Next offset starts at 256+2 ('self' and 'thread-self'), then adds the
// PID, and adds 1 for the next offset.
if want := int64(256 + 2 + pid + 1); d.NextOff != want {
t.Errorf("Wrong dirent offset want: %d got: %d: %+v", want, d.NextOff, d)
}
}
// Test lookup.
@@ -246,6 +267,126 @@ func TestTasks(t *testing.T) {
}
}
func TestTasksOffset(t *testing.T) {
ctx, vfsObj, root, err := setup()
if err != nil {
t.Fatalf("Setup failed: %v", err)
}
defer root.DecRef()
k := kernel.KernelFromContext(ctx)
for i := 0; i < 3; i++ {
tc := k.NewThreadGroup(nil, k.RootPIDNamespace(), kernel.NewSignalHandlers(), linux.SIGCHLD, k.GlobalInit().Limits())
if _, err := createTask(ctx, fmt.Sprintf("name-%d", i), tc); err != nil {
t.Fatalf("CreateTask(): %v", err)
}
}
for _, tc := range []struct {
name string
offset int64
wants map[string]vfs.Dirent
}{
{
name: "small offset",
offset: 100,
wants: map[string]vfs.Dirent{
"self": selfLink,
"thread-self": threadSelfLink,
"1": proc1,
"2": proc2,
"3": proc3,
},
},
{
name: "offset at start",
offset: 256,
wants: map[string]vfs.Dirent{
"self": selfLink,
"thread-self": threadSelfLink,
"1": proc1,
"2": proc2,
"3": proc3,
},
},
{
name: "skip /proc/self",
offset: 257,
wants: map[string]vfs.Dirent{
"thread-self": threadSelfLink,
"1": proc1,
"2": proc2,
"3": proc3,
},
},
{
name: "skip symlinks",
offset: 258,
wants: map[string]vfs.Dirent{
"1": proc1,
"2": proc2,
"3": proc3,
},
},
{
name: "skip first process",
offset: 260,
wants: map[string]vfs.Dirent{
"2": proc2,
"3": proc3,
},
},
{
name: "last process",
offset: 261,
wants: map[string]vfs.Dirent{
"3": proc3,
},
},
{
name: "after last",
offset: 262,
wants: nil,
},
{
name: "TaskLimit+1",
offset: kernel.TasksLimit + 1,
wants: nil,
},
{
name: "max",
offset: math.MaxInt64,
wants: nil,
},
} {
t.Run(tc.name, func(t *testing.T) {
fd, err := vfsObj.OpenAt(
ctx,
auth.CredentialsFromContext(ctx),
&vfs.PathOperation{Root: root, Start: root, Path: fspath.Parse("/")},
&vfs.OpenOptions{},
)
if err != nil {
t.Fatalf("vfsfs.OpenAt(/) failed: %v", err)
}
if _, err := fd.Impl().Seek(ctx, tc.offset, linux.SEEK_SET); err != nil {
t.Fatalf("Seek(%d, SEEK_SET): %v", tc.offset, err)
}
cb := testIterDirentsCallback{}
if err := fd.Impl().IterDirents(ctx, &cb); err != nil {
t.Fatalf("IterDirents(): %v", err)
}
if cb.dirents, err = checkFiles(cb.dirents, tc.wants); err != nil {
t.Error(err.Error())
}
if len(cb.dirents) != 0 {
t.Errorf("found more files than expected: %+v", cb.dirents)
}
})
}
}
func TestTask(t *testing.T) {
ctx, vfsObj, root, err := setup()
if err != nil {