Skip to content

Commit

Permalink
libct/init: call Init from containerInit
Browse files Browse the repository at this point in the history
Instead of having newContainerInit return an interface, and let its
caller call Init(), it is easier to call Init directly.

Do that, and rename newContainerInit to containerInit.

I think it makes the code more readable and straightforward.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed May 12, 2022
1 parent 2a295a8 commit 6b98a38
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions libcontainer/init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,52 +167,45 @@ func StartInitialization() (err error) {
}
}()

i, err := newContainerInit(it, pipe, consoleSocket, fifofd, logPipeFd, mountFds)
if err != nil {
return err
}

// If Init succeeds, syscall.Exec will not return, hence none of the defers will be called.
return i.Init()
}

type initer interface {
Init() error
// If init succeeds, it will not return, hence none of the defers will be called.
return containerInit(it, pipe, consoleSocket, fifofd, logPipeFd, mountFds)
}

func newContainerInit(t initType, pipe *os.File, consoleSocket *os.File, fifoFd, logFd int, mountFds []int) (initer, error) {
func containerInit(t initType, pipe *os.File, consoleSocket *os.File, fifoFd, logFd int, mountFds []int) error {
var config *initConfig
if err := json.NewDecoder(pipe).Decode(&config); err != nil {
return nil, err
return err
}
if err := populateProcessEnvironment(config.Env); err != nil {
return nil, err
return err
}
switch t {
case initSetns:
// mountFds must be nil in this case. We don't mount while doing runc exec.
if mountFds != nil {
return nil, errors.New("mountFds must be nil; can't mount from exec")
return errors.New("mountFds must be nil; can't mount from exec")
}

return &linuxSetnsInit{
i := &linuxSetnsInit{
pipe: pipe,
consoleSocket: consoleSocket,
config: config,
logFd: logFd,
}, nil
}
return i.Init()
case initStandard:
return &linuxStandardInit{
i := &linuxStandardInit{
pipe: pipe,
consoleSocket: consoleSocket,
parentPid: unix.Getppid(),
config: config,
fifoFd: fifoFd,
logFd: logFd,
mountFds: mountFds,
}, nil
}
return i.Init()
}
return nil, fmt.Errorf("unknown init type %q", t)
return fmt.Errorf("unknown init type %q", t)
}

// populateProcessEnvironment loads the provided environment variables into the
Expand Down

0 comments on commit 6b98a38

Please sign in to comment.