Skip to content

Commit 912ffef

Browse files
committed
Preserve the swtpm startup deadline
1 parent a8362e3 commit 912ffef

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎lib/hypervisor/qemu/swtpm.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ func startSWTPM(config *hypervisor.TPMConfig, instanceDir string) (*startedProce
6767
cmd.Stderr = logFile
6868
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
6969

70+
if !time.Now().Before(deadline) {
71+
return nil, fmt.Errorf("timeout before starting swtpm")
72+
}
7073
proc, err := startManagedProcess(cmd, config.SocketPath)
7174
if err != nil {
7275
return nil, fmt.Errorf("start swtpm: %w", err)
@@ -122,6 +125,9 @@ func waitForSWTPMExit(record swtpmProcessRecord, recordPath string, deadline tim
122125
return fmt.Errorf("inspect previous swtpm process %d: %w", record.pid, err)
123126
}
124127
if !alive || identity != record.identity {
128+
if !time.Now().Before(deadline) {
129+
return fmt.Errorf("timeout waiting for previous swtpm process %d", record.pid)
130+
}
125131
_ = os.Remove(recordPath)
126132
return nil
127133
}

‎lib/hypervisor/qemu/swtpm_test.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package qemu
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
"time"
9+
10+
"github.com/kernel/hypeman/lib/hypervisor"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestWaitForPreviousSWTPMRejectsExpiredDeadline(t *testing.T) {
15+
recordPath := filepath.Join(t.TempDir(), "swtpm.pid")
16+
identity, alive, err := swtpmProcessIdentity(os.Getpid())
17+
require.NoError(t, err)
18+
require.True(t, alive)
19+
require.NoError(t, os.WriteFile(recordPath, []byte(fmt.Sprintf("%d %s\n", os.Getpid(), identity)), 0600))
20+
21+
err = waitForPreviousSWTPM(&hypervisor.TPMConfig{}, recordPath, time.Now().Add(-time.Second))
22+
require.ErrorContains(t, err, "timeout waiting for previous swtpm process")
23+
}

0 commit comments

Comments
 (0)