|
1 | 1 | package ipc |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "encoding/binary" |
4 | 6 | "fmt" |
5 | 7 | "syscall" |
6 | 8 | "unsafe" |
7 | 9 | ) |
8 | 10 |
|
9 | 11 | // Msgsnd calls the msgsnd() syscall. |
10 | 12 | func Msgsnd(qid uint, msg *Msgbuf, flags uint) error { |
11 | | - if len(msg.Mtext) > bufSize { |
12 | | - return fmt.Errorf("mtext is too large, %d > %d", len(msg.Mtext), bufSize) |
| 13 | + if len(msg.Mtext) > msgmax { |
| 14 | + return fmt.Errorf("mtext is too large, %d > %d", len(msg.Mtext), msgmax) |
13 | 15 | } |
14 | | - qbuf := msgbufInternal{ |
15 | | - Mtype: msg.Mtype, |
16 | | - } |
17 | | - copy(qbuf.Mtext[:], msg.Mtext) |
18 | 16 |
|
19 | | - _, _, err := syscall.Syscall6(syscall.SYS_MSGSND, |
| 17 | + buf := make([]byte, uintSize+msgmax) |
| 18 | + buffer := bytes.NewBuffer(buf) |
| 19 | + buffer.Reset() |
| 20 | + var err error |
| 21 | + switch uintSize { |
| 22 | + case 4: |
| 23 | + err = binary.Write(buffer, binary.LittleEndian, uint32(msg.Mtype)) |
| 24 | + case 8: |
| 25 | + err = binary.Write(buffer, binary.LittleEndian, uint64(msg.Mtype)) |
| 26 | + } |
| 27 | + if err != nil { |
| 28 | + return fmt.Errorf("Can't write binary: %v", err) |
| 29 | + } |
| 30 | + buffer.Write(msg.Mtext) |
| 31 | + _, _, errno := syscall.Syscall6(syscall.SYS_MSGSND, |
20 | 32 | uintptr(qid), |
21 | | - uintptr(unsafe.Pointer(&qbuf)), |
| 33 | + uintptr(unsafe.Pointer(&buf[0])), |
22 | 34 | uintptr(len(msg.Mtext)), |
23 | 35 | uintptr(flags), |
24 | 36 | 0, |
25 | 37 | 0, |
26 | 38 | ) |
27 | | - if err != 0 { |
28 | | - return err |
| 39 | + if errno != 0 { |
| 40 | + return errno |
29 | 41 | } |
30 | 42 | return nil |
31 | 43 | } |
0 commit comments