...

Source file src/github.com/Microsoft/hcsshim/internal/jobcontainers/env.go

Documentation: github.com/Microsoft/hcsshim/internal/jobcontainers

     1  //go:build windows
     2  
     3  package jobcontainers
     4  
     5  import (
     6  	"unicode/utf16"
     7  	"unsafe"
     8  
     9  	"github.com/pkg/errors"
    10  	"golang.org/x/sys/windows"
    11  )
    12  
    13  // defaultEnvBlock will return a new environment block in the context of the user token
    14  // `token`.
    15  //
    16  // This is almost a direct copy of the go stdlib implementation with some slight changes
    17  // to force a valid token to be passed.
    18  // https://github.com/golang/go/blob/f21be2fdc6f1becdbed1592ea0b245cdeedc5ac8/src/internal/syscall/execenv/execenv_windows.go#L24
    19  func defaultEnvBlock(token windows.Token) (env []string, err error) {
    20  	if token == 0 {
    21  		return nil, errors.New("invalid token for creating environment block")
    22  	}
    23  
    24  	var block *uint16
    25  	if err := windows.CreateEnvironmentBlock(&block, token, false); err != nil {
    26  		return nil, err
    27  	}
    28  	defer func() {
    29  		_ = windows.DestroyEnvironmentBlock(block)
    30  	}()
    31  
    32  	blockp := uintptr(unsafe.Pointer(block))
    33  	for {
    34  		// find NUL terminator
    35  		end := unsafe.Pointer(blockp)
    36  		for *(*uint16)(end) != 0 {
    37  			end = unsafe.Pointer(uintptr(end) + 2)
    38  		}
    39  
    40  		n := (uintptr(end) - uintptr(unsafe.Pointer(blockp))) / 2
    41  		if n == 0 {
    42  			// environment block ends with empty string
    43  			break
    44  		}
    45  		entry := (*[(1 << 30) - 1]uint16)(unsafe.Pointer(blockp))[:n:n]
    46  		env = append(env, string(utf16.Decode(entry)))
    47  		blockp += 2 * (uintptr(len(entry)) + 1)
    48  	}
    49  	return
    50  }
    51  

View as plain text