forked from siadat/ipc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgrcv.go
45 lines (42 loc) · 926 Bytes
/
msgrcv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package ipc
import (
"bytes"
"encoding/binary"
"fmt"
"syscall"
"unsafe"
)
// Msgrcv calls the msgrcv() syscall.
func Msgrcv(qid uint, msg *Msgbuf, flags uint) error {
var buf = make([]byte, uintSize+msgmax)
lengthRead, _, errno := syscall.Syscall6(syscall.SYS_MSGRCV,
uintptr(qid),
uintptr(unsafe.Pointer(&buf[0])),
uintptr(msgmax),
uintptr(msg.Mtype),
uintptr(flags),
0,
)
if errno != 0 {
return errno
}
buffer := bytes.NewBuffer(buf)
switch uintSize {
case 4:
var mtype uint32
err := binary.Write(buffer, binary.LittleEndian, &mtype)
if err != nil {
return fmt.Errorf("Can't write binary: %v", err)
}
msg.Mtype = uint(mtype)
case 8:
var mtype uint64
err := binary.Write(buffer, binary.LittleEndian, &mtype)
if err != nil {
return fmt.Errorf("Can't write binary: %v", err)
}
msg.Mtype = uint(mtype)
}
msg.Mtext = buf[uintSize:uintSize+int(lengthRead)]
return nil
}