sysv_msg.c revision 1.10 1 1.10 cgd /* $NetBSD: sysv_msg.c,v 1.10 1994/10/20 04:23:15 cgd Exp $ */
2 1.9 cgd
3 1.1 cgd /*
4 1.1 cgd * Implementation of SVID messages
5 1.1 cgd *
6 1.1 cgd * Author: Daniel Boulet
7 1.1 cgd *
8 1.1 cgd * Copyright 1993 Daniel Boulet and RTMX Inc.
9 1.1 cgd *
10 1.1 cgd * This system call was implemented by Daniel Boulet under contract from RTMX.
11 1.1 cgd *
12 1.1 cgd * Redistribution and use in source forms, with and without modification,
13 1.1 cgd * are permitted provided that this entire comment appears intact.
14 1.1 cgd *
15 1.1 cgd * Redistribution in binary form may occur without any restrictions.
16 1.1 cgd * Obviously, it would be nice if you gave credit where credit is due
17 1.1 cgd * but requiring it would be too onerous.
18 1.1 cgd *
19 1.1 cgd * This software is provided ``AS IS'' without any warranties of any kind.
20 1.1 cgd */
21 1.1 cgd
22 1.2 mycroft #include <sys/param.h>
23 1.2 mycroft #include <sys/systm.h>
24 1.2 mycroft #include <sys/kernel.h>
25 1.2 mycroft #include <sys/proc.h>
26 1.2 mycroft #include <sys/msg.h>
27 1.2 mycroft #include <sys/malloc.h>
28 1.1 cgd
29 1.10 cgd #include <sys/mount.h>
30 1.10 cgd #include <sys/syscallargs.h>
31 1.10 cgd
32 1.1 cgd #define MSG_DEBUG
33 1.1 cgd #undef MSG_DEBUG_OK
34 1.1 cgd
35 1.1 cgd int nfree_msgmaps; /* # of free map entries */
36 1.1 cgd short free_msgmaps; /* head of linked list of free map entries */
37 1.1 cgd struct msg *free_msghdrs; /* list of free msg headers */
38 1.1 cgd
39 1.1 cgd int
40 1.1 cgd msginit()
41 1.1 cgd {
42 1.3 mycroft register int i;
43 1.3 mycroft vm_offset_t whocares1, whocares2;
44 1.1 cgd
45 1.3 mycroft /*
46 1.3 mycroft * msginfo.msgssz should be a power of two for efficiency reasons.
47 1.3 mycroft * It is also pretty silly if msginfo.msgssz is less than 8
48 1.3 mycroft * or greater than about 256 so ...
49 1.3 mycroft */
50 1.1 cgd
51 1.3 mycroft i = 8;
52 1.3 mycroft while (i < 1024 && i != msginfo.msgssz)
53 1.3 mycroft i <<= 1;
54 1.3 mycroft if (i != msginfo.msgssz) {
55 1.3 mycroft printf("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
56 1.3 mycroft msginfo.msgssz);
57 1.3 mycroft panic("msginfo.msgssz not a small power of 2");
58 1.3 mycroft }
59 1.3 mycroft
60 1.3 mycroft if (msginfo.msgseg > 32767) {
61 1.3 mycroft printf("msginfo.msgseg=%d\n", msginfo.msgseg);
62 1.3 mycroft panic("msginfo.msgseg > 32767");
63 1.3 mycroft }
64 1.3 mycroft
65 1.3 mycroft if (msgmaps == NULL)
66 1.3 mycroft panic("msgmaps is NULL");
67 1.3 mycroft
68 1.3 mycroft for (i = 0; i < msginfo.msgseg; i++) {
69 1.3 mycroft if (i > 0)
70 1.3 mycroft msgmaps[i-1].next = i;
71 1.3 mycroft msgmaps[i].next = -1; /* implies entry is available */
72 1.3 mycroft }
73 1.3 mycroft free_msgmaps = 0;
74 1.3 mycroft nfree_msgmaps = msginfo.msgseg;
75 1.3 mycroft
76 1.3 mycroft if (msghdrs == NULL)
77 1.3 mycroft panic("msghdrs is NULL");
78 1.3 mycroft
79 1.3 mycroft for (i = 0; i < msginfo.msgtql; i++) {
80 1.3 mycroft msghdrs[i].msg_type = 0;
81 1.3 mycroft if (i > 0)
82 1.3 mycroft msghdrs[i-1].msg_next = &msghdrs[i];
83 1.3 mycroft msghdrs[i].msg_next = NULL;
84 1.3 mycroft }
85 1.3 mycroft free_msghdrs = &msghdrs[0];
86 1.3 mycroft
87 1.3 mycroft if (msqids == NULL)
88 1.3 mycroft panic("msqids is NULL");
89 1.3 mycroft
90 1.4 mycroft for (i = 0; i < msginfo.msgmni; i++) {
91 1.3 mycroft msqids[i].msg_qbytes = 0; /* implies entry is available */
92 1.3 mycroft msqids[i].msg_perm.seq = 0; /* reset to a known value */
93 1.3 mycroft }
94 1.1 cgd }
95 1.1 cgd
96 1.3 mycroft static void
97 1.1 cgd msg_freehdr(msghdr)
98 1.3 mycroft struct msg *msghdr;
99 1.1 cgd {
100 1.3 mycroft while (msghdr->msg_ts > 0) {
101 1.3 mycroft short next;
102 1.3 mycroft if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
103 1.3 mycroft panic("msghdr->msg_spot out of range");
104 1.3 mycroft next = msgmaps[msghdr->msg_spot].next;
105 1.3 mycroft msgmaps[msghdr->msg_spot].next = free_msgmaps;
106 1.3 mycroft free_msgmaps = msghdr->msg_spot;
107 1.5 mycroft nfree_msgmaps++;
108 1.3 mycroft msghdr->msg_spot = next;
109 1.3 mycroft if (msghdr->msg_ts >= msginfo.msgssz)
110 1.3 mycroft msghdr->msg_ts -= msginfo.msgssz;
111 1.3 mycroft else
112 1.3 mycroft msghdr->msg_ts = 0;
113 1.3 mycroft }
114 1.3 mycroft if (msghdr->msg_spot != -1)
115 1.3 mycroft panic("msghdr->msg_spot != -1");
116 1.3 mycroft msghdr->msg_next = free_msghdrs;
117 1.3 mycroft free_msghdrs = msghdr;
118 1.1 cgd }
119 1.1 cgd
120 1.1 cgd int
121 1.1 cgd msgctl(p, uap, retval)
122 1.1 cgd struct proc *p;
123 1.10 cgd register struct msgctl_args /* {
124 1.10 cgd syscallarg(int) msqid;
125 1.10 cgd syscallarg(int) cmd;
126 1.10 cgd syscallarg(struct msqid_ds *) buf;
127 1.10 cgd } */ *uap;
128 1.10 cgd register_t *retval;
129 1.1 cgd {
130 1.10 cgd int msqid = SCARG(uap, msqid);
131 1.10 cgd int cmd = SCARG(uap, cmd);
132 1.10 cgd struct msqid_ds *user_msqptr = SCARG(uap, buf);
133 1.3 mycroft struct ucred *cred = p->p_ucred;
134 1.3 mycroft int i, rval, eval;
135 1.3 mycroft struct msqid_ds msqbuf;
136 1.3 mycroft register struct msqid_ds *msqptr;
137 1.1 cgd
138 1.1 cgd #ifdef MSG_DEBUG_OK
139 1.3 mycroft printf("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, user_msqptr);
140 1.1 cgd #endif
141 1.1 cgd
142 1.3 mycroft msqid = IPCID_TO_IX(msqid);
143 1.1 cgd
144 1.3 mycroft if (msqid < 0 || msqid >= msginfo.msgmni) {
145 1.1 cgd #ifdef MSG_DEBUG_OK
146 1.3 mycroft printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
147 1.3 mycroft msginfo.msgmni);
148 1.1 cgd #endif
149 1.3 mycroft return(EINVAL);
150 1.3 mycroft }
151 1.1 cgd
152 1.3 mycroft msqptr = &msqids[msqid];
153 1.1 cgd
154 1.3 mycroft if (msqptr->msg_qbytes == 0) {
155 1.1 cgd #ifdef MSG_DEBUG_OK
156 1.3 mycroft printf("no such msqid\n");
157 1.1 cgd #endif
158 1.3 mycroft return(EINVAL);
159 1.3 mycroft }
160 1.10 cgd if (msqptr->msg_perm.seq != IPCID_TO_SEQ(SCARG(uap, msqid))) {
161 1.1 cgd #ifdef MSG_DEBUG_OK
162 1.3 mycroft printf("wrong sequence number\n");
163 1.1 cgd #endif
164 1.3 mycroft return(EINVAL);
165 1.3 mycroft }
166 1.1 cgd
167 1.3 mycroft eval = 0;
168 1.3 mycroft rval = 0;
169 1.1 cgd
170 1.3 mycroft switch (cmd) {
171 1.1 cgd
172 1.3 mycroft case IPC_RMID:
173 1.1 cgd {
174 1.3 mycroft struct msg *msghdr;
175 1.8 mycroft if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_M)))
176 1.7 mycroft return(eval);
177 1.3 mycroft /* Free the message headers */
178 1.3 mycroft msghdr = msqptr->msg_first;
179 1.3 mycroft while (msghdr != NULL) {
180 1.3 mycroft struct msg *msghdr_tmp;
181 1.3 mycroft
182 1.3 mycroft /* Free the segments of each message */
183 1.3 mycroft msqptr->msg_cbytes -= msghdr->msg_ts;
184 1.5 mycroft msqptr->msg_qnum--;
185 1.3 mycroft msghdr_tmp = msghdr;
186 1.3 mycroft msghdr = msghdr->msg_next;
187 1.3 mycroft msg_freehdr(msghdr_tmp);
188 1.3 mycroft }
189 1.1 cgd
190 1.3 mycroft if (msqptr->msg_cbytes != 0)
191 1.3 mycroft panic("msg_cbytes is screwed up");
192 1.3 mycroft if (msqptr->msg_qnum != 0)
193 1.3 mycroft panic("msg_qnum is screwed up");
194 1.1 cgd
195 1.3 mycroft msqptr->msg_qbytes = 0; /* Mark it as free */
196 1.1 cgd
197 1.3 mycroft wakeup((caddr_t)msqptr);
198 1.1 cgd }
199 1.1 cgd
200 1.3 mycroft break;
201 1.1 cgd
202 1.3 mycroft case IPC_SET:
203 1.8 mycroft if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_M)))
204 1.7 mycroft return(eval);
205 1.3 mycroft if ((eval = copyin(user_msqptr, &msqbuf, sizeof(msqbuf))) != 0)
206 1.3 mycroft return(eval);
207 1.3 mycroft if (msqbuf.msg_qbytes > msqptr->msg_qbytes && cred->cr_uid != 0)
208 1.3 mycroft return(EPERM);
209 1.3 mycroft if (msqbuf.msg_qbytes > msginfo.msgmnb) {
210 1.1 cgd #ifdef MSG_DEBUG_OK
211 1.3 mycroft printf("can't increase msg_qbytes beyond %d (truncating)\n",
212 1.3 mycroft msginfo.msgmnb);
213 1.1 cgd #endif
214 1.3 mycroft msqbuf.msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */
215 1.3 mycroft }
216 1.3 mycroft if (msqbuf.msg_qbytes == 0) {
217 1.1 cgd #ifdef MSG_DEBUG_OK
218 1.3 mycroft printf("can't reduce msg_qbytes to 0\n");
219 1.1 cgd #endif
220 1.3 mycroft return(EINVAL); /* non-standard errno! */
221 1.3 mycroft }
222 1.3 mycroft msqptr->msg_perm.uid = msqbuf.msg_perm.uid; /* change the owner */
223 1.3 mycroft msqptr->msg_perm.gid = msqbuf.msg_perm.gid; /* change the owner */
224 1.3 mycroft msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) |
225 1.3 mycroft (msqbuf.msg_perm.mode & 0777);
226 1.3 mycroft msqptr->msg_qbytes = msqbuf.msg_qbytes;
227 1.3 mycroft msqptr->msg_ctime = time.tv_sec;
228 1.3 mycroft break;
229 1.1 cgd
230 1.3 mycroft case IPC_STAT:
231 1.6 hpeyerl if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_R))) {
232 1.1 cgd #ifdef MSG_DEBUG_OK
233 1.3 mycroft printf("requester doesn't have read access\n");
234 1.1 cgd #endif
235 1.3 mycroft return(eval);
236 1.3 mycroft }
237 1.3 mycroft eval = copyout((caddr_t)msqptr, user_msqptr,
238 1.3 mycroft sizeof(struct msqid_ds));
239 1.3 mycroft break;
240 1.1 cgd
241 1.3 mycroft default:
242 1.1 cgd #ifdef MSG_DEBUG_OK
243 1.3 mycroft printf("invalid command %d\n", cmd);
244 1.1 cgd #endif
245 1.3 mycroft return(EINVAL);
246 1.3 mycroft }
247 1.3 mycroft
248 1.3 mycroft if (eval == 0)
249 1.3 mycroft *retval = rval;
250 1.3 mycroft return(eval);
251 1.1 cgd }
252 1.1 cgd
253 1.1 cgd int
254 1.1 cgd msgget(p, uap, retval)
255 1.1 cgd struct proc *p;
256 1.10 cgd register struct msgget_args /* {
257 1.10 cgd syscallarg(key_t) key;
258 1.10 cgd syscallarg(int) msgflg;
259 1.10 cgd } */ *uap;
260 1.10 cgd register_t *retval;
261 1.1 cgd {
262 1.3 mycroft int msqid, eval;
263 1.10 cgd int key = SCARG(uap, key);
264 1.10 cgd int msgflg = SCARG(uap, msgflg);
265 1.3 mycroft struct ucred *cred = p->p_ucred;
266 1.3 mycroft register struct msqid_ds *msqptr;
267 1.1 cgd
268 1.1 cgd #ifdef MSG_DEBUG_OK
269 1.3 mycroft printf("msgget(0x%x, 0%o)\n", key, msgflg);
270 1.1 cgd #endif
271 1.1 cgd
272 1.5 mycroft if (key != IPC_PRIVATE) {
273 1.5 mycroft for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
274 1.3 mycroft msqptr = &msqids[msqid];
275 1.3 mycroft if (msqptr->msg_qbytes != 0 &&
276 1.3 mycroft msqptr->msg_perm.key == key)
277 1.3 mycroft break;
278 1.3 mycroft }
279 1.3 mycroft if (msqid < msginfo.msgmni) {
280 1.1 cgd #ifdef MSG_DEBUG_OK
281 1.3 mycroft printf("found public key\n");
282 1.1 cgd #endif
283 1.3 mycroft if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
284 1.1 cgd #ifdef MSG_DEBUG_OK
285 1.3 mycroft printf("not exclusive\n");
286 1.1 cgd #endif
287 1.3 mycroft return(EEXIST);
288 1.3 mycroft }
289 1.6 hpeyerl if ((eval = ipcperm(cred, &msqptr->msg_perm, msgflg & 0700 ))) {
290 1.1 cgd #ifdef MSG_DEBUG_OK
291 1.3 mycroft printf("requester doesn't have 0%o access\n",
292 1.3 mycroft msgflg & 0700);
293 1.1 cgd #endif
294 1.3 mycroft return(eval);
295 1.3 mycroft }
296 1.5 mycroft goto found;
297 1.3 mycroft }
298 1.1 cgd }
299 1.1 cgd
300 1.1 cgd #ifdef MSG_DEBUG_OK
301 1.5 mycroft printf("need to allocate the msqid_ds\n");
302 1.1 cgd #endif
303 1.5 mycroft if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
304 1.5 mycroft for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
305 1.5 mycroft /*
306 1.5 mycroft * Look for an unallocated and unlocked msqid_ds.
307 1.5 mycroft * msqid_ds's can be locked by msgsnd or msgrcv while
308 1.5 mycroft * they are copying the message in/out. We can't
309 1.5 mycroft * re-use the entry until they release it.
310 1.5 mycroft */
311 1.5 mycroft msqptr = &msqids[msqid];
312 1.5 mycroft if (msqptr->msg_qbytes == 0 &&
313 1.5 mycroft (msqptr->msg_perm.mode & MSG_LOCKED) == 0)
314 1.5 mycroft break;
315 1.5 mycroft }
316 1.5 mycroft if (msqid == msginfo.msgmni) {
317 1.1 cgd #ifdef MSG_DEBUG_OK
318 1.5 mycroft printf("no more msqid_ds's available\n");
319 1.1 cgd #endif
320 1.5 mycroft return(ENOSPC);
321 1.5 mycroft }
322 1.1 cgd #ifdef MSG_DEBUG_OK
323 1.5 mycroft printf("msqid %d is available\n", msqid);
324 1.3 mycroft #endif
325 1.5 mycroft msqptr->msg_perm.key = key;
326 1.5 mycroft msqptr->msg_perm.cuid = cred->cr_uid;
327 1.5 mycroft msqptr->msg_perm.uid = cred->cr_uid;
328 1.5 mycroft msqptr->msg_perm.cgid = cred->cr_gid;
329 1.5 mycroft msqptr->msg_perm.gid = cred->cr_gid;
330 1.5 mycroft msqptr->msg_perm.mode = (msgflg & 0777);
331 1.5 mycroft /* Make sure that the returned msqid is unique */
332 1.5 mycroft msqptr->msg_perm.seq++;
333 1.5 mycroft msqptr->msg_first = NULL;
334 1.5 mycroft msqptr->msg_last = NULL;
335 1.5 mycroft msqptr->msg_cbytes = 0;
336 1.5 mycroft msqptr->msg_qnum = 0;
337 1.5 mycroft msqptr->msg_qbytes = msginfo.msgmnb;
338 1.5 mycroft msqptr->msg_lspid = 0;
339 1.5 mycroft msqptr->msg_lrpid = 0;
340 1.5 mycroft msqptr->msg_stime = 0;
341 1.5 mycroft msqptr->msg_rtime = 0;
342 1.5 mycroft msqptr->msg_ctime = time.tv_sec;
343 1.5 mycroft } else {
344 1.1 cgd #ifdef MSG_DEBUG_OK
345 1.5 mycroft printf("didn't find it and wasn't asked to create it\n");
346 1.1 cgd #endif
347 1.5 mycroft return(ENOENT);
348 1.1 cgd }
349 1.1 cgd
350 1.5 mycroft found:
351 1.3 mycroft /* Construct the unique msqid */
352 1.3 mycroft *retval = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm);
353 1.3 mycroft return(0);
354 1.1 cgd }
355 1.1 cgd
356 1.1 cgd int
357 1.1 cgd msgsnd(p, uap, retval)
358 1.1 cgd struct proc *p;
359 1.10 cgd register struct msgsnd_args /* {
360 1.10 cgd syscallarg(int) msqid;
361 1.10 cgd syscallarg(void *) msgp;
362 1.10 cgd syscallarg(size_t) msgsz;
363 1.10 cgd syscallarg(int) msgflg;
364 1.10 cgd } */ *uap;
365 1.10 cgd register_t *retval;
366 1.1 cgd {
367 1.10 cgd int msqid = SCARG(uap, msqid);
368 1.10 cgd char *user_msgp = SCARG(uap, msgp);
369 1.10 cgd size_t msgsz = SCARG(uap, msgsz);
370 1.10 cgd int msgflg = SCARG(uap, msgflg);
371 1.3 mycroft int segs_needed, eval;
372 1.3 mycroft struct ucred *cred = p->p_ucred;
373 1.3 mycroft register struct msqid_ds *msqptr;
374 1.3 mycroft register struct msg *msghdr;
375 1.3 mycroft short next;
376 1.1 cgd
377 1.1 cgd #ifdef MSG_DEBUG_OK
378 1.3 mycroft printf("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz,
379 1.3 mycroft msgflg);
380 1.1 cgd #endif
381 1.1 cgd
382 1.3 mycroft msqid = IPCID_TO_IX(msqid);
383 1.1 cgd
384 1.3 mycroft if (msqid < 0 || msqid >= msginfo.msgmni) {
385 1.1 cgd #ifdef MSG_DEBUG_OK
386 1.3 mycroft printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
387 1.3 mycroft msginfo.msgmni);
388 1.1 cgd #endif
389 1.3 mycroft return(EINVAL);
390 1.3 mycroft }
391 1.1 cgd
392 1.3 mycroft msqptr = &msqids[msqid];
393 1.3 mycroft if (msqptr->msg_qbytes == 0) {
394 1.1 cgd #ifdef MSG_DEBUG_OK
395 1.3 mycroft printf("no such message queue id\n");
396 1.1 cgd #endif
397 1.3 mycroft return(EINVAL);
398 1.3 mycroft }
399 1.10 cgd if (msqptr->msg_perm.seq != IPCID_TO_SEQ(SCARG(uap, msqid))) {
400 1.1 cgd #ifdef MSG_DEBUG_OK
401 1.3 mycroft printf("wrong sequence number\n");
402 1.1 cgd #endif
403 1.3 mycroft return(EINVAL);
404 1.3 mycroft }
405 1.1 cgd
406 1.6 hpeyerl if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_W))) {
407 1.1 cgd #ifdef MSG_DEBUG_OK
408 1.3 mycroft printf("requester doesn't have write access\n");
409 1.1 cgd #endif
410 1.3 mycroft return(eval);
411 1.3 mycroft }
412 1.1 cgd
413 1.3 mycroft segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
414 1.1 cgd #ifdef MSG_DEBUG_OK
415 1.3 mycroft printf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz,
416 1.3 mycroft segs_needed);
417 1.1 cgd #endif
418 1.3 mycroft for (;;) {
419 1.3 mycroft int need_more_resources = 0;
420 1.1 cgd
421 1.3 mycroft /*
422 1.3 mycroft * check msgsz
423 1.3 mycroft * (inside this loop in case msg_qbytes changes while we sleep)
424 1.3 mycroft */
425 1.1 cgd
426 1.3 mycroft if (msgsz < 0 || msgsz > msqptr->msg_qbytes) {
427 1.1 cgd #ifdef MSG_DEBUG_OK
428 1.3 mycroft printf("msgsz > msqptr->msg_qbytes\n");
429 1.1 cgd #endif
430 1.3 mycroft return(EINVAL);
431 1.3 mycroft }
432 1.1 cgd
433 1.3 mycroft if (msqptr->msg_perm.mode & MSG_LOCKED) {
434 1.1 cgd #ifdef MSG_DEBUG_OK
435 1.3 mycroft printf("msqid is locked\n");
436 1.1 cgd #endif
437 1.3 mycroft need_more_resources = 1;
438 1.3 mycroft }
439 1.3 mycroft if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) {
440 1.1 cgd #ifdef MSG_DEBUG_OK
441 1.3 mycroft printf("msgsz + msg_cbytes > msg_qbytes\n");
442 1.1 cgd #endif
443 1.3 mycroft need_more_resources = 1;
444 1.3 mycroft }
445 1.3 mycroft if (segs_needed > nfree_msgmaps) {
446 1.1 cgd #ifdef MSG_DEBUG_OK
447 1.3 mycroft printf("segs_needed > nfree_msgmaps\n");
448 1.1 cgd #endif
449 1.3 mycroft need_more_resources = 1;
450 1.3 mycroft }
451 1.3 mycroft if (free_msghdrs == NULL) {
452 1.1 cgd #ifdef MSG_DEBUG_OK
453 1.3 mycroft printf("no more msghdrs\n");
454 1.1 cgd #endif
455 1.3 mycroft need_more_resources = 1;
456 1.3 mycroft }
457 1.1 cgd
458 1.3 mycroft if (need_more_resources) {
459 1.3 mycroft int we_own_it;
460 1.1 cgd
461 1.3 mycroft if ((msgflg & IPC_NOWAIT) != 0) {
462 1.1 cgd #ifdef MSG_DEBUG_OK
463 1.3 mycroft printf("need more resources but caller doesn't want to wait\n");
464 1.1 cgd #endif
465 1.3 mycroft return(EAGAIN);
466 1.3 mycroft }
467 1.1 cgd
468 1.3 mycroft if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) {
469 1.1 cgd #ifdef MSG_DEBUG_OK
470 1.3 mycroft printf("we don't own the msqid_ds\n");
471 1.1 cgd #endif
472 1.3 mycroft we_own_it = 0;
473 1.3 mycroft } else {
474 1.3 mycroft /* Force later arrivals to wait for our
475 1.3 mycroft request */
476 1.1 cgd #ifdef MSG_DEBUG_OK
477 1.3 mycroft printf("we own the msqid_ds\n");
478 1.1 cgd #endif
479 1.3 mycroft msqptr->msg_perm.mode |= MSG_LOCKED;
480 1.3 mycroft we_own_it = 1;
481 1.3 mycroft }
482 1.1 cgd #ifdef MSG_DEBUG_OK
483 1.3 mycroft printf("goodnight\n");
484 1.1 cgd #endif
485 1.3 mycroft eval = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH,
486 1.3 mycroft "msgwait", 0);
487 1.1 cgd #ifdef MSG_DEBUG_OK
488 1.3 mycroft printf("good morning, eval=%d\n", eval);
489 1.1 cgd #endif
490 1.3 mycroft if (we_own_it)
491 1.3 mycroft msqptr->msg_perm.mode &= ~MSG_LOCKED;
492 1.3 mycroft if (eval != 0) {
493 1.1 cgd #ifdef MSG_DEBUG_OK
494 1.3 mycroft printf("msgsnd: interrupted system call\n");
495 1.1 cgd #endif
496 1.3 mycroft return(EINTR);
497 1.3 mycroft }
498 1.1 cgd
499 1.3 mycroft /*
500 1.3 mycroft * Make sure that the msq queue still exists
501 1.3 mycroft */
502 1.1 cgd
503 1.3 mycroft if (msqptr->msg_qbytes == 0) {
504 1.1 cgd #ifdef MSG_DEBUG_OK
505 1.3 mycroft printf("msqid deleted\n");
506 1.1 cgd #endif
507 1.3 mycroft /* The SVID says to return EIDRM. */
508 1.1 cgd #ifdef EIDRM
509 1.3 mycroft return(EIDRM);
510 1.1 cgd #else
511 1.3 mycroft /* Unfortunately, BSD doesn't define that code
512 1.3 mycroft yet! */
513 1.3 mycroft return(EINVAL);
514 1.1 cgd #endif
515 1.3 mycroft }
516 1.1 cgd
517 1.3 mycroft } else {
518 1.1 cgd #ifdef MSG_DEBUG_OK
519 1.3 mycroft printf("got all the resources that we need\n");
520 1.1 cgd #endif
521 1.3 mycroft break;
522 1.3 mycroft }
523 1.1 cgd }
524 1.1 cgd
525 1.3 mycroft /*
526 1.3 mycroft * We have the resources that we need.
527 1.3 mycroft * Make sure!
528 1.3 mycroft */
529 1.1 cgd
530 1.3 mycroft if (msqptr->msg_perm.mode & MSG_LOCKED)
531 1.3 mycroft panic("msg_perm.mode & MSG_LOCKED");
532 1.3 mycroft if (segs_needed > nfree_msgmaps)
533 1.3 mycroft panic("segs_needed > nfree_msgmaps");
534 1.3 mycroft if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes)
535 1.3 mycroft panic("msgsz + msg_cbytes > msg_qbytes");
536 1.3 mycroft if (free_msghdrs == NULL)
537 1.3 mycroft panic("no more msghdrs");
538 1.1 cgd
539 1.3 mycroft /*
540 1.3 mycroft * Re-lock the msqid_ds in case we page-fault when copying in the
541 1.3 mycroft * message
542 1.3 mycroft */
543 1.1 cgd
544 1.3 mycroft if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0)
545 1.3 mycroft panic("msqid_ds is already locked");
546 1.3 mycroft msqptr->msg_perm.mode |= MSG_LOCKED;
547 1.1 cgd
548 1.3 mycroft /*
549 1.3 mycroft * Allocate a message header
550 1.3 mycroft */
551 1.1 cgd
552 1.3 mycroft msghdr = free_msghdrs;
553 1.3 mycroft free_msghdrs = msghdr->msg_next;
554 1.3 mycroft msghdr->msg_spot = -1;
555 1.3 mycroft msghdr->msg_ts = msgsz;
556 1.1 cgd
557 1.3 mycroft /*
558 1.3 mycroft * Allocate space for the message
559 1.3 mycroft */
560 1.1 cgd
561 1.3 mycroft while (segs_needed > 0) {
562 1.3 mycroft if (nfree_msgmaps <= 0)
563 1.3 mycroft panic("not enough msgmaps");
564 1.3 mycroft if (free_msgmaps == -1)
565 1.3 mycroft panic("nil free_msgmaps");
566 1.3 mycroft next = free_msgmaps;
567 1.3 mycroft if (next <= -1)
568 1.3 mycroft panic("next too low #1");
569 1.3 mycroft if (next >= msginfo.msgseg)
570 1.3 mycroft panic("next out of range #1");
571 1.3 mycroft #ifdef MSG_DEBUG_OK
572 1.3 mycroft printf("allocating segment %d to message\n", next);
573 1.3 mycroft #endif
574 1.3 mycroft free_msgmaps = msgmaps[next].next;
575 1.5 mycroft nfree_msgmaps--;
576 1.3 mycroft msgmaps[next].next = msghdr->msg_spot;
577 1.3 mycroft msghdr->msg_spot = next;
578 1.5 mycroft segs_needed--;
579 1.1 cgd }
580 1.1 cgd
581 1.3 mycroft /*
582 1.3 mycroft * Copy in the message type
583 1.3 mycroft */
584 1.1 cgd
585 1.3 mycroft if ((eval = copyin(user_msgp, &msghdr->msg_type,
586 1.3 mycroft sizeof(msghdr->msg_type))) != 0) {
587 1.1 cgd #ifdef MSG_DEBUG_OK
588 1.3 mycroft printf("error %d copying the message type\n", eval);
589 1.1 cgd #endif
590 1.3 mycroft msg_freehdr(msghdr);
591 1.3 mycroft msqptr->msg_perm.mode &= ~MSG_LOCKED;
592 1.3 mycroft wakeup((caddr_t)msqptr);
593 1.3 mycroft return(eval);
594 1.3 mycroft }
595 1.3 mycroft user_msgp += sizeof(msghdr->msg_type);
596 1.1 cgd
597 1.3 mycroft /*
598 1.3 mycroft * Validate the message type
599 1.3 mycroft */
600 1.1 cgd
601 1.3 mycroft if (msghdr->msg_type < 1) {
602 1.3 mycroft msg_freehdr(msghdr);
603 1.3 mycroft msqptr->msg_perm.mode &= ~MSG_LOCKED;
604 1.3 mycroft wakeup((caddr_t)msqptr);
605 1.1 cgd #ifdef MSG_DEBUG_OK
606 1.3 mycroft printf("mtype (%d) < 1\n", msghdr->msg_type);
607 1.1 cgd #endif
608 1.3 mycroft return(EINVAL);
609 1.3 mycroft }
610 1.1 cgd
611 1.3 mycroft /*
612 1.3 mycroft * Copy in the message body
613 1.3 mycroft */
614 1.3 mycroft
615 1.3 mycroft next = msghdr->msg_spot;
616 1.3 mycroft while (msgsz > 0) {
617 1.3 mycroft size_t tlen;
618 1.3 mycroft if (msgsz > msginfo.msgssz)
619 1.3 mycroft tlen = msginfo.msgssz;
620 1.3 mycroft else
621 1.3 mycroft tlen = msgsz;
622 1.3 mycroft if (next <= -1)
623 1.3 mycroft panic("next too low #2");
624 1.3 mycroft if (next >= msginfo.msgseg)
625 1.3 mycroft panic("next out of range #2");
626 1.3 mycroft if ((eval = copyin(user_msgp, &msgpool[next * msginfo.msgssz],
627 1.3 mycroft tlen)) != 0) {
628 1.3 mycroft #ifdef MSG_DEBUG_OK
629 1.3 mycroft printf("error %d copying in message segment\n", eval);
630 1.3 mycroft #endif
631 1.3 mycroft msg_freehdr(msghdr);
632 1.3 mycroft msqptr->msg_perm.mode &= ~MSG_LOCKED;
633 1.3 mycroft wakeup((caddr_t)msqptr);
634 1.3 mycroft return(eval);
635 1.3 mycroft }
636 1.3 mycroft msgsz -= tlen;
637 1.3 mycroft user_msgp += tlen;
638 1.3 mycroft next = msgmaps[next].next;
639 1.1 cgd }
640 1.3 mycroft if (next != -1)
641 1.3 mycroft panic("didn't use all the msg segments");
642 1.1 cgd
643 1.3 mycroft /*
644 1.3 mycroft * We've got the message. Unlock the msqid_ds.
645 1.3 mycroft */
646 1.1 cgd
647 1.3 mycroft msqptr->msg_perm.mode &= ~MSG_LOCKED;
648 1.1 cgd
649 1.3 mycroft /*
650 1.3 mycroft * Make sure that the msqid_ds is still allocated.
651 1.3 mycroft */
652 1.1 cgd
653 1.3 mycroft if (msqptr->msg_qbytes == 0) {
654 1.3 mycroft msg_freehdr(msghdr);
655 1.3 mycroft wakeup((caddr_t)msqptr);
656 1.3 mycroft /* The SVID says to return EIDRM. */
657 1.1 cgd #ifdef EIDRM
658 1.3 mycroft return(EIDRM);
659 1.1 cgd #else
660 1.3 mycroft /* Unfortunately, BSD doesn't define that code yet! */
661 1.3 mycroft return(EINVAL);
662 1.1 cgd #endif
663 1.3 mycroft }
664 1.3 mycroft
665 1.3 mycroft /*
666 1.3 mycroft * Put the message into the queue
667 1.3 mycroft */
668 1.1 cgd
669 1.3 mycroft if (msqptr->msg_first == NULL) {
670 1.3 mycroft msqptr->msg_first = msghdr;
671 1.3 mycroft msqptr->msg_last = msghdr;
672 1.3 mycroft } else {
673 1.3 mycroft msqptr->msg_last->msg_next = msghdr;
674 1.3 mycroft msqptr->msg_last = msghdr;
675 1.3 mycroft }
676 1.3 mycroft msqptr->msg_last->msg_next = NULL;
677 1.3 mycroft
678 1.3 mycroft msqptr->msg_cbytes += msghdr->msg_ts;
679 1.5 mycroft msqptr->msg_qnum++;
680 1.3 mycroft msqptr->msg_lspid = p->p_pid;
681 1.3 mycroft msqptr->msg_stime = time.tv_sec;
682 1.3 mycroft
683 1.3 mycroft wakeup((caddr_t)msqptr);
684 1.3 mycroft *retval = 0;
685 1.3 mycroft return(0);
686 1.1 cgd }
687 1.1 cgd
688 1.1 cgd int
689 1.1 cgd msgrcv(p, uap, retval)
690 1.1 cgd struct proc *p;
691 1.10 cgd register struct msgrcv_args /* {
692 1.10 cgd syscallarg(int) msqid;
693 1.10 cgd syscallarg(void *) msgp;
694 1.10 cgd syscallarg(size_t) msgsz;
695 1.10 cgd syscallarg(long) msgtyp;
696 1.10 cgd syscallarg(int) msgflg;
697 1.10 cgd } */ *uap;
698 1.10 cgd register_t *retval;
699 1.1 cgd {
700 1.10 cgd int msqid = SCARG(uap, msqid);
701 1.10 cgd char *user_msgp = SCARG(uap, msgp);
702 1.10 cgd size_t msgsz = SCARG(uap, msgsz);
703 1.10 cgd long msgtyp = SCARG(uap, msgtyp);
704 1.10 cgd int msgflg = SCARG(uap, msgflg);
705 1.3 mycroft size_t len;
706 1.3 mycroft struct ucred *cred = p->p_ucred;
707 1.3 mycroft register struct msqid_ds *msqptr;
708 1.3 mycroft register struct msg *msghdr;
709 1.3 mycroft int eval;
710 1.3 mycroft short next;
711 1.1 cgd
712 1.1 cgd #ifdef MSG_DEBUG_OK
713 1.3 mycroft printf("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid, user_msgp,
714 1.3 mycroft msgsz, msgtyp, msgflg);
715 1.1 cgd #endif
716 1.1 cgd
717 1.3 mycroft msqid = IPCID_TO_IX(msqid);
718 1.1 cgd
719 1.3 mycroft if (msqid < 0 || msqid >= msginfo.msgmni) {
720 1.1 cgd #ifdef MSG_DEBUG_OK
721 1.3 mycroft printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
722 1.3 mycroft msginfo.msgmni);
723 1.1 cgd #endif
724 1.3 mycroft return(EINVAL);
725 1.3 mycroft }
726 1.1 cgd
727 1.3 mycroft msqptr = &msqids[msqid];
728 1.3 mycroft if (msqptr->msg_qbytes == 0) {
729 1.1 cgd #ifdef MSG_DEBUG_OK
730 1.3 mycroft printf("no such message queue id\n");
731 1.1 cgd #endif
732 1.3 mycroft return(EINVAL);
733 1.3 mycroft }
734 1.10 cgd if (msqptr->msg_perm.seq != IPCID_TO_SEQ(SCARG(uap, msqid))) {
735 1.1 cgd #ifdef MSG_DEBUG_OK
736 1.3 mycroft printf("wrong sequence number\n");
737 1.1 cgd #endif
738 1.3 mycroft return(EINVAL);
739 1.3 mycroft }
740 1.1 cgd
741 1.6 hpeyerl if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_R))) {
742 1.1 cgd #ifdef MSG_DEBUG_OK
743 1.3 mycroft printf("requester doesn't have read access\n");
744 1.1 cgd #endif
745 1.3 mycroft return(eval);
746 1.3 mycroft }
747 1.1 cgd
748 1.3 mycroft if (msgsz < 0) {
749 1.1 cgd #ifdef MSG_DEBUG_OK
750 1.3 mycroft printf("msgsz < 0\n");
751 1.1 cgd #endif
752 1.3 mycroft return(EINVAL);
753 1.3 mycroft }
754 1.1 cgd
755 1.3 mycroft msghdr = NULL;
756 1.3 mycroft while (msghdr == NULL) {
757 1.3 mycroft if (msgtyp == 0) {
758 1.3 mycroft msghdr = msqptr->msg_first;
759 1.3 mycroft if (msghdr != NULL) {
760 1.3 mycroft if (msgsz < msghdr->msg_ts &&
761 1.3 mycroft (msgflg & MSG_NOERROR) == 0) {
762 1.3 mycroft #ifdef MSG_DEBUG_OK
763 1.3 mycroft printf("first message on the queue is too big (want %d, got %d)\n",
764 1.3 mycroft msgsz, msghdr->msg_ts);
765 1.3 mycroft #endif
766 1.3 mycroft return(E2BIG);
767 1.3 mycroft }
768 1.3 mycroft if (msqptr->msg_first == msqptr->msg_last) {
769 1.3 mycroft msqptr->msg_first = NULL;
770 1.3 mycroft msqptr->msg_last = NULL;
771 1.3 mycroft } else {
772 1.3 mycroft msqptr->msg_first = msghdr->msg_next;
773 1.3 mycroft if (msqptr->msg_first == NULL)
774 1.3 mycroft panic("msg_first/last screwed up #1");
775 1.3 mycroft }
776 1.3 mycroft }
777 1.3 mycroft } else {
778 1.3 mycroft struct msg *previous;
779 1.3 mycroft struct msg **prev;
780 1.1 cgd
781 1.3 mycroft previous = NULL;
782 1.3 mycroft prev = &(msqptr->msg_first);
783 1.3 mycroft while ((msghdr = *prev) != NULL) {
784 1.3 mycroft /*
785 1.3 mycroft * Is this message's type an exact match or is
786 1.3 mycroft * this message's type less than or equal to
787 1.3 mycroft * the absolute value of a negative msgtyp?
788 1.3 mycroft * Note that the second half of this test can
789 1.3 mycroft * NEVER be true if msgtyp is positive since
790 1.3 mycroft * msg_type is always positive!
791 1.3 mycroft */
792 1.3 mycroft
793 1.3 mycroft if (msgtyp == msghdr->msg_type ||
794 1.3 mycroft msghdr->msg_type <= -msgtyp) {
795 1.3 mycroft #ifdef MSG_DEBUG_OK
796 1.3 mycroft printf("found message type %d, requested %d\n",
797 1.3 mycroft msghdr->msg_type, msgtyp);
798 1.3 mycroft #endif
799 1.3 mycroft if (msgsz < msghdr->msg_ts &&
800 1.3 mycroft (msgflg & MSG_NOERROR) == 0) {
801 1.3 mycroft #ifdef MSG_DEBUG_OK
802 1.3 mycroft printf("requested message on the queue is too big (want %d, got %d)\n",
803 1.3 mycroft msgsz, msghdr->msg_ts);
804 1.3 mycroft #endif
805 1.3 mycroft return(E2BIG);
806 1.3 mycroft }
807 1.3 mycroft *prev = msghdr->msg_next;
808 1.3 mycroft if (msghdr == msqptr->msg_last) {
809 1.3 mycroft if (previous == NULL) {
810 1.3 mycroft if (prev !=
811 1.3 mycroft &msqptr->msg_first)
812 1.3 mycroft panic("msg_first/last screwed up #2");
813 1.3 mycroft msqptr->msg_first =
814 1.3 mycroft NULL;
815 1.3 mycroft msqptr->msg_last =
816 1.3 mycroft NULL;
817 1.3 mycroft } else {
818 1.3 mycroft if (prev ==
819 1.3 mycroft &msqptr->msg_first)
820 1.3 mycroft panic("msg_first/last screwed up #3");
821 1.3 mycroft msqptr->msg_last =
822 1.3 mycroft previous;
823 1.3 mycroft }
824 1.3 mycroft }
825 1.3 mycroft break;
826 1.3 mycroft }
827 1.3 mycroft previous = msghdr;
828 1.3 mycroft prev = &(msghdr->msg_next);
829 1.3 mycroft }
830 1.1 cgd }
831 1.1 cgd
832 1.3 mycroft /*
833 1.3 mycroft * We've either extracted the msghdr for the appropriate
834 1.3 mycroft * message or there isn't one.
835 1.3 mycroft * If there is one then bail out of this loop.
836 1.3 mycroft */
837 1.1 cgd
838 1.3 mycroft if (msghdr != NULL)
839 1.3 mycroft break;
840 1.1 cgd
841 1.1 cgd /*
842 1.3 mycroft * Hmph! No message found. Does the user want to wait?
843 1.1 cgd */
844 1.1 cgd
845 1.3 mycroft if ((msgflg & IPC_NOWAIT) != 0) {
846 1.1 cgd #ifdef MSG_DEBUG_OK
847 1.3 mycroft printf("no appropriate message found (msgtyp=%d)\n",
848 1.3 mycroft msgtyp);
849 1.1 cgd #endif
850 1.3 mycroft /* The SVID says to return ENOMSG. */
851 1.1 cgd #ifdef ENOMSG
852 1.3 mycroft return(ENOMSG);
853 1.1 cgd #else
854 1.3 mycroft /* Unfortunately, BSD doesn't define that code yet! */
855 1.3 mycroft return(EAGAIN);
856 1.1 cgd #endif
857 1.3 mycroft }
858 1.1 cgd
859 1.3 mycroft /*
860 1.3 mycroft * Wait for something to happen
861 1.3 mycroft */
862 1.1 cgd
863 1.1 cgd #ifdef MSG_DEBUG_OK
864 1.3 mycroft printf("msgrcv: goodnight\n");
865 1.1 cgd #endif
866 1.3 mycroft eval = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH, "msgwait",
867 1.3 mycroft 0);
868 1.1 cgd #ifdef MSG_DEBUG_OK
869 1.3 mycroft printf("msgrcv: good morning (eval=%d)\n", eval);
870 1.1 cgd #endif
871 1.1 cgd
872 1.3 mycroft if (eval != 0) {
873 1.1 cgd #ifdef MSG_DEBUG_OK
874 1.3 mycroft printf("msgsnd: interrupted system call\n");
875 1.1 cgd #endif
876 1.3 mycroft return(EINTR);
877 1.3 mycroft }
878 1.1 cgd
879 1.3 mycroft /*
880 1.3 mycroft * Make sure that the msq queue still exists
881 1.3 mycroft */
882 1.1 cgd
883 1.3 mycroft if (msqptr->msg_qbytes == 0 ||
884 1.10 cgd msqptr->msg_perm.seq != IPCID_TO_SEQ(SCARG(uap, msqid))) {
885 1.1 cgd #ifdef MSG_DEBUG_OK
886 1.3 mycroft printf("msqid deleted\n");
887 1.1 cgd #endif
888 1.3 mycroft /* The SVID says to return EIDRM. */
889 1.1 cgd #ifdef EIDRM
890 1.3 mycroft return(EIDRM);
891 1.1 cgd #else
892 1.3 mycroft /* Unfortunately, BSD doesn't define that code yet! */
893 1.3 mycroft return(EINVAL);
894 1.1 cgd #endif
895 1.3 mycroft }
896 1.1 cgd }
897 1.1 cgd
898 1.3 mycroft /*
899 1.3 mycroft * Return the message to the user.
900 1.3 mycroft *
901 1.3 mycroft * First, do the bookkeeping (before we risk being interrupted).
902 1.3 mycroft */
903 1.1 cgd
904 1.3 mycroft msqptr->msg_cbytes -= msghdr->msg_ts;
905 1.5 mycroft msqptr->msg_qnum--;
906 1.3 mycroft msqptr->msg_lrpid = p->p_pid;
907 1.3 mycroft msqptr->msg_rtime = time.tv_sec;
908 1.1 cgd
909 1.3 mycroft /*
910 1.3 mycroft * Make msgsz the actual amount that we'll be returning.
911 1.3 mycroft * Note that this effectively truncates the message if it is too long
912 1.3 mycroft * (since msgsz is never increased).
913 1.3 mycroft */
914 1.1 cgd
915 1.1 cgd #ifdef MSG_DEBUG_OK
916 1.3 mycroft printf("found a message, msgsz=%d, msg_ts=%d\n", msgsz,
917 1.3 mycroft msghdr->msg_ts);
918 1.1 cgd #endif
919 1.3 mycroft if (msgsz > msghdr->msg_ts)
920 1.3 mycroft msgsz = msghdr->msg_ts;
921 1.1 cgd
922 1.3 mycroft /*
923 1.3 mycroft * Return the type to the user.
924 1.3 mycroft */
925 1.1 cgd
926 1.3 mycroft eval = copyout((caddr_t)&(msghdr->msg_type), user_msgp,
927 1.3 mycroft sizeof(msghdr->msg_type));
928 1.3 mycroft if (eval != 0) {
929 1.1 cgd #ifdef MSG_DEBUG_OK
930 1.3 mycroft printf("error (%d) copying out message type\n", eval);
931 1.1 cgd #endif
932 1.3 mycroft msg_freehdr(msghdr);
933 1.3 mycroft wakeup((caddr_t)msqptr);
934 1.3 mycroft return(eval);
935 1.3 mycroft }
936 1.3 mycroft user_msgp += sizeof(msghdr->msg_type);
937 1.3 mycroft
938 1.3 mycroft /*
939 1.3 mycroft * Return the segments to the user
940 1.3 mycroft */
941 1.1 cgd
942 1.3 mycroft next = msghdr->msg_spot;
943 1.3 mycroft for (len = 0; len < msgsz; len += msginfo.msgssz) {
944 1.3 mycroft size_t tlen;
945 1.3 mycroft
946 1.3 mycroft if (msgsz > msginfo.msgssz)
947 1.3 mycroft tlen = msginfo.msgssz;
948 1.3 mycroft else
949 1.3 mycroft tlen = msgsz;
950 1.3 mycroft if (next <= -1)
951 1.3 mycroft panic("next too low #3");
952 1.3 mycroft if (next >= msginfo.msgseg)
953 1.3 mycroft panic("next out of range #3");
954 1.3 mycroft eval = copyout((caddr_t)&msgpool[next * msginfo.msgssz],
955 1.3 mycroft user_msgp, tlen);
956 1.3 mycroft if (eval != 0) {
957 1.3 mycroft #ifdef MSG_DEBUG_OK
958 1.3 mycroft printf("error (%d) copying out message segment\n",
959 1.3 mycroft eval);
960 1.3 mycroft #endif
961 1.3 mycroft msg_freehdr(msghdr);
962 1.3 mycroft wakeup((caddr_t)msqptr);
963 1.3 mycroft return(eval);
964 1.3 mycroft }
965 1.3 mycroft user_msgp += tlen;
966 1.3 mycroft next = msgmaps[next].next;
967 1.1 cgd }
968 1.1 cgd
969 1.3 mycroft /*
970 1.3 mycroft * Done, return the actual number of bytes copied out.
971 1.3 mycroft */
972 1.1 cgd
973 1.3 mycroft msg_freehdr(msghdr);
974 1.3 mycroft wakeup((caddr_t)msqptr);
975 1.3 mycroft *retval = msgsz;
976 1.3 mycroft return(0);
977 1.1 cgd }
978 1.10 cgd
979 1.10 cgd #if defined(COMPAT_10) && !defined(alpha)
980 1.10 cgd int
981 1.10 cgd compat_10_msgsys(p, uap, retval)
982 1.10 cgd struct caller *p;
983 1.10 cgd struct compat_10_msgsys_args /* {
984 1.10 cgd syscallarg(int) which;
985 1.10 cgd syscallarg(int) a2;
986 1.10 cgd syscallarg(int) a3;
987 1.10 cgd syscallarg(int) a4;
988 1.10 cgd syscallarg(int) a5;
989 1.10 cgd syscallarg(int) a6;
990 1.10 cgd } */ *uap;
991 1.10 cgd register_t *retval;
992 1.10 cgd {
993 1.10 cgd struct msgctl_args {
994 1.10 cgd syscallarg(int) msqid;
995 1.10 cgd syscallarg(int) cmd;
996 1.10 cgd syscallarg(struct msqid_ds *) buf;
997 1.10 cgd } msgctl_args;
998 1.10 cgd struct msgget_args {
999 1.10 cgd syscallarg(key_t) key;
1000 1.10 cgd syscallarg(int) msgflg;
1001 1.10 cgd } msgget_args;
1002 1.10 cgd struct msgsnd_args {
1003 1.10 cgd syscallarg(int) msqid;
1004 1.10 cgd syscallarg(void *) msgp;
1005 1.10 cgd syscallarg(size_t) msgsz;
1006 1.10 cgd syscallarg(int) msgflg;
1007 1.10 cgd } msgsnd_args;
1008 1.10 cgd struct msgrcv_args {
1009 1.10 cgd syscallarg(int) msqid;
1010 1.10 cgd syscallarg(void *) msgp;
1011 1.10 cgd syscallarg(size_t) msgsz;
1012 1.10 cgd syscallarg(long) msgtyp;
1013 1.10 cgd syscallarg(int) msgflg;
1014 1.10 cgd } msgrcv_args;
1015 1.10 cgd
1016 1.10 cgd switch (SCARG(uap, which)) {
1017 1.10 cgd case 0: /* msgctl()*/
1018 1.10 cgd SCARG(&msgctl_args, msqid) = SCARG(uap, a2);
1019 1.10 cgd SCARG(&msgctl_args, cmd) = SCARG(uap, a3);
1020 1.10 cgd SCARG(&msgctl_args, buf) =
1021 1.10 cgd (struct msqid_ds *)SCARG(uap, a4);
1022 1.10 cgd return (msgctl(p, &msgctl_args, retval));
1023 1.10 cgd
1024 1.10 cgd case 1: /* msgget() */
1025 1.10 cgd SCARG(&msgctl_args, key) = SCARG(uap, a2);
1026 1.10 cgd SCARG(&msgctl_args, msgflg) = SCARG(uap, a3);
1027 1.10 cgd return (msgget(p, &msgget_args, retval));
1028 1.10 cgd
1029 1.10 cgd case 2: /* msgsnd() */
1030 1.10 cgd SCARG(&msgsnd_args, msqid) = SCARG(uap, a2);
1031 1.10 cgd SCARG(&msgsnd_args, msgp) = (void *)SCARG(uap, a3);
1032 1.10 cgd SCARG(&msgsnd_args, msgsz) = SCARG(uap, a4);
1033 1.10 cgd SCARG(&msgsnd_args, msgflg) = SCARG(uap, a5);
1034 1.10 cgd return (msgget(p, &msgget_args, retval));
1035 1.10 cgd
1036 1.10 cgd case 3: /* msgrcv() */
1037 1.10 cgd SCARG(&msgsnd_args, msqid) = SCARG(uap, a2);
1038 1.10 cgd SCARG(&msgsnd_args, msgp) = (void *)SCARG(uap, a3);
1039 1.10 cgd SCARG(&msgsnd_args, msgsz) = SCARG(uap, a4);
1040 1.10 cgd SCARG(&msgsnd_args, msgtyp) = SCARG(uap, a5);
1041 1.10 cgd SCARG(&msgsnd_args, msgflg) = SCARG(uap, a6);
1042 1.10 cgd return (msgget(p, &msgget_args, retval));
1043 1.10 cgd
1044 1.10 cgd default:
1045 1.10 cgd return (EINVAL);
1046 1.10 cgd }
1047 1.10 cgd }
1048 1.10 cgd #endif /* defined(COMPAT_10) && !defined(alpha) */
1049