Skip to content

Commit

Permalink
libcontainer/logs: use int for Config.LogPipeFd
Browse files Browse the repository at this point in the history
It does not make sense to have a string for a numeric type.

Signed-off-by: Kir Kolyshkin <[email protected]>
  • Loading branch information
kolyshkin committed Mar 26, 2021
1 parent ec7ca2a commit d38d1f9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
8 changes: 7 additions & 1 deletion init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"runtime"
"strconv"

"github.com/opencontainers/runc/libcontainer"
"github.com/opencontainers/runc/libcontainer/logs"
Expand All @@ -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,
})
Expand Down
13 changes: 5 additions & 8 deletions libcontainer/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io"
"os"
"strconv"
"sync"

"github.com/sirupsen/logrus"
Expand All @@ -23,7 +22,7 @@ type Config struct {
LogLevel logrus.Level
LogFormat string
LogFilePath string
LogPipeFd string
LogPipeFd int
}

func ForwardLogs(logPipe io.Reader) {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d38d1f9

Please sign in to comment.