From d38d1f9f790cc221f090cdd4dd33afb5ed853730 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Tue, 14 Jul 2020 14:54:43 -0700 Subject: [PATCH] libcontainer/logs: use int for Config.LogPipeFd It does not make sense to have a string for a numeric type. Signed-off-by: Kir Kolyshkin --- init.go | 8 +++++++- libcontainer/logs/logs.go | 13 +++++-------- main.go | 4 ++-- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/init.go b/init.go index 08351fdb488..59ce1e8c739 100644 --- a/init.go +++ b/init.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "runtime" + "strconv" "github.com/opencontainers/runc/libcontainer" "github.com/opencontainers/runc/libcontainer/logs" @@ -23,8 +24,13 @@ func init() { panic(fmt.Sprintf("libcontainer: failed to parse log level: %q: %v", level, err)) } + logPipeFdStr := os.Getenv("_LIBCONTAINER_LOGPIPE") + logPipeFd, err := strconv.Atoi(logPipeFdStr) + if err != nil { + panic(fmt.Sprintf("libcontainer: failed to convert environment variable _LIBCONTAINER_LOGPIPE=%s to int: %s", logPipeFdStr, err)) + } err = logs.ConfigureLogging(logs.Config{ - LogPipeFd: os.Getenv("_LIBCONTAINER_LOGPIPE"), + LogPipeFd: logPipeFd, LogFormat: "json", LogLevel: logLevel, }) diff --git a/libcontainer/logs/logs.go b/libcontainer/logs/logs.go index 1077e7b0145..a49a074eeae 100644 --- a/libcontainer/logs/logs.go +++ b/libcontainer/logs/logs.go @@ -6,7 +6,6 @@ import ( "fmt" "io" "os" - "strconv" "sync" "github.com/sirupsen/logrus" @@ -23,7 +22,7 @@ type Config struct { LogLevel logrus.Level LogFormat string LogFilePath string - LogPipeFd string + LogPipeFd int } func ForwardLogs(logPipe io.Reader) { @@ -74,12 +73,10 @@ func ConfigureLogging(config Config) error { logrus.SetLevel(config.LogLevel) - if config.LogPipeFd != "" { - logPipeFdInt, err := strconv.Atoi(config.LogPipeFd) - if err != nil { - return fmt.Errorf("failed to convert _LIBCONTAINER_LOGPIPE environment variable value %q to int: %v", config.LogPipeFd, err) - } - logrus.SetOutput(os.NewFile(uintptr(logPipeFdInt), "logpipe")) + // XXX: while 0 is a valid fd (usually stdin), here we assume + // that we never deliberately set LogPipeFd to 0. + if config.LogPipeFd > 0 { + logrus.SetOutput(os.NewFile(uintptr(config.LogPipeFd), "logpipe")) } else if config.LogFilePath != "" { f, err := os.OpenFile(config.LogFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND|os.O_SYNC, 0644) if err != nil { diff --git a/main.go b/main.go index 57d7f4282e6..9f8beb16f0d 100644 --- a/main.go +++ b/main.go @@ -176,9 +176,9 @@ func (f *FatalWriter) Write(p []byte) (n int, err error) { func createLogConfig(context *cli.Context) logs.Config { logFilePath := context.GlobalString("log") - logPipeFd := "" + logPipeFd := 0 if logFilePath == "" { - logPipeFd = "2" + logPipeFd = 2 } config := logs.Config{ LogPipeFd: logPipeFd,