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"
|
2021-06-29 15:05:27 -07:00
|
|
|
"gvisor.dev/gvisor/pkg/errors/linuxerr"
|
2020-09-11 17:40:57 -07:00
|
|
|
"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"
|
2020-09-01 12:59:49 -07:00
|
|
|
"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"
|
|
|
|
|
)
|
|
|
|
|
|
2020-09-01 14:41:54 -07:00
|
|
|
// replicaInode is the inode for the replica end of the Terminal.
|
2020-09-24 10:11:10 -07:00
|
|
|
//
|
|
|
|
|
// +stateify savable
|
2020-09-01 14:41:54 -07:00
|
|
|
type replicaInode struct {
|
2020-08-28 14:29:16 -07:00
|
|
|
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
|
2024-12-13 00:54:07 -08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-01 14:41:54 -07:00
|
|
|
var _ kernfs.Inode = (*replicaInode)(nil)
|
2020-04-23 17:32:59 -07:00
|
|
|
|
|
|
|
|
// Open implements kernfs.Inode.Open.
|
2020-09-24 13:44:25 -07:00
|
|
|
func (ri *replicaInode) Open(ctx context.Context, rp *vfs.ResolvingPath, d *kernfs.Dentry, opts vfs.OpenOptions) (*vfs.FileDescription, error) {
|
2024-10-02 16:15:21 -07:00
|
|
|
return ri.t.Open(ctx, rp.Mount(), d.VFSDentry(), opts)
|
2020-04-23 17:32:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Valid implements kernfs.Inode.Valid.
|
2024-05-03 13:10:00 -07:00
|
|
|
func (ri *replicaInode) Valid(context.Context, *kernfs.Dentry, string) bool {
|
2020-09-01 14:41:54 -07:00
|
|
|
// Return valid if the replica still exists.
|
2020-09-24 13:44:25 -07:00
|
|
|
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.
|
2020-09-24 13:44:25 -07:00
|
|
|
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
|
2020-09-01 14:41:54 -07:00
|
|
|
statx.RdevMajor = linux.UNIX98_PTY_REPLICA_MAJOR
|
2020-09-24 13:44:25 -07:00
|
|
|
statx.RdevMinor = ri.t.n
|
2020-04-23 17:32:59 -07:00
|
|
|
return statx, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetStat implements kernfs.Inode.SetStat
|
2020-09-24 13:44:25 -07:00
|
|
|
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 {
|
2021-06-29 15:05:27 -07:00
|
|
|
return linuxerr.EINVAL
|
2020-04-23 17:32:59 -07:00
|
|
|
}
|
2020-09-24 13:44:25 -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
|
2020-09-01 14:41:54 -07:00
|
|
|
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
|
|
|
|
2020-09-01 14:41:54 -07:00
|
|
|
inode *replicaInode
|
2020-04-23 17:32:59 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-01 14:41:54 -07:00
|
|
|
var _ vfs.FileDescriptionImpl = (*replicaFileDescription)(nil)
|
2020-04-23 17:32:59 -07:00
|
|
|
|
|
|
|
|
// Release implements fs.FileOperations.Release.
|
2023-05-16 10:11:52 -07:00
|
|
|
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.
|
2021-12-07 12:32:43 -08:00
|
|
|
func (rfd *replicaFileDescription) EventRegister(e *waiter.Entry) error {
|
2021-11-13 12:51:42 -08:00
|
|
|
rfd.inode.t.ld.replicaWaiter.EventRegister(e)
|
2021-12-07 12:32:43 -08:00
|
|
|
return nil
|
2020-04-23 17:32:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EventUnregister implements waiter.Waitable.EventUnregister.
|
2020-09-24 13:44:25 -07:00
|
|
|
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.
|
2020-09-24 13:44:25 -07:00
|
|
|
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.
|
2020-09-24 13:44:25 -07:00
|
|
|
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.
|
2020-09-24 13:44:25 -07:00
|
|
|
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.
|
2023-03-23 16:59:03 -07:00
|
|
|
func (rfd *replicaFileDescription) Ioctl(ctx context.Context, io usermem.IO, sysno uintptr, args arch.SyscallArguments) (uintptr, error) {
|
2020-09-01 12:59:49 -07:00
|
|
|
t := kernel.TaskFromContext(ctx)
|
|
|
|
|
if t == nil {
|
|
|
|
|
// ioctl(2) may only be called from a task goroutine.
|
2021-07-12 15:24:04 -07:00
|
|
|
return 0, linuxerr.ENOTTY
|
2020-09-01 12:59:49 -07:00
|
|
|
}
|
|
|
|
|
|
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.
|
2020-09-24 13:44:25 -07:00
|
|
|
return 0, rfd.inode.t.ld.inputQueueReadSize(t, io, args)
|
2020-04-23 17:32:59 -07:00
|
|
|
case linux.TCGETS:
|
2020-09-24 13:44:25 -07:00
|
|
|
return rfd.inode.t.ld.getTermios(t, args)
|
2020-04-23 17:32:59 -07:00
|
|
|
case linux.TCSETS:
|
2020-09-24 13:44:25 -07:00
|
|
|
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.
|
2020-09-24 13:44:25 -07:00
|
|
|
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:
|
2020-09-24 13:44:25 -07:00
|
|
|
nP := primitive.Uint32(rfd.inode.t.n)
|
2020-09-01 12:59:49 -07:00
|
|
|
_, err := nP.CopyOut(t, args[2].Pointer())
|
2020-04-23 17:32:59 -07:00
|
|
|
return 0, err
|
|
|
|
|
case linux.TIOCGWINSZ:
|
2020-09-24 13:44:25 -07:00
|
|
|
return 0, rfd.inode.t.ld.windowSize(t, args)
|
2020-04-23 17:32:59 -07:00
|
|
|
case linux.TIOCSWINSZ:
|
2020-09-24 13:44:25 -07:00
|
|
|
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.
|
2021-02-11 11:06:56 -08:00
|
|
|
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.
|
2023-02-25 14:06:36 -08:00
|
|
|
return 0, t.ThreadGroup().ReleaseControllingTTY(rfd.inode.t.replicaKTTY)
|
2020-04-23 17:32:59 -07:00
|
|
|
case linux.TIOCGPGRP:
|
2023-02-25 14:06:36 -08:00
|
|
|
// 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:
|
2023-02-25 14:06:36 -08:00
|
|
|
// Set the foreground process group id.
|
|
|
|
|
var pgid primitive.Int32
|
|
|
|
|
if _, err := pgid.CopyIn(t, args[2].Pointer()); err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
2025-03-11 16:42:58 -07:00
|
|
|
return 0, t.ThreadGroup().SetForegroundProcessGroupID(ctx, rfd.inode.t.replicaKTTY, kernel.ProcessGroupID(pgid))
|
2020-04-23 17:32:59 -07:00
|
|
|
default:
|
2023-03-23 16:59:03 -07:00
|
|
|
maybeEmitUnimplementedEvent(ctx, sysno, cmd)
|
2021-07-12 15:24:04 -07:00
|
|
|
return 0, linuxerr.ENOTTY
|
2020-04-23 17:32:59 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetStat implements vfs.FileDescriptionImpl.SetStat.
|
2020-09-24 13:44:25 -07:00
|
|
|
func (rfd *replicaFileDescription) SetStat(ctx context.Context, opts vfs.SetStatOptions) error {
|
2020-04-23 17:32:59 -07:00
|
|
|
creds := auth.CredentialsFromContext(ctx)
|
2020-09-24 13:44:25 -07:00
|
|
|
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.
|
2020-09-24 13:44:25 -07:00
|
|
|
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
|
|
|
}
|