Source file src/cmd/go/internal/work/exportpipe_unix.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !cmd_go_bootstrap && unix
     6  
     7  package work
     8  
     9  import (
    10  	"errors"
    11  	"io"
    12  	"os"
    13  	"path/filepath"
    14  	"syscall"
    15  
    16  	"golang.org/x/sys/unix"
    17  )
    18  
    19  const earlyExportSupported = true
    20  
    21  type exportPipe struct {
    22  	r, w *os.File
    23  }
    24  
    25  func newExportPipe(objdir string) (*exportPipe, error) {
    26  	path := filepath.Join(objdir, "export.fifo")
    27  	if err := unix.Mkfifo(path, 0600); err != nil {
    28  		return nil, err
    29  	}
    30  	// Without NONBLOCK, open(2) would wait for the writing peer.
    31  	r0, err := os.OpenFile(path, os.O_RDONLY|syscall.O_NONBLOCK, 0)
    32  	if err != nil {
    33  		os.Remove(path)
    34  		return nil, err
    35  	}
    36  	// Keep a write descriptor open in this process so reading from r waits
    37  	// instead of returning immediate EOF before the child opens path.
    38  	w, err := os.OpenFile(path, os.O_WRONLY|syscall.O_NONBLOCK, 0)
    39  	if err != nil {
    40  		r0.Close()
    41  		os.Remove(path)
    42  		return nil, err
    43  	}
    44  	// Open r without O_NONBLOCK now that w is open so os.Open configures
    45  	// polling on platforms that support FIFO polling and blocking mode on
    46  	// Darwin/iOS (issue #24164).
    47  	r, err := os.Open(path)
    48  	r0.Close()
    49  	if err != nil {
    50  		w.Close()
    51  		os.Remove(path)
    52  		return nil, err
    53  	}
    54  	return &exportPipe{
    55  		r: r,
    56  		w: w,
    57  	}, nil
    58  }
    59  
    60  func (ep *exportPipe) name() string { return ep.r.Name() }
    61  
    62  func (ep *exportPipe) wait() error {
    63  	var b [1]byte
    64  	_, err := ep.r.Read(b[:])
    65  	if errors.Is(err, os.ErrClosed) {
    66  		return io.EOF
    67  	}
    68  	return err
    69  }
    70  
    71  func (ep *exportPipe) close() {
    72  	path := ep.r.Name()
    73  	ep.w.Close()
    74  	ep.r.Close()
    75  	os.Remove(path)
    76  }
    77  

View as plain text