Skip to content
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
126 changes: 3 additions & 123 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,146 +19,26 @@
package runc

import (
"fmt"
"net"
"os"
"path/filepath"

"github.com/containerd/console"
"golang.org/x/sys/unix"
)

// NewConsoleSocket creates a new unix socket at the provided path to accept a
// pty master created by runc for use by the container
func NewConsoleSocket(path string) (*Socket, error) {
abs, err := filepath.Abs(path)
if err != nil {
return nil, err
}
addr, err := net.ResolveUnixAddr("unix", abs)
if err != nil {
return nil, err
}
l, err := net.ListenUnix("unix", addr)
if err != nil {
return nil, err
}
return &Socket{
l: l,
}, nil
return newSocket(path)
}

// NewTempConsoleSocket returns a temp console socket for use with a container
// On Close(), the socket is deleted
func NewTempConsoleSocket() (*Socket, error) {
runtimeDir := os.Getenv("XDG_RUNTIME_DIR")
dir, err := os.MkdirTemp(runtimeDir, "pty")
if err != nil {
return nil, err
}
abs, err := filepath.Abs(filepath.Join(dir, "pty.sock"))
if err != nil {
return nil, err
}
addr, err := net.ResolveUnixAddr("unix", abs)
if err != nil {
return nil, err
}
l, err := net.ListenUnix("unix", addr)
if err != nil {
return nil, err
}
if runtimeDir != "" {
if err := os.Chmod(abs, 0o755|os.ModeSticky); err != nil {
return nil, err
}
}
return &Socket{
l: l,
rmdir: true,
}, nil
}

// Socket is a unix socket that accepts the pty master created by runc
type Socket struct {
rmdir bool
l *net.UnixListener
}

// Path returns the path to the unix socket on disk
func (c *Socket) Path() string {
return c.l.Addr().String()
}

// recvFd waits for a file descriptor to be sent over the given AF_UNIX
// socket. The file name of the remote file descriptor will be recreated
// locally (it is sent as non-auxiliary data in the same payload).
func recvFd(socket *net.UnixConn) (*os.File, error) {
const MaxNameLen = 4096
oobSpace := unix.CmsgSpace(4)

name := make([]byte, MaxNameLen)
oob := make([]byte, oobSpace)

n, oobn, _, _, err := socket.ReadMsgUnix(name, oob)
if err != nil {
return nil, err
}

if n >= MaxNameLen || oobn != oobSpace {
return nil, fmt.Errorf("recvfd: incorrect number of bytes read (n=%d oobn=%d)", n, oobn)
}

// Truncate.
name = name[:n]
oob = oob[:oobn]

scms, err := unix.ParseSocketControlMessage(oob)
if err != nil {
return nil, err
}
if len(scms) != 1 {
return nil, fmt.Errorf("recvfd: number of SCMs is not 1: %d", len(scms))
}
scm := scms[0]

fds, err := unix.ParseUnixRights(&scm)
if err != nil {
return nil, err
}
if len(fds) != 1 {
return nil, fmt.Errorf("recvfd: number of fds is not 1: %d", len(fds))
}
fd := uintptr(fds[0])

return os.NewFile(fd, string(name)), nil
return newTempSocket("pty", "pty.sock")
}

// ReceiveMaster blocks until the socket receives the pty master
func (c *Socket) ReceiveMaster() (console.Console, error) {
conn, err := c.l.Accept()
if err != nil {
return nil, err
}
defer conn.Close()
uc, ok := conn.(*net.UnixConn)
if !ok {
return nil, fmt.Errorf("received connection which was not a unix socket")
}
f, err := recvFd(uc)
f, err := c.receive()
if err != nil {
return nil, err
}
return console.ConsoleFromFile(f)
}

// Close closes the unix socket
func (c *Socket) Close() error {
err := c.l.Close()
if c.rmdir {
if rerr := os.RemoveAll(filepath.Dir(c.Path())); err == nil {
err = rerr
}
}
return err
}
54 changes: 54 additions & 0 deletions pidfd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//go:build !windows

/*
Copyright The containerd 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 runc

import (
"os"
)

// NewPidfdSocket creates a new unix socket at the provided path to accept the
// pidfd of the process runc creates.
//
// Requires runc v1.2.0 or newer and a v5.3 or newer kernel.
func NewPidfdSocket(path string) (*Socket, error) {
return newSocket(path)
}

// NewTempPidfdSocket returns a temp socket to accept the pidfd of the process
// runc creates. On Close(), the socket is deleted.
//
// Requires runc v1.2.0 or newer and a v5.3 or newer kernel.
func NewTempPidfdSocket() (*Socket, error) {
return newTempSocket("pidfd", "pidfd.sock")
}

// ReceivePidfd blocks until the socket receives the pidfd of the process runc
// created. The pidfd refers to the container's init process for create and run,
// and to the exec'd process for exec.
//
// The pidfd is sent while the process is being set up, which is before `runc
// create` returns and before the container's start fifo is opened, so it is
// safe to receive it before starting the container.
//
// It is the caller's responsibility to close the returned file. The pidfd can
// be used to signal or wait on the process without the pid reuse races that
// come with a raw pid, e.g. with unix.PidfdSendSignal.
func (c *Socket) ReceivePidfd() (*os.File, error) {
return c.receive()
}
Loading
Loading