本文整理汇总了Golang中syscall.Write函数的典型用法代码示例。如果您正苦于以下问题:Golang Write函数的具体用法?Golang Write怎么用?Golang Write使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Write函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
log.SetFlags(log.Lshortfile)
c, err := fdrpc.Dial("/tmp/test.socket")
if err != nil {
log.Fatal(err)
}
var fd fdrpc.FD
if err := c.Call("Obj.GetStdOut", struct{}{}, &fd); err != nil {
log.Fatal(err)
}
syscall.Write(fd.FD, []byte("Hello from request 1\n"))
if err := c.Call("Obj.GetStdOut", struct{}{}, &fd); err != nil {
log.Fatal(err)
}
syscall.Write(fd.FD, []byte("Hello from request 2\n"))
var streams []fdrpc.FD
if err := c.Call("Obj.GetStreams", struct{}{}, &streams); err != nil {
log.Fatal(err)
}
syscall.Write(streams[0].FD, []byte("Hello stdout\n"))
syscall.Write(streams[1].FD, []byte("Hello stderr\n"))
}
示例2: TestSelect
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])
}
示例3: 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)
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 {
// 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)
}
}
示例4: screenio
func screenio() (err error) {
var (
bytesread int
c_in, c_out [1]byte
up []byte = []byte("\033[A")
zed []byte = []byte("Z")
eightbitchars [256]byte
)
println("screenio")
for i := range eightbitchars {
eightbitchars[i] = byte(i)
}
for {
println("Read")
bytesread, err = syscall.Read(ttyfd, c_in[0:])
println("bytesread", bytesread)
if err != nil {
return
} else if bytesread < 0 {
return errors.New("read error")
}
if bytesread == 0 {
c_out[0] = 'T'
_, err = syscall.Write(ttyfd, c_out[0:])
if err != nil {
return
}
} else {
switch c_in[0] {
case 'q':
return nil
case 'z':
_, err = syscall.Write(ttyfd, zed[:1])
if err != nil {
return nil
}
case 'u':
_, err = syscall.Write(ttyfd, up)
if err != nil {
return nil
}
default:
c_out[0] = '*'
_, err = syscall.Write(ttyfd, c_out[0:])
if err != nil {
return nil
}
}
}
}
}
示例5: ScreenIO
func ScreenIO() (err error) {
var (
errno error
bytesread int
c_in, c_out [1]byte
up []byte = []byte("\033[A")
eightbitchars [256]byte
)
for i := range eightbitchars {
eightbitchars[i] = byte(i)
}
for {
bytesread, errno = syscall.Read(ttyfd, c_in[0:])
if err = os.NewSyscallError("SYS_READ", errno); err != nil {
return
} else if bytesread < 0 {
return errors.New("read error")
}
if bytesread == 0 {
c_out[0] = 'T'
_, errno = syscall.Write(ttyfd, c_out[0:])
if err = os.NewSyscallError("SYS_WRITE", errno); err != nil {
return
}
} else {
switch c_in[0] {
case 'q':
return nil
case 'z':
_, errno = syscall.Write(ttyfd, []byte{'Z'})
if err = os.NewSyscallError("SYS_WRITE", errno); err != nil {
return nil
}
case 'u':
_, errno = syscall.Write(ttyfd, up)
if err = os.NewSyscallError("SYS_WRITE", errno); err != nil {
return nil
}
default:
c_out[0] = '*'
_, errno = syscall.Write(ttyfd, c_out[0:])
if err = os.NewSyscallError("SYS_WRITE", errno); err != nil {
return nil
}
}
}
}
return nil
}
示例6: 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")
}
}
}
示例7: 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)
}
}
示例8: 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)
}
}
示例9: 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)
}
}
示例10: CgcSyscall
func CgcSyscall(u models.Usercorn) {
// TODO: handle errors or something
args, _ := u.ReadRegs(LinuxRegs)
eax, _ := u.RegRead(uc.X86_REG_EAX)
var ret uint64
switch eax {
case 1: // _terminate
syscall.Exit(int(args[0]))
case 2: // transmit
mem, _ := u.MemRead(args[1], args[2])
n, _ := syscall.Write(int(args[0]), mem)
writeAddr(u, args[3], uint64(n))
case 3: // receive
tmp := make([]byte, args[2])
n, _ := syscall.Read(int(args[0]), tmp)
u.MemWrite(args[1], tmp[:n])
writeAddr(u, args[3], uint64(n))
case 5: // allocate
addr, _ := u.Mmap(0, args[0])
// args[1] == is executable
writeAddr(u, args[2], addr)
case 7: // random
tmp := make([]byte, args[1])
rand.Read(tmp)
u.MemWrite(args[0], tmp)
writeAddr(u, args[2], args[1])
}
u.RegWrite(uc.X86_REG_EAX, ret)
}
示例11: put
func (tfd testFd) put(t *testing.T) {
buf := make([]byte, 10)
_, errno := syscall.Write(tfd[1], buf)
if errno != nil {
t.Fatalf("Failed to write to pipe: %v", errno)
}
}
示例12: Write
func (file *File) Write(b []byte) (ret int, err error) {
if file == nil {
return -1, syscall.EINVAL
}
r, e := syscall.Write(file.fd, b)
return int(r), e
}
示例13: Write
// Write writes the contents of p into the buffer. It returns the number of
// bytes written. If nn < len(p), it also returns an error explaining why the
// write is short.
func (w *Writer) Write(p []byte) (nn int, err error) {
if w.err != nil {
return 0, w.err
}
var bcopied int
var n int
nn = 0
for len(p) > 0 {
bcopied = copy(w.buff[w.pbuff:], p)
if bcopied < len(p) {
p = p[bcopied:]
n, w.err = syscall.Write(int(w.file.Fd()), w.buff)
nn += n
if w.err != nil {
return nn, w.err
}
w.pbuff = 0
} else { // Buffer not full
w.pbuff += bcopied
}
}
nn += bcopied
return nn, nil
}
示例14: fileWrite
func fileWrite(args []interface{}) interface{} {
ensureArity(2, len(args))
fd := args[0].(int)
data := args[1].(string)
syscall.Write(fd, []byte(data))
return nil
}
示例15: Pause
func (su *Supervise) Pause() {
if su.AcquireLock() == nil {
su.WriteLog("process is not running.")
} else {
syscall.Write(su.ctrlPipeFd, []byte("p"))
}
}