2017-05-04 08:15:47 +08:00
|
|
|
// Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package build
|
|
|
|
|
2019-01-03 04:24:44 +08:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"os/user"
|
2020-04-16 01:42:59 +08:00
|
|
|
"path/filepath"
|
2019-01-03 04:24:44 +08:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2017-05-04 08:15:47 +08:00
|
|
|
)
|
|
|
|
|
2019-01-03 04:24:44 +08:00
|
|
|
type Sandbox struct {
|
|
|
|
Enabled bool
|
|
|
|
DisableWhenUsingGoma bool
|
2019-04-10 01:22:43 +08:00
|
|
|
|
|
|
|
AllowBuildBrokenUsesNetwork bool
|
2019-01-03 04:24:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
noSandbox = Sandbox{}
|
|
|
|
basicSandbox = Sandbox{
|
|
|
|
Enabled: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
dumpvarsSandbox = basicSandbox
|
|
|
|
katiSandbox = basicSandbox
|
|
|
|
soongSandbox = basicSandbox
|
|
|
|
ninjaSandbox = Sandbox{
|
|
|
|
Enabled: true,
|
|
|
|
DisableWhenUsingGoma: true,
|
2019-04-10 01:22:43 +08:00
|
|
|
|
|
|
|
AllowBuildBrokenUsesNetwork: true,
|
2019-01-03 04:24:44 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const nsjailPath = "prebuilts/build-tools/linux-x86/bin/nsjail"
|
|
|
|
|
|
|
|
var sandboxConfig struct {
|
|
|
|
once sync.Once
|
|
|
|
|
|
|
|
working bool
|
|
|
|
group string
|
2020-04-03 02:11:28 +08:00
|
|
|
srcDir string
|
|
|
|
outDir string
|
|
|
|
distDir string
|
2019-01-03 04:24:44 +08:00
|
|
|
}
|
|
|
|
|
2017-05-04 08:15:47 +08:00
|
|
|
func (c *Cmd) sandboxSupported() bool {
|
2019-01-03 04:24:44 +08:00
|
|
|
if !c.Sandbox.Enabled {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Goma is incompatible with PID namespaces and Mount namespaces. b/122767582
|
|
|
|
if c.Sandbox.DisableWhenUsingGoma && c.config.UseGoma() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
sandboxConfig.once.Do(func() {
|
|
|
|
sandboxConfig.group = "nogroup"
|
|
|
|
if _, err := user.LookupGroup(sandboxConfig.group); err != nil {
|
|
|
|
sandboxConfig.group = "nobody"
|
|
|
|
}
|
|
|
|
|
2020-04-16 01:42:59 +08:00
|
|
|
// These directories will be bind mounted
|
|
|
|
// so we need full non-symlink paths
|
2020-04-03 02:11:28 +08:00
|
|
|
sandboxConfig.srcDir = absPath(c.ctx, ".")
|
2020-04-16 01:42:59 +08:00
|
|
|
if derefPath, err := filepath.EvalSymlinks(sandboxConfig.srcDir); err == nil {
|
|
|
|
sandboxConfig.srcDir = absPath(c.ctx, derefPath)
|
|
|
|
}
|
2020-04-03 02:11:28 +08:00
|
|
|
sandboxConfig.outDir = absPath(c.ctx, c.config.OutDir())
|
2020-04-16 01:42:59 +08:00
|
|
|
if derefPath, err := filepath.EvalSymlinks(sandboxConfig.outDir); err == nil {
|
|
|
|
sandboxConfig.outDir = absPath(c.ctx, derefPath)
|
|
|
|
}
|
2020-04-03 02:11:28 +08:00
|
|
|
sandboxConfig.distDir = absPath(c.ctx, c.config.DistDir())
|
2020-04-16 01:42:59 +08:00
|
|
|
if derefPath, err := filepath.EvalSymlinks(sandboxConfig.distDir); err == nil {
|
|
|
|
sandboxConfig.distDir = absPath(c.ctx, derefPath)
|
|
|
|
}
|
2020-04-03 02:11:28 +08:00
|
|
|
|
2020-04-07 06:02:38 +08:00
|
|
|
sandboxArgs := []string{
|
2019-01-03 04:24:44 +08:00
|
|
|
"-H", "android-build",
|
|
|
|
"-e",
|
|
|
|
"-u", "nobody",
|
|
|
|
"-g", sandboxConfig.group,
|
2020-04-03 02:11:28 +08:00
|
|
|
"-R", "/",
|
|
|
|
"-B", sandboxConfig.srcDir,
|
|
|
|
"-B", "/tmp",
|
|
|
|
"-B", sandboxConfig.outDir,
|
2020-04-07 06:02:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(sandboxConfig.distDir); !os.IsNotExist(err) {
|
|
|
|
//Mount dist dir as read-write if it already exists
|
|
|
|
sandboxArgs = append(sandboxArgs, "-B",
|
|
|
|
sandboxConfig.distDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
sandboxArgs = append(sandboxArgs,
|
2019-01-03 04:24:44 +08:00
|
|
|
"--disable_clone_newcgroup",
|
|
|
|
"--",
|
|
|
|
"/bin/bash", "-c", `if [ $(hostname) == "android-build" ]; then echo "Android" "Success"; else echo Failure; fi`)
|
2020-04-07 06:02:38 +08:00
|
|
|
|
|
|
|
cmd := exec.CommandContext(c.ctx.Context, nsjailPath, sandboxArgs...)
|
|
|
|
|
2019-01-03 04:24:44 +08:00
|
|
|
cmd.Env = c.config.Environment().Environ()
|
|
|
|
|
|
|
|
c.ctx.Verboseln(cmd.Args)
|
|
|
|
data, err := cmd.CombinedOutput()
|
|
|
|
if err == nil && bytes.Contains(data, []byte("Android Success")) {
|
|
|
|
sandboxConfig.working = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-03 04:36:04 +08:00
|
|
|
c.ctx.Println("Build sandboxing disabled due to nsjail error.")
|
2019-01-03 04:24:44 +08:00
|
|
|
|
|
|
|
for _, line := range strings.Split(strings.TrimSpace(string(data)), "\n") {
|
|
|
|
c.ctx.Verboseln(line)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
c.ctx.Verboseln("nsjail exited successfully, but without the correct output")
|
|
|
|
} else if e, ok := err.(*exec.ExitError); ok {
|
|
|
|
c.ctx.Verbosef("nsjail failed with %v", e.ProcessState.String())
|
|
|
|
} else {
|
|
|
|
c.ctx.Verbosef("nsjail failed with %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return sandboxConfig.working
|
2017-05-04 08:15:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cmd) wrapSandbox() {
|
2019-01-03 04:24:44 +08:00
|
|
|
wd, _ := os.Getwd()
|
|
|
|
|
|
|
|
sandboxArgs := []string{
|
|
|
|
// The executable to run
|
|
|
|
"-x", c.Path,
|
|
|
|
|
|
|
|
// Set the hostname to something consistent
|
|
|
|
"-H", "android-build",
|
|
|
|
|
|
|
|
// Use the current working dir
|
|
|
|
"--cwd", wd,
|
|
|
|
|
|
|
|
// No time limit
|
|
|
|
"-t", "0",
|
|
|
|
|
|
|
|
// Keep all environment variables, we already filter them out
|
|
|
|
// in soong_ui
|
|
|
|
"-e",
|
|
|
|
|
2019-01-17 15:02:24 +08:00
|
|
|
// Mount /proc read-write, necessary to run a nested nsjail or minijail0
|
|
|
|
"--proc_rw",
|
|
|
|
|
2019-01-03 04:24:44 +08:00
|
|
|
// Use a consistent user & group.
|
|
|
|
// Note that these are mapped back to the real UID/GID when
|
|
|
|
// doing filesystem operations, so they're rather arbitrary.
|
|
|
|
"-u", "nobody",
|
|
|
|
"-g", sandboxConfig.group,
|
|
|
|
|
|
|
|
// Set high values, as nsjail uses low defaults.
|
|
|
|
"--rlimit_as", "soft",
|
|
|
|
"--rlimit_core", "soft",
|
|
|
|
"--rlimit_cpu", "soft",
|
|
|
|
"--rlimit_fsize", "soft",
|
|
|
|
"--rlimit_nofile", "soft",
|
|
|
|
|
2020-04-03 02:11:28 +08:00
|
|
|
// For now, just map everything. Make most things readonly.
|
|
|
|
"-R", "/",
|
|
|
|
|
2020-05-02 07:26:56 +08:00
|
|
|
// Mount a writable tmp dir
|
|
|
|
"-B", "/tmp",
|
|
|
|
|
2020-04-03 02:11:28 +08:00
|
|
|
// Mount source are read-write
|
|
|
|
"-B", sandboxConfig.srcDir,
|
|
|
|
|
|
|
|
//Mount out dir as read-write
|
|
|
|
"-B", sandboxConfig.outDir,
|
|
|
|
|
2019-01-03 04:24:44 +08:00
|
|
|
// Disable newcgroup for now, since it may require newer kernels
|
|
|
|
// TODO: try out cgroups
|
|
|
|
"--disable_clone_newcgroup",
|
|
|
|
|
|
|
|
// Only log important warnings / errors
|
|
|
|
"-q",
|
2019-04-10 01:22:43 +08:00
|
|
|
}
|
2019-01-03 04:24:44 +08:00
|
|
|
|
2020-04-07 06:02:38 +08:00
|
|
|
if _, err := os.Stat(sandboxConfig.distDir); !os.IsNotExist(err) {
|
|
|
|
//Mount dist dir as read-write if it already exists
|
|
|
|
sandboxArgs = append(sandboxArgs, "-B", sandboxConfig.distDir)
|
|
|
|
}
|
|
|
|
|
2019-04-10 01:22:43 +08:00
|
|
|
if c.Sandbox.AllowBuildBrokenUsesNetwork && c.config.BuildBrokenUsesNetwork() {
|
|
|
|
c.ctx.Printf("AllowBuildBrokenUsesNetwork: %v", c.Sandbox.AllowBuildBrokenUsesNetwork)
|
|
|
|
c.ctx.Printf("BuildBrokenUsesNetwork: %v", c.config.BuildBrokenUsesNetwork())
|
|
|
|
sandboxArgs = append(sandboxArgs, "-N")
|
2019-06-20 04:33:24 +08:00
|
|
|
} else if dlv, _ := c.config.Environment().Get("SOONG_DELVE"); dlv != "" {
|
|
|
|
// The debugger is enabled and soong_build will pause until a remote delve process connects, allow
|
|
|
|
// network connections.
|
|
|
|
sandboxArgs = append(sandboxArgs, "-N")
|
2019-01-03 04:24:44 +08:00
|
|
|
}
|
2019-04-10 01:22:43 +08:00
|
|
|
|
|
|
|
// Stop nsjail from parsing arguments
|
|
|
|
sandboxArgs = append(sandboxArgs, "--")
|
|
|
|
|
2019-01-03 04:24:44 +08:00
|
|
|
c.Args = append(sandboxArgs, c.Args[1:]...)
|
|
|
|
c.Path = nsjailPath
|
|
|
|
|
|
|
|
env := Environment(c.Env)
|
|
|
|
if _, hasUser := env.Get("USER"); hasUser {
|
|
|
|
env.Set("USER", "nobody")
|
|
|
|
}
|
|
|
|
c.Env = []string(env)
|
2017-05-04 08:15:47 +08:00
|
|
|
}
|