Browse code

gcplogs: forcibly set HOME on static UNIX binary

Fix #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>

Akihiro Suda authored on 2016/12/16 18:50:14
Showing 5 changed files
... ...
@@ -114,6 +114,15 @@ func New(ctx logger.Context) (logger.Logger, error) {
114 114
 		return nil, fmt.Errorf("No project was specified and couldn't read project from the meatadata server. Please specify a project")
115 115
 	}
116 116
 
117
+	// Issue #29344: gcplogs segfaults (static binary)
118
+	// If HOME is not set, logging.NewClient() will call os/user.Current() via oauth2/google.
119
+	// However, in static binary, os/user.Current() leads to segfault due to a glibc issue that won't be fixed
120
+	// in a short term. (golang/go#13470, https://sourceware.org/bugzilla/show_bug.cgi?id=19341)
121
+	// So we forcibly set HOME so as to avoid call to os/user/Current()
122
+	if err := ensureHomeIfIAmStatic(); err != nil {
123
+		return nil, err
124
+	}
125
+
117 126
 	c, err := logging.NewClient(context.Background(), project)
118 127
 	if err != nil {
119 128
 		return nil, err
120 129
new file mode 100644
... ...
@@ -0,0 +1,31 @@
0
+// +build linux
1
+
2
+package gcplogs
3
+
4
+import (
5
+	"os"
6
+
7
+	"github.com/Sirupsen/logrus"
8
+	"github.com/docker/docker/dockerversion"
9
+	"github.com/docker/docker/pkg/homedir"
10
+)
11
+
12
+// ensureHomeIfIAmStatic ensure $HOME to be set if dockerversion.IAmStatic is "true".
13
+// See issue #29344: gcplogs segfaults (static binary)
14
+// If HOME is not set, logging.NewClient() will call os/user.Current() via oauth2/google.
15
+// However, in static binary, os/user.Current() leads to segfault due to a glibc issue that won't be fixed
16
+// in a short term. (golang/go#13470, https://sourceware.org/bugzilla/show_bug.cgi?id=19341)
17
+// So we forcibly set HOME so as to avoid call to os/user/Current()
18
+func ensureHomeIfIAmStatic() error {
19
+	// Note: dockerversion.IAmStatic and homedir.GetStatic() is only available for linux.
20
+	// So we need to use them in this gcplogging_linux.go rather than in gcplogging.go
21
+	if dockerversion.IAmStatic == "true" && os.Getenv("HOME") == "" {
22
+		home, err := homedir.GetStatic()
23
+		if err != nil {
24
+			return err
25
+		}
26
+		logrus.Warnf("gcplogs requires HOME to be set for static daemon binary. Forcibly setting HOME to %s.", home)
27
+		os.Setenv("HOME", home)
28
+	}
29
+	return nil
30
+}
0 31
new file mode 100644
... ...
@@ -0,0 +1,7 @@
0
+// +build !linux
1
+
2
+package gcplogs
3
+
4
+func ensureHomeIfIAmStatic() error {
5
+	return nil
6
+}
0 7
new file mode 100644
... ...
@@ -0,0 +1,23 @@
0
+// +build linux
1
+
2
+package homedir
3
+
4
+import (
5
+	"os"
6
+
7
+	"github.com/docker/docker/pkg/idtools"
8
+)
9
+
10
+// GetStatic returns the home directory for the current user without calling
11
+// os/user.Current(). This is useful for static-linked binary on glibc-based
12
+// system, because a call to os/user.Current() in a static binary leads to
13
+// segfault due to a glibc issue that won't be fixed in a short term.
14
+// (#29344, golang/go#13470, https://sourceware.org/bugzilla/show_bug.cgi?id=19341)
15
+func GetStatic() (string, error) {
16
+	uid := os.Getuid()
17
+	usr, err := idtools.LookupUID(uid)
18
+	if err != nil {
19
+		return "", err
20
+	}
21
+	return usr.Home, nil
22
+}
0 23
new file mode 100644
... ...
@@ -0,0 +1,13 @@
0
+// +build !linux
1
+
2
+package homedir
3
+
4
+import (
5
+	"errors"
6
+)
7
+
8
+// GetStatic is not needed for non-linux systems.
9
+// (Precisely, it is needed only for glibc-based linux systems.)
10
+func GetStatic() (string, error) {
11
+	return "", errors.New("homedir.GetStatic() is not supported on this system")
12
+}