本文整理汇总了Golang中os.Pipe函数的典型用法代码示例。如果您正苦于以下问题:Golang Pipe函数的具体用法?Golang Pipe怎么用?Golang Pipe使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Pipe函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: compile
func compile(w *World) *bytes.Buffer {
ioutil.WriteFile(TEMPPATH+".go", []byte(w.source()), 0644)
err := new(bytes.Buffer)
re, e, _ := os.Pipe()
attr := &os.ProcAttr{Env: os.Environ(), Files: []*os.File{nil, e, nil}}
args := []string{bin + "/" + arch + "g", "-o", TEMPPATH + ".6", TEMPPATH + ".go"}
os.StartProcess(bin+"/"+arch+"g", args, attr)
e.Close()
io.Copy(err, re)
if err.Len() > 0 {
return err
}
re, e, _ = os.Pipe()
attr = &os.ProcAttr{Env: os.Environ(), Files: []*os.File{nil, e, nil}}
args = []string{bin + "/" + arch + "l", "-o", TEMPPATH + "", TEMPPATH + ".6"}
os.StartProcess(bin+"/"+arch+"l", args, attr)
e.Close()
io.Copy(err, re)
return err
}
示例2: captureOutput
func captureOutput(f func(), t *testing.T) (string, string) {
// Capture STDOUT with a pipe
stdout := os.Stdout
stderr := os.Stderr
so, op, _ := os.Pipe() //outpipe
oe, ep, _ := os.Pipe() //errpipe
defer func(stdout, stderr *os.File) {
os.Stdout = stdout
os.Stderr = stderr
}(stdout, stderr)
os.Stdout = op
os.Stderr = ep
f()
os.Stdout = stdout
os.Stderr = stderr
op.Close()
ep.Close()
errOutput, err := ioutil.ReadAll(oe)
if err != nil {
t.Error("Could not get output from stderr")
}
stdOutput, err := ioutil.ReadAll(so)
if err != nil {
t.Error("Could not get output from stdout")
}
return string(stdOutput), string(errOutput)
}
示例3: Dial
func (s *server) Dial(config *ssh.ClientConfig) *ssh.ClientConn {
s.cmd = exec.Command("sshd", "-f", s.configfile, "-i")
r1, w1, err := os.Pipe()
if err != nil {
s.t.Fatal(err)
}
s.cmd.Stdout = w1
r2, w2, err := os.Pipe()
if err != nil {
s.t.Fatal(err)
}
s.cmd.Stdin = r2
s.cmd.Stderr = os.Stderr
if err := s.cmd.Start(); err != nil {
s.t.Fail()
s.Shutdown()
s.t.Fatalf("s.cmd.Start: %v", err)
}
conn, err := ssh.Client(&client{wc: w2, r: r1}, config)
if err != nil {
s.t.Fail()
s.Shutdown()
s.t.Fatalf("ssh.Client: %v", err)
}
return conn
}
示例4: startChild
func startChild(filename string) (*childInfo, error) {
cmdPath, err := exec.LookPath(cmdName)
if err != nil {
return nil, err
}
pr1, pw1, err := os.Pipe()
if err != nil {
return nil, err
}
pr2, pw2, err := os.Pipe()
if err != nil {
return nil, err
}
pr3, pw3, err := os.Pipe()
if err != nil {
return nil, err
}
args := []string{cmdPath, filename}
fds := []*os.File{pr1, pw2, pw3}
p, err := os.StartProcess(cmdPath, args, &os.ProcAttr{Dir: "/", Files: fds})
if err != nil {
return nil, err
}
return &childInfo{
r: bufio.NewReader(pr2),
w: pw1,
proc: p,
er: bufio.NewReader(pr3),
}, nil
}
示例5: TestPassExtraFiles
func TestPassExtraFiles(t *testing.T) {
if testing.Short() {
return
}
rootfs, err := newRootfs()
if err != nil {
t.Fatal(err)
}
defer remove(rootfs)
config := newTemplateConfig(rootfs)
container, err := factory.Create("test", config)
if err != nil {
t.Fatal(err)
}
defer container.Destroy()
var stdout bytes.Buffer
pipeout1, pipein1, err := os.Pipe()
pipeout2, pipein2, err := os.Pipe()
process := libcontainer.Process{
Args: []string{"sh", "-c", "cd /proc/$$/fd; echo -n *; echo -n 1 >3; echo -n 2 >4"},
Env: []string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"},
ExtraFiles: []*os.File{pipein1, pipein2},
Stdin: nil,
Stdout: &stdout,
}
err = container.Start(&process)
if err != nil {
t.Fatal(err)
}
waitProcess(&process, t)
out := string(stdout.Bytes())
// fd 5 is the directory handle for /proc/$$/fd
if out != "0 1 2 3 4 5" {
t.Fatalf("expected to have the file descriptors '0 1 2 3 4 5' passed to init, got '%s'", out)
}
var buf = []byte{0}
_, err = pipeout1.Read(buf)
if err != nil {
t.Fatal(err)
}
out1 := string(buf)
if out1 != "1" {
t.Fatalf("expected first pipe to receive '1', got '%s'", out1)
}
_, err = pipeout2.Read(buf)
if err != nil {
t.Fatal(err)
}
out2 := string(buf)
if out2 != "2" {
t.Fatalf("expected second pipe to receive '2', got '%s'", out2)
}
}
示例6: startProcess
func (ss *SubService) startProcess() (proc *os.Process, err error) {
cmd := exec.Command(ss.binPath, ss.argv...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
dRead, ssWrite, err := os.Pipe()
ssRead, dWrite, err := os.Pipe()
// Store Fd's for us to read/write to SubService
ss.pipe = daemon.NewPipe(dRead, dWrite)
// Pass SubService their side of the connection
cmd.ExtraFiles = append(cmd.ExtraFiles, ssRead, ssWrite)
err = cmd.Start()
proc = cmd.Process
startupTimer := time.NewTimer(RerunWait)
if proc != nil {
go ss.watchSignals()
go ss.watchProcess(proc, startupTimer)
}
return
}
示例7: CaptureStdOutAndErr
// CaptureStdOutAndErr captures all std out/err until the returned func is
// called. That func returns any captured output as a string. This is useful
// for capturing remote output (e.g. on a server) and writing it to a file or
// piping it to a log etc.
func CaptureStdOutAndErr() func() string {
old := os.Stdout // keep backup of the real stdout
oldErr := os.Stderr
r, w, _ := os.Pipe()
os.Stdout = w
rErr, wErr, _ := os.Pipe()
os.Stderr = wErr
outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
outErrC := make(chan string)
go func() {
var buf bytes.Buffer
io.Copy(&buf, rErr)
outErrC <- buf.String()
}()
return func() string {
w.Close()
wErr.Close()
os.Stdout = old // restoring the real stdout
os.Stderr = oldErr // restoring the real stdout
return <-outC + <-outErrC
}
}
示例8: startProc
func startProc(path, args interface{}) interface{} {
p, ok := path.(string)
if !ok {
TypeError("string", path)
}
argv := make([]string, ListLen(args))
for cur, i := args, 0; cur != EMPTY_LIST; cur, i = Cdr(cur), i+1 {
x := Car(cur)
s, ok := x.(string)
if !ok {
TypeError("string", x)
}
argv[i] = s
}
inr, inw, err := os.Pipe()
if err != nil {
SystemError(err)
}
outr, outw, err := os.Pipe()
if err != nil {
SystemError(err)
}
_, err = os.ForkExec(p, argv, os.Envs, "", []*os.File{inr, outw, os.Stderr})
if err != nil {
SystemError(err)
}
return Cons(NewOutput(inw), NewInput(outr))
}
示例9: TryDial
func (s *server) TryDial(config *ssh.ClientConfig) (*ssh.ClientConn, error) {
sshd, err := exec.LookPath("sshd")
if err != nil {
s.t.Skipf("skipping test: %v", err)
}
s.cmd = exec.Command(sshd, "-f", s.configfile, "-i", "-e")
r1, w1, err := os.Pipe()
if err != nil {
s.t.Fatal(err)
}
s.cmd.Stdout = w1
r2, w2, err := os.Pipe()
if err != nil {
s.t.Fatal(err)
}
s.cmd.Stdin = r2
s.cmd.Stderr = os.Stderr
if err := s.cmd.Start(); err != nil {
s.t.Fail()
s.Shutdown()
s.t.Fatalf("s.cmd.Start: %v", err)
}
return ssh.Client(&client{wc: w2, r: r1}, config)
}
示例10: TestTurnOn
func TestTurnOn(t *testing.T) {
// use pipes to capture stdout and stderr
oldStdout := os.Stdout
oldStderr := os.Stderr
r, w, _ := os.Pipe()
re, we, _ := os.Pipe()
os.Stdout = w
os.Stderr = we
turnOnLogging(LevelWarning, nil)
Info("info %d", 123)
Warning("warning %d", 123)
Error(nil, "error %d", 123)
we.Close()
w.Close()
out, _ := ioutil.ReadAll(r)
errOut, _ := ioutil.ReadAll(re)
os.Stdout = oldStdout
os.Stderr = oldStderr
outStr := string(out[:])
errStr := string(errOut[:])
assert.True(t, strings.Contains(outStr, "warning 123"), "Should have warning printed")
assert.True(t, strings.Contains(errStr, "error 123"), "Should have error")
assert.False(t, strings.Contains(outStr, "info"), "Should not have info printed")
}
示例11: Keyspace
func Keyspace(servers string) (*KeyspaceProxy, os.Error) {
serverList := strings.Split(servers, " ", -1)
argv := make([]string, len(serverList)+1)
argv[0] = scriptPath
for idx, server := range serverList {
argv[idx+1] = server
}
stdinRead, stdinWrite, err := os.Pipe()
if err != nil {
return nil, err
}
stdoutRead, stdoutWrite, err := os.Pipe()
if err != nil {
return nil, err
}
stderrRead, stderrWrite, err := os.Pipe()
if err != nil {
return nil, err
}
fd := []*os.File{stdinRead, stdoutWrite, stderrWrite}
pid, err := os.ForkExec(scriptPath, argv, os.Environ(), "", fd)
if err != nil {
return nil, err
}
return &KeyspaceProxy{
Servers: servers,
pid: pid,
stdin: stdinWrite,
stdout: stdoutRead,
stderr: stderrRead,
lock: &sync.Mutex{},
},
nil
}
示例12: TestSendComplex
// Tests both incremental and verifies actual file changes
func (s *internal) TestSendComplex() {
name := "/" + s.pool + "/a/2/markerfile"
NoError := s.Require().NoError
NoError(touch(name))
NoError(snap(s.pool + "/a/[email protected]"))
NoError(command("sudo", "rm", name).Run())
NoError(snap(s.pool + "/a/[email protected]"))
reader, writer, err := os.Pipe()
NoError(err)
NoError(send(s.pool+"/a/[email protected]", writer.Fd(), "", true, true))
writer.Close() // must be before receiver, otherwise can block
s.NoError(receive(reader, s.pool+"/c"))
reader.Close()
name = strings.Replace(name, "/a/", "/c/", 1)
_, err = os.Stat(name)
s.NoError(err)
reader, writer, err = os.Pipe()
NoError(err)
s.NoError(send(s.pool+"/a/[email protected]", writer.Fd(), s.pool+"/a/[email protected]", false, false))
writer.Close() // must be before receiver, otherwise can block
s.NoError(receive(reader, s.pool+"/c"))
reader.Close()
_, err = os.Stat(name)
s.IsType(&os.PathError{}, err)
}
示例13: createStdioPipes
// setup standard pipes so that the TTY of the calling runc process
// is not inherited by the container.
func createStdioPipes(p *libcontainer.Process, rootuid int) (*tty, error) {
var (
t = &tty{}
fds []int
)
r, w, err := os.Pipe()
if err != nil {
return nil, err
}
fds = append(fds, int(r.Fd()), int(w.Fd()))
go io.Copy(w, os.Stdin)
t.closers = append(t.closers, w)
p.Stdin = r
if r, w, err = os.Pipe(); err != nil {
return nil, err
}
fds = append(fds, int(r.Fd()), int(w.Fd()))
go io.Copy(os.Stdout, r)
p.Stdout = w
t.closers = append(t.closers, r)
if r, w, err = os.Pipe(); err != nil {
return nil, err
}
fds = append(fds, int(r.Fd()), int(w.Fd()))
go io.Copy(os.Stderr, r)
p.Stderr = w
t.closers = append(t.closers, r)
// change the ownership of the pipe fds incase we are in a user namespace.
for _, fd := range fds {
if err := syscall.Fchown(fd, rootuid, rootuid); err != nil {
return nil, err
}
}
return t, nil
}
示例14: TestSendSimple
func (s *internal) TestSendSimple() {
s.EqualError(send("should-not-exist", 0, "", true, true), enoent)
s.EqualError(send(s.pool+"/a/[email protected]", 42, "", true, true), ebadf)
s.EqualError(send(s.pool+"/"+longName, 42, "", true, true), einval) // WANTE(ENAMETOOLONG)
// expect epipe
reader, writer, err := os.Pipe()
s.Require().NoError(err)
reader.Close()
s.EqualError(send(s.pool+"/a/[email protected]", writer.Fd(), "", true, true), epipe)
writer.Close()
// expect ebadf
reader, writer, err = os.Pipe()
s.Require().NoError(err)
writer.Close()
s.EqualError(send(s.pool+"/a/[email protected]", writer.Fd(), "", true, true), ebadf)
reader.Close()
// ok
reader, writer, err = os.Pipe()
s.Require().NoError(err)
s.NoError(send(s.pool+"/a/4", writer.Fd(), "", true, true))
s.NoError(receive(reader, s.pool+"/c"))
reader.Close()
writer.Close()
}
示例15: testClientGoSvr
func testClientGoSvr(t testing.TB, readonly bool, delay time.Duration) (*Client, *exec.Cmd) {
txPipeRd, txPipeWr, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
rxPipeRd, rxPipeWr, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
server, err := NewServer(txPipeRd, rxPipeWr, os.Stderr, 0, readonly, ".")
if err != nil {
t.Fatal(err)
}
go server.Serve()
var ctx io.WriteCloser = txPipeWr
if delay > NO_DELAY {
ctx = newDelayedWriter(ctx, delay)
}
client, err := NewClientPipe(rxPipeRd, ctx)
if err != nil {
t.Fatal(err)
}
// dummy command...
return client, exec.Command("true")
}