github.com/hashicorp/packer@v1.14.3/helper/wrappedstreams/streams_windows.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  //go:build windows
     5  // +build windows
     6  
     7  package wrappedstreams
     8  
     9  import (
    10  	"log"
    11  	"os"
    12  	"syscall"
    13  )
    14  
    15  func initPlatform() {
    16  	wrappedStdin = openConsole("CONIN$", os.Stdin)
    17  	wrappedStdout = openConsole("CONOUT$", os.Stdout)
    18  	wrappedStderr = wrappedStdout
    19  }
    20  
    21  // openConsole opens a console handle, using a backup if it fails.
    22  // This is used to get the exact console handle instead of the redirected
    23  // handles from panicwrap.
    24  func openConsole(name string, backup *os.File) *os.File {
    25  	// Convert to UTF16
    26  	path, err := syscall.UTF16PtrFromString(name)
    27  	if err != nil {
    28  		log.Printf("[ERROR] wrappedstreams: %s", err)
    29  		return backup
    30  	}
    31  
    32  	// Determine the share mode
    33  	var shareMode uint32
    34  	switch name {
    35  	case "CONIN$":
    36  		shareMode = syscall.FILE_SHARE_READ
    37  	case "CONOUT$":
    38  		shareMode = syscall.FILE_SHARE_WRITE
    39  	}
    40  
    41  	// Get the file
    42  	h, err := syscall.CreateFile(
    43  		path,
    44  		syscall.GENERIC_READ|syscall.GENERIC_WRITE,
    45  		shareMode,
    46  		nil,
    47  		syscall.OPEN_EXISTING,
    48  		0, 0)
    49  	if err != nil {
    50  		log.Printf("[ERROR] wrappedstreams: %s", err)
    51  		return backup
    52  	}
    53  
    54  	// Create the Go file
    55  	return os.NewFile(uintptr(h), name)
    56  }