github.com/containers/podman/v4@v4.9.4/pkg/machine/qemu/machine_unix.go (about)

     1  //go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
     2  // +build darwin dragonfly freebsd linux netbsd openbsd
     3  
     4  package qemu
     5  
     6  import (
     7  	"bytes"
     8  	"fmt"
     9  	"strings"
    10  	"syscall"
    11  
    12  	"golang.org/x/sys/unix"
    13  )
    14  
    15  func isProcessAlive(pid int) bool {
    16  	err := unix.Kill(pid, syscall.Signal(0))
    17  	if err == nil || err == unix.EPERM {
    18  		return true
    19  	}
    20  	return false
    21  }
    22  
    23  func checkProcessStatus(processHint string, pid int, stderrBuf *bytes.Buffer) error {
    24  	var status syscall.WaitStatus
    25  	pid, err := syscall.Wait4(pid, &status, syscall.WNOHANG, nil)
    26  	if err != nil {
    27  		return fmt.Errorf("failed to read qem%su process status: %w", processHint, err)
    28  	}
    29  	if pid > 0 {
    30  		// child exited
    31  		return fmt.Errorf("%s exited unexpectedly with exit code %d, stderr: %s", processHint, status.ExitStatus(), stderrBuf.String())
    32  	}
    33  	return nil
    34  }
    35  
    36  func pathsFromVolume(volume string) []string {
    37  	return strings.SplitN(volume, ":", 3)
    38  }
    39  
    40  func extractTargetPath(paths []string) string {
    41  	if len(paths) > 1 {
    42  		return paths[1]
    43  	}
    44  	return paths[0]
    45  }
    46  
    47  func sigKill(pid int) error {
    48  	return unix.Kill(pid, unix.SIGKILL)
    49  }
    50  
    51  func findProcess(pid int) (int, error) {
    52  	if err := unix.Kill(pid, 0); err != nil {
    53  		if err == unix.ESRCH {
    54  			return -1, nil
    55  		}
    56  		return -1, fmt.Errorf("pinging QEMU process: %w", err)
    57  	}
    58  	return pid, nil
    59  }