github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fsimpl/devpts/terminal.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 devpts 16 17 import ( 18 "github.com/SagerNet/gvisor/pkg/abi/linux" 19 "github.com/SagerNet/gvisor/pkg/context" 20 "github.com/SagerNet/gvisor/pkg/marshal/primitive" 21 "github.com/SagerNet/gvisor/pkg/sentry/arch" 22 "github.com/SagerNet/gvisor/pkg/sentry/kernel" 23 ) 24 25 // Terminal is a pseudoterminal. 26 // 27 // +stateify savable 28 type Terminal struct { 29 // n is the terminal index. It is immutable. 30 n uint32 31 32 // ld is the line discipline of the terminal. It is immutable. 33 ld *lineDiscipline 34 35 // masterKTTY contains the controlling process of the master end of 36 // this terminal. This field is immutable. 37 masterKTTY *kernel.TTY 38 39 // replicaKTTY contains the controlling process of the replica end of this 40 // terminal. This field is immutable. 41 replicaKTTY *kernel.TTY 42 } 43 44 func newTerminal(n uint32) *Terminal { 45 termios := linux.DefaultReplicaTermios 46 t := Terminal{ 47 n: n, 48 ld: newLineDiscipline(termios), 49 masterKTTY: &kernel.TTY{Index: n}, 50 replicaKTTY: &kernel.TTY{Index: n}, 51 } 52 return &t 53 } 54 55 // setControllingTTY makes tm the controlling terminal of the calling thread 56 // group. 57 func (tm *Terminal) setControllingTTY(ctx context.Context, steal bool, isMaster, isReadable bool) error { 58 task := kernel.TaskFromContext(ctx) 59 if task == nil { 60 panic("setControllingTTY must be called from a task context") 61 } 62 63 return task.ThreadGroup().SetControllingTTY(tm.tty(isMaster), steal, isReadable) 64 } 65 66 // releaseControllingTTY removes tm as the controlling terminal of the calling 67 // thread group. 68 func (tm *Terminal) releaseControllingTTY(ctx context.Context, isMaster bool) error { 69 task := kernel.TaskFromContext(ctx) 70 if task == nil { 71 panic("releaseControllingTTY must be called from a task context") 72 } 73 74 return task.ThreadGroup().ReleaseControllingTTY(tm.tty(isMaster)) 75 } 76 77 // foregroundProcessGroup gets the process group ID of tm's foreground process. 78 func (tm *Terminal) foregroundProcessGroup(ctx context.Context, args arch.SyscallArguments, isMaster bool) (uintptr, error) { 79 task := kernel.TaskFromContext(ctx) 80 if task == nil { 81 panic("foregroundProcessGroup must be called from a task context") 82 } 83 84 ret, err := task.ThreadGroup().ForegroundProcessGroup(tm.tty(isMaster)) 85 if err != nil { 86 return 0, err 87 } 88 89 // Write it out to *arg. 90 retP := primitive.Int32(ret) 91 _, err = retP.CopyOut(task, args[2].Pointer()) 92 return 0, err 93 } 94 95 // foregroundProcessGroup sets tm's foreground process. 96 func (tm *Terminal) setForegroundProcessGroup(ctx context.Context, args arch.SyscallArguments, isMaster bool) (uintptr, error) { 97 task := kernel.TaskFromContext(ctx) 98 if task == nil { 99 panic("setForegroundProcessGroup must be called from a task context") 100 } 101 102 // Read in the process group ID. 103 var pgid primitive.Int32 104 if _, err := pgid.CopyIn(task, args[2].Pointer()); err != nil { 105 return 0, err 106 } 107 108 ret, err := task.ThreadGroup().SetForegroundProcessGroup(tm.tty(isMaster), kernel.ProcessGroupID(pgid)) 109 return uintptr(ret), err 110 } 111 112 func (tm *Terminal) tty(isMaster bool) *kernel.TTY { 113 if isMaster { 114 return tm.masterKTTY 115 } 116 return tm.replicaKTTY 117 }