Files

210 lines
6.9 KiB
Go
Raw Permalink Normal View History

2020-04-23 17:32:59 -07:00
// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package devpts
import (
"gvisor.dev/gvisor/pkg/abi/linux"
"gvisor.dev/gvisor/pkg/context"
"gvisor.dev/gvisor/pkg/errors/linuxerr"
"gvisor.dev/gvisor/pkg/marshal/primitive"
2020-04-23 17:32:59 -07:00
"gvisor.dev/gvisor/pkg/sentry/arch"
"gvisor.dev/gvisor/pkg/sentry/fsimpl/kernfs"
"gvisor.dev/gvisor/pkg/sentry/kernel"
2020-04-23 17:32:59 -07:00
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
"gvisor.dev/gvisor/pkg/sentry/vfs"
"gvisor.dev/gvisor/pkg/usermem"
"gvisor.dev/gvisor/pkg/waiter"
)
// replicaInode is the inode for the replica end of the Terminal.
2020-09-24 10:11:10 -07:00
//
// +stateify savable
type replicaInode struct {
implStatFS
2020-04-23 17:32:59 -07:00
kernfs.InodeAttrs
kernfs.InodeNoopRefCount
2023-06-08 10:22:10 -07:00
kernfs.InodeNotAnonymous
2020-04-23 17:32:59 -07:00
kernfs.InodeNotDirectory
kernfs.InodeNotSymlink
2022-07-22 09:43:00 -07:00
kernfs.InodeWatches
kernfs.InodeFSOwned
2020-04-23 17:32:59 -07:00
2020-06-17 10:02:41 -07:00
locks vfs.FileLocks
2020-06-09 18:44:57 -07:00
2020-04-23 17:32:59 -07:00
// root is the devpts root inode.
root *rootInode
// t is the connected Terminal.
t *Terminal
}
var _ kernfs.Inode = (*replicaInode)(nil)
2020-04-23 17:32:59 -07:00
// Open implements kernfs.Inode.Open.
func (ri *replicaInode) Open(ctx context.Context, rp *vfs.ResolvingPath, d *kernfs.Dentry, opts vfs.OpenOptions) (*vfs.FileDescription, error) {
return ri.t.Open(ctx, rp.Mount(), d.VFSDentry(), opts)
2020-04-23 17:32:59 -07:00
}
// Valid implements kernfs.Inode.Valid.
func (ri *replicaInode) Valid(context.Context, *kernfs.Dentry, string) bool {
// Return valid if the replica still exists.
ri.root.mu.Lock()
defer ri.root.mu.Unlock()
_, ok := ri.root.replicas[ri.t.n]
2020-04-23 17:32:59 -07:00
return ok
}
// Stat implements kernfs.Inode.Stat.
func (ri *replicaInode) Stat(ctx context.Context, vfsfs *vfs.Filesystem, opts vfs.StatOptions) (linux.Statx, error) {
statx, err := ri.InodeAttrs.Stat(ctx, vfsfs, opts)
2020-04-23 17:32:59 -07:00
if err != nil {
return linux.Statx{}, err
}
statx.Blksize = 1024
statx.RdevMajor = linux.UNIX98_PTY_REPLICA_MAJOR
statx.RdevMinor = ri.t.n
2020-04-23 17:32:59 -07:00
return statx, nil
}
// SetStat implements kernfs.Inode.SetStat
func (ri *replicaInode) SetStat(ctx context.Context, vfsfs *vfs.Filesystem, creds *auth.Credentials, opts vfs.SetStatOptions) error {
2020-04-23 17:32:59 -07:00
if opts.Stat.Mask&linux.STATX_SIZE != 0 {
return linuxerr.EINVAL
2020-04-23 17:32:59 -07:00
}
return ri.InodeAttrs.SetStat(ctx, vfsfs, creds, opts)
2020-04-23 17:32:59 -07:00
}
2020-09-24 10:11:10 -07:00
// +stateify savable
type replicaFileDescription struct {
2020-04-23 17:32:59 -07:00
vfsfd vfs.FileDescription
vfs.FileDescriptionDefaultImpl
2020-06-09 18:44:57 -07:00
vfs.LockFD
2020-04-23 17:32:59 -07:00
inode *replicaInode
2020-04-23 17:32:59 -07:00
}
var _ vfs.FileDescriptionImpl = (*replicaFileDescription)(nil)
2020-04-23 17:32:59 -07:00
// Release implements fs.FileOperations.Release.
func (rfd *replicaFileDescription) Release(ctx context.Context) {
rfd.inode.t.ld.replicaClose()
}
2020-04-23 17:32:59 -07:00
// EventRegister implements waiter.Waitable.EventRegister.
func (rfd *replicaFileDescription) EventRegister(e *waiter.Entry) error {
2021-11-13 12:51:42 -08:00
rfd.inode.t.ld.replicaWaiter.EventRegister(e)
return nil
2020-04-23 17:32:59 -07:00
}
// EventUnregister implements waiter.Waitable.EventUnregister.
func (rfd *replicaFileDescription) EventUnregister(e *waiter.Entry) {
rfd.inode.t.ld.replicaWaiter.EventUnregister(e)
2020-04-23 17:32:59 -07:00
}
// Readiness implements waiter.Waitable.Readiness.
func (rfd *replicaFileDescription) Readiness(mask waiter.EventMask) waiter.EventMask {
return rfd.inode.t.ld.replicaReadiness()
2020-04-23 17:32:59 -07:00
}
2022-02-17 15:10:15 -08:00
// Epollable implements FileDescriptionImpl.Epollable.
func (rfd *replicaFileDescription) Epollable() bool {
return true
}
2020-04-23 17:32:59 -07:00
// Read implements vfs.FileDescriptionImpl.Read.
func (rfd *replicaFileDescription) Read(ctx context.Context, dst usermem.IOSequence, _ vfs.ReadOptions) (int64, error) {
return rfd.inode.t.ld.inputQueueRead(ctx, dst)
2020-04-23 17:32:59 -07:00
}
// Write implements vfs.FileDescriptionImpl.Write.
func (rfd *replicaFileDescription) Write(ctx context.Context, src usermem.IOSequence, _ vfs.WriteOptions) (int64, error) {
return rfd.inode.t.ld.outputQueueWrite(ctx, src)
2020-04-23 17:32:59 -07:00
}
2020-07-23 18:43:20 -07:00
// Ioctl implements vfs.FileDescriptionImpl.Ioctl.
func (rfd *replicaFileDescription) Ioctl(ctx context.Context, io usermem.IO, sysno uintptr, args arch.SyscallArguments) (uintptr, error) {
t := kernel.TaskFromContext(ctx)
if t == nil {
// ioctl(2) may only be called from a task goroutine.
return 0, linuxerr.ENOTTY
}
2020-04-23 17:32:59 -07:00
switch cmd := args[1].Uint(); cmd {
case linux.FIONREAD: // linux.FIONREAD == linux.TIOCINQ
// Get the number of bytes in the input queue read buffer.
return 0, rfd.inode.t.ld.inputQueueReadSize(t, io, args)
2020-04-23 17:32:59 -07:00
case linux.TCGETS:
return rfd.inode.t.ld.getTermios(t, args)
2020-04-23 17:32:59 -07:00
case linux.TCSETS:
return rfd.inode.t.ld.setTermios(t, args)
2020-04-23 17:32:59 -07:00
case linux.TCSETSW:
2024-12-04 22:40:39 -08:00
// Note that this should drain the output queue first, but we
// don't implement that yet.
return rfd.inode.t.ld.setTermios(t, args)
2023-09-05 09:53:50 -07:00
case linux.TCSETSF:
2024-12-04 22:40:39 -08:00
// This should drain the output queue and clear the input queue
// first, but we don't implement that yet.
2023-09-05 09:53:50 -07:00
return rfd.inode.t.ld.setTermios(t, args)
2020-04-23 17:32:59 -07:00
case linux.TIOCGPTN:
nP := primitive.Uint32(rfd.inode.t.n)
_, err := nP.CopyOut(t, args[2].Pointer())
2020-04-23 17:32:59 -07:00
return 0, err
case linux.TIOCGWINSZ:
return 0, rfd.inode.t.ld.windowSize(t, args)
2020-04-23 17:32:59 -07:00
case linux.TIOCSWINSZ:
return 0, rfd.inode.t.ld.setWindowSize(t, args)
2020-04-23 17:32:59 -07:00
case linux.TIOCSCTTY:
// Make the given terminal the controlling terminal of the
// calling process.
steal := args[2].Int() == 1
2024-10-03 11:23:05 -07:00
return 0, t.ThreadGroup().SetControllingTTY(ctx, rfd.inode.t.replicaKTTY, steal, rfd.vfsfd.IsReadable())
2020-04-23 17:32:59 -07:00
case linux.TIOCNOTTY:
// Release this process's controlling terminal.
return 0, t.ThreadGroup().ReleaseControllingTTY(rfd.inode.t.replicaKTTY)
2020-04-23 17:32:59 -07:00
case linux.TIOCGPGRP:
// Get the foreground process group id.
pgid, err := t.ThreadGroup().ForegroundProcessGroupID(rfd.inode.t.replicaKTTY)
if err != nil {
return 0, err
}
ret := primitive.Int32(pgid)
_, err = ret.CopyOut(t, args[2].Pointer())
return 0, err
2020-04-23 17:32:59 -07:00
case linux.TIOCSPGRP:
// Set the foreground process group id.
var pgid primitive.Int32
if _, err := pgid.CopyIn(t, args[2].Pointer()); err != nil {
return 0, err
}
return 0, t.ThreadGroup().SetForegroundProcessGroupID(ctx, rfd.inode.t.replicaKTTY, kernel.ProcessGroupID(pgid))
2020-04-23 17:32:59 -07:00
default:
maybeEmitUnimplementedEvent(ctx, sysno, cmd)
return 0, linuxerr.ENOTTY
2020-04-23 17:32:59 -07:00
}
}
// SetStat implements vfs.FileDescriptionImpl.SetStat.
func (rfd *replicaFileDescription) SetStat(ctx context.Context, opts vfs.SetStatOptions) error {
2020-04-23 17:32:59 -07:00
creds := auth.CredentialsFromContext(ctx)
fs := rfd.vfsfd.VirtualDentry().Mount().Filesystem()
return rfd.inode.SetStat(ctx, fs, creds, opts)
2020-04-23 17:32:59 -07:00
}
// Stat implements vfs.FileDescriptionImpl.Stat.
func (rfd *replicaFileDescription) Stat(ctx context.Context, opts vfs.StatOptions) (linux.Statx, error) {
fs := rfd.vfsfd.VirtualDentry().Mount().Filesystem()
return rfd.inode.Stat(ctx, fs, opts)
2020-04-23 17:32:59 -07:00
}