github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/sentry/devices/ttydev/ttydev.go (about)

     1  // Copyright 2020 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 ttydev implements an unopenable vfs.Device for /dev/tty.
    16  package ttydev
    17  
    18  import (
    19  	"github.com/nicocha30/gvisor-ligolo/pkg/abi/linux"
    20  	"github.com/nicocha30/gvisor-ligolo/pkg/context"
    21  	"github.com/nicocha30/gvisor-ligolo/pkg/errors/linuxerr"
    22  	"github.com/nicocha30/gvisor-ligolo/pkg/sentry/fsimpl/devtmpfs"
    23  	"github.com/nicocha30/gvisor-ligolo/pkg/sentry/vfs"
    24  )
    25  
    26  const (
    27  	// See drivers/tty/tty_io.c:tty_init().
    28  	ttyDevMinor     = 0
    29  	consoleDevMinor = 1
    30  )
    31  
    32  // ttyDevice implements vfs.Device for /dev/tty.
    33  //
    34  // +stateify savable
    35  type ttyDevice struct{}
    36  
    37  // Open implements vfs.Device.Open.
    38  func (ttyDevice) Open(ctx context.Context, mnt *vfs.Mount, vfsd *vfs.Dentry, opts vfs.OpenOptions) (*vfs.FileDescription, error) {
    39  	return nil, linuxerr.EIO
    40  }
    41  
    42  // Register registers all devices implemented by this package in vfsObj.
    43  func Register(vfsObj *vfs.VirtualFilesystem) error {
    44  	return vfsObj.RegisterDevice(vfs.CharDevice, linux.TTYAUX_MAJOR, ttyDevMinor, ttyDevice{}, &vfs.RegisterDeviceOptions{
    45  		GroupName: "tty",
    46  	})
    47  }
    48  
    49  // CreateDevtmpfsFiles creates device special files in dev representing all
    50  // devices implemented by this package.
    51  func CreateDevtmpfsFiles(ctx context.Context, dev *devtmpfs.Accessor) error {
    52  	return dev.CreateDeviceFile(ctx, "tty", vfs.CharDevice, linux.TTYAUX_MAJOR, ttyDevMinor, 0666 /* mode */)
    53  }