aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/ipc/ipc_simple.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2017-09-24 10:05:21 +0200
committerDmitry Vyukov <dvyukov@google.com>2017-09-25 15:19:06 +0200
commit255e8b5e54e93fc77302a546dbb7a932412d1bde (patch)
tree2df4f2894995bb15f6897ab5a9b5333b5ce0206d /pkg/ipc/ipc_simple.go
parentdcf893f99cdcead3d82e987953341caa030b30f9 (diff)
pkg/ipc: windows port
Diffstat (limited to 'pkg/ipc/ipc_simple.go')
-rw-r--r--pkg/ipc/ipc_simple.go108
1 files changed, 108 insertions, 0 deletions
diff --git a/pkg/ipc/ipc_simple.go b/pkg/ipc/ipc_simple.go
new file mode 100644
index 000000000..9e5c52b8c
--- /dev/null
+++ b/pkg/ipc/ipc_simple.go
@@ -0,0 +1,108 @@
+// Copyright 2017 syzkaller project authors. All rights reserved.
+// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
+
+// +build fuchsia windows
+
+package ipc
+
+import (
+ "bytes"
+ "encoding/binary"
+ "fmt"
+ "io/ioutil"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "strings"
+ "time"
+
+ "github.com/google/syzkaller/pkg/osutil"
+ "github.com/google/syzkaller/prog"
+)
+
+type Env struct {
+ bin []string
+ pid int
+ config Config
+
+ StatExecs uint64
+ StatRestarts uint64
+}
+
+func MakeEnv(bin string, pid int, config Config) (*Env, error) {
+ if config.Timeout < 7*time.Second {
+ config.Timeout = 7 * time.Second
+ }
+ env := &Env{
+ bin: strings.Split(bin, " "),
+ pid: pid,
+ config: config,
+ }
+ if len(env.bin) == 0 {
+ return nil, fmt.Errorf("binary is empty string")
+ }
+ if false {
+ env.bin[0] = osutil.Abs(env.bin[0])
+ base := filepath.Base(env.bin[0])
+ pidStr := fmt.Sprint(pid)
+ if len(base)+len(pidStr) >= 16 {
+ // TASK_COMM_LEN is currently set to 16
+ base = base[:15-len(pidStr)]
+ }
+ binCopy := filepath.Join(filepath.Dir(env.bin[0]), base+pidStr)
+ if err := os.Link(env.bin[0], binCopy); err == nil {
+ env.bin[0] = binCopy
+ }
+ }
+ return env, nil
+}
+
+func (env *Env) Close() error {
+ return nil
+}
+
+func (env *Env) Exec(opts *ExecOpts, p *prog.Prog) (output []byte, info []CallInfo, failed, hanged bool, err0 error) {
+ dir, err := ioutil.TempDir("./", "syzkaller-testdir")
+ if err != nil {
+ err0 = fmt.Errorf("failed to create temp dir: %v", err)
+ return
+ }
+ defer os.RemoveAll(dir)
+
+ data := make([]byte, prog.ExecBufferSize)
+ if err := p.SerializeForExec(data, env.pid); err != nil {
+ err0 = err
+ return
+ }
+ inbuf := new(bytes.Buffer)
+ binary.Write(inbuf, binary.LittleEndian, uint64(env.config.Flags))
+ binary.Write(inbuf, binary.LittleEndian, uint64(opts.Flags))
+ binary.Write(inbuf, binary.LittleEndian, uint64(env.pid))
+ inbuf.Write(data)
+
+ cmd := exec.Command(env.bin[0], env.bin[1:]...)
+ cmd.Env = []string{}
+ cmd.Dir = dir
+ cmd.Stdin = inbuf
+ if env.config.Flags&FlagDebug != 0 {
+ cmd.Stdout = os.Stdout
+ cmd.Stderr = os.Stdout
+ }
+ if err := cmd.Start(); err != nil {
+ err0 = err
+ return
+ }
+ done := make(chan error)
+ go func() {
+ done <- cmd.Wait()
+ }()
+ t := time.NewTimer(env.config.Timeout)
+ select {
+ case <-done:
+ t.Stop()
+ case <-t.C:
+ cmd.Process.Kill()
+ <-done
+ }
+ return
+}