Skip to content

Commit 789a73d

Browse files
committed
init.go: move logger setup to StartInitialization
Currently, logrus is used from the Go part of runc init, mostly for a few debug messages (see setns_init_linux.go and standard_init_linux.go), and a single warning (see rootfs_linux.go). This means logrus is part of init implementation, and thus, its setup belongs to StartInitialization(). Move the code there. As a nice side effect, now we don't have to convert _LIBCONTAINER_LOGPIPE twice. Note that since this initialization is now also called from libct/int tests, which do not set _LIBCONTAINER_LOGLEVEL, let's make _LIBCONTAINER_LOGLEVEL optional. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 23e41ef commit 789a73d

File tree

3 files changed

+26
-30
lines changed

3 files changed

+26
-30
lines changed

init.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ package main
33
import (
44
"os"
55
"runtime"
6-
"strconv"
76

87
"github.com/opencontainers/runc/libcontainer"
98
_ "github.com/opencontainers/runc/libcontainer/nsenter"
10-
"github.com/sirupsen/logrus"
119
)
1210

1311
func init() {
@@ -17,21 +15,6 @@ func init() {
1715
runtime.GOMAXPROCS(1)
1816
runtime.LockOSThread()
1917

20-
level, err := strconv.Atoi(os.Getenv("_LIBCONTAINER_LOGLEVEL"))
21-
if err != nil {
22-
panic(err)
23-
}
24-
25-
logPipeFd, err := strconv.Atoi(os.Getenv("_LIBCONTAINER_LOGPIPE"))
26-
if err != nil {
27-
panic(err)
28-
}
29-
30-
logrus.SetLevel(logrus.Level(level))
31-
logrus.SetOutput(os.NewFile(uintptr(logPipeFd), "logpipe"))
32-
logrus.SetFormatter(new(logrus.JSONFormatter))
33-
logrus.Debug("child process in init()")
34-
3518
if err := libcontainer.StartInitialization(); err != nil {
3619
// as the error is sent back to the parent there is no need to log
3720
// or write it to stderr because the parent process will handle this

libcontainer/container_linux.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,10 @@ func (c *Container) commandTemplate(p *Process, childInitPipe *os.File, childLog
501501

502502
cmd.ExtraFiles = append(cmd.ExtraFiles, childLogPipe)
503503
cmd.Env = append(cmd.Env,
504-
"_LIBCONTAINER_LOGPIPE="+strconv.Itoa(stdioFdCount+len(cmd.ExtraFiles)-1),
505-
"_LIBCONTAINER_LOGLEVEL="+p.LogLevel,
506-
)
504+
"_LIBCONTAINER_LOGPIPE="+strconv.Itoa(stdioFdCount+len(cmd.ExtraFiles)-1))
505+
if p.LogLevel != "" {
506+
cmd.Env = append(cmd.Env, "_LIBCONTAINER_LOGLEVEL="+p.LogLevel)
507+
}
507508

508509
// NOTE: when running a container with no PID namespace and the parent process spawning the container is
509510
// PID1 the pdeathsig is being delivered to the container's init process by the kernel for some reason

libcontainer/init_linux.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ func StartInitialization() (retErr error) {
9292
envInitPipe := os.Getenv("_LIBCONTAINER_INITPIPE")
9393
pipefd, err := strconv.Atoi(envInitPipe)
9494
if err != nil {
95-
err = fmt.Errorf("unable to convert _LIBCONTAINER_INITPIPE: %w", err)
96-
logrus.Error(err)
97-
return err
95+
return fmt.Errorf("unable to convert _LIBCONTAINER_INITPIPE: %w", err)
9896
}
9997
pipe := os.NewFile(uintptr(pipefd), "pipe")
10098
defer pipe.Close()
@@ -112,6 +110,26 @@ func StartInitialization() (retErr error) {
112110
}
113111
}()
114112

113+
// Set up logging. This is used rarely, and mostly for init debugging.
114+
115+
// Passing log level is optional; currently libcontainer/integration does not do it.
116+
if levelStr := os.Getenv("_LIBCONTAINER_LOGLEVEL"); levelStr != "" {
117+
logLevel, err := strconv.Atoi(levelStr)
118+
if err != nil {
119+
return fmt.Errorf("unable to convert _LIBCONTAINER_LOGLEVEL: %w", err)
120+
}
121+
logrus.SetLevel(logrus.Level(logLevel))
122+
}
123+
124+
logFD, err := strconv.Atoi(os.Getenv("_LIBCONTAINER_LOGPIPE"))
125+
if err != nil {
126+
return fmt.Errorf("unable to convert _LIBCONTAINER_LOGPIPE: %w", err)
127+
}
128+
129+
logrus.SetOutput(os.NewFile(uintptr(logFD), "logpipe"))
130+
logrus.SetFormatter(new(logrus.JSONFormatter))
131+
logrus.Debug("child process in init()")
132+
115133
// Only init processes have FIFOFD.
116134
fifofd := -1
117135
envInitType := os.Getenv("_LIBCONTAINER_INITTYPE")
@@ -133,12 +151,6 @@ func StartInitialization() (retErr error) {
133151
defer consoleSocket.Close()
134152
}
135153

136-
logPipeFdStr := os.Getenv("_LIBCONTAINER_LOGPIPE")
137-
logPipeFd, err := strconv.Atoi(logPipeFdStr)
138-
if err != nil {
139-
return fmt.Errorf("unable to convert _LIBCONTAINER_LOGPIPE: %w", err)
140-
}
141-
142154
// Get mount files (O_PATH).
143155
mountSrcFds, err := parseFdsFromEnv("_LIBCONTAINER_MOUNT_FDS")
144156
if err != nil {
@@ -166,7 +178,7 @@ func StartInitialization() (retErr error) {
166178
}()
167179

168180
// If init succeeds, it will not return, hence none of the defers will be called.
169-
return containerInit(it, pipe, consoleSocket, fifofd, logPipeFd, mountFds{sourceFds: mountSrcFds, idmapFds: idmapFds})
181+
return containerInit(it, pipe, consoleSocket, fifofd, logFD, mountFds{sourceFds: mountSrcFds, idmapFds: idmapFds})
170182
}
171183

172184
func containerInit(t initType, pipe *os.File, consoleSocket *os.File, fifoFd, logFd int, mountFds mountFds) error {

0 commit comments

Comments
 (0)