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