Skip to content

Commit

Permalink
gcplogs: forcibly set HOME on static UNIX binary
Browse files Browse the repository at this point in the history
Fix moby#29344

If HOME is not set, the gcplogs logging driver will call os/user.Current() via oauth2/google.
However, in static binary, os/user.Current() leads to segfault due to a glibc issue that won't be fixed
in a short term. (golang/go#13470, https://sourceware.org/bugzilla/show_bug.cgi?id=19341)
So we forcibly set HOME so as to avoid call to os/user/Current().

Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
  • Loading branch information
AkihiroSuda committed Dec 29, 2016
1 parent a2b12b5 commit b86e3be
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
9 changes: 9 additions & 0 deletions daemon/logger/gcplogs/gcplogging.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ func New(ctx logger.Context) (logger.Logger, error) {
return nil, fmt.Errorf("No project was specified and couldn't read project from the meatadata server. Please specify a project")
}

// Issue #29344: gcplogs segfaults (static binary)
// If HOME is not set, logging.NewClient() will call os/user.Current() via oauth2/google.
// However, in static binary, os/user.Current() leads to segfault due to a glibc issue that won't be fixed
// in a short term. (golang/go#13470, https://sourceware.org/bugzilla/show_bug.cgi?id=19341)
// So we forcibly set HOME so as to avoid call to os/user/Current()
if err := ensureHomeIfIAmStatic(); err != nil {
return nil, err
}

c, err := logging.NewClient(context.Background(), project)
if err != nil {
return nil, err
Expand Down
31 changes: 31 additions & 0 deletions daemon/logger/gcplogs/gcplogging_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// +build linux

package gcplogs

import (
"os"

"github.com/Sirupsen/logrus"
"github.com/docker/docker/dockerversion"
"github.com/docker/docker/pkg/homedir"
)

// ensureHomeIfIAmStatic ensure $HOME to be set if dockerversion.IAmStatic is "true".
// See issue #29344: gcplogs segfaults (static binary)
// If HOME is not set, logging.NewClient() will call os/user.Current() via oauth2/google.
// However, in static binary, os/user.Current() leads to segfault due to a glibc issue that won't be fixed
// in a short term. (golang/go#13470, https://sourceware.org/bugzilla/show_bug.cgi?id=19341)
// So we forcibly set HOME so as to avoid call to os/user/Current()
func ensureHomeIfIAmStatic() error {
// Note: dockerversion.IAmStatic and homedir.GetStatic() is only available for linux.
// So we need to use them in this gcplogging_linux.go rather than in gcplogging.go
if dockerversion.IAmStatic == "true" && os.Getenv("HOME") == "" {
home, err := homedir.GetStatic()
if err != nil {
return err
}
logrus.Warnf("gcplogs requires HOME to be set for static daemon binary. Forcibly setting HOME to %s.", home)
os.Setenv("HOME", home)
}
return nil
}
7 changes: 7 additions & 0 deletions daemon/logger/gcplogs/gcplogging_others.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build !linux

package gcplogs

func ensureHomeIfIAmStatic() error {
return nil
}
23 changes: 23 additions & 0 deletions pkg/homedir/homedir_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// +build linux

package homedir

import (
"os"

"github.com/docker/docker/pkg/idtools"
)

// GetStatic returns the home directory for the current user without calling
// os/user.Current(). This is useful for static-linked binary on glibc-based
// system, because a call to os/user.Current() in a static binary leads to
// segfault due to a glibc issue that won't be fixed in a short term.
// (#29344, golang/go#13470, https://sourceware.org/bugzilla/show_bug.cgi?id=19341)
func GetStatic() (string, error) {
uid := os.Getuid()
usr, err := idtools.LookupUID(uid)
if err != nil {
return "", err
}
return usr.Home, nil
}
13 changes: 13 additions & 0 deletions pkg/homedir/homedir_others.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// +build !linux

package homedir

import (
"errors"
)

// GetStatic is not needed for non-linux systems.
// (Precisely, it is needed only for glibc-based linux systems.)
func GetStatic() (string, error) {
return "", errors.New("homedir.GetStatic() is not supported on this system")
}

0 comments on commit b86e3be

Please sign in to comment.