internal/app: relocate params state initialisation
All checks were successful
Test / Create distribution (push) Successful in 35s
Test / Sandbox (push) Successful in 2m13s
Test / Hakurei (push) Successful in 3m5s
Test / Hpkg (push) Successful in 4m9s
Test / Hakurei (race detector) (push) Successful in 5m18s
Test / Sandbox (race detector) (push) Successful in 2m9s
Test / Flake checks (push) Successful in 1m40s

This is useful for testing.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
Ophestra 2025-10-10 21:59:16 +09:00
parent 24de7c50a0
commit 070e346587
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
3 changed files with 24 additions and 24 deletions

View File

@ -9,7 +9,6 @@ import (
"io" "io"
"io/fs" "io/fs"
"log" "log"
"maps"
"os/exec" "os/exec"
"os/user" "os/user"
"reflect" "reflect"
@ -469,7 +468,7 @@ func TestApp(t *testing.T) {
}() }()
} }
var gotParams container.Params var gotParams *container.Params
{ {
var sShim outcomeState var sShim outcomeState
@ -481,17 +480,13 @@ func TestApp(t *testing.T) {
t.Fatalf("populateLocal: error = %#v", err) t.Fatalf("populateLocal: error = %#v", err)
} }
stateParams := outcomeStateParams{params: &gotParams, outcomeState: &sShim} stateParams := sShim.newParams()
if sShim.Container.Env == nil {
stateParams.env = make(map[string]string, envAllocSize)
} else {
stateParams.env = maps.Clone(sShim.Container.Env)
}
for _, op := range sShim.Shim.Ops { for _, op := range sShim.Shim.Ops {
if err := op.toContainer(&stateParams); err != nil { if err := op.toContainer(stateParams); err != nil {
t.Fatalf("toContainer: error = %#v", err) t.Fatalf("toContainer: error = %#v", err)
} }
} }
gotParams = stateParams.params
} }
t.Run("sys", func(t *testing.T) { t.Run("sys", func(t *testing.T) {
@ -501,8 +496,8 @@ func TestApp(t *testing.T) {
}) })
t.Run("params", func(t *testing.T) { t.Run("params", func(t *testing.T) {
if !reflect.DeepEqual(&gotParams, tc.wantParams) { if !reflect.DeepEqual(gotParams, tc.wantParams) {
t.Errorf("toContainer: params =\n%s\n, want\n%s", mustMarshal(&gotParams), mustMarshal(tc.wantParams)) t.Errorf("toContainer: params =\n%s\n, want\n%s", mustMarshal(gotParams), mustMarshal(tc.wantParams))
} }
}) })
}) })

View File

@ -2,6 +2,7 @@ package app
import ( import (
"errors" "errors"
"maps"
"strconv" "strconv"
"hakurei.app/container" "hakurei.app/container"
@ -163,7 +164,7 @@ type outcomeStateSys struct {
*outcomeState *outcomeState
} }
// outcomeState returns the address of a new outcomeStateSys embedding the current outcomeState. // newSys returns the address of a new outcomeStateSys embedding the current outcomeState.
func (s *outcomeState) newSys(config *hst.Config, sys *system.I) *outcomeStateSys { func (s *outcomeState) newSys(config *hst.Config, sys *system.I) *outcomeStateSys {
return &outcomeStateSys{ return &outcomeStateSys{
appId: config.ID, et: config.Enablements.Unwrap(), appId: config.ID, et: config.Enablements.Unwrap(),
@ -173,6 +174,17 @@ func (s *outcomeState) newSys(config *hst.Config, sys *system.I) *outcomeStateSy
} }
} }
// newParams returns the address of a new outcomeStateParams embedding the current outcomeState.
func (s *outcomeState) newParams() *outcomeStateParams {
stateParams := outcomeStateParams{params: new(container.Params), outcomeState: s}
if s.Container.Env == nil {
stateParams.env = make(map[string]string, envAllocSize)
} else {
stateParams.env = maps.Clone(s.Container.Env)
}
return &stateParams
}
// ensureRuntimeDir must be called if access to paths within XDG_RUNTIME_DIR is required. // ensureRuntimeDir must be called if access to paths within XDG_RUNTIME_DIR is required.
func (state *outcomeStateSys) ensureRuntimeDir() { func (state *outcomeStateSys) ensureRuntimeDir() {
if state.useRuntimeDir { if state.useRuntimeDir {

View File

@ -5,7 +5,6 @@ import (
"errors" "errors"
"io" "io"
"log" "log"
"maps"
"os" "os"
"os/exec" "os/exec"
"os/signal" "os/signal"
@ -102,15 +101,9 @@ func ShimMain() {
log.Fatalf("cannot set parent-death signal: %v", errno) log.Fatalf("cannot set parent-death signal: %v", errno)
} }
var params container.Params stateParams := state.newParams()
stateParams := outcomeStateParams{params: &params, outcomeState: &state}
if state.Container.Env == nil {
stateParams.env = make(map[string]string, envAllocSize)
} else {
stateParams.env = maps.Clone(state.Container.Env)
}
for _, op := range state.Shim.Ops { for _, op := range state.Shim.Ops {
if err := op.toContainer(&stateParams); err != nil { if err := op.toContainer(stateParams); err != nil {
if m, ok := message.GetMessage(err); ok { if m, ok := message.GetMessage(err); ok {
log.Fatal(m) log.Fatal(m)
} else { } else {
@ -130,7 +123,7 @@ func ShimMain() {
switch buf[0] { switch buf[0] {
case 0: // got SIGCONT from monitor: shim exit requested case 0: // got SIGCONT from monitor: shim exit requested
if fp := cancelContainer.Load(); params.ForwardCancel && fp != nil && *fp != nil { if fp := cancelContainer.Load(); stateParams.params.ForwardCancel && fp != nil && *fp != nil {
(*fp)() (*fp)()
// shim now bound by ShimWaitDelay, implemented below // shim now bound by ShimWaitDelay, implemented below
continue continue
@ -158,7 +151,7 @@ func ShimMain() {
} }
}() }()
if params.Ops == nil { if stateParams.params.Ops == nil {
log.Fatal("invalid container params") log.Fatal("invalid container params")
} }
@ -171,7 +164,7 @@ func ShimMain() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
cancelContainer.Store(&stop) cancelContainer.Store(&stop)
z := container.New(ctx, msg) z := container.New(ctx, msg)
z.Params = params z.Params = *stateParams.params
z.Stdin, z.Stdout, z.Stderr = os.Stdin, os.Stdout, os.Stderr z.Stdin, z.Stdout, z.Stderr = os.Stdin, os.Stdout, os.Stderr
// bounds and default enforced in finalise.go // bounds and default enforced in finalise.go