Skip to content

Implement /dev/tty for donated host TTYs #10968

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/sentry/control/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@ func (proc *Proc) execAsync(args *ExecArgs) (*kernel.ThreadGroup, kernel.ThreadI
return nil, 0, nil, err
}

if ttyFile != nil {
// Index does not matter here. This tty is not coming from a
// devpts mount, so it won't collide with any of the ptys
// created there.
initArgs.TTY = kernel.NewTTY(0, ttyFile)
}

// Set cgroups to the new exec task if cgroups are mounted.
cgroupRegistry := proc.Kernel.CgroupRegistry()
initialCgrps := map[kernel.Cgroup]struct{}{}
Expand Down
9 changes: 9 additions & 0 deletions pkg/sentry/fsimpl/host/tty.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
// TTYFileDescription implements vfs.FileDescriptionImpl for a host file
// descriptor that wraps a TTY FD.
//
// It implements kernel.TTYOperations.
//
// +stateify savable
type TTYFileDescription struct {
fileDescription
Expand All @@ -49,6 +51,13 @@ type TTYFileDescription struct {
termios linux.KernelTermios
}

// Open re-opens the tty fd, for example via open(/dev/tty). See Linux's
// tty_repoen().
func (t *TTYFileDescription) Open(_ context.Context, _ *vfs.Mount, _ *vfs.Dentry, _ vfs.OpenOptions) (*vfs.FileDescription, error) {
t.vfsfd.IncRef()
return &t.vfsfd, nil
}

// InitForegroundProcessGroup sets the foreground process group and session for
// the TTY. This should only be called once, after the foreground process group
// has been created, but before it has started running.
Expand Down
9 changes: 9 additions & 0 deletions pkg/sentry/kernel/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,9 @@ type CreateProcessArgs struct {

// Origin indicates how the task was first created.
Origin TaskOrigin

// TTY is the optional TTY to associate with this process.
TTY *TTY
}

// NewContext returns a context.Context that represents the task that will be
Expand Down Expand Up @@ -1222,6 +1225,12 @@ func (k *Kernel) CreateProcess(args CreateProcessArgs) (*ThreadGroup, ThreadID,
}
t.traceExecEvent(image) // Simulate exec for tracing.

// Set TTY if configured.
if args.TTY != nil {
t.tg.tty = args.TTY
args.TTY.tg = t.tg
}

// Success.
cu.Release()
tgid := k.tasks.Root.IDOfThreadGroup(tg)
Expand Down
7 changes: 7 additions & 0 deletions runsc/boot/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,13 @@ func (l *Loader) createContainerProcess(info *containerInfo) (*kernel.ThreadGrou
// ours either way.
info.procArgs.FDTable = fdTable

if ttyFile != nil {
// Index does not matter here. This tty is not coming from a
// devpts mount, so it won't collide with any of the ptys
// created there.
info.procArgs.TTY = kernel.NewTTY(0, ttyFile)
}

if info.execFD != nil {
if info.procArgs.Filename != "" {
return nil, nil, fmt.Errorf("process must either be started from a file or a filename, not both")
Expand Down
10 changes: 10 additions & 0 deletions runsc/container/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,16 @@ func TestMultiContainerTerminal(t *testing.T) {
if err := testutil.WaitUntilRead(ptyBuf, "foo-/-123", 5*time.Second); err != nil {
t.Fatalf("echo didn't execute: %v", err)
}

// Make sure we can open /dev/tty. We do this
// by asking `head` to to read 0 bytes, which
// causes it to simply open & close the file.
if _, err := tc.master.Write([]byte("head -n 0 /dev/tty; echo $?\n")); err != nil {
t.Fatalf("master.Write(): %v", err)
}
if err := testutil.WaitUntilRead(ptyBuf, "0", 5*time.Second); err != nil {
t.Fatalf("head didn't execute: %v", err)
}
}
})
}
Expand Down
Loading