forked from rosa/hakurei
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>
This commit is contained in:
+30
-10
@@ -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 {
|
||||
log.SetPrefix(initName + ": ")
|
||||
log.SetFlags(0)
|
||||
msg = message.New(log.Default())
|
||||
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) > 0 && filepath.Base(os.Args[0]) == initName {
|
||||
if len(os.Args) == 1 && filepath.Base(os.Args[0]) == initName {
|
||||
log.SetPrefix(initName + ": ")
|
||||
log.SetFlags(0)
|
||||
msg := message.New(log.Default())
|
||||
|
||||
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) {}
|
||||
|
||||
Reference in New Issue
Block a user