-
Notifications
You must be signed in to change notification settings - Fork 9
/
msg.go
229 lines (194 loc) · 4.82 KB
/
msg.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package sysvipc
/*
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int msgget(key_t key, int msgflg);
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
*/
import "C"
import (
"time"
"unsafe"
)
// MessageQueue is a kernel-maintained queue.
type MessageQueue int64
// GetMsgQueue creates or retrieves a message queue id for a given IPC key.
func GetMsgQueue(key int64, flags *MQFlags) (MessageQueue, error) {
rc, err := C.msgget(C.key_t(key), C.int(flags.flags()))
if rc == -1 {
return -1, err
}
return MessageQueue(rc), nil
}
// Send places a new message onto the queue
func (mq MessageQueue) Send(mtyp int64, body []byte, flags *MQSendFlags) error {
b := make([]byte, len(body)+8)
copy(b[:8], serialize(mtyp))
copy(b[8:], body)
rc, err := C.msgsnd(
C.int(mq),
unsafe.Pointer(&b[0]),
C.size_t(len(body)),
C.int(flags.flags()),
)
if rc == -1 {
return err
}
return nil
}
// Receive retrieves a message from the queue.
func (mq MessageQueue) Receive(maxlen uint, msgtyp int64, flags *MQRecvFlags) ([]byte, int64, error) {
b := make([]byte, maxlen+8)
rc, err := C.msgrcv(
C.int(mq),
unsafe.Pointer(&b[0]),
C.size_t(maxlen),
C.long(msgtyp),
C.int(flags.flags()),
)
if rc == -1 {
return nil, 0, err
}
mtyp := deserialize(b[:8])
return b[8 : rc+8], mtyp, nil
}
// Stat produces information about the queue.
func (mq MessageQueue) Stat() (*MQInfo, error) {
mqds := C.struct_msqid_ds{}
rc, err := C.msgctl(C.int(mq), C.IPC_STAT, &mqds)
if rc == -1 {
return nil, err
}
mqinf := MQInfo{
Perms: IpcPerms{
OwnerUID: int(mqds.msg_perm.uid),
OwnerGID: int(mqds.msg_perm.gid),
CreatorUID: int(mqds.msg_perm.cuid),
CreatorGID: int(mqds.msg_perm.cgid),
Mode: uint16(mqds.msg_perm.mode),
},
LastSend: time.Unix(int64(mqds.msg_stime), 0),
LastRcv: time.Unix(int64(mqds.msg_rtime), 0),
LastChange: time.Unix(int64(mqds.msg_ctime), 0),
MsgCount: uint(mqds.msg_qnum),
MaxBytes: uint(mqds.msg_qbytes),
LastSender: int(mqds.msg_lspid),
LastRcver: int(mqds.msg_lrpid),
}
return &mqinf, nil
}
// Set updates parameters of the queue.
func (mq MessageQueue) Set(mqi *MQInfo) error {
mqds := &C.struct_msqid_ds{
msg_perm: C.struct_ipc_perm{
uid: C.__uid_t(mqi.Perms.OwnerUID),
gid: C.__gid_t(mqi.Perms.OwnerGID),
mode: C.ushort(mqi.Perms.Mode & 0x1FF),
},
msg_qbytes: C.msglen_t(mqi.MaxBytes),
}
rc, err := C.msgctl(C.int(mq), C.IPC_SET, mqds)
if rc == -1 {
return err
}
return nil
}
// Remove deletes the queue.
// This will also awake all waiting readers and writers with EIDRM.
func (mq MessageQueue) Remove() error {
rc, err := C.msgctl(C.int(mq), C.IPC_RMID, nil)
if rc == -1 {
return err
}
return nil
}
// MQInfo holds meta information about a message queue.
type MQInfo struct {
Perms IpcPerms
LastSend time.Time
LastRcv time.Time
LastChange time.Time
MsgCount uint
MaxBytes uint
LastSender int
LastRcver int
}
// MQFlags holds the flags/options for GetMsgQueue
type MQFlags struct {
// Create controls whether to create the queue if it doesn't exist.
Create bool
// Exclusive causes GetMsgQueue to fail if the queue already exists (only
// useful with Create).
Exclusive bool
// Perms is the file-style (rwxrwxrwx) permissions with which to create the
// queue (also only useful with Create).
Perms int
}
func (mf *MQFlags) flags() int64 {
if mf == nil {
return 0
}
var f int64 = int64(mf.Perms) & 0777
if mf.Create {
f |= int64(C.IPC_CREAT)
}
if mf.Exclusive {
f |= int64(C.IPC_EXCL)
}
return f
}
// MQSendFlags hold the options for a MessageQueue.Send()
type MQSendFlags struct {
// DontWait causes Send() calls that would otherwise
// block to instead fail with syscall.EAGAIN
DontWait bool
}
func (mf *MQSendFlags) flags() int64 {
if mf == nil {
return 0
}
var f int64
if mf.DontWait {
f |= int64(C.IPC_NOWAIT)
}
return f
}
// MQRecvFlags hold the options for a MessageQueue.Receive()
type MQRecvFlags struct {
// DontWait causes Receive() calls that would otherwise
// block to instead fail with syscall.EAGAIN or syscall.ENOMSG
DontWait bool
// Truncate allows shortening the message if maxlen is
// shorter than the message being received
Truncate bool
}
func (mf *MQRecvFlags) flags() int64 {
if mf == nil {
return 0
}
var f int64
if mf.DontWait {
f |= int64(C.IPC_NOWAIT)
}
if mf.Truncate {
f |= int64(C.MSG_NOERROR)
}
return f
}
/*
real c-style pointer casting
*/
func serialize(num int64) []byte {
b := make([]byte, 8)
base := uintptr(unsafe.Pointer(&num))
for i := 0; i < 8; i++ {
b[i] = *(*byte)(unsafe.Pointer(base + uintptr(i)))
}
return b
}
func deserialize(b []byte) int64 {
return *(*int64)(unsafe.Pointer(&b[0]))
}