// epollinit opens an epoll file descriptor and creates a pipe which will be // used to wake up the epoll_wait(2) function. Then, file descriptor associated // with inotify event queue and the read end of the pipe are added to epoll set. // Note that `fd` member must be set before this function is called. func (i *inotify) epollinit() (err error) { if i.epfd, err = syscall.EpollCreate1(0); err != nil { return } if err = syscall.Pipe(i.pipefd); err != nil { return } i.epes = []syscall.EpollEvent{ {Events: syscall.EPOLLIN, Fd: i.fd}, {Events: syscall.EPOLLIN, Fd: int32(i.pipefd[0])}, } if err = syscall.EpollCtl(i.epfd, syscall.EPOLL_CTL_ADD, int(i.fd), &i.epes[0]); err != nil { return } return syscall.EpollCtl(i.epfd, syscall.EPOLL_CTL_ADD, i.pipefd[0], &i.epes[1]) }
func TestSelect(t *testing.T) { var p1, p2 [2]int mustNil(syscall.Pipe(p1[:])) mustNil(syscall.Pipe(p2[:])) fs := NewFdSet(p1[0], p2[0]) var maxfd int if p1[0] > p2[0] { maxfd = p1[0] + 1 } else { maxfd = p2[0] + 1 } go func() { syscall.Write(p1[1], []byte("to p1")) syscall.Write(p2[1], []byte("to p2")) syscall.Close(p1[1]) syscall.Close(p2[1]) }() e := Select(maxfd+1, fs, nil, nil, nil) if e != nil { t.Errorf("Select(%v, %v, nil, nil, nil) => %v, want <nil>", maxfd+1, fs, e) } syscall.Close(p1[0]) syscall.Close(p2[0]) }
func (sc *selectCtx) Init() error { sc.rset.Zero() sc.wset.Zero() sc.fdmax = -1 // Create and configure the pipe-ends. b := make([]int, 2) err := syscall.Pipe(b) if err != nil { return err } sc.pfdr, sc.pfdw = b[0], b[1] syscall.CloseOnExec(sc.pfdr) syscall.CloseOnExec(sc.pfdw) err = syscall.SetNonblock(sc.pfdr, true) if err != nil { syscall.Close(sc.pfdr) syscall.Close(sc.pfdw) return err } err = syscall.SetNonblock(sc.pfdw, true) if err != nil { syscall.Close(sc.pfdr) syscall.Close(sc.pfdw) return err } // pfdr (read-end of pipe) is set (and remains set forever) on // rset. sc.rset.Set(sc.pfdr) sc.fdmax = sc.pfdr // allocate dummy selectCtx.{b,b1} buffers. sc.b = make([]byte, 1) sc.b1 = make([]byte, 128) return nil }
func NewGTKUI() *GTKUI { gtk.Init(nil) window := gtk.Window(gtk.GTK_WINDOW_TOPLEVEL) window.SetPosition(gtk.GTK_WIN_POS_CENTER) window.SetTitle("Pond") window.SetDefaultSize(1000, 800) ui := >KUI{ window: window, actions: make(chan interface{}, uiActionsQueueLen), events: make(chan interface{}, 8), } window.Connect("destroy", func(ctx *glib.CallbackContext) { close(ui.events) for { if _, ok := <-ui.actions; !ok { break } } gtk.MainQuit() }) if err := syscall.Pipe(ui.pipe[:]); err != nil { panic(err) } syscall.SetNonblock(ui.pipe[0], true) glib.FdWatchAdd(ui.pipe[0], glib.IOIn, func(conditions int) bool { ui.onAction() return true }) return ui }
func main() { fd := make([]int, 2) if err := syscall.Pipe(fd); err != nil { log.Fatal("Socketpair:", err) } defer syscall.Close(fd[0]) defer syscall.Close(fd[1]) name := fmt.Sprintf("/proc/%d/fd/%d", os.Getpid(), fd[1]) fmt.Println(name) cmd := exec.Command("ubertooth-btle", "-f", "-d", name) if err := cmd.Start(); err != nil { log.Fatal(err) } f := os.NewFile(uintptr(fd[0]), "server") buf := make([]byte, 1024) for { n, err := f.Read(buf) if err == io.EOF { break } if err != nil { log.Fatal(err) } fmt.Printf("%q\n", buf[:n]) } }
func makeTestFd(t *testing.T) testFd { var tfd testFd errno := syscall.Pipe(tfd[:]) if errno != nil { t.Fatalf("Failed to create pipe: %v", errno) } return tfd }
// Pipe returns a connected pair of Files; reads from r return bytes // written to w. It returns the files and an error, if any. func Pipe() (r *File, w *File, err error) { var p [2]int if e := syscall.Pipe(p[0:]); e != nil { return nil, nil, NewSyscallError("pipe", e) } return NewFile(uintptr(p[0]), "|0"), NewFile(uintptr(p[1]), "|1"), nil }
// Try to open a pipe with O_CLOEXEC set on both file descriptors. func forkExecPipe(p []int) error { err := syscall.Pipe(p) if err != nil { return err } _, err = fcntl(p[0], syscall.F_SETFD, syscall.FD_CLOEXEC) if err != nil { return err } _, err = fcntl(p[1], syscall.F_SETFD, syscall.FD_CLOEXEC) return err }
// Pipe returns a connected pair of Files; reads from r return bytes // written to w. It returns the files and an error, if any. func Pipe() (r *File, w *File, err error) { var p [2]int syscall.ForkLock.RLock() if e := syscall.Pipe(p[0:]); e != nil { syscall.ForkLock.RUnlock() return nil, nil, NewSyscallError("pipe", e) } syscall.ForkLock.RUnlock() return NewFile(uintptr(p[0]), "|0"), NewFile(uintptr(p[1]), "|1"), nil }
func Pipe() (r *File, w *File, err Error) { var p [2]int syscall.ForkLock.RLock() if e := syscall.Pipe(p[0:]); iserror(e) { syscall.ForkLock.RUnlock() return nil, nil, NewSyscallError("pipe", e) } syscall.ForkLock.RUnlock() return NewFile(p[0], "|0"), NewFile(p[1], "|1"), nil }
// launchQemu run qemu and wait it's quit, includes func launchQemu(ctx *VmContext) { qemu, err := exec.LookPath("qemu-system-x86_64") if err != nil { ctx.hub <- &QemuExitEvent{message: "can not find qemu executable"} return } args := ctx.QemuArguments() if glog.V(1) { glog.Info("cmdline arguments: ", strings.Join(args, " ")) } go waitConsoleOutput(ctx) pipe := make([]int, 2) err = syscall.Pipe(pipe) if err != nil { glog.Error("fail to create pipe") ctx.hub <- &QemuExitEvent{message: "fail to create pipe"} return } err = daemon(qemu, append([]string{"qemu-system-x86_64"}, args...), pipe[1]) if err != nil { //fail to daemonize glog.Error("try to start qemu failed") ctx.hub <- &QemuExitEvent{message: "try to start qemu failed"} return } buf := make([]byte, 4) nr, err := syscall.Read(pipe[0], buf) if err != nil || nr != 4 { glog.Error("try to start qemu failed") ctx.hub <- &QemuExitEvent{message: "try to start qemu failed"} return } syscall.Close(pipe[1]) syscall.Close(pipe[0]) pid := binary.BigEndian.Uint32(buf[:nr]) glog.V(1).Infof("starting daemon with pid: %d", pid) err = ctx.watchPid(int(pid)) if err != nil { glog.Error("watch qemu process failed") ctx.hub <- &QemuExitEvent{message: "watch qemu process failed"} return } }
func (s *Splicer) splice(direction string, inFD, outFD int, wg *sync.WaitGroup) { // Signal to the caller that we're done defer func() { // If a reliable delivery socket has data associated with it when a close takes place, the system continues to attempt data transfer. if err := syscall.Shutdown(inFD, SHUT_RDWR); err != nil { log.Printf("Shutdown err %v", err) } if err := syscall.Shutdown(outFD, SHUT_RDWR); err != nil { log.Printf("Shutdown err %v", err) } if wg != nil { wg.Done() } }() pipe := make([]int, 2) if err := syscall.Pipe(pipe); err != nil { log.Fatal(err) } defer func() { syscall.Close(pipe[0]) syscall.Close(pipe[1]) }() var netWrittenBytes, netReadBytes int64 log.Printf("[%v] Splicing pipe %+v, tcpfds %+v", direction, s.pipe, s.tcpFD) for { // SPLICE_F_NONBLOCK: don't block if TCP buffer is empty // SPLICE_F_MOVE: directly move pages into splice buffer in kernel memory // SPLICE_F_MORE: just makes mysql connections slow log.Printf("[input (%v)] Entering input", direction) inBytes, err := syscall.Splice(inFD, nil, pipe[1], nil, s.bufferSize, SPLICE_F_NONBLOCK) if err := s.checkSpliceErr(inBytes, err, fmt.Sprintf("input (%v)", direction)); err != nil { log.Printf("ERROR [input (%v)] error: %v", direction, err) return } netReadBytes += inBytes log.Printf("[input (%v)] %d bytes read", direction, inBytes) log.Printf("[input (%v)] Entering output", direction) outBytes, err := syscall.Splice(pipe[0], nil, outFD, nil, s.bufferSize, SPLICE_F_NONBLOCK) if err := s.checkSpliceErr(inBytes, err, fmt.Sprintf("output (%v)", direction)); err != nil { log.Printf("ERROR [output (%v)] error: %v", direction, err) return } log.Printf("[output (%v)] %d bytes written, out of given input %d", direction, outBytes, inBytes) netWrittenBytes += outBytes } log.Printf("[%v] Spliced %d bytes read %d bytes written", direction, netWrittenBytes, netReadBytes) }
// init initializes kqueue. func (k *kqueue) init() (err error) { if k.fd, err = syscall.Kqueue(); err != nil { return } // Creates pipe used to stop `Kevent` call by registering it, // watching read end and writing to other end of it. if err = syscall.Pipe(k.pipefds[:]); err != nil { return } var kevn [1]syscall.Kevent_t syscall.SetKevent(&kevn[0], k.pipefds[0], syscall.EVFILT_READ, syscall.EV_ADD) _, err = syscall.Kevent(k.fd, kevn[:], nil, nil) return }
// launchQemu run qemu and wait it's quit, includes func launchQemu(qc *QemuContext, ctx *hypervisor.VmContext) { qemu := qc.driver.executable if qemu == "" { ctx.Hub <- &hypervisor.VmStartFailEvent{Message: "can not find qemu executable"} return } args := qc.arguments(ctx) if glog.V(1) { glog.Info("cmdline arguments: ", strings.Join(args, " ")) } pipe := make([]int, 2) err := syscall.Pipe(pipe) if err != nil { glog.Error("fail to create pipe") ctx.Hub <- &hypervisor.VmStartFailEvent{Message: "fail to create pipe"} return } err = daemon(qemu, append([]string{"qemu-system-x86_64"}, args...), pipe[1]) if err != nil { //fail to daemonize glog.Error("try to start qemu failed") ctx.Hub <- &hypervisor.VmStartFailEvent{Message: "try to start qemu failed"} return } buf := make([]byte, 4) nr, err := syscall.Read(pipe[0], buf) if err != nil || nr != 4 { glog.Error("try to start qemu failed") ctx.Hub <- &hypervisor.VmStartFailEvent{Message: "try to start qemu failed"} return } syscall.Close(pipe[1]) syscall.Close(pipe[0]) pid := binary.BigEndian.Uint32(buf[:nr]) glog.V(1).Infof("starting daemon with pid: %d", pid) err = ctx.DCtx.(*QemuContext).watchPid(int(pid), ctx.Hub) if err != nil { glog.Error("watch qemu process failed") ctx.Hub <- &hypervisor.VmStartFailEvent{Message: "watch qemu process failed"} return } }
func ext۰os۰Pipe(fr *frame, args []value) value { // func os.Pipe() (r *File, w *File, err error) // The portable POSIX pipe(2) call is good enough for our needs. var p [2]int if err := syscall.Pipe(p[:]); err != nil { // TODO(adonovan): fix: return an *os.SyscallError. return tuple{nil, nil, wrapError(err)} } NewFile := fr.i.prog.ImportedPackage("os").Func("NewFile") r := call(fr.i, fr, 0, NewFile, []value{uintptr(p[0]), "|0"}) w := call(fr.i, fr, 0, NewFile, []value{uintptr(p[1]), "|1"}) return tuple{r, w, wrapError(nil)} }
func (k *PosixKernel) Pipe(files co.Buf) uint64 { var fds [2]int err := syscall.Pipe(fds[:]) if err == nil { st := files.Struc() err := st.Pack(int32(fds[0])) if err == nil { err = st.Pack(int32(fds[1])) } if err != nil { return UINT64_MAX // FIXME } } return Errno(err) }
// Pipe returns a connected pair of Files; reads from r return bytes written to w. // It returns the files and an error, if any. func Pipe() (r *File, w *File, err error) { var p [2]syscall.Handle // See ../syscall/exec.go for description of lock. syscall.ForkLock.RLock() e := syscall.Pipe(p[0:]) if e != nil { syscall.ForkLock.RUnlock() return nil, nil, NewSyscallError("pipe", e) } syscall.CloseOnExec(p[0]) syscall.CloseOnExec(p[1]) syscall.ForkLock.RUnlock() return NewFile(uintptr(p[0]), "|0"), NewFile(uintptr(p[1]), "|1"), nil }
// Pipe returns a connected pair of Files; reads from r return bytes written to w. // It returns the files and an Error, if any. func Pipe() (r *File, w *File, err Error) { var p [2]int // See ../syscall/exec.go for description of lock. syscall.ForkLock.RLock() e := syscall.Pipe(&p) if e != 0 { syscall.ForkLock.RUnlock() return nil, nil, NewSyscallError("pipe", e) } syscall.CloseOnExec(p[0]) syscall.CloseOnExec(p[1]) syscall.ForkLock.RUnlock() return NewFile(p[0], "|0"), NewFile(p[1], "|1"), nil }
func TestGetSetNonblock(t *testing.T) { var p [2]int mustNil(syscall.Pipe(p[:])) for _, b := range []bool{true, false} { if e := SetNonblock(p[0], b); e != nil { t.Errorf("SetNonblock(%v, %v) => %v, want <nil>", p[0], b, e) } if nb, e := GetNonblock(p[0]); nb != b || e != nil { t.Errorf("GetNonblock(%v) => (%v, %v), want (%v, <nil>)", p[0], nb, e, b) } } syscall.Close(p[0]) syscall.Close(p[1]) if e := SetNonblock(p[0], true); e == nil { t.Errorf("SetNonblock(%v, true) => <nil>, want non-<nil>", p[0]) } }
func NewSrvConn(srvname string) (net.Conn, error) { var pip [2]int err := syscall.Pipe(pip[:]) if err != nil { return nil, err } srvfile, err := PostFD(srvname, pip[1]) if err != nil { syscall.Close(pip[0]) syscall.Close(pip[1]) return nil, err } syscall.Close(pip[1]) f := os.NewFile(uintptr(pip[0]), "|0") return &SrvConn{Name: srvname, SrvFile: srvfile, file: f}, nil }
func main() { log.Printf("Input file %v", *inFile) in, err := os.Open(*inFile) if err != nil { log.Fatal(err) } defer in.Close() inFD := int(in.Fd()) log.Printf("Output file %v", *outFile) out, err := os.Create(*outFile) if err != nil { log.Fatal(err) } defer out.Close() outFD := int(out.Fd()) pipe := make([]int, 2) if err := syscall.Pipe(pipe); err != nil { log.Fatal(err) } defer syscall.Close(pipe[0]) defer syscall.Close(pipe[1]) flags := SPLICE_F_NONBLOCK blockSize := 1 inFileSize := fileSize(in) var netRead int64 for { inBytes, err := syscall.Splice(inFD, nil, pipe[1], nil, blockSize, flags) checkSpliceErr(inBytes, err, "input") netRead += inBytes log.Printf("[input] %d bytes read, %d remaining", inBytes, inFileSize-netRead) if err := syscall.SetNonblock(inFD, true); err != nil { log.Fatalf("Unable to close in fd") } outBytes, err := syscall.Splice(pipe[0], nil, outFD, nil, blockSize, flags) checkSpliceErr(outBytes, err, "output") log.Printf("[output] %d bytes written, out of given input %d", outBytes, inBytes) } }
// Pipe returns a connected pair of Files; reads from // r return bytes written to w. It returns the files and an error, if any. // Optionally, r or w might be set to non-blocking mode using the appropriate // flags. To obtain a blocking pipe just pass 0 as the flag. func Pipe(flag PipeFlag) (r *os.File, w *os.File, err error) { var p [2]int syscall.ForkLock.RLock() if err := syscall.Pipe(p[:]); err != nil { syscall.ForkLock.RUnlock() return nil, nil, os.NewSyscallError("pipe", err) } syscall.CloseOnExec(p[0]) syscall.CloseOnExec(p[1]) if flag&ReadNonBlock != 0 { syscall.SetNonblock(p[0], true) } if flag&WriteNonBlock != 0 { syscall.SetNonblock(p[1], true) } syscall.ForkLock.RUnlock() return os.NewFile(uintptr(p[0]), "|0"), os.NewFile(uintptr(p[1]), "|1"), nil }
func init() { var p [2]int if e := syscall.Pipe(p[0:]); e != nil { fmt.Fprintf(os.Stderr, "pipe: %v\n", e) os.Exit(2) } if e := dup2(p[0], fd); e != nil { fmt.Fprintf(os.Stderr, "dup2: %v\n", e) os.Exit(2) } const str = "PASS" if n, e := syscall.Write(p[1], []byte(str)); e != nil || n != len(str) { fmt.Fprintf(os.Stderr, "write: %d %v\n", n, e) os.Exit(2) } if e := syscall.Close(p[1]); e != nil { fmt.Fprintf(os.Stderr, "close: %v\n", e) os.Exit(2) } }
// Pipe returns a connected pair of Files; reads from r return bytes written to w. // It returns the files and an error, if any. func Pipe() (r *File, w *File, err error) { var p [2]int e := syscall.Pipe2(p[0:], syscall.O_CLOEXEC) // pipe2 was added in 2.6.27 and our minimum requirement is 2.6.23, so it // might not be implemented. if e == syscall.ENOSYS { // See ../syscall/exec.go for description of lock. syscall.ForkLock.RLock() e = syscall.Pipe(p[0:]) if e != nil { syscall.ForkLock.RUnlock() return nil, nil, NewSyscallError("pipe", e) } syscall.CloseOnExec(p[0]) syscall.CloseOnExec(p[1]) syscall.ForkLock.RUnlock() } else if e != nil { return nil, nil, NewSyscallError("pipe2", e) } return NewFile(uintptr(p[0]), "|0"), NewFile(uintptr(p[1]), "|1"), nil }
func (port InputPort) Connect(source Source) (portConnection, error) { fd := make([]int, 2) syscall.Pipe(fd) readFd := fd[0] writeFd := C.int(fd[1]) port.writeFds = append(port.writeFds, &writeFd) C.MIDIPortConnectSource(port.port, source.endpoint, unsafe.Pointer(&writeFd)) go func() { dataForLength := make([]byte, 1) for { n, err := syscall.Read(readFd, dataForLength) if err != nil || n != 1 { break } length := dataForLength[0] data := make([]byte, length) n, err = syscall.Read(readFd, data) if err != nil || n != int(length) { break } port.readProc(source, data) } syscall.Close(readFd) }() return portConnection{port, source, &writeFd}, nil }
func runFunction(m *llgo.Module, name string) (output []string, err error) { addExterns(m) err = addRuntime(m) if err != nil { return } err = llvm.VerifyModule(m.Module, llvm.ReturnStatusAction) if err != nil { return } engine, err := llvm.NewJITCompiler(m.Module, 0) if err != nil { return } defer engine.Dispose() fn := engine.FindFunction(name) if fn.IsNil() { err = fmt.Errorf("Couldn't find function '%s'", name) return } // Redirect stdout to a pipe. pipe_fds := make([]int, 2) err = syscall.Pipe(pipe_fds) if err != nil { return } defer syscall.Close(pipe_fds[0]) defer syscall.Close(pipe_fds[1]) old_stdout, err := syscall.Dup(syscall.Stdout) if err != nil { return } defer syscall.Close(old_stdout) err = syscall.Dup2(pipe_fds[1], syscall.Stdout) if err != nil { return } defer syscall.Dup2(old_stdout, syscall.Stdout) c := make(chan string) go readPipe(pipe_fds[0], c) exec_args := []llvm.GenericValue{} engine.RunStaticConstructors() engine.RunFunction(fn, exec_args) defer engine.RunStaticDestructors() // Call fflush to flush stdio (printf), then sync and close the write // end of the pipe. fflush := engine.FindFunction("fflush") ptr0 := unsafe.Pointer(uintptr(0)) exec_args = []llvm.GenericValue{llvm.NewGenericValueFromPointer(ptr0)} engine.RunFunction(fflush, exec_args) syscall.Fsync(pipe_fds[1]) syscall.Close(pipe_fds[1]) syscall.Close(syscall.Stdout) output_str := <-c output = strings.Split(strings.TrimSpace(output_str), "\n") return }
func runMainFunction(m *llgo.Module) (output []string, err error) { addExterns(m) err = addRuntime(m) if err != nil { return } err = llvm.VerifyModule(m.Module, llvm.ReturnStatusAction) if err != nil { err = fmt.Errorf("Verification failed: %v", err) return } engine, err := llvm.NewExecutionEngine(m.Module) if err != nil { return } defer engine.Dispose() fn := engine.FindFunction("main") if fn.IsNil() { err = fmt.Errorf("Couldn't find function 'main'") return } // Redirect stdout to a pipe. pipe_fds := make([]int, 2) err = syscall.Pipe(pipe_fds) if err != nil { return } defer syscall.Close(pipe_fds[0]) defer syscall.Close(pipe_fds[1]) old_stdout, err := syscall.Dup(syscall.Stdout) if err != nil { return } defer syscall.Close(old_stdout) err = syscall.Dup2(pipe_fds[1], syscall.Stdout) if err != nil { return } defer syscall.Dup2(old_stdout, syscall.Stdout) c := make(chan string) go readPipe(pipe_fds[0], c) // FIXME implement and use RunFunctionAsMain argv0 := []byte("llgo-test\000") var envs [1]*byte argv0ptr := &argv0 exec_args := []llvm.GenericValue{ llvm.NewGenericValueFromInt(llvm.Int32Type(), 1, true), llvm.NewGenericValueFromPointer(unsafe.Pointer(&argv0ptr)), llvm.NewGenericValueFromPointer(unsafe.Pointer(&envs)), } engine.RunStaticConstructors() engine.RunFunction(fn, exec_args) defer engine.RunStaticDestructors() // Call fflush to flush stdio (printf), then sync and close the write // end of the pipe. fflush := engine.FindFunction("fflush") ptr0 := unsafe.Pointer(uintptr(0)) exec_args = []llvm.GenericValue{llvm.NewGenericValueFromPointer(ptr0)} engine.RunFunction(fflush, exec_args) syscall.Fsync(pipe_fds[1]) syscall.Close(pipe_fds[1]) syscall.Close(syscall.Stdout) output_str := <-c output = strings.Split(strings.TrimSpace(output_str), "\n") return }