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