diff --git a/pkg/pillar/hypervisor/kvm.go b/pkg/pillar/hypervisor/kvm.go index 6cd5d94fc6..19ff0c858f 100644 --- a/pkg/pillar/hypervisor/kvm.go +++ b/pkg/pillar/hypervisor/kvm.go @@ -1443,7 +1443,8 @@ func requestvTPMLaunch(id uuid.UUID, wp *types.WatchdogParam, timeoutSeconds uin } // One last time, check SWTPM is not dead right after launch. - if !utils.IsProcAlive(pid) { + swtpmCtrlSock := fmt.Sprintf(types.SwtpmCtrlSocketPath, id.String()) + if !utils.IsUnixDomSockAlive(swtpmCtrlSock) { return fmt.Errorf("SWTPM (pid: %d) is dead", pid) } diff --git a/pkg/pillar/utils/proc.go b/pkg/pillar/utils/proc.go index e078a2bdb5..d5cbced753 100644 --- a/pkg/pillar/utils/proc.go +++ b/pkg/pillar/utils/proc.go @@ -5,6 +5,7 @@ package utils import ( "fmt" + "net" "os" "strconv" "strings" @@ -13,6 +14,7 @@ import ( "github.com/lf-edge/eve/pkg/pillar/base" "github.com/lf-edge/eve/pkg/pillar/pubsub" + log "github.com/sirupsen/logrus" ) // WatchdogKicker is used in some proc functions that have a timeout, @@ -110,7 +112,34 @@ func GetPidFromFileTimeout(pidFile string, timeoutSeconds uint, wk *WatchdogKick } } -// IsProcAlive checks if a process is alive or not. +// IsUnixDomSockAlive checks if a unix domain socket is alive. +func IsUnixDomSockAlive(sock string) bool { + conn, err := net.Dial("unix", sock) + if err != nil { + log.Errorf("failed to dial socket %s, %v", sock, err) + return false + } + rawConn, err := conn.(syscall.Conn).SyscallConn() + if err != nil { + log.Errorf("failed to get raw connection %v", err) + return false + } + + var peerAlive bool + err = rawConn.Control(func(fd uintptr) { + _, err := syscall.Getpeername(int(fd)) + peerAlive = (err == nil) + }) + + if err != nil { + log.Errorf("failed to get peer name %v", err) + return false + } + + return peerAlive +} + +// IsProcAlive checks current proc namespace to see process is alive or not. func IsProcAlive(pid int) bool { err := syscall.Kill(pid, syscall.Signal(0)) if err != nil { @@ -122,7 +151,7 @@ func IsProcAlive(pid int) bool { return true } -// IsProcAliveTimeout checks if a process is alive for a given timeout. +// IsProcAliveTimeout checks current proc namespace to see process is alive or not for a given timeout. func IsProcAliveTimeout(pid int, timeoutSeconds uint, wk *WatchdogKicker) bool { startTime := time.Now() for {