本文整理匯總了Golang中syscall.Dup函數的典型用法代碼示例。如果您正苦於以下問題:Golang Dup函數的具體用法?Golang Dup怎麽用?Golang Dup使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Dup函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: main
func main() {
// I assume that in is 0
in2, err := syscall.Dup(0)
if err != nil {
log.Fatal("dupping 0", err)
}
out2, err := syscall.Dup(1)
if err != nil {
log.Fatal("dupping 1", err)
}
os.Stdin.Close()
os.Stdout.Close()
os.Stdout = os.NewFile(uintptr(2), "/dev/stdout")
/* Connections to the application. */
cin := os.NewFile(uintptr(in2), "fromapp")
cout := os.NewFile(uintptr(out2), "toapp")
modifyEnvironment()
// Fire up a new devdraw here
devdraw, err := drawfcall.New()
if err != nil {
log.Fatal("making a Conn", err)
}
// There is probably a nicer way to do this.
// TODO(rjkroege): do it the nicer way.
var app App
app.o = cout
app.i = cin
json := NewJsonRecorder()
for {
// read crap from cin
log.Print("about to read from host")
inbuffy, err := drawfcall.ReadMsg(cin)
log.Print("read from host")
if err != nil {
devdraw.Close()
break
}
go marshalsxtx(inbuffy, &app, devdraw, json)
}
log.Print("waiting on completion")
json.WaitToComplete()
}
示例2: Dup
func (k *PosixKernel) Dup(oldFd co.Fd) uint64 {
if newFd, err := syscall.Dup(int(oldFd)); err != nil {
return Errno(err)
} else {
return uint64(newFd)
}
}
示例3: Dup
func (t *FlushErrorTest) Dup() {
var err error
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err)
fd1 := t.f1.Fd()
// Use dup(2) to get another copy.
fd2, err := syscall.Dup(int(fd1))
AssertEq(nil, err)
t.f2 = os.NewFile(uintptr(fd2), t.f1.Name())
// Close by the first handle. On OS X, where the semantics of file handles
// are different (cf. https://github.com/osxfuse/osxfuse/issues/199), this
// does not result in an error.
err = t.f1.Close()
t.f1 = nil
if isDarwin {
AssertEq(nil, err)
} else {
ExpectThat(err, Error(HasSubstr("no such file")))
}
// Close by the second handle.
err = t.f2.Close()
t.f2 = nil
ExpectThat(err, Error(HasSubstr("no such file")))
}
示例4: file
func (fd *netFD) file(f *os.File, s string) (*os.File, error) {
dfd, err := syscall.Dup(int(f.Fd()), -1)
if err != nil {
return nil, os.NewSyscallError("dup", err)
}
return os.NewFile(uintptr(dfd), s), nil
}
示例5: Lock
func (sf *StateFile) Lock(create bool) (*Lock, error) {
sf.lockFdMutex.Lock()
defer sf.lockFdMutex.Unlock()
if sf.lockFd != nil {
return &Lock{sf.lockFd}, nil
}
flags := os.O_RDWR
if create {
flags |= os.O_CREATE | os.O_EXCL
}
file, err := os.OpenFile(sf.Path, flags, 0600)
if err != nil {
return nil, err
}
defer file.Close()
fd := int(file.Fd())
newFd, err := syscall.Dup(fd)
if err != nil {
return nil, err
}
if syscall.Flock(newFd, syscall.LOCK_EX|syscall.LOCK_NB) != nil {
syscall.Close(newFd)
return nil, nil
}
sf.lockFd = &newFd
return &Lock{sf.lockFd}, nil
}
示例6: CaptureStream
func CaptureStream(file *os.File) (*os.File, func(), error) {
pipeR, pipeW, err := os.Pipe()
if err != nil {
return nil, nil, err
}
bkupFD, err := syscall.Dup(int(file.Fd()))
if err != nil {
pipeW.Close()
pipeR.Close()
return nil, nil, err
}
err = syscall.Dup2(int(pipeW.Fd()), int(file.Fd()))
if err != nil {
syscall.Close(bkupFD)
pipeW.Close()
pipeR.Close()
}
cleanFunc := func() {
syscall.Dup2(bkupFD, int(file.Fd()))
syscall.Close(bkupFD)
pipeW.Close()
pipeR.Close()
}
return pipeR, cleanFunc, nil
}
示例7: mount
func mount(mountPoint string, opts *MountOptions, ready chan<- error) (fd int, err error) {
f, err := openFUSEDevice()
if err != nil {
return 0, err
}
cmd := exec.Command(bin, "-o", strings.Join(opts.optionsStrings(), ","), "-o", fmt.Sprintf("iosize=%d", opts.MaxWrite), "3", mountPoint)
cmd.ExtraFiles = []*os.File{f}
cmd.Env = append(os.Environ(), "MOUNT_FUSEFS_CALL_BY_LIB=", "MOUNT_OSXFUSE_CALL_BY_LIB=",
"MOUNT_OSXFUSE_DAEMON_PATH="+os.Args[0],
"MOUNT_FUSEFS_DAEMON_PATH="+os.Args[0])
var out, errOut bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &errOut
if err := cmd.Start(); err != nil {
f.Close()
return 0, err
}
go func() {
err := cmd.Wait()
if err != nil {
err = fmt.Errorf("mount_osxfusefs failed: %v. Stderr: %s, Stdout: %s", err, errOut.String(), out.String())
}
ready <- err
close(ready)
}()
// The finalizer for f will close its fd so we return a dup.
defer f.Close()
return syscall.Dup(int(f.Fd()))
}
示例8: TestSendErrorFromChild
func TestSendErrorFromChild(t *testing.T) {
pipe, err := NewSyncPipe()
if err != nil {
t.Fatal(err)
}
defer func() {
if err := pipe.Close(); err != nil {
t.Fatal(err)
}
}()
childfd, err := syscall.Dup(int(pipe.Child().Fd()))
if err != nil {
t.Fatal(err)
}
childPipe, _ := NewSyncPipeFromFd(0, uintptr(childfd))
pipe.CloseChild()
pipe.SendToChild(nil)
expected := "something bad happened"
childPipe.ReportChildError(fmt.Errorf(expected))
childError := pipe.ReadFromChild()
if childError == nil {
t.Fatal("expected an error to be returned but did not receive anything")
}
if childError.Error() != expected {
t.Fatalf("expected %q but received error message %q", expected, childError.Error())
}
}
示例9: NewBufferFile
// NewBufferFile maps a file to shared memory and returns a handle to the shared memory buffer
func NewBufferFile(file *os.File, size, prot int) (Buffer, error) {
fi, err := file.Stat()
if err != nil {
return nil, err
}
sys := fi.Sys().(*syscall.Stat_t)
if sys.Size != int64(size) {
return nil, errWrongSize
}
// Dup to allow file parameter to be closed regardless
fd, err := syscall.Dup(int(file.Fd()))
if err != nil {
return nil, err
}
const flags = syscall.MAP_SHARED
b, err := syscall.Mmap(fd, 0, size, prot, flags)
if err != nil {
return nil, err
}
// localFile is nil because fd is from somewhere else
buf := &sharedBuffer{os.NewFile(uintptr(fd), ""), nil, b, stackToKeep()}
runtime.SetFinalizer(buf, (*sharedBuffer).finalize)
return buf, nil
}
示例10: getBlockDeviceTopology
// Get topology data using blkid APIs. In case of an error getting any of the
// topology properties the field will be set to 0
func getBlockDeviceTopology(file File) (DeviceTopology, error) {
var res DeviceTopology
probe := C.blkid_new_probe()
if probe == nil {
return res, fmt.Errorf("Could not probe device")
}
C.blkid_reset_probe(probe)
fd, err := syscall.Dup(int(file.Fd()))
if err != nil {
return res, fmt.Errorf("Could not dup FD as part of device probing: %s", err)
}
rv := C.blkid_probe_set_device(probe, C.int(fd), 0, 0)
if rv != 0 {
syscall.Close(fd)
}
defer C.blkid_free_probe(probe)
topology := C.blkid_probe_get_topology(probe)
if topology == nil {
return res, fmt.Errorf("Could not get topology for device")
}
res.AlignmentOffset = uint64(C.blkid_topology_get_alignment_offset(topology))
res.MinimumIOSize = uint64(C.blkid_topology_get_minimum_io_size(topology))
res.OptimalIOSize = uint64(C.blkid_topology_get_optimal_io_size(topology))
res.LogicalSectorSize = uint64(C.blkid_topology_get_logical_sector_size(topology))
res.PhysicalSectorSize = uint64(C.blkid_topology_get_physical_sector_size(topology))
return res, nil
}
示例11: dup
func dup(file *os.File, name string) (newfile *os.File, err error) {
fd, err := syscall.Dup(int(file.Fd()))
if err != nil {
return nil, err
}
return os.NewFile(uintptr(fd), "<stdin>"), nil
}
示例12: DupFd
// DupFd returns the integer Unix file descriptor duplicated from Listener.
// This method can't invoke simultaneously.
func (l *Listener) DupFd() (int, error) {
f, err := l.fileListener.File()
if err != nil {
return 0, err
}
return syscall.Dup(int(f.Fd()))
}
示例13: newFileFD
func newFileFD(f *os.File) (*netFD, error) {
syscall.ForkLock.RLock()
fd, err := syscall.Dup(int(f.Fd()))
if err != nil {
syscall.ForkLock.RUnlock()
return nil, os.NewSyscallError("dup", err)
}
syscall.CloseOnExec(fd)
syscall.ForkLock.RUnlock()
sotype, err := syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_TYPE)
if err != nil {
closesocket(fd)
return nil, os.NewSyscallError("getsockopt", err)
}
family := syscall.AF_UNSPEC
toAddr := sockaddrToTCP
sa, _ := syscall.Getsockname(fd)
switch sa.(type) {
default:
closesocket(fd)
return nil, syscall.EINVAL
case *syscall.SockaddrInet4:
family = syscall.AF_INET
if sotype == syscall.SOCK_DGRAM {
toAddr = sockaddrToUDP
} else if sotype == syscall.SOCK_RAW {
toAddr = sockaddrToIP
}
case *syscall.SockaddrInet6:
family = syscall.AF_INET6
if sotype == syscall.SOCK_DGRAM {
toAddr = sockaddrToUDP
} else if sotype == syscall.SOCK_RAW {
toAddr = sockaddrToIP
}
case *syscall.SockaddrUnix:
family = syscall.AF_UNIX
toAddr = sockaddrToUnix
if sotype == syscall.SOCK_DGRAM {
toAddr = sockaddrToUnixgram
} else if sotype == syscall.SOCK_SEQPACKET {
toAddr = sockaddrToUnixpacket
}
}
laddr := toAddr(sa)
sa, _ = syscall.Getpeername(fd)
raddr := toAddr(sa)
netfd, err := newFD(fd, family, sotype, laddr.Network())
if err != nil {
closesocket(fd)
return nil, err
}
netfd.setAddr(laddr, raddr)
return netfd, nil
}
示例14: file
func (fd *netFD) file(f *os.File, s string) (*os.File, error) {
syscall.ForkLock.RLock()
dfd, err := syscall.Dup(int(f.Fd()), -1)
syscall.ForkLock.RUnlock()
if err != nil {
return nil, &OpError{"dup", s, fd.laddr, err}
}
return os.NewFile(uintptr(dfd), s), nil
}
示例15: newFileFD
func newFileFD(f *os.File) (net *netFD, err error) {
var ctl *os.File
close := func(fd int) {
if err != nil {
syscall.Close(fd)
}
}
path, err := syscall.Fd2path(int(f.Fd()))
if err != nil {
return nil, os.NewSyscallError("fd2path", err)
}
comp := splitAtBytes(path, "/")
n := len(comp)
if n < 3 || comp[0] != "net" {
return nil, syscall.EPLAN9
}
name := comp[2]
switch file := comp[n-1]; file {
case "ctl", "clone":
syscall.ForkLock.RLock()
fd, err := syscall.Dup(int(f.Fd()), -1)
syscall.ForkLock.RUnlock()
if err != nil {
return nil, os.NewSyscallError("dup", err)
}
defer close(fd)
dir := "/net/" + comp[n-2]
ctl = os.NewFile(uintptr(fd), dir+"/"+file)
ctl.Seek(0, 0)
var buf [16]byte
n, err := ctl.Read(buf[:])
if err != nil {
return nil, err
}
name = string(buf[:n])
default:
if len(comp) < 4 {
return nil, errors.New("could not find control file for connection")
}
dir := "/net/" + comp[1] + "/" + name
ctl, err = os.OpenFile(dir+"/ctl", os.O_RDWR, 0)
if err != nil {
return nil, err
}
defer close(int(ctl.Fd()))
}
dir := "/net/" + comp[1] + "/" + name
laddr, err := readPlan9Addr(comp[1], dir+"/local")
if err != nil {
return nil, err
}
return newFD(comp[1], name, ctl, nil, laddr, nil), nil
}