Skip to content

Commit

Permalink
last changes
Browse files Browse the repository at this point in the history
Signed-off-by: Shahriyar Jalayeri <[email protected]>
  • Loading branch information
shjala committed Oct 28, 2024
1 parent 7a41976 commit 36e4d69
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pkg/pillar/hypervisor/kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
33 changes: 31 additions & 2 deletions pkg/pillar/utils/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package utils

import (
"fmt"
"net"
"os"
"strconv"
"strings"
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 36e4d69

Please sign in to comment.