aboutsummaryrefslogtreecommitdiffhomepage
path: root/container/init.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2026-08-25 14:48:27 +0900
committerOphestra <cat@gensokyo.uk>2026-08-25 14:53:29 +0900
commit1c3761bb53c95d75751f95e8f77d9d1e5928b968 (patch)
tree180e272979b83f68e4c32b4f83cd242715a84a72 /container/init.go
parentac7abbbe3fa2eea01defb670abc7dc1dc336cf21 (diff)
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 <cat@gensokyo.uk>
Diffstat (limited to 'container/init.go')
-rw-r--r--container/init.go38
1 files changed, 29 insertions, 9 deletions
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) {}