2013-09-18 18:59:00 -06:00
|
|
|
// Copyright 2012 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// +build !appengine
|
|
|
|
|
|
|
|
// Package socket implements an WebSocket-based playground backend.
|
|
|
|
// Clients connect to a websocket handler and send run/kill commands, and
|
|
|
|
// the server sends the output and exit status of the running processes.
|
|
|
|
// Multiple clients running multiple processes may be served concurrently.
|
|
|
|
// The wire format is JSON and is described by the Message type.
|
|
|
|
//
|
|
|
|
// This will not run on App Engine as WebSockets are not supported there.
|
2014-12-08 21:00:58 -07:00
|
|
|
package socket // import "golang.org/x/tools/playground/socket"
|
2013-09-18 18:59:00 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2014-06-02 06:39:33 -06:00
|
|
|
"net"
|
2014-05-05 09:42:12 -06:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2013-09-18 18:59:00 -06:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
2014-11-09 14:50:40 -07:00
|
|
|
"golang.org/x/net/websocket"
|
2019-10-30 02:46:03 -06:00
|
|
|
"golang.org/x/tools/txtar"
|
2013-09-18 18:59:00 -06:00
|
|
|
)
|
|
|
|
|
2014-03-16 23:57:36 -06:00
|
|
|
// RunScripts specifies whether the socket handler should execute shell scripts
|
|
|
|
// (snippets that start with a shebang).
|
|
|
|
var RunScripts = true
|
|
|
|
|
2013-09-18 18:59:00 -06:00
|
|
|
// Environ provides an environment when a binary, such as the go tool, is
|
|
|
|
// invoked.
|
|
|
|
var Environ func() []string = os.Environ
|
|
|
|
|
|
|
|
const (
|
|
|
|
// The maximum number of messages to send per session (avoid flooding).
|
|
|
|
msgLimit = 1000
|
|
|
|
|
|
|
|
// Batch messages sent in this interval and send as a single message.
|
|
|
|
msgDelay = 10 * time.Millisecond
|
|
|
|
)
|
|
|
|
|
|
|
|
// Message is the wire format for the websocket connection to the browser.
|
|
|
|
// It is used for both sending output messages and receiving commands, as
|
|
|
|
// distinguished by the Kind field.
|
|
|
|
type Message struct {
|
|
|
|
Id string // client-provided unique id for the process
|
|
|
|
Kind string // in: "run", "kill" out: "stdout", "stderr", "end"
|
|
|
|
Body string
|
|
|
|
Options *Options `json:",omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options specify additional message options.
|
|
|
|
type Options struct {
|
|
|
|
Race bool // use -race flag when building code (for "run" only)
|
|
|
|
}
|
|
|
|
|
2014-05-05 09:42:12 -06:00
|
|
|
// NewHandler returns a websocket server which checks the origin of requests.
|
|
|
|
func NewHandler(origin *url.URL) websocket.Server {
|
|
|
|
return websocket.Server{
|
|
|
|
Config: websocket.Config{Origin: origin},
|
|
|
|
Handshake: handshake,
|
|
|
|
Handler: websocket.Handler(socketHandler),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handshake checks the origin of a request during the websocket handshake.
|
|
|
|
func handshake(c *websocket.Config, req *http.Request) error {
|
|
|
|
o, err := websocket.Origin(c, req)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("bad websocket origin:", err)
|
|
|
|
return websocket.ErrBadWebSocketOrigin
|
|
|
|
}
|
2014-06-02 06:39:33 -06:00
|
|
|
_, port, err := net.SplitHostPort(c.Origin.Host)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("bad websocket origin:", err)
|
|
|
|
return websocket.ErrBadWebSocketOrigin
|
|
|
|
}
|
|
|
|
ok := c.Origin.Scheme == o.Scheme && (c.Origin.Host == o.Host || c.Origin.Host == net.JoinHostPort(o.Host, port))
|
2014-05-05 09:42:12 -06:00
|
|
|
if !ok {
|
|
|
|
log.Println("bad websocket origin:", o)
|
|
|
|
return websocket.ErrBadWebSocketOrigin
|
|
|
|
}
|
2014-07-08 20:14:27 -06:00
|
|
|
log.Println("accepting connection from:", req.RemoteAddr)
|
2014-05-05 09:42:12 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-18 18:59:00 -06:00
|
|
|
// socketHandler handles the websocket connection for a given present session.
|
|
|
|
// It handles transcoding Messages to and from JSON format, and starting
|
|
|
|
// and killing processes.
|
|
|
|
func socketHandler(c *websocket.Conn) {
|
|
|
|
in, out := make(chan *Message), make(chan *Message)
|
|
|
|
errc := make(chan error, 1)
|
|
|
|
|
|
|
|
// Decode messages from client and send to the in channel.
|
|
|
|
go func() {
|
|
|
|
dec := json.NewDecoder(c)
|
|
|
|
for {
|
|
|
|
var m Message
|
|
|
|
if err := dec.Decode(&m); err != nil {
|
|
|
|
errc <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
in <- &m
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Receive messages from the out channel and encode to the client.
|
|
|
|
go func() {
|
|
|
|
enc := json.NewEncoder(c)
|
|
|
|
for m := range out {
|
|
|
|
if err := enc.Encode(m); err != nil {
|
|
|
|
errc <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-10-29 02:28:03 -06:00
|
|
|
defer close(out)
|
2013-09-18 18:59:00 -06:00
|
|
|
|
|
|
|
// Start and kill processes and handle errors.
|
|
|
|
proc := make(map[string]*process)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case m := <-in:
|
|
|
|
switch m.Kind {
|
|
|
|
case "run":
|
2014-07-08 20:14:27 -06:00
|
|
|
log.Println("running snippet from:", c.Request().RemoteAddr)
|
2013-09-18 18:59:00 -06:00
|
|
|
proc[m.Id].Kill()
|
2015-10-29 02:28:03 -06:00
|
|
|
proc[m.Id] = startProcess(m.Id, m.Body, out, m.Options)
|
2013-09-18 18:59:00 -06:00
|
|
|
case "kill":
|
|
|
|
proc[m.Id].Kill()
|
|
|
|
}
|
|
|
|
case err := <-errc:
|
|
|
|
if err != io.EOF {
|
|
|
|
// A encode or decode has failed; bail.
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
// Shut down any running processes.
|
|
|
|
for _, p := range proc {
|
|
|
|
p.Kill()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// process represents a running process.
|
|
|
|
type process struct {
|
|
|
|
out chan<- *Message
|
|
|
|
done chan struct{} // closed when wait completes
|
|
|
|
run *exec.Cmd
|
2019-10-30 02:46:03 -06:00
|
|
|
path string
|
2013-09-18 18:59:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// startProcess builds and runs the given program, sending its output
|
|
|
|
// and end event as Messages on the provided channel.
|
2015-10-29 02:28:03 -06:00
|
|
|
func startProcess(id, body string, dest chan<- *Message, opt *Options) *process {
|
|
|
|
var (
|
|
|
|
done = make(chan struct{})
|
|
|
|
out = make(chan *Message)
|
|
|
|
p = &process{out: out, done: done}
|
|
|
|
)
|
|
|
|
go func() {
|
|
|
|
defer close(done)
|
2016-12-10 12:39:41 -07:00
|
|
|
for m := range buffer(limiter(out, p), time.After) {
|
2015-10-29 02:28:03 -06:00
|
|
|
m.Id = id
|
|
|
|
dest <- m
|
|
|
|
}
|
|
|
|
}()
|
2013-10-14 15:14:08 -06:00
|
|
|
var err error
|
2014-08-01 16:04:49 -06:00
|
|
|
if path, args := shebang(body); path != "" {
|
|
|
|
if RunScripts {
|
|
|
|
err = p.startProcess(path, args, body)
|
|
|
|
} else {
|
|
|
|
err = errors.New("script execution is not allowed")
|
|
|
|
}
|
2013-10-14 15:14:08 -06:00
|
|
|
} else {
|
|
|
|
err = p.start(body, opt)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2013-09-18 18:59:00 -06:00
|
|
|
p.end(err)
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-29 02:28:03 -06:00
|
|
|
go func() {
|
|
|
|
p.end(p.run.Wait())
|
|
|
|
}()
|
2013-09-18 18:59:00 -06:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2015-10-29 02:28:03 -06:00
|
|
|
// end sends an "end" message to the client, containing the process id and the
|
|
|
|
// given error value. It also removes the binary, if present.
|
|
|
|
func (p *process) end(err error) {
|
2019-10-30 02:46:03 -06:00
|
|
|
if p.path != "" {
|
|
|
|
defer os.RemoveAll(p.path)
|
2015-10-29 02:28:03 -06:00
|
|
|
}
|
|
|
|
m := &Message{Kind: "end"}
|
|
|
|
if err != nil {
|
|
|
|
m.Body = err.Error()
|
|
|
|
}
|
|
|
|
p.out <- m
|
|
|
|
close(p.out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A killer provides a mechanism to terminate a process.
|
|
|
|
// The Kill method returns only once the process has exited.
|
|
|
|
type killer interface {
|
|
|
|
Kill()
|
|
|
|
}
|
|
|
|
|
|
|
|
// limiter returns a channel that wraps the given channel.
|
|
|
|
// It receives Messages from the given channel and sends them to the returned
|
|
|
|
// channel until it passes msgLimit messages, at which point it will kill the
|
|
|
|
// process and pass only the "end" message.
|
|
|
|
// When the given channel is closed, or when the "end" message is received,
|
|
|
|
// it closes the returned channel.
|
|
|
|
func limiter(in <-chan *Message, p killer) <-chan *Message {
|
|
|
|
out := make(chan *Message)
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
n := 0
|
|
|
|
for m := range in {
|
|
|
|
switch {
|
|
|
|
case n < msgLimit || m.Kind == "end":
|
|
|
|
out <- m
|
|
|
|
if m.Kind == "end" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case n == msgLimit:
|
|
|
|
// Kill in a goroutine as Kill will not return
|
|
|
|
// until the process' output has been
|
|
|
|
// processed, and we're doing that in this loop.
|
|
|
|
go p.Kill()
|
|
|
|
default:
|
|
|
|
continue // don't increment
|
|
|
|
}
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// buffer returns a channel that wraps the given channel. It receives messages
|
|
|
|
// from the given channel and sends them to the returned channel.
|
|
|
|
// Message bodies are gathered over the period msgDelay and coalesced into a
|
2015-10-29 23:59:40 -06:00
|
|
|
// single Message before they are passed on. Messages of the same kind are
|
|
|
|
// coalesced; when a message of a different kind is received, any buffered
|
|
|
|
// messages are flushed. When the given channel is closed, buffer flushes the
|
|
|
|
// remaining buffered messages and closes the returned channel.
|
2016-12-10 12:39:41 -07:00
|
|
|
// The timeAfter func should be time.After. It exists for testing.
|
|
|
|
func buffer(in <-chan *Message, timeAfter func(time.Duration) <-chan time.Time) <-chan *Message {
|
2015-10-29 02:28:03 -06:00
|
|
|
out := make(chan *Message)
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
2015-10-29 23:59:40 -06:00
|
|
|
var (
|
|
|
|
tc <-chan time.Time
|
|
|
|
buf []byte
|
|
|
|
kind string
|
|
|
|
flush = func() {
|
|
|
|
if len(buf) == 0 {
|
|
|
|
return
|
2015-10-29 02:28:03 -06:00
|
|
|
}
|
2015-10-29 23:59:40 -06:00
|
|
|
out <- &Message{Kind: kind, Body: safeString(buf)}
|
|
|
|
buf = buf[:0] // recycle buffer
|
|
|
|
kind = ""
|
2015-10-29 02:28:03 -06:00
|
|
|
}
|
2015-10-29 23:59:40 -06:00
|
|
|
)
|
2015-10-29 02:28:03 -06:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case m, ok := <-in:
|
|
|
|
if !ok {
|
|
|
|
flush()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if m.Kind == "end" {
|
|
|
|
flush()
|
|
|
|
out <- m
|
|
|
|
return
|
|
|
|
}
|
2015-10-29 23:59:40 -06:00
|
|
|
if kind != m.Kind {
|
|
|
|
flush()
|
|
|
|
kind = m.Kind
|
|
|
|
if tc == nil {
|
2016-12-10 12:39:41 -07:00
|
|
|
tc = timeAfter(msgDelay)
|
2015-10-29 23:59:40 -06:00
|
|
|
}
|
2015-10-29 02:28:03 -06:00
|
|
|
}
|
2015-10-29 23:59:40 -06:00
|
|
|
buf = append(buf, m.Body...)
|
2015-10-29 02:28:03 -06:00
|
|
|
case <-tc:
|
|
|
|
flush()
|
|
|
|
tc = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2013-09-18 18:59:00 -06:00
|
|
|
// Kill stops the process if it is running and waits for it to exit.
|
|
|
|
func (p *process) Kill() {
|
2015-10-29 02:28:03 -06:00
|
|
|
if p == nil || p.run == nil {
|
2013-09-18 18:59:00 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
p.run.Process.Kill()
|
|
|
|
<-p.done // block until process exits
|
|
|
|
}
|
|
|
|
|
2013-10-14 15:14:08 -06:00
|
|
|
// shebang looks for a shebang ('#!') at the beginning of the passed string.
|
|
|
|
// If found, it returns the path and args after the shebang.
|
2014-02-18 10:44:12 -07:00
|
|
|
// args includes the command as args[0].
|
2013-10-14 15:14:08 -06:00
|
|
|
func shebang(body string) (path string, args []string) {
|
|
|
|
body = strings.TrimSpace(body)
|
|
|
|
if !strings.HasPrefix(body, "#!") {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
if i := strings.Index(body, "\n"); i >= 0 {
|
|
|
|
body = body[:i]
|
|
|
|
}
|
|
|
|
fs := strings.Fields(body[2:])
|
2014-02-18 10:44:12 -07:00
|
|
|
return fs[0], fs
|
2013-10-14 15:14:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// startProcess starts a given program given its path and passing the given body
|
|
|
|
// to the command standard input.
|
|
|
|
func (p *process) startProcess(path string, args []string, body string) error {
|
|
|
|
cmd := &exec.Cmd{
|
|
|
|
Path: path,
|
|
|
|
Args: args,
|
|
|
|
Stdin: strings.NewReader(body),
|
2015-10-29 02:28:03 -06:00
|
|
|
Stdout: &messageWriter{kind: "stdout", out: p.out},
|
|
|
|
Stderr: &messageWriter{kind: "stderr", out: p.out},
|
2013-10-14 15:14:08 -06:00
|
|
|
}
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.run = cmd
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-18 18:59:00 -06:00
|
|
|
// start builds and starts the given program, sending its output to p.out,
|
|
|
|
// and stores the running *exec.Cmd in the run field.
|
|
|
|
func (p *process) start(body string, opt *Options) error {
|
|
|
|
// We "go build" and then exec the binary so that the
|
|
|
|
// resultant *exec.Cmd is a handle to the user's program
|
|
|
|
// (rather than the go tool process).
|
|
|
|
// This makes Kill work.
|
|
|
|
|
2019-10-30 02:46:03 -06:00
|
|
|
path, err := ioutil.TempDir("", "present-")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(path)
|
|
|
|
|
|
|
|
out := "prog"
|
2013-09-18 18:59:00 -06:00
|
|
|
if runtime.GOOS == "windows" {
|
2019-10-30 02:46:03 -06:00
|
|
|
out = "prog.exe"
|
2013-09-18 18:59:00 -06:00
|
|
|
}
|
2019-10-30 02:46:03 -06:00
|
|
|
bin := filepath.Join(path, out)
|
2013-09-18 18:59:00 -06:00
|
|
|
|
2019-10-30 02:46:03 -06:00
|
|
|
// write body to x.go files
|
|
|
|
a := txtar.Parse([]byte(body))
|
|
|
|
if len(a.Comment) != 0 {
|
|
|
|
a.Files = append(a.Files, txtar.File{Name: "prog.go", Data: a.Comment})
|
|
|
|
a.Comment = nil
|
|
|
|
}
|
|
|
|
hasModfile := false
|
|
|
|
for _, f := range a.Files {
|
|
|
|
err = ioutil.WriteFile(filepath.Join(path, f.Name), f.Data, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if f.Name == "go.mod" {
|
|
|
|
hasModfile = true
|
|
|
|
}
|
2013-09-18 18:59:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// build x.go, creating x
|
2019-10-30 02:46:03 -06:00
|
|
|
p.path = path // to be removed by p.end
|
2013-09-18 18:59:00 -06:00
|
|
|
args := []string{"go", "build", "-tags", "OMIT"}
|
|
|
|
if opt != nil && opt.Race {
|
|
|
|
p.out <- &Message{
|
2015-10-29 02:28:03 -06:00
|
|
|
Kind: "stderr",
|
2013-09-18 18:59:00 -06:00
|
|
|
Body: "Running with race detector.\n",
|
|
|
|
}
|
|
|
|
args = append(args, "-race")
|
|
|
|
}
|
2019-10-30 02:46:03 -06:00
|
|
|
args = append(args, "-o", bin)
|
|
|
|
cmd := p.cmd(path, args...)
|
|
|
|
if !hasModfile {
|
|
|
|
cmd.Env = append(cmd.Env, "GO111MODULE=off")
|
|
|
|
}
|
2013-09-18 18:59:00 -06:00
|
|
|
cmd.Stdout = cmd.Stderr // send compiler output to stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// run x
|
2014-07-07 18:44:34 -06:00
|
|
|
if isNacl() {
|
|
|
|
cmd, err = p.naclCmd(bin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cmd = p.cmd("", bin)
|
|
|
|
}
|
2013-09-18 18:59:00 -06:00
|
|
|
if opt != nil && opt.Race {
|
|
|
|
cmd.Env = append(cmd.Env, "GOMAXPROCS=2")
|
|
|
|
}
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
// If we failed to exec, that might be because they built
|
|
|
|
// a non-main package instead of an executable.
|
|
|
|
// Check and report that.
|
|
|
|
if name, err := packageName(body); err == nil && name != "main" {
|
|
|
|
return errors.New(`executable programs must use "package main"`)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.run = cmd
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cmd builds an *exec.Cmd that writes its standard output and error to the
|
|
|
|
// process' output channel.
|
|
|
|
func (p *process) cmd(dir string, args ...string) *exec.Cmd {
|
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
|
|
|
cmd.Dir = dir
|
|
|
|
cmd.Env = Environ()
|
2015-10-29 02:28:03 -06:00
|
|
|
cmd.Stdout = &messageWriter{kind: "stdout", out: p.out}
|
|
|
|
cmd.Stderr = &messageWriter{kind: "stderr", out: p.out}
|
2013-09-18 18:59:00 -06:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2014-07-07 18:44:34 -06:00
|
|
|
func isNacl() bool {
|
|
|
|
for _, v := range append(Environ(), os.Environ()...) {
|
|
|
|
if v == "GOOS=nacl" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// naclCmd returns an *exec.Cmd that executes bin under native client.
|
|
|
|
func (p *process) naclCmd(bin string) (*exec.Cmd, error) {
|
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-07-08 20:40:37 -06:00
|
|
|
var args []string
|
2014-07-07 18:44:34 -06:00
|
|
|
env := []string{
|
|
|
|
"NACLENV_GOOS=" + runtime.GOOS,
|
|
|
|
"NACLENV_GOROOT=/go",
|
|
|
|
"NACLENV_NACLPWD=" + strings.Replace(pwd, runtime.GOROOT(), "/go", 1),
|
|
|
|
}
|
|
|
|
switch runtime.GOARCH {
|
|
|
|
case "amd64":
|
2014-07-08 20:40:37 -06:00
|
|
|
env = append(env, "NACLENV_GOARCH=amd64p32")
|
|
|
|
args = []string{"sel_ldr_x86_64"}
|
2014-07-07 18:44:34 -06:00
|
|
|
case "386":
|
|
|
|
env = append(env, "NACLENV_GOARCH=386")
|
2014-07-08 20:40:37 -06:00
|
|
|
args = []string{"sel_ldr_x86_32"}
|
|
|
|
case "arm":
|
|
|
|
env = append(env, "NACLENV_GOARCH=arm")
|
2014-07-09 22:46:43 -06:00
|
|
|
selLdr, err := exec.LookPath("sel_ldr_arm")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
args = []string{"nacl_helper_bootstrap_arm", selLdr, "--reserved_at_zero=0xXXXXXXXXXXXXXXXX"}
|
2014-07-07 18:44:34 -06:00
|
|
|
default:
|
|
|
|
return nil, errors.New("native client does not support GOARCH=" + runtime.GOARCH)
|
|
|
|
}
|
|
|
|
|
2014-07-08 20:40:37 -06:00
|
|
|
cmd := p.cmd("", append(args, "-l", "/dev/null", "-S", "-e", bin)...)
|
2014-07-07 18:44:34 -06:00
|
|
|
cmd.Env = append(cmd.Env, env...)
|
|
|
|
|
|
|
|
return cmd, nil
|
|
|
|
}
|
|
|
|
|
2013-09-18 18:59:00 -06:00
|
|
|
func packageName(body string) (string, error) {
|
|
|
|
f, err := parser.ParseFile(token.NewFileSet(), "prog.go",
|
|
|
|
strings.NewReader(body), parser.PackageClauseOnly)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return f.Name.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// messageWriter is an io.Writer that converts all writes to Message sends on
|
|
|
|
// the out channel with the specified id and kind.
|
|
|
|
type messageWriter struct {
|
2015-10-29 02:28:03 -06:00
|
|
|
kind string
|
|
|
|
out chan<- *Message
|
2013-09-18 18:59:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *messageWriter) Write(b []byte) (n int, err error) {
|
2015-10-29 02:28:03 -06:00
|
|
|
w.out <- &Message{Kind: w.kind, Body: safeString(b)}
|
2013-09-18 18:59:00 -06:00
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// safeString returns b as a valid UTF-8 string.
|
|
|
|
func safeString(b []byte) string {
|
|
|
|
if utf8.Valid(b) {
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for len(b) > 0 {
|
|
|
|
r, size := utf8.DecodeRune(b)
|
|
|
|
b = b[size:]
|
|
|
|
buf.WriteRune(r)
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|