forked from siadat/ipc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgsnd.go
43 lines (40 loc) · 870 Bytes
/
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
package ipc
import (
"bytes"
"encoding/binary"
"fmt"
"syscall"
"unsafe"
)
// Msgsnd calls the msgsnd() syscall.
func Msgsnd(qid uint, msg *Msgbuf, flags uint) error {
if len(msg.Mtext) > msgmax {
return 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 fmt.Errorf("Can't write binary: %v", err)
}
buffer.Write(msg.Mtext)
_, _, errno := syscall.Syscall6(syscall.SYS_MSGSND,
uintptr(qid),
uintptr(unsafe.Pointer(&buf[0])),
uintptr(len(msg.Mtext)),
uintptr(flags),
0,
0,
)
if errno != 0 {
return errno
}
return nil
}