From 1dc92a8c71c32fc82a6938a25c7e9dbca435abb0 Mon Sep 17 00:00:00 2001 From: Wim Date: Wed, 22 Feb 2023 22:32:34 +0100 Subject: [PATCH] Add reference to terminal in lineDiscipline --- pkg/sentry/fsimpl/devpts/line_discipline.go | 15 +++++++-------- pkg/sentry/fsimpl/devpts/terminal.go | 9 +++++++-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/pkg/sentry/fsimpl/devpts/line_discipline.go b/pkg/sentry/fsimpl/devpts/line_discipline.go index 3fd90db84..3ca11a985 100644 --- a/pkg/sentry/fsimpl/devpts/line_discipline.go +++ b/pkg/sentry/fsimpl/devpts/line_discipline.go @@ -112,9 +112,8 @@ type lineDiscipline struct { // replicaWaiter is used to wait on the replica end of the TTY. replicaWaiter waiter.Queue - // fgProcessGroup is the foreground process group that is currently - // connected to this TTY. - fgProcessGroup *kernel.ProcessGroup + // terminal is the terminal linked to this lineDiscipline + terminal *Terminal } func newLineDiscipline(termios linux.KernelTermios) *lineDiscipline { @@ -124,8 +123,8 @@ func newLineDiscipline(termios linux.KernelTermios) *lineDiscipline { return &ld } -func (l *lineDiscipline) SetForegroundProcessGroup(pg *kernel.ProcessGroup) { - l.fgProcessGroup = pg +func (l *lineDiscipline) SetTerminal(tm *Terminal) { + l.terminal = tm } // getTermios gets the linux.Termios for the tty. @@ -402,11 +401,11 @@ func (*inputQueueTransformer) transform(l *lineDiscipline, q *queue, buf []byte) cBytes[0] = '\r' } case l.termios.ControlCharacters[linux.VINTR]: // ctrl-c - l.fgProcessGroup.SendSignal(kernel.SignalInfoPriv(linux.SIGINT)) + l.terminal.fgProcessGroup.SendSignal(kernel.SignalInfoPriv(linux.SIGINT)) case l.termios.ControlCharacters[linux.VSUSP]: // ctrl-z - l.fgProcessGroup.SendSignal(kernel.SignalInfoPriv(linux.SIGTSTP)) + l.terminal.fgProcessGroup.SendSignal(kernel.SignalInfoPriv(linux.SIGTSTP)) case l.termios.ControlCharacters[linux.VQUIT]: // ctrl-\ - l.fgProcessGroup.SendSignal(kernel.SignalInfoPriv(linux.SIGQUIT)) + l.terminal.fgProcessGroup.SendSignal(kernel.SignalInfoPriv(linux.SIGQUIT)) } // In canonical mode, we discard non-terminating characters diff --git a/pkg/sentry/fsimpl/devpts/terminal.go b/pkg/sentry/fsimpl/devpts/terminal.go index 6c153f9c8..ed03ffda0 100644 --- a/pkg/sentry/fsimpl/devpts/terminal.go +++ b/pkg/sentry/fsimpl/devpts/terminal.go @@ -39,6 +39,10 @@ type Terminal struct { // replicaKTTY contains the controlling process of the replica end of this // terminal. This field is immutable. replicaKTTY *kernel.TTY + + // fgProcessGroup is the foreground process group that is currently + // connected to this TTY. + fgProcessGroup *kernel.ProcessGroup } func newTerminal(n uint32) *Terminal { @@ -49,6 +53,8 @@ func newTerminal(n uint32) *Terminal { masterKTTY: &kernel.TTY{Index: n}, replicaKTTY: &kernel.TTY{Index: n}, } + + t.ld.SetTerminal(&t) return &t } @@ -105,8 +111,7 @@ func (tm *Terminal) setForegroundProcessGroup(ctx context.Context, args arch.Sys return 0, err } - // Put the processgroup also in line discipline for further signal handling - tm.ld.SetForegroundProcessGroup(task.ThreadGroup().ProcessGroup()) + tm.fgProcessGroup = task.ThreadGroup().ProcessGroup() ret, err := task.ThreadGroup().SetForegroundProcessGroup(tm.tty(isMaster), kernel.ProcessGroupID(pgid)) return uintptr(ret), err