-
Notifications
You must be signed in to change notification settings - Fork 18
/
msgsnd.go
61 lines (52 loc) · 1.27 KB
/
msgsnd.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package ipc
import (
"bytes"
"encoding/binary"
"fmt"
"syscall"
"unsafe"
)
// Msgsnd calls the msgsnd() syscall.
func Msgsnd(qid uint, msg *Msgbuf, flags uint) error {
buffer, err := PrepareMsg(msg)
if err != nil {
return err
}
return MsgsndPrepared(qid, len(msg.Mtext), buffer, flags)
}
// PrepareMsg creates a buffer containing the message to send
func PrepareMsg(msg *Msgbuf) (*bytes.Buffer, error) {
if len(msg.Mtext) > msgmax {
return nil, fmt.Errorf("mtext is too large, %d > %d", len(msg.Mtext), msgmax)
}
buf := make([]byte, uintSize+msgmax)
buffer := bytes.NewBuffer(buf)
buffer.Reset()
var err error
switch uintSize {
case 4:
err = binary.Write(buffer, binary.LittleEndian, uint32(msg.Mtype))
case 8:
err = binary.Write(buffer, binary.LittleEndian, uint64(msg.Mtype))
}
if err != nil {
return nil, fmt.Errorf("Can't write binary: %v", err)
}
buffer.Write(msg.Mtext)
return buffer, nil
}
//MsgsndPrepared sends a prepared message using the msgsnd() syscall
func MsgsndPrepared(qid uint, len int, buffer *bytes.Buffer, flags uint) error {
_, _, errno := syscall.Syscall6(syscall.SYS_MSGSND,
uintptr(qid),
uintptr(unsafe.Pointer(&buffer.Bytes()[0])),
uintptr(len),
uintptr(flags),
0,
0,
)
if errno != 0 {
return errno
}
return nil
}