当前位置: 首页>>代码示例>>Golang>>正文


Golang syscall.Read函数代码示例

本文整理汇总了Golang中syscall.Read函数的典型用法代码示例。如果您正苦于以下问题:Golang Read函数的具体用法?Golang Read怎么用?Golang Read使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Read函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: watcher

// watcher tracks the changes to the value file of a pin
func (pin *Pin) watcher() {
	buf := make([]byte, syscall.SizeofInotifyEvent)
	n, err := syscall.Read(pin.Fd, buf)
	for err == nil && n == syscall.SizeofInotifyEvent {
		data, _ := ioutil.ReadFile(pin.ValuePath)
		pin.State = data[0]
		pin.EventChannel <- &Event{State: pin.State}
		n, err = syscall.Read(pin.Fd, buf)
	}
}
开发者ID:jsgoecke,项目名称:gpio,代码行数:11,代码来源:gpio.go

示例2: readPipe

func readPipe(p int, c chan<- string) {
	var s string
	buf := make([]byte, 4096)
	n, _ := syscall.Read(p, buf)
	for n > 0 {
		s += string(buf[:n])
		n, _ = syscall.Read(p, buf)
	}
	c <- s
}
开发者ID:c0der007,项目名称:llgo,代码行数:10,代码来源:utils_test.go

示例3: handle

// handle requests
func (nbd *NBD) handle() {
	buf := make([]byte, 2<<19)
	var x request

	for {
		syscall.Read(nbd.socket, buf[0:28])

		x.magic = binary.BigEndian.Uint32(buf)
		x.typus = binary.BigEndian.Uint32(buf[4:8])
		x.handle = binary.BigEndian.Uint64(buf[8:16])
		x.from = binary.BigEndian.Uint64(buf[16:24])
		x.len = binary.BigEndian.Uint32(buf[24:28])

		switch x.magic {
		case NBD_REPLY_MAGIC:
			fallthrough
		case NBD_REQUEST_MAGIC:
			switch x.typus {
			case NBD_CMD_READ:
				nbd.device.ReadAt(buf[16:16+x.len], int64(x.from))
				binary.BigEndian.PutUint32(buf[0:4], NBD_REPLY_MAGIC)
				binary.BigEndian.PutUint32(buf[4:8], 0)
				syscall.Write(nbd.socket, buf[0:16+x.len])
			case NBD_CMD_WRITE:
				n, _ := syscall.Read(nbd.socket, buf[28:28+x.len])
				for uint32(n) < x.len {
					m, _ := syscall.Read(nbd.socket, buf[28+n:28+x.len])
					n += m
				}
				nbd.device.WriteAt(buf[28:28+x.len], int64(x.from))
				binary.BigEndian.PutUint32(buf[0:4], NBD_REPLY_MAGIC)
				binary.BigEndian.PutUint32(buf[4:8], 0)
				syscall.Write(nbd.socket, buf[0:16])
			case NBD_CMD_DISC:
				panic("Disconnect")
			case NBD_CMD_FLUSH:
				nbd.device.Sync()
				binary.BigEndian.PutUint32(buf[0:4], NBD_REPLY_MAGIC)
				binary.BigEndian.PutUint32(buf[4:8], 1)
				syscall.Write(nbd.socket, buf[0:16])
			case NBD_CMD_TRIM:
				binary.BigEndian.PutUint32(buf[0:4], NBD_REPLY_MAGIC)
				binary.BigEndian.PutUint32(buf[4:8], 1)
				syscall.Write(nbd.socket, buf[0:16])
			default:
				panic("unknown command")
			}
		default:
			panic("Invalid packet")
		}
	}
}
开发者ID:ccp0101,项目名称:go-nbd,代码行数:53,代码来源:nbd.go

示例4: kmemleakScan

func kmemleakScan(report bool) {
	fd, err := syscall.Open("/sys/kernel/debug/kmemleak", syscall.O_RDWR, 0)
	if err != nil {
		panic(err)
	}
	defer syscall.Close(fd)
	// Kmemleak has false positives. To mitigate most of them, it checksums
	// potentially leaked objects, and reports them only on the next scan
	// iff the checksum does not change. Because of that we do the following
	// intricate dance:
	// Scan, sleep, scan again. At this point we can get some leaks.
	// If there are leaks, we sleep and scan again, this can remove
	// false leaks. Then, read kmemleak again. If we get leaks now, then
	// hopefully these are true positives during the previous testing cycle.
	if _, err := syscall.Write(fd, []byte("scan")); err != nil {
		panic(err)
	}
	time.Sleep(time.Second)
	if _, err := syscall.Write(fd, []byte("scan")); err != nil {
		panic(err)
	}
	if report {
		if kmemleakBuf == nil {
			kmemleakBuf = make([]byte, 128<<10)
		}
		n, err := syscall.Read(fd, kmemleakBuf)
		if err != nil {
			panic(err)
		}
		if n != 0 {
			time.Sleep(time.Second)
			if _, err := syscall.Write(fd, []byte("scan")); err != nil {
				panic(err)
			}
			n, err := syscall.Read(fd, kmemleakBuf)
			if err != nil {
				panic(err)
			}
			if n != 0 {
				// BUG in output should be recognized by manager.
				Logf(0, "BUG: memory leak:\n%s\n", kmemleakBuf[:n])
			}
		}
	}
	if _, err := syscall.Write(fd, []byte("clear")); err != nil {
		panic(err)
	}
}
开发者ID:google,项目名称:syzkaller,代码行数:48,代码来源:fuzzer.go

示例5: Read

func (fd *netFD) Read(p []byte) (n int, err os.Error) {
	if fd == nil || fd.sysfile == nil {
		return 0, os.EINVAL
	}
	fd.rio.Lock()
	defer fd.rio.Unlock()
	fd.incref()
	defer fd.decref()
	if fd.rdeadline_delta > 0 {
		fd.rdeadline = pollserver.Now() + fd.rdeadline_delta
	} else {
		fd.rdeadline = 0
	}
	var oserr os.Error
	for {
		var errno int
		n, errno = syscall.Read(fd.sysfile.Fd(), p)
		if errno == syscall.EAGAIN && fd.rdeadline >= 0 {
			pollserver.WaitRead(fd)
			continue
		}
		if errno != 0 {
			n = 0
			oserr = os.Errno(errno)
		} else if n == 0 && errno == 0 && fd.proto != syscall.SOCK_DGRAM {
			err = os.EOF
		}
		break
	}
	if oserr != nil {
		err = &OpError{"read", fd.net, fd.raddr, oserr}
	}
	return
}
开发者ID:rapgamer,项目名称:golang-china,代码行数:34,代码来源:fd.go

示例6: main

func main() {
	acceptingFd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
	check(err)

	addr := &syscall.SockaddrInet4{Port: 3000, Addr: [4]byte{0, 0, 0, 0}}
	err = syscall.Bind(acceptingFd, addr)
	check(err)

	err = syscall.Listen(acceptingFd, 100)
	check(err)

	for {
		connectionFd, _, err := syscall.Accept(acceptingFd)
		check(err)
		fmt.Println("Accepted new connectrion")

		data := make([]byte, 1024)
		_, err = syscall.Read(connectionFd, data)
		check(err)
		fmt.Printf("Received: %s\n", string(data))

		_, err = syscall.Write(connectionFd, data)
		check(err)

		err = syscall.Close(connectionFd)
		check(err)
	}
}
开发者ID:jingweno,项目名称:eventloopclass,代码行数:28,代码来源:main.go

示例7: read

func read(u U, a []uint64) uint64 {
	fd, buf, size := int(a[0]), a[1], a[2]
	tmp := make([]byte, size)
	n, _ := syscall.Read(fd, tmp)
	u.MemWrite(buf, tmp[:n])
	return uint64(n)
}
开发者ID:eqv,项目名称:usercorn,代码行数:7,代码来源:syscalls.go

示例8: Read

func (fd *netFD) Read(p []byte) (n int, err error) {
	if err := fd.readLock(); err != nil {
		return 0, err
	}
	defer fd.readUnlock()
	if err := fd.pd.prepareRead(); err != nil {
		return 0, err
	}
	for {
		n, err = syscall.Read(fd.sysfd, p)
		if err != nil {
			n = 0
			if err == syscall.EAGAIN {
				if err = fd.pd.waitRead(); err == nil {
					continue
				}
			}
		}
		err = fd.eofError(n, err)
		break
	}
	if _, ok := err.(syscall.Errno); ok {
		err = os.NewSyscallError("read", err)
	}
	return
}
开发者ID:duhaibo0404,项目名称:go-1,代码行数:26,代码来源:fd_unix.go

示例9: readOnce

func (tc *TunnelConn) readOnce(fd int) (num int, err error) {
	num, err = syscall.Read(fd, buf)
	if num == 0 || err != nil && err != syscall.EAGAIN {
		tc.shutdown()
	}
	return
}
开发者ID:008by,项目名称:cf-services-contrib-release,代码行数:7,代码来源:tunnel_conn.go

示例10: Read

func (fd *netFD) Read(p []byte) (n int, err error) {
	fd.rio.Lock()
	defer fd.rio.Unlock()
	if err := fd.incref(false); err != nil {
		return 0, err
	}
	defer fd.decref()
	for {
		n, err = syscall.Read(int(fd.sysfd), p)
		if err == syscall.EAGAIN {
			err = errTimeout
			if fd.rdeadline >= 0 {
				if err = fd.pollServer.WaitRead(fd); err == nil {
					continue
				}
			}
		}
		if err != nil {
			n = 0
		} else if n == 0 && err == nil && fd.sotype != syscall.SOCK_DGRAM {
			err = io.EOF
		}
		break
	}
	if err != nil && err != io.EOF {
		err = &OpError{"read", fd.net, fd.raddr, err}
	}
	return
}
开发者ID:hfeeki,项目名称:go,代码行数:29,代码来源:fd_unix.go

示例11: Read

// Read reads up to len(p) bytes into p.  It returns the number of
// bytes read (0 <= n <= len(p)) and any error encountered. If some
// data is available but not len(p) bytes, Read returns what is
// available instead of waiting for more. Read is formally and
// semantically compatible with the Read method of the io.Reader
// interface. See documentation of this interface method for more
// details. In addition Read honors the timeout set by
// (*FD).SetDeadline and (*FD).SetReadDeadline. If no data are read
// before the timeout expires Read returns with err == ErrTimeout (and
// n == 0). If the read(2) system-call returns 0, Read returns with
// err = io.EOF (and n == 0).
func (fd *FD) Read(p []byte) (n int, err error) {
	if err = fd.readLock(); err != nil {
		return 0, err
	}
	defer fd.readUnlock()
	if err = fd.pd.PrepareRead(); err != nil {
		return 0, err
	}
	for {
		n, err = syscall.Read(int(fd.sysfd), p)
		if err != nil {
			n = 0
			if err != syscall.EAGAIN {
				break
			}
			if err = fd.pd.WaitRead(); err != nil {
				break
			}
			continue
		}
		if n == 0 && len(p) > 0 {
			err = io.EOF
		}
		break
	}
	return n, err
}
开发者ID:npat-efault,项目名称:pollfd,代码行数:38,代码来源:pollfd.go

示例12: main

func main() {
	args := Arguments()

	infile, args, err := args.String("in-file", nil)
	Check(err)
	outfile, args, err := args.String("out-file", nil)
	Check(err)

	infd, err := syscall.Open(infile, int(ReadOnly), 0)
	Check(err)
	defer func() { Check(syscall.Close(infd)) }()

	flags := Create | WriteOnly | Truncate
	perms := 0 |
		UserRead | UserWrite |
		GroupRead | GroupWrite |
		OtherRead | OtherWrite
	outfd, err := syscall.Open(outfile, int(flags), uint32(perms))
	Check(err)
	defer func() { Check(syscall.Close(outfd)) }()

	bufSize := 1024
	buf := make([]byte, bufSize)
	var n int
	for {
		n, err = syscall.Read(infd, buf)
		Check(err)
		if n == 0 {
			break
		}
		_, err = syscall.Write(outfd, buf[:n])
		Check(err)
	}
}
开发者ID:chichunchen,项目名称:tlpi,代码行数:34,代码来源:copy.go

示例13: main

func main() {
	flag.Parse()
	args = flag.Args()

	if len(args) <= 0 {
		fmt.Fprintf(os.Stderr, "Must specify at least one argument to run:\n")
		fmt.Fprintf(os.Stderr, "\t%s <options> <command> <arguments>...\n", os.Args[0])
		flag.PrintDefaults()
		return
	}

	startCommand(false)

	usr1c := make(chan os.Signal)
	signal.Notify(usr1c, syscall.SIGUSR1)

	go func() {
		for {
			<-usr1c
			if canExecute() {
				startCommand(true)
			}
		}
	}()

	for {
		inotifyFd, err := syscall.InotifyInit()
		if err != nil {
			log.Fatalf("Inotify init failed: %v", err)
		}

		recdepth := 0
		if *recurse {
			recdepth = *depth
		}

		registerDirectory(inotifyFd, ".", recdepth)

		inotifyBuf := make([]byte, 1024*syscall.SizeofInotifyEvent+16)

		for {
			n, err := syscall.Read(inotifyFd, inotifyBuf[0:])
			if err == io.EOF {
				break
			}
			if err != nil {
				log.Printf("Can not read inotify: %v", err)
				break
			}

			if n > syscall.SizeofInotifyEvent {
				if canExecute() {
					startCommand(true)
				}
			}
		}

		syscall.Close(inotifyFd)
	}
}
开发者ID:aarzilli,项目名称:util,代码行数:60,代码来源:Watch.go

示例14: Read

func (fd *netFD) Read(p []byte) (n int, err error) { // 每个读写都被加锁,因此可以序列化运行
	if err := fd.readLock(); err != nil { // 对每个fd的读都序列化
		return 0, err
	}
	defer fd.readUnlock()                       // 在退出时解锁
	if err := fd.pd.PrepareRead(); err != nil { // 如果读错误,返回
		return 0, err
	}
	for {
		n, err = syscall.Read(fd.sysfd, p)
		if err != nil { // 如果读出现错误
			n = 0
			if err == syscall.EAGAIN { // 如果是EAGAIN调用WaitRead,等待读事件
				if err = fd.pd.WaitRead(); err == nil { // 事件出现后返回,异步变为同步操作
					continue
				}
			}
		}
		err = fd.eofError(n, err)
		break // 跳出循环
	}
	if _, ok := err.(syscall.Errno); ok {
		err = os.NewSyscallError("read", err)
	}
	return
}
开发者ID:wenfang,项目名称:golang1.5-src,代码行数:26,代码来源:fd_unix.go

示例15: readEvent

func (w *Watcher) readEvent() {
	var (
		buf   [syscall.SizeofInotifyEvent * 4096]byte // Buffer for a maximum of 4096 raw events
		n     int                                     // Number of bytes read with read()
		errno error                                   // Syscall errno
	)

	for {

		select {
		case <-w.done:
			syscall.Close(w.fd)
			close(w.acceptEvent)
			close(w.Error)
			return
		default:
		}

		n, errno = syscall.Read(w.fd, buf[0:])

		if n == 0 {
			syscall.Close(w.fd)
			close(w.acceptEvent)
			close(w.Error)
			return
		}

		if n < syscall.SizeofInotifyEvent {
			log.Fatal("size of InotifyEvent error", errno)
		}

		var offset uint32 = 0

		for offset <= uint32(n-syscall.SizeofInotifyEvent) {
			raw := (*syscall.InotifyEvent)(unsafe.Pointer(&buf[offset]))
			event := new(FileEvent)
			event.wd = raw.Wd
			event.nameLen = raw.Len
			event.mask = raw.Mask
			event.cookie = raw.Cookie
			path := w.wm.paths[int(raw.Wd)]
			if raw.Len > 0 {
				// Point "bytes" at the first byte of the filename
				bytes := (*[syscall.PathMax]byte)(unsafe.Pointer(&buf[offset+syscall.SizeofInotifyEvent]))
				// The filename is padded with NUL bytes. TrimRight() gets rid of those.
				event.fileName = path + "/" + strings.TrimRight(string(bytes[0:raw.Len]), "\000")
			}

			if _, found := w.skipExt[filepath.Ext(event.fileName)]; !found {
				fmt.Println("--->", w.skipExt, "--->", filepath.Ext(event.fileName), "--->", found)
				//发送事件acceptEvent通道
				w.acceptEvent <- event
			} else {
				fmt.Println("过滤文件:", event.fileName)
			}

			offset += syscall.SizeofInotifyEvent + raw.Len
		}
	}
}
开发者ID:nopsky,项目名称:sfserver,代码行数:60,代码来源:notify.go


注:本文中的syscall.Read函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。