From 1c3761bb53c95d75751f95e8f77d9d1e5928b968 Mon Sep 17 00:00:00 2001 From: Ophestra Date: Tue, 25 Aug 2026 14:48:27 +0900 Subject: container: enter init path early There is generally no use case where any setup is required before init, and requiring the explicit function call is error-prone and unnecessary. It also causes trouble with packages using a similar trick. This change moves argv0 check early and makes it an import side effect. The stub will be removed in v0.5. Signed-off-by: Ophestra --- container/init.go | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) (limited to 'container/init.go') diff --git a/container/init.go b/container/init.go index 41de58cd..510d9f59 100644 --- a/container/init.go +++ b/container/init.go @@ -2,6 +2,7 @@ package container import ( "context" + "encoding/gob" "errors" "fmt" "log" @@ -130,7 +131,7 @@ type initParams struct { Verbose bool } -// Init is called by [TryArgv0] if the current process is the container init. +// Init is called if the current process is the container init. func Init(msg message.Msg) { initEntrypoint(direct{}, msg) } func initEntrypoint(k syscallDispatcher, msg message.Msg) { @@ -642,18 +643,37 @@ func initEntrypoint(k syscallDispatcher, msg message.Msg) { // initName is the prefix used by log.std in the init process. const initName = "init" -// TryArgv0 calls [Init] if the last element of argv0 is "init". -// If a nil msg is passed, the system logger is used instead. -func TryArgv0(msg message.Msg) { - if msg == nil { +var _ = func() struct{} { + for _, v := range []any{ + (*AutoEtcOp)(nil), + (*AutoRootOp)(nil), + (*BindMountOp)(nil), + (*DaemonOp)(nil), + (*MkdirOp)(nil), + (*MountDevOp)(nil), + (*MountOverlayOp)(nil), + (*MountProcOp)(nil), + (*MountTmpfsOp)(nil), + (*RemountOp)(nil), + (*SymlinkOp)(nil), + (*TmpfileOp)(nil), + } { + gob.Register(v) + } + + if len(os.Args) == 1 && filepath.Base(os.Args[0]) == initName { log.SetPrefix(initName + ": ") log.SetFlags(0) - msg = message.New(log.Default()) - } + msg := message.New(log.Default()) - if len(os.Args) > 0 && filepath.Base(os.Args[0]) == initName { Init(msg) msg.BeforeExit() os.Exit(0) } -} + return struct{}{} +}() + +// TryArgv0 is a noop. +// +// Deprecated: init is now implemented as an import side effect. +func TryArgv0(_ message.Msg) {} -- cgit v1.3.1