github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fs/ramfs/socket.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 ramfs
    16  
    17  import (
    18  	"github.com/SagerNet/gvisor/pkg/abi/linux"
    19  	"github.com/SagerNet/gvisor/pkg/context"
    20  	"github.com/SagerNet/gvisor/pkg/errors/linuxerr"
    21  	"github.com/SagerNet/gvisor/pkg/sentry/fs"
    22  	"github.com/SagerNet/gvisor/pkg/sentry/fs/fsutil"
    23  	"github.com/SagerNet/gvisor/pkg/sentry/socket/unix/transport"
    24  	"github.com/SagerNet/gvisor/pkg/waiter"
    25  )
    26  
    27  // Socket represents a socket.
    28  //
    29  // +stateify savable
    30  type Socket struct {
    31  	fsutil.InodeGenericChecker `state:"nosave"`
    32  	fsutil.InodeNoopRelease    `state:"nosave"`
    33  	fsutil.InodeNoopWriteOut   `state:"nosave"`
    34  	fsutil.InodeNotAllocatable `state:"nosave"`
    35  	fsutil.InodeNotDirectory   `state:"nosave"`
    36  	fsutil.InodeNotMappable    `state:"nosave"`
    37  	fsutil.InodeNotSymlink     `state:"nosave"`
    38  	fsutil.InodeNotTruncatable `state:"nosave"`
    39  	fsutil.InodeVirtual        `state:"nosave"`
    40  
    41  	fsutil.InodeSimpleAttributes
    42  	fsutil.InodeSimpleExtendedAttributes
    43  
    44  	// ep is the bound endpoint.
    45  	ep transport.BoundEndpoint
    46  }
    47  
    48  var _ fs.InodeOperations = (*Socket)(nil)
    49  
    50  // NewSocket returns a new Socket.
    51  func NewSocket(ctx context.Context, ep transport.BoundEndpoint, owner fs.FileOwner, perms fs.FilePermissions) *Socket {
    52  	return &Socket{
    53  		InodeSimpleAttributes: fsutil.NewInodeSimpleAttributes(ctx, owner, perms, linux.SOCKFS_MAGIC),
    54  		ep:                    ep,
    55  	}
    56  }
    57  
    58  // BoundEndpoint returns the socket data.
    59  func (s *Socket) BoundEndpoint(*fs.Inode, string) transport.BoundEndpoint {
    60  	// ramfs only supports stored sentry internal sockets. Only gofer sockets
    61  	// care about the path argument.
    62  	return s.ep
    63  }
    64  
    65  // GetFile implements fs.FileOperations.GetFile.
    66  func (s *Socket) GetFile(ctx context.Context, dirent *fs.Dirent, flags fs.FileFlags) (*fs.File, error) {
    67  	return nil, linuxerr.ENXIO
    68  }
    69  
    70  // +stateify savable
    71  type socketFileOperations struct {
    72  	fsutil.FileNoIoctl              `state:"nosave"`
    73  	fsutil.FileNoMMap               `state:"nosave"`
    74  	fsutil.FileNoRead               `state:"nosave"`
    75  	fsutil.FileNoSeek               `state:"nosave"`
    76  	fsutil.FileNoSplice             `state:"nosave"`
    77  	fsutil.FileNoWrite              `state:"nosave"`
    78  	fsutil.FileNoopFlush            `state:"nosave"`
    79  	fsutil.FileNoopFsync            `state:"nosave"`
    80  	fsutil.FileNoopRelease          `state:"nosave"`
    81  	fsutil.FileNotDirReaddir        `state:"nosave"`
    82  	fsutil.FileUseInodeUnstableAttr `state:"nosave"`
    83  	waiter.AlwaysReady              `state:"nosave"`
    84  }
    85  
    86  var _ fs.FileOperations = (*socketFileOperations)(nil)