github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fs/proc/filesystems.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package proc
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  
    21  	"github.com/SagerNet/gvisor/pkg/context"
    22  	"github.com/SagerNet/gvisor/pkg/sentry/fs"
    23  	"github.com/SagerNet/gvisor/pkg/sentry/fs/proc/seqfile"
    24  )
    25  
    26  // LINT.IfChange
    27  
    28  // filesystemsData backs /proc/filesystems.
    29  //
    30  // +stateify savable
    31  type filesystemsData struct{}
    32  
    33  // NeedsUpdate returns true on the first generation. The set of registered file
    34  // systems doesn't change so there's no need to generate SeqData more than once.
    35  func (*filesystemsData) NeedsUpdate(generation int64) bool {
    36  	return generation == 0
    37  }
    38  
    39  // ReadSeqFileData returns data for the SeqFile reader.
    40  // SeqData, the current generation and where in the file the handle corresponds to.
    41  func (*filesystemsData) ReadSeqFileData(ctx context.Context, h seqfile.SeqHandle) ([]seqfile.SeqData, int64) {
    42  	// We don't ever expect to see a non-nil SeqHandle.
    43  	if h != nil {
    44  		return nil, 0
    45  	}
    46  
    47  	// Generate the file contents.
    48  	var buf bytes.Buffer
    49  	for _, sys := range fs.GetFilesystems() {
    50  		if !sys.AllowUserList() {
    51  			continue
    52  		}
    53  		nodev := "nodev"
    54  		if sys.Flags()&fs.FilesystemRequiresDev != 0 {
    55  			nodev = ""
    56  		}
    57  		// Matches the format of fs/filesystems.c:filesystems_proc_show.
    58  		fmt.Fprintf(&buf, "%s\t%s\n", nodev, sys.Name())
    59  	}
    60  
    61  	// Return the SeqData and advance the generation counter.
    62  	return []seqfile.SeqData{{Buf: buf.Bytes(), Handle: (*filesystemsData)(nil)}}, 1
    63  }
    64  
    65  // LINT.ThenChange(../../fsimpl/proc/filesystem.go)