Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions lib/hypervisor/vz_shim_pids.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@ import (
"strings"
)

// vzShimPIDsFromPS returns the PIDs in `ps -o pid=,command=` output whose
// vz-shim configuration names socketPath as its control socket.
// Match the kernel's process name as well as the control socket in the
// shim's -config argument. Other processes can mention that socket in argv.
func vzShimPIDsFromPS(out []byte, socketPath string) []int {
quoted, _ := json.Marshal(socketPath) // a string always marshals
marker := `"control_socket":` + string(quoted)
var pids []int
for _, line := range strings.Split(string(out), "\n") {
fields := strings.Fields(line)
if len(fields) < 2 || !strings.Contains(line, " -config ") || !strings.Contains(line, marker) {
command, arg, ok := strings.Cut(line, " -config ")
if !ok {
continue
}
if pid, err := strconv.Atoi(fields[0]); err == nil {
fields := strings.Fields(command)
if len(fields) < 3 {
continue
}
if name := fields[1]; name != "vz-shim" && !strings.HasPrefix(name, "vz-shim-") {
continue
}
var config struct {
ControlSocket string `json:"control_socket"`
}
if err := json.Unmarshal([]byte(arg), &config); err != nil || config.ControlSocket != socketPath {
continue
}
if pid, err := strconv.Atoi(fields[0]); err == nil && pid > 0 {
pids = append(pids, pid)
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/hypervisor/vz_shim_pids_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func VZShimPIDs(socketPath string) ([]int, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
out, err := exec.CommandContext(ctx, "/bin/ps", "-axww", "-o", "pid=,command=").Output()
out, err := exec.CommandContext(ctx, "/bin/ps", "-axww", "-o", "pid=,ucomm=,command=").Output()
if err != nil {
return nil, fmt.Errorf("list processes: %w", err)
}
Expand Down
9 changes: 5 additions & 4 deletions lib/hypervisor/vz_shim_pids_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ func TestVZShimPIDsFromPS(t *testing.T) {
require.NoError(t, err)
return string(data)
}
out := " 101 /tmp/vz-shim-1 -config " + config("/data/guests/a/vz.sock") + "\n" +
" 202 /tmp/vz-shim-2 -config " + config("/data/guests/b/vz.sock") + "\n" +
" 303 grep control_socket\n" +
" 404 /tmp/vz-shim-3 -config " + config("/data/guests/a/vz.sock") + "\n"
out := " 101 vz-shim-1 /tmp/with spaces/vz-shim-1 -config " + config("/data/guests/a/vz.sock") + "\n" +
" 202 vz-shim-2 /tmp/vz-shim-2 -config " + config("/data/guests/b/vz.sock") + "\n" +
" 303 editor editor -config " + config("/data/guests/a/vz.sock") + "\n" +
" 404 vz-shim /tmp/vz-shim -config " + config("/data/guests/a/vz.sock") + "\n" +
` 505 vz-shim-4 /tmp/vz-shim-4 -config {"nested":{"control_socket":"/data/guests/a/vz.sock"}}`
require.Equal(t, []int{101, 404}, vzShimPIDsFromPS([]byte(out), "/data/guests/a/vz.sock"))
require.Empty(t, vzShimPIDsFromPS([]byte(out), "/data/guests/c/vz.sock"))
require.Empty(t, vzShimPIDsFromPS([]byte(out), "/data/guests/a/vz"), "a socket path prefix does not match")
Expand Down
3 changes: 1 addition & 2 deletions lib/instances/macos.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,7 @@ func sameMACAddress(a, b string) bool {
return bytes.Equal(x, y)
}

// vzShimOwner finds the vz-shim for socketPath when no live shim PID is recorded,
// as after the API dies mid-boot or mid-restore. More than one is ambiguous.
// vzShimOwner finds the unique vz-shim configured for socketPath, even before it listens.
func vzShimOwner(socketPath string) (int, error) {
if socketPath == "" {
return 0, nil
Expand Down
9 changes: 6 additions & 3 deletions lib/instances/process_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,13 @@ func resolveLiveHypervisorPID(id HypervisorProcessIdentity, socketPath string) (
}
}
if runtime.GOOS != "linux" {
if stored == 0 {
return vzShimOwner(socketPath)
// Darwin has no recorded start time. Never signal a PID based only on
// its continued existence, even when the control socket is stale.
owner, err := vzShimOwner(socketPath)
if err == nil && owner == 0 && stored != 0 {
return 0, fmt.Errorf("cannot confirm stored shim PID %d for socket %q", stored, socketPath)
}
return stored, nil
return owner, err
}
bootID := hostBootID()
if stored != 0 && id.HypervisorBootID != "" && bootID != "" && id.HypervisorBootID != bootID {
Expand Down
43 changes: 43 additions & 0 deletions lib/instances/process_identity_darwin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package instances

import (
"errors"
"os"
"testing"

"github.com/stretchr/testify/require"
)

func TestDarwinTeardownRequiresMatchingShim(t *testing.T) {
const socket = "/data/guests/fork/vz.sock"
// The stored PID is live but belongs to this test, not to the guest.
stored := os.Getpid()
identity := HypervisorProcessIdentity{HypervisorPID: &stored}
var matches []int
var lookupErr error
prev := findVZShimPIDs
t.Cleanup(func() { findVZShimPIDs = prev })
findVZShimPIDs = func(path string) ([]int, error) {
require.Equal(t, socket, path)
return matches, lookupErr
}

pid, err := resolveLiveHypervisorPID(identity, socket)
require.Error(t, err)
require.Zero(t, pid)

matches = []int{123, 456}
pid, err = resolveLiveHypervisorPID(identity, socket)
require.Error(t, err)
require.Zero(t, pid)

matches = []int{123}
pid, err = resolveLiveHypervisorPID(identity, socket)
require.NoError(t, err)
require.Equal(t, matches[0], pid)

lookupErr = errors.New("cannot inspect processes")
pid, err = resolveLiveHypervisorPID(identity, socket)
require.ErrorIs(t, err, lookupErr)
require.Zero(t, pid)
}
Loading