github.com/criyle/go-sandbox@v0.10.3/pkg/forkexec/fork_util.go (about)

     1  package forkexec
     2  
     3  import (
     4  	"syscall"
     5  )
     6  
     7  // prepareExec prepares execve parameters
     8  func prepareExec(Args, Env []string) (*byte, []*byte, []*byte, error) {
     9  	// make exec args0
    10  	argv0, err := syscall.BytePtrFromString(Args[0])
    11  	if err != nil {
    12  		return nil, nil, nil, err
    13  	}
    14  	// make exec args
    15  	argv, err := syscall.SlicePtrFromStrings(Args)
    16  	if err != nil {
    17  		return nil, nil, nil, err
    18  	}
    19  	// make env
    20  	env, err := syscall.SlicePtrFromStrings(Env)
    21  	if err != nil {
    22  		return nil, nil, nil, err
    23  	}
    24  	return argv0, argv, env, nil
    25  }
    26  
    27  // prepareFds prepares fd array
    28  func prepareFds(files []uintptr) ([]int, int) {
    29  	fd := make([]int, len(files))
    30  	nextfd := len(files)
    31  	for i, ufd := range files {
    32  		if nextfd < int(ufd) {
    33  			nextfd = int(ufd)
    34  		}
    35  		fd[i] = int(ufd)
    36  	}
    37  	nextfd++
    38  	return fd, nextfd
    39  }
    40  
    41  // syscallStringFromString prepares *byte if string is not empty, other wise nil
    42  func syscallStringFromString(str string) (*byte, error) {
    43  	if str != "" {
    44  		return syscall.BytePtrFromString(str)
    45  	}
    46  	return nil, nil
    47  }