github.com/opencontainers/runc@v1.2.0-rc.1.0.20240520010911-492dc558cdd6/libcontainer/sync_unix.go (about)

     1  package libcontainer
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  	"sync/atomic"
     8  
     9  	"golang.org/x/sys/unix"
    10  )
    11  
    12  // syncSocket is a wrapper around a SOCK_SEQPACKET socket, providing
    13  // packet-oriented methods. This is needed because SOCK_SEQPACKET does not
    14  // allow for partial reads, but the Go stdlib treats it as a streamable source,
    15  // which ends up making things like json.Decoder hang forever if the packet is
    16  // bigger than the internal read buffer.
    17  type syncSocket struct {
    18  	f      *os.File
    19  	closed atomic.Bool
    20  }
    21  
    22  func newSyncSocket(f *os.File) *syncSocket {
    23  	return &syncSocket{f: f}
    24  }
    25  
    26  func (s *syncSocket) File() *os.File {
    27  	return s.f
    28  }
    29  
    30  func (s *syncSocket) Close() error {
    31  	// Even with errors from Close(), we have to assume the pipe was closed.
    32  	s.closed.Store(true)
    33  	return s.f.Close()
    34  }
    35  
    36  func (s *syncSocket) isClosed() bool {
    37  	return s.closed.Load()
    38  }
    39  
    40  func (s *syncSocket) WritePacket(b []byte) (int, error) {
    41  	return s.f.Write(b)
    42  }
    43  
    44  func (s *syncSocket) ReadPacket() ([]byte, error) {
    45  	size, _, err := unix.Recvfrom(int(s.f.Fd()), nil, unix.MSG_TRUNC|unix.MSG_PEEK)
    46  	if err != nil {
    47  		return nil, fmt.Errorf("fetch packet length from socket: %w", err)
    48  	}
    49  	// We will only get a zero size if the socket has been closed from the
    50  	// other end (otherwise recvfrom(2) will block until a packet is ready). In
    51  	// addition, SOCK_SEQPACKET is treated as a stream source by Go stdlib so
    52  	// returning io.EOF here is correct from that perspective too.
    53  	if size == 0 {
    54  		return nil, io.EOF
    55  	}
    56  	buf := make([]byte, size)
    57  	n, err := s.f.Read(buf)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	if n != size {
    62  		return nil, fmt.Errorf("packet read too short: expected %d byte packet but only %d bytes read", size, n)
    63  	}
    64  	return buf, nil
    65  }
    66  
    67  func (s *syncSocket) Shutdown(how int) error {
    68  	if err := unix.Shutdown(int(s.f.Fd()), how); err != nil {
    69  		return &os.PathError{Op: "shutdown", Path: s.f.Name() + " (sync pipe)", Err: err}
    70  	}
    71  	return nil
    72  }
    73  
    74  // newSyncSockpair returns a new SOCK_SEQPACKET unix socket pair to be used for
    75  // runc-init synchronisation.
    76  func newSyncSockpair(name string) (parent, child *syncSocket, err error) {
    77  	fds, err := unix.Socketpair(unix.AF_LOCAL, unix.SOCK_SEQPACKET|unix.SOCK_CLOEXEC, 0)
    78  	if err != nil {
    79  		return nil, nil, err
    80  	}
    81  	parentFile := os.NewFile(uintptr(fds[1]), name+"-p")
    82  	childFile := os.NewFile(uintptr(fds[0]), name+"-c")
    83  	return newSyncSocket(parentFile), newSyncSocket(childFile), nil
    84  }