From 05488bfb8f43f12bc0306e03b675d4f415fc6328 Mon Sep 17 00:00:00 2001 From: Ophestra Date: Thu, 23 Oct 2025 23:19:55 +0900 Subject: [PATCH] hst/instance: store priv side pid This can receive signals, so is more useful to the caller. Signed-off-by: Ophestra --- cmd/hakurei/print.go | 2 +- cmd/hakurei/print_test.go | 21 ++++++++++++--------- hst/instance.go | 5 ++++- internal/app/process.go | 9 +++++---- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/cmd/hakurei/print.go b/cmd/hakurei/print.go index c28146a..b1a0c86 100644 --- a/cmd/hakurei/print.go +++ b/cmd/hakurei/print.go @@ -66,7 +66,7 @@ func printShowInstance( if instance != nil { t.Printf("State\n") - t.Printf(" Instance:\t%s (%d)\n", instance.ID.String(), instance.PID) + t.Printf(" Instance:\t%s (%d -> %d)\n", instance.ID.String(), instance.PID, instance.ShimPID) t.Printf(" Uptime:\t%s\n", now.Sub(instance.Time).Round(time.Second).String()) t.Printf("\n") } diff --git a/cmd/hakurei/print_test.go b/cmd/hakurei/print_test.go index da5c815..cbf23d1 100644 --- a/cmd/hakurei/print_test.go +++ b/cmd/hakurei/print_test.go @@ -17,10 +17,11 @@ var ( 0xb4, 0x6e, 0xb5, 0xc1, } testState = &hst.State{ - ID: testID, - PID: 0xDEADBEEF, - Config: hst.Template(), - Time: testAppTime, + ID: testID, + PID: 0xcafebabe, + ShimPID: 0xdeadbeef, + Config: hst.Template(), + Time: testAppTime, } testTime = time.Unix(3752, 1).UTC() testAppTime = time.Unix(0, 9).UTC() @@ -131,7 +132,7 @@ Session bus `, false}, {"instance", testState, hst.Template(), false, false, `State - Instance: 8e2c76b066dabe574cf073bdb46eb5c1 (3735928559) + Instance: 8e2c76b066dabe574cf073bdb46eb5c1 (3405691582 -> 3735928559) Uptime: 1h2m32s App @@ -173,7 +174,7 @@ System bus {"instance pd", testState, new(hst.Config), false, false, `Error: configuration missing container state! State - Instance: 8e2c76b066dabe574cf073bdb46eb5c1 (3735928559) + Instance: 8e2c76b066dabe574cf073bdb46eb5c1 (3405691582 -> 3735928559) Uptime: 1h2m32s App @@ -186,7 +187,8 @@ App `, true}, {"json instance", testState, nil, false, true, `{ "instance": "8e2c76b066dabe574cf073bdb46eb5c1", - "pid": 3735928559, + "pid": 3405691582, + "shim_pid": 3735928559, "config": { "id": "org.chromium.Chromium", "enablements": { @@ -527,13 +529,14 @@ func TestPrintPs(t *testing.T) { `}, {"valid", map[hst.ID]*hst.State{testID: testState}, false, false, ` Instance PID Application Uptime - 8e2c76b0 3735928559 9 (org.chromium.Chromium) 1h2m32s + 8e2c76b0 3405691582 9 (org.chromium.Chromium) 1h2m32s `}, {"valid short", map[hst.ID]*hst.State{testID: testState}, true, false, "8e2c76b0\n"}, {"valid json", map[hst.ID]*hst.State{testID: testState}, false, true, `{ "8e2c76b066dabe574cf073bdb46eb5c1": { "instance": "8e2c76b066dabe574cf073bdb46eb5c1", - "pid": 3735928559, + "pid": 3405691582, + "shim_pid": 3735928559, "config": { "id": "org.chromium.Chromium", "enablements": { diff --git a/hst/instance.go b/hst/instance.go index 5d15df6..e2ad5a5 100644 --- a/hst/instance.go +++ b/hst/instance.go @@ -74,8 +74,11 @@ func (a *ID) UnmarshalText(text []byte) error { type State struct { // Unique instance id, created by [NewInstanceID]. ID ID `json:"instance"` - // Shim process pid. Runs as the target user. + // Monitoring process pid. Runs as the priv user. PID int `json:"pid"` + // Shim process pid. Runs as the target user. + ShimPID int `json:"shim_pid"` + // Configuration used to start the container. Config *Config `json:"config"` diff --git a/internal/app/process.go b/internal/app/process.go index f71479b..8e18ae6 100644 --- a/internal/app/process.go +++ b/internal/app/process.go @@ -290,10 +290,11 @@ func (k *outcome) main(msg message.Msg) { // shim accepted setup payload, create process state if ok, err := ms.store.Do(k.state.identity.unwrap(), func(c state.Cursor) { if err := c.Save(&hst.State{ - ID: k.state.id.unwrap(), - PID: ms.cmd.Process.Pid, - Config: k.config, - Time: *ms.Time, + ID: k.state.id.unwrap(), + PID: os.Getpid(), + ShimPID: ms.cmd.Process.Pid, + Config: k.config, + Time: *ms.Time, }); err != nil { ms.fatal("cannot save state entry:", err) }