1c3aac50fSPeter Wemm /* $FreeBSD$ */ 23d903220SDoug Rabson 33d903220SDoug Rabson /* 43d903220SDoug Rabson * Implementation of SVID messages 53d903220SDoug Rabson * 63d903220SDoug Rabson * Author: Daniel Boulet 73d903220SDoug Rabson * 83d903220SDoug Rabson * Copyright 1993 Daniel Boulet and RTMX Inc. 93d903220SDoug Rabson * 103d903220SDoug Rabson * This system call was implemented by Daniel Boulet under contract from RTMX. 113d903220SDoug Rabson * 123d903220SDoug Rabson * Redistribution and use in source forms, with and without modification, 133d903220SDoug Rabson * are permitted provided that this entire comment appears intact. 143d903220SDoug Rabson * 153d903220SDoug Rabson * Redistribution in binary form may occur without any restrictions. 163d903220SDoug Rabson * Obviously, it would be nice if you gave credit where credit is due 173d903220SDoug Rabson * but requiring it would be too onerous. 183d903220SDoug Rabson * 193d903220SDoug Rabson * This software is provided ``AS IS'' without any warranties of any kind. 203d903220SDoug Rabson */ 213d903220SDoug Rabson 22ab063af9SPeter Wemm #include "opt_sysvipc.h" 23ab063af9SPeter Wemm 243d903220SDoug Rabson #include <sys/param.h> 253d903220SDoug Rabson #include <sys/systm.h> 26725db531SBruce Evans #include <sys/sysproto.h> 273d903220SDoug Rabson #include <sys/kernel.h> 283d903220SDoug Rabson #include <sys/proc.h> 293d903220SDoug Rabson #include <sys/msg.h> 3078525ce3SAlfred Perlstein #include <sys/syscall.h> 31725db531SBruce Evans #include <sys/sysent.h> 32ab063af9SPeter Wemm #include <sys/sysctl.h> 33ab063af9SPeter Wemm #include <sys/malloc.h> 34cb1f0db9SRobert Watson #include <sys/jail.h> 35ab063af9SPeter Wemm 36ab063af9SPeter Wemm static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues"); 373d903220SDoug Rabson 3878525ce3SAlfred Perlstein static void msginit __P((void)); 3978525ce3SAlfred Perlstein static int msgunload __P((void)); 4078525ce3SAlfred Perlstein static int sysvmsg_modload __P((struct module *, int, void *)); 412b14f991SJulian Elischer 423d903220SDoug Rabson #define MSG_DEBUG 433d903220SDoug Rabson #undef MSG_DEBUG_OK 443d903220SDoug Rabson 45725db531SBruce Evans static void msg_freehdr __P((struct msg *msghdr)); 463d903220SDoug Rabson 47725db531SBruce Evans /* XXX casting to (sy_call_t *) is bogus, as usual. */ 4887b6de2bSPoul-Henning Kamp static sy_call_t *msgcalls[] = { 49725db531SBruce Evans (sy_call_t *)msgctl, (sy_call_t *)msgget, 50725db531SBruce Evans (sy_call_t *)msgsnd, (sy_call_t *)msgrcv 51725db531SBruce Evans }; 523d903220SDoug Rabson 53ab063af9SPeter Wemm struct msg { 54ab063af9SPeter Wemm struct msg *msg_next; /* next msg in the chain */ 55ab063af9SPeter Wemm long msg_type; /* type of this message */ 56ab063af9SPeter Wemm /* >0 -> type of this message */ 57ab063af9SPeter Wemm /* 0 -> free header */ 58ab063af9SPeter Wemm u_short msg_ts; /* size of this message */ 59ab063af9SPeter Wemm short msg_spot; /* location of start of msg in buffer */ 60ab063af9SPeter Wemm }; 61ab063af9SPeter Wemm 62ab063af9SPeter Wemm 63ab063af9SPeter Wemm #ifndef MSGSSZ 64ab063af9SPeter Wemm #define MSGSSZ 8 /* Each segment must be 2^N long */ 65ab063af9SPeter Wemm #endif 66ab063af9SPeter Wemm #ifndef MSGSEG 67ab063af9SPeter Wemm #define MSGSEG 2048 /* must be less than 32767 */ 68ab063af9SPeter Wemm #endif 69ab063af9SPeter Wemm #define MSGMAX (MSGSSZ*MSGSEG) 70ab063af9SPeter Wemm #ifndef MSGMNB 71ab063af9SPeter Wemm #define MSGMNB 2048 /* max # of bytes in a queue */ 72ab063af9SPeter Wemm #endif 73ab063af9SPeter Wemm #ifndef MSGMNI 74ab063af9SPeter Wemm #define MSGMNI 40 75ab063af9SPeter Wemm #endif 76ab063af9SPeter Wemm #ifndef MSGTQL 77ab063af9SPeter Wemm #define MSGTQL 40 78ab063af9SPeter Wemm #endif 79ab063af9SPeter Wemm 80ab063af9SPeter Wemm /* 81ab063af9SPeter Wemm * Based on the configuration parameters described in an SVR2 (yes, two) 82ab063af9SPeter Wemm * config(1m) man page. 83ab063af9SPeter Wemm * 84ab063af9SPeter Wemm * Each message is broken up and stored in segments that are msgssz bytes 85ab063af9SPeter Wemm * long. For efficiency reasons, this should be a power of two. Also, 86ab063af9SPeter Wemm * it doesn't make sense if it is less than 8 or greater than about 256. 87ab063af9SPeter Wemm * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of 88ab063af9SPeter Wemm * two between 8 and 1024 inclusive (and panic's if it isn't). 89ab063af9SPeter Wemm */ 90ab063af9SPeter Wemm struct msginfo msginfo = { 91ab063af9SPeter Wemm MSGMAX, /* max chars in a message */ 92ab063af9SPeter Wemm MSGMNI, /* # of message queue identifiers */ 93ab063af9SPeter Wemm MSGMNB, /* max chars in a queue */ 94ab063af9SPeter Wemm MSGTQL, /* max messages in system */ 95ab063af9SPeter Wemm MSGSSZ, /* size of a message segment */ 96ab063af9SPeter Wemm /* (must be small power of 2 greater than 4) */ 97ab063af9SPeter Wemm MSGSEG /* number of message segments */ 98ab063af9SPeter Wemm }; 99ab063af9SPeter Wemm 100ab063af9SPeter Wemm /* 101ab063af9SPeter Wemm * macros to convert between msqid_ds's and msqid's. 102ab063af9SPeter Wemm * (specific to this implementation) 103ab063af9SPeter Wemm */ 104ab063af9SPeter Wemm #define MSQID(ix,ds) ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000)) 105ab063af9SPeter Wemm #define MSQID_IX(id) ((id) & 0xffff) 106ab063af9SPeter Wemm #define MSQID_SEQ(id) (((id) >> 16) & 0xffff) 107ab063af9SPeter Wemm 108ab063af9SPeter Wemm /* 109ab063af9SPeter Wemm * The rest of this file is specific to this particular implementation. 110ab063af9SPeter Wemm */ 111ab063af9SPeter Wemm 112ab063af9SPeter Wemm struct msgmap { 113ab063af9SPeter Wemm short next; /* next segment in buffer */ 114ab063af9SPeter Wemm /* -1 -> available */ 115ab063af9SPeter Wemm /* 0..(MSGSEG-1) -> index of next segment */ 116ab063af9SPeter Wemm }; 117ab063af9SPeter Wemm 118ab063af9SPeter Wemm #define MSG_LOCKED 01000 /* Is this msqid_ds locked? */ 119ab063af9SPeter Wemm 12087b6de2bSPoul-Henning Kamp static int nfree_msgmaps; /* # of free map entries */ 12187b6de2bSPoul-Henning Kamp static short free_msgmaps; /* head of linked list of free map entries */ 12287b6de2bSPoul-Henning Kamp static struct msg *free_msghdrs;/* list of free msg headers */ 123ab063af9SPeter Wemm static char *msgpool; /* MSGMAX byte long msg buffer pool */ 124ab063af9SPeter Wemm static struct msgmap *msgmaps; /* MSGSEG msgmap structures */ 125ab063af9SPeter Wemm static struct msg *msghdrs; /* MSGTQL msg headers */ 126ab063af9SPeter Wemm static struct msqid_ds *msqids; /* MSGMNI msqid_ds struct's */ 1273d903220SDoug Rabson 128ab063af9SPeter Wemm static void 12978525ce3SAlfred Perlstein msginit() 1303d903220SDoug Rabson { 1313d903220SDoug Rabson register int i; 1323d903220SDoug Rabson 133ab063af9SPeter Wemm msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK); 134ab063af9SPeter Wemm if (msgpool == NULL) 135ab063af9SPeter Wemm panic("msgpool is NULL"); 136ab063af9SPeter Wemm msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK); 137ab063af9SPeter Wemm if (msgmaps == NULL) 138ab063af9SPeter Wemm panic("msgmaps is NULL"); 139ab063af9SPeter Wemm msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK); 140ab063af9SPeter Wemm if (msghdrs == NULL) 141ab063af9SPeter Wemm panic("msghdrs is NULL"); 142ab063af9SPeter Wemm msqids = malloc(sizeof(struct msqid_ds) * msginfo.msgmni, M_MSG, M_WAITOK); 143ab063af9SPeter Wemm if (msqids == NULL) 144ab063af9SPeter Wemm panic("msqids is NULL"); 145ab063af9SPeter Wemm 1463d903220SDoug Rabson /* 1473d903220SDoug Rabson * msginfo.msgssz should be a power of two for efficiency reasons. 1483d903220SDoug Rabson * It is also pretty silly if msginfo.msgssz is less than 8 1493d903220SDoug Rabson * or greater than about 256 so ... 1503d903220SDoug Rabson */ 1513d903220SDoug Rabson 1523d903220SDoug Rabson i = 8; 1533d903220SDoug Rabson while (i < 1024 && i != msginfo.msgssz) 1543d903220SDoug Rabson i <<= 1; 1553d903220SDoug Rabson if (i != msginfo.msgssz) { 1563d903220SDoug Rabson printf("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz, 1573d903220SDoug Rabson msginfo.msgssz); 1583d903220SDoug Rabson panic("msginfo.msgssz not a small power of 2"); 1593d903220SDoug Rabson } 1603d903220SDoug Rabson 1613d903220SDoug Rabson if (msginfo.msgseg > 32767) { 1623d903220SDoug Rabson printf("msginfo.msgseg=%d\n", msginfo.msgseg); 1633d903220SDoug Rabson panic("msginfo.msgseg > 32767"); 1643d903220SDoug Rabson } 1653d903220SDoug Rabson 1663d903220SDoug Rabson if (msgmaps == NULL) 1673d903220SDoug Rabson panic("msgmaps is NULL"); 1683d903220SDoug Rabson 1693d903220SDoug Rabson for (i = 0; i < msginfo.msgseg; i++) { 1703d903220SDoug Rabson if (i > 0) 1713d903220SDoug Rabson msgmaps[i-1].next = i; 1723d903220SDoug Rabson msgmaps[i].next = -1; /* implies entry is available */ 1733d903220SDoug Rabson } 1743d903220SDoug Rabson free_msgmaps = 0; 1753d903220SDoug Rabson nfree_msgmaps = msginfo.msgseg; 1763d903220SDoug Rabson 1773d903220SDoug Rabson if (msghdrs == NULL) 1783d903220SDoug Rabson panic("msghdrs is NULL"); 1793d903220SDoug Rabson 1803d903220SDoug Rabson for (i = 0; i < msginfo.msgtql; i++) { 1813d903220SDoug Rabson msghdrs[i].msg_type = 0; 1823d903220SDoug Rabson if (i > 0) 1833d903220SDoug Rabson msghdrs[i-1].msg_next = &msghdrs[i]; 1843d903220SDoug Rabson msghdrs[i].msg_next = NULL; 1853d903220SDoug Rabson } 1863d903220SDoug Rabson free_msghdrs = &msghdrs[0]; 1873d903220SDoug Rabson 1883d903220SDoug Rabson if (msqids == NULL) 1893d903220SDoug Rabson panic("msqids is NULL"); 1903d903220SDoug Rabson 1913d903220SDoug Rabson for (i = 0; i < msginfo.msgmni; i++) { 1923d903220SDoug Rabson msqids[i].msg_qbytes = 0; /* implies entry is available */ 1933d903220SDoug Rabson msqids[i].msg_perm.seq = 0; /* reset to a known value */ 1946413a4bcSPeter Wemm msqids[i].msg_perm.mode = 0; 1953d903220SDoug Rabson } 1963d903220SDoug Rabson } 19778525ce3SAlfred Perlstein 19878525ce3SAlfred Perlstein static int 19978525ce3SAlfred Perlstein msgunload() 20078525ce3SAlfred Perlstein { 20178525ce3SAlfred Perlstein struct msqid_ds *msqptr; 20278525ce3SAlfred Perlstein int msqid; 20378525ce3SAlfred Perlstein 20478525ce3SAlfred Perlstein for (msqid = 0; msqid < msginfo.msgmni; msqid++) { 20578525ce3SAlfred Perlstein /* 20678525ce3SAlfred Perlstein * Look for an unallocated and unlocked msqid_ds. 20778525ce3SAlfred Perlstein * msqid_ds's can be locked by msgsnd or msgrcv while 20878525ce3SAlfred Perlstein * they are copying the message in/out. We can't 20978525ce3SAlfred Perlstein * re-use the entry until they release it. 21078525ce3SAlfred Perlstein */ 21178525ce3SAlfred Perlstein msqptr = &msqids[msqid]; 21278525ce3SAlfred Perlstein if (msqptr->msg_qbytes != 0 || 21378525ce3SAlfred Perlstein (msqptr->msg_perm.mode & MSG_LOCKED) != 0) 21478525ce3SAlfred Perlstein break; 21578525ce3SAlfred Perlstein } 21678525ce3SAlfred Perlstein if (msqid != msginfo.msgmni) 21778525ce3SAlfred Perlstein return (EBUSY); 21878525ce3SAlfred Perlstein 21978525ce3SAlfred Perlstein free(msgpool, M_MSG); 22078525ce3SAlfred Perlstein free(msgmaps, M_MSG); 22178525ce3SAlfred Perlstein free(msghdrs, M_MSG); 22278525ce3SAlfred Perlstein free(msqids, M_MSG); 22378525ce3SAlfred Perlstein return (0); 22478525ce3SAlfred Perlstein } 22578525ce3SAlfred Perlstein 22678525ce3SAlfred Perlstein 22778525ce3SAlfred Perlstein static int 22878525ce3SAlfred Perlstein sysvmsg_modload(struct module *module, int cmd, void *arg) 22978525ce3SAlfred Perlstein { 23078525ce3SAlfred Perlstein int error = 0; 23178525ce3SAlfred Perlstein 23278525ce3SAlfred Perlstein switch (cmd) { 23378525ce3SAlfred Perlstein case MOD_LOAD: 23478525ce3SAlfred Perlstein msginit(); 23578525ce3SAlfred Perlstein break; 23678525ce3SAlfred Perlstein case MOD_UNLOAD: 23778525ce3SAlfred Perlstein error = msgunload(); 23878525ce3SAlfred Perlstein break; 23978525ce3SAlfred Perlstein case MOD_SHUTDOWN: 24078525ce3SAlfred Perlstein break; 24178525ce3SAlfred Perlstein default: 24278525ce3SAlfred Perlstein error = EINVAL; 24378525ce3SAlfred Perlstein break; 24478525ce3SAlfred Perlstein } 24578525ce3SAlfred Perlstein return (error); 24678525ce3SAlfred Perlstein } 24778525ce3SAlfred Perlstein 248faa784b7SDag-Erling Smørgrav static moduledata_t sysvmsg_mod = { 249faa784b7SDag-Erling Smørgrav "sysvmsg", 25078525ce3SAlfred Perlstein &sysvmsg_modload, 25178525ce3SAlfred Perlstein NULL 25278525ce3SAlfred Perlstein }; 25378525ce3SAlfred Perlstein 25489b54bffSAlfred Perlstein SYSCALL_MODULE_HELPER(msgsys, 6); 25578525ce3SAlfred Perlstein SYSCALL_MODULE_HELPER(msgctl, 3); 25678525ce3SAlfred Perlstein SYSCALL_MODULE_HELPER(msgget, 2); 25778525ce3SAlfred Perlstein SYSCALL_MODULE_HELPER(msgsnd, 4); 25878525ce3SAlfred Perlstein SYSCALL_MODULE_HELPER(msgrcv, 5); 25978525ce3SAlfred Perlstein 260faa784b7SDag-Erling Smørgrav DECLARE_MODULE(sysvmsg, sysvmsg_mod, 26178525ce3SAlfred Perlstein SI_SUB_SYSV_MSG, SI_ORDER_FIRST); 262faa784b7SDag-Erling Smørgrav MODULE_VERSION(sysvmsg, 1); 2633d903220SDoug Rabson 2643d903220SDoug Rabson /* 2653d903220SDoug Rabson * Entry point for all MSG calls 2663d903220SDoug Rabson */ 2673d903220SDoug Rabson int 268cb226aaaSPoul-Henning Kamp msgsys(p, uap) 269725db531SBruce Evans struct proc *p; 270725db531SBruce Evans /* XXX actually varargs. */ 271725db531SBruce Evans struct msgsys_args /* { 272725db531SBruce Evans u_int which; 273725db531SBruce Evans int a2; 274725db531SBruce Evans int a3; 275725db531SBruce Evans int a4; 276725db531SBruce Evans int a5; 277725db531SBruce Evans int a6; 278725db531SBruce Evans } */ *uap; 2793d903220SDoug Rabson { 2803d903220SDoug Rabson 281cb1f0db9SRobert Watson if (!jail_sysvipc_allowed && p->p_prison != NULL) 282cb1f0db9SRobert Watson return (ENOSYS); 283cb1f0db9SRobert Watson 2843d903220SDoug Rabson if (uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0])) 2853d903220SDoug Rabson return (EINVAL); 286cb226aaaSPoul-Henning Kamp return ((*msgcalls[uap->which])(p, &uap->a2)); 2873d903220SDoug Rabson } 2883d903220SDoug Rabson 2893d903220SDoug Rabson static void 2903d903220SDoug Rabson msg_freehdr(msghdr) 2913d903220SDoug Rabson struct msg *msghdr; 2923d903220SDoug Rabson { 2933d903220SDoug Rabson while (msghdr->msg_ts > 0) { 2943d903220SDoug Rabson short next; 2953d903220SDoug Rabson if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg) 2963d903220SDoug Rabson panic("msghdr->msg_spot out of range"); 2973d903220SDoug Rabson next = msgmaps[msghdr->msg_spot].next; 2983d903220SDoug Rabson msgmaps[msghdr->msg_spot].next = free_msgmaps; 2993d903220SDoug Rabson free_msgmaps = msghdr->msg_spot; 3003d903220SDoug Rabson nfree_msgmaps++; 3013d903220SDoug Rabson msghdr->msg_spot = next; 3023d903220SDoug Rabson if (msghdr->msg_ts >= msginfo.msgssz) 3033d903220SDoug Rabson msghdr->msg_ts -= msginfo.msgssz; 3043d903220SDoug Rabson else 3053d903220SDoug Rabson msghdr->msg_ts = 0; 3063d903220SDoug Rabson } 3073d903220SDoug Rabson if (msghdr->msg_spot != -1) 3083d903220SDoug Rabson panic("msghdr->msg_spot != -1"); 3093d903220SDoug Rabson msghdr->msg_next = free_msghdrs; 3103d903220SDoug Rabson free_msghdrs = msghdr; 3113d903220SDoug Rabson } 3123d903220SDoug Rabson 313b5d5c0c9SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 3143d903220SDoug Rabson struct msgctl_args { 3153d903220SDoug Rabson int msqid; 3163d903220SDoug Rabson int cmd; 317b5d5c0c9SPeter Wemm struct msqid_ds *buf; 3183d903220SDoug Rabson }; 319b5d5c0c9SPeter Wemm #endif 3203d903220SDoug Rabson 321b5d5c0c9SPeter Wemm int 322cb226aaaSPoul-Henning Kamp msgctl(p, uap) 3233d903220SDoug Rabson struct proc *p; 3243d903220SDoug Rabson register struct msgctl_args *uap; 3253d903220SDoug Rabson { 3263d903220SDoug Rabson int msqid = uap->msqid; 3273d903220SDoug Rabson int cmd = uap->cmd; 328b5d5c0c9SPeter Wemm struct msqid_ds *user_msqptr = uap->buf; 329797f2d22SPoul-Henning Kamp int rval, eval; 3303d903220SDoug Rabson struct msqid_ds msqbuf; 3313d903220SDoug Rabson register struct msqid_ds *msqptr; 3323d903220SDoug Rabson 3333d903220SDoug Rabson #ifdef MSG_DEBUG_OK 3343d903220SDoug Rabson printf("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, user_msqptr); 3353d903220SDoug Rabson #endif 3363d903220SDoug Rabson 337cb1f0db9SRobert Watson if (!jail_sysvipc_allowed && p->p_prison != NULL) 338cb1f0db9SRobert Watson return (ENOSYS); 339cb1f0db9SRobert Watson 3403d903220SDoug Rabson msqid = IPCID_TO_IX(msqid); 3413d903220SDoug Rabson 3423d903220SDoug Rabson if (msqid < 0 || msqid >= msginfo.msgmni) { 3433d903220SDoug Rabson #ifdef MSG_DEBUG_OK 3443d903220SDoug Rabson printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid, 3453d903220SDoug Rabson msginfo.msgmni); 3463d903220SDoug Rabson #endif 3473d903220SDoug Rabson return(EINVAL); 3483d903220SDoug Rabson } 3493d903220SDoug Rabson 3503d903220SDoug Rabson msqptr = &msqids[msqid]; 3513d903220SDoug Rabson 3523d903220SDoug Rabson if (msqptr->msg_qbytes == 0) { 3533d903220SDoug Rabson #ifdef MSG_DEBUG_OK 3543d903220SDoug Rabson printf("no such msqid\n"); 3553d903220SDoug Rabson #endif 3563d903220SDoug Rabson return(EINVAL); 3573d903220SDoug Rabson } 3583d903220SDoug Rabson if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) { 3593d903220SDoug Rabson #ifdef MSG_DEBUG_OK 3603d903220SDoug Rabson printf("wrong sequence number\n"); 3613d903220SDoug Rabson #endif 3623d903220SDoug Rabson return(EINVAL); 3633d903220SDoug Rabson } 3643d903220SDoug Rabson 3653d903220SDoug Rabson eval = 0; 3663d903220SDoug Rabson rval = 0; 3673d903220SDoug Rabson 3683d903220SDoug Rabson switch (cmd) { 3693d903220SDoug Rabson 3703d903220SDoug Rabson case IPC_RMID: 3713d903220SDoug Rabson { 3723d903220SDoug Rabson struct msg *msghdr; 3731c308b81SPoul-Henning Kamp if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_M))) 3743d903220SDoug Rabson return(eval); 3753d903220SDoug Rabson /* Free the message headers */ 3763d903220SDoug Rabson msghdr = msqptr->msg_first; 3773d903220SDoug Rabson while (msghdr != NULL) { 3783d903220SDoug Rabson struct msg *msghdr_tmp; 3793d903220SDoug Rabson 3803d903220SDoug Rabson /* Free the segments of each message */ 3813d903220SDoug Rabson msqptr->msg_cbytes -= msghdr->msg_ts; 3823d903220SDoug Rabson msqptr->msg_qnum--; 3833d903220SDoug Rabson msghdr_tmp = msghdr; 3843d903220SDoug Rabson msghdr = msghdr->msg_next; 3853d903220SDoug Rabson msg_freehdr(msghdr_tmp); 3863d903220SDoug Rabson } 3873d903220SDoug Rabson 3883d903220SDoug Rabson if (msqptr->msg_cbytes != 0) 3893d903220SDoug Rabson panic("msg_cbytes is screwed up"); 3903d903220SDoug Rabson if (msqptr->msg_qnum != 0) 3913d903220SDoug Rabson panic("msg_qnum is screwed up"); 3923d903220SDoug Rabson 3933d903220SDoug Rabson msqptr->msg_qbytes = 0; /* Mark it as free */ 3943d903220SDoug Rabson 3953d903220SDoug Rabson wakeup((caddr_t)msqptr); 3963d903220SDoug Rabson } 3973d903220SDoug Rabson 3983d903220SDoug Rabson break; 3993d903220SDoug Rabson 4003d903220SDoug Rabson case IPC_SET: 4011c308b81SPoul-Henning Kamp if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_M))) 4023d903220SDoug Rabson return(eval); 4033d903220SDoug Rabson if ((eval = copyin(user_msqptr, &msqbuf, sizeof(msqbuf))) != 0) 4043d903220SDoug Rabson return(eval); 40557c90d6fSPoul-Henning Kamp if (msqbuf.msg_qbytes > msqptr->msg_qbytes) { 4061c308b81SPoul-Henning Kamp eval = suser(p); 40757c90d6fSPoul-Henning Kamp if (eval) 40857c90d6fSPoul-Henning Kamp return(eval); 40957c90d6fSPoul-Henning Kamp } 4103d903220SDoug Rabson if (msqbuf.msg_qbytes > msginfo.msgmnb) { 4113d903220SDoug Rabson #ifdef MSG_DEBUG_OK 4123d903220SDoug Rabson printf("can't increase msg_qbytes beyond %d (truncating)\n", 4133d903220SDoug Rabson msginfo.msgmnb); 4143d903220SDoug Rabson #endif 4153d903220SDoug Rabson msqbuf.msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */ 4163d903220SDoug Rabson } 4173d903220SDoug Rabson if (msqbuf.msg_qbytes == 0) { 4183d903220SDoug Rabson #ifdef MSG_DEBUG_OK 4193d903220SDoug Rabson printf("can't reduce msg_qbytes to 0\n"); 4203d903220SDoug Rabson #endif 4213d903220SDoug Rabson return(EINVAL); /* non-standard errno! */ 4223d903220SDoug Rabson } 4233d903220SDoug Rabson msqptr->msg_perm.uid = msqbuf.msg_perm.uid; /* change the owner */ 4243d903220SDoug Rabson msqptr->msg_perm.gid = msqbuf.msg_perm.gid; /* change the owner */ 4253d903220SDoug Rabson msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) | 4263d903220SDoug Rabson (msqbuf.msg_perm.mode & 0777); 4273d903220SDoug Rabson msqptr->msg_qbytes = msqbuf.msg_qbytes; 428227ee8a1SPoul-Henning Kamp msqptr->msg_ctime = time_second; 4293d903220SDoug Rabson break; 4303d903220SDoug Rabson 4313d903220SDoug Rabson case IPC_STAT: 4321c308b81SPoul-Henning Kamp if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_R))) { 4333d903220SDoug Rabson #ifdef MSG_DEBUG_OK 4343d903220SDoug Rabson printf("requester doesn't have read access\n"); 4353d903220SDoug Rabson #endif 4363d903220SDoug Rabson return(eval); 4373d903220SDoug Rabson } 4383d903220SDoug Rabson eval = copyout((caddr_t)msqptr, user_msqptr, 4393d903220SDoug Rabson sizeof(struct msqid_ds)); 4403d903220SDoug Rabson break; 4413d903220SDoug Rabson 4423d903220SDoug Rabson default: 4433d903220SDoug Rabson #ifdef MSG_DEBUG_OK 4443d903220SDoug Rabson printf("invalid command %d\n", cmd); 4453d903220SDoug Rabson #endif 4463d903220SDoug Rabson return(EINVAL); 4473d903220SDoug Rabson } 4483d903220SDoug Rabson 4493d903220SDoug Rabson if (eval == 0) 450cb226aaaSPoul-Henning Kamp p->p_retval[0] = rval; 4513d903220SDoug Rabson return(eval); 4523d903220SDoug Rabson } 4533d903220SDoug Rabson 454b5d5c0c9SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 4553d903220SDoug Rabson struct msgget_args { 4563d903220SDoug Rabson key_t key; 4573d903220SDoug Rabson int msgflg; 4583d903220SDoug Rabson }; 459b5d5c0c9SPeter Wemm #endif 4603d903220SDoug Rabson 461b5d5c0c9SPeter Wemm int 462cb226aaaSPoul-Henning Kamp msgget(p, uap) 4633d903220SDoug Rabson struct proc *p; 4643d903220SDoug Rabson register struct msgget_args *uap; 4653d903220SDoug Rabson { 4663d903220SDoug Rabson int msqid, eval; 4673d903220SDoug Rabson int key = uap->key; 4683d903220SDoug Rabson int msgflg = uap->msgflg; 4693d903220SDoug Rabson struct ucred *cred = p->p_ucred; 470789668e2SDavid Greenman register struct msqid_ds *msqptr = NULL; 4713d903220SDoug Rabson 4723d903220SDoug Rabson #ifdef MSG_DEBUG_OK 4733d903220SDoug Rabson printf("msgget(0x%x, 0%o)\n", key, msgflg); 4743d903220SDoug Rabson #endif 4753d903220SDoug Rabson 476cb1f0db9SRobert Watson if (!jail_sysvipc_allowed && p->p_prison != NULL) 477cb1f0db9SRobert Watson return (ENOSYS); 478cb1f0db9SRobert Watson 4793d903220SDoug Rabson if (key != IPC_PRIVATE) { 4803d903220SDoug Rabson for (msqid = 0; msqid < msginfo.msgmni; msqid++) { 4813d903220SDoug Rabson msqptr = &msqids[msqid]; 4823d903220SDoug Rabson if (msqptr->msg_qbytes != 0 && 4833d903220SDoug Rabson msqptr->msg_perm.key == key) 4843d903220SDoug Rabson break; 4853d903220SDoug Rabson } 4863d903220SDoug Rabson if (msqid < msginfo.msgmni) { 4873d903220SDoug Rabson #ifdef MSG_DEBUG_OK 4883d903220SDoug Rabson printf("found public key\n"); 4893d903220SDoug Rabson #endif 4903d903220SDoug Rabson if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) { 4913d903220SDoug Rabson #ifdef MSG_DEBUG_OK 4923d903220SDoug Rabson printf("not exclusive\n"); 4933d903220SDoug Rabson #endif 4943d903220SDoug Rabson return(EEXIST); 4953d903220SDoug Rabson } 4961c308b81SPoul-Henning Kamp if ((eval = ipcperm(p, &msqptr->msg_perm, msgflg & 0700 ))) { 4973d903220SDoug Rabson #ifdef MSG_DEBUG_OK 4983d903220SDoug Rabson printf("requester doesn't have 0%o access\n", 4993d903220SDoug Rabson msgflg & 0700); 5003d903220SDoug Rabson #endif 5013d903220SDoug Rabson return(eval); 5023d903220SDoug Rabson } 5033d903220SDoug Rabson goto found; 5043d903220SDoug Rabson } 5053d903220SDoug Rabson } 5063d903220SDoug Rabson 5073d903220SDoug Rabson #ifdef MSG_DEBUG_OK 5083d903220SDoug Rabson printf("need to allocate the msqid_ds\n"); 5093d903220SDoug Rabson #endif 5103d903220SDoug Rabson if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) { 5113d903220SDoug Rabson for (msqid = 0; msqid < msginfo.msgmni; msqid++) { 5123d903220SDoug Rabson /* 5133d903220SDoug Rabson * Look for an unallocated and unlocked msqid_ds. 5143d903220SDoug Rabson * msqid_ds's can be locked by msgsnd or msgrcv while 5153d903220SDoug Rabson * they are copying the message in/out. We can't 5163d903220SDoug Rabson * re-use the entry until they release it. 5173d903220SDoug Rabson */ 5183d903220SDoug Rabson msqptr = &msqids[msqid]; 5193d903220SDoug Rabson if (msqptr->msg_qbytes == 0 && 5203d903220SDoug Rabson (msqptr->msg_perm.mode & MSG_LOCKED) == 0) 5213d903220SDoug Rabson break; 5223d903220SDoug Rabson } 5233d903220SDoug Rabson if (msqid == msginfo.msgmni) { 5243d903220SDoug Rabson #ifdef MSG_DEBUG_OK 5253d903220SDoug Rabson printf("no more msqid_ds's available\n"); 5263d903220SDoug Rabson #endif 5273d903220SDoug Rabson return(ENOSPC); 5283d903220SDoug Rabson } 5293d903220SDoug Rabson #ifdef MSG_DEBUG_OK 5303d903220SDoug Rabson printf("msqid %d is available\n", msqid); 5313d903220SDoug Rabson #endif 5323d903220SDoug Rabson msqptr->msg_perm.key = key; 5333d903220SDoug Rabson msqptr->msg_perm.cuid = cred->cr_uid; 5343d903220SDoug Rabson msqptr->msg_perm.uid = cred->cr_uid; 5353d903220SDoug Rabson msqptr->msg_perm.cgid = cred->cr_gid; 5363d903220SDoug Rabson msqptr->msg_perm.gid = cred->cr_gid; 5373d903220SDoug Rabson msqptr->msg_perm.mode = (msgflg & 0777); 5383d903220SDoug Rabson /* Make sure that the returned msqid is unique */ 5393d903220SDoug Rabson msqptr->msg_perm.seq++; 5403d903220SDoug Rabson msqptr->msg_first = NULL; 5413d903220SDoug Rabson msqptr->msg_last = NULL; 5423d903220SDoug Rabson msqptr->msg_cbytes = 0; 5433d903220SDoug Rabson msqptr->msg_qnum = 0; 5443d903220SDoug Rabson msqptr->msg_qbytes = msginfo.msgmnb; 5453d903220SDoug Rabson msqptr->msg_lspid = 0; 5463d903220SDoug Rabson msqptr->msg_lrpid = 0; 5473d903220SDoug Rabson msqptr->msg_stime = 0; 5483d903220SDoug Rabson msqptr->msg_rtime = 0; 549227ee8a1SPoul-Henning Kamp msqptr->msg_ctime = time_second; 5503d903220SDoug Rabson } else { 5513d903220SDoug Rabson #ifdef MSG_DEBUG_OK 5523d903220SDoug Rabson printf("didn't find it and wasn't asked to create it\n"); 5533d903220SDoug Rabson #endif 5543d903220SDoug Rabson return(ENOENT); 5553d903220SDoug Rabson } 5563d903220SDoug Rabson 5573d903220SDoug Rabson found: 5583d903220SDoug Rabson /* Construct the unique msqid */ 559cb226aaaSPoul-Henning Kamp p->p_retval[0] = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm); 5603d903220SDoug Rabson return(0); 5613d903220SDoug Rabson } 5623d903220SDoug Rabson 563b5d5c0c9SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 5643d903220SDoug Rabson struct msgsnd_args { 5653d903220SDoug Rabson int msqid; 566b5d5c0c9SPeter Wemm void *msgp; 5673d903220SDoug Rabson size_t msgsz; 5683d903220SDoug Rabson int msgflg; 5693d903220SDoug Rabson }; 570b5d5c0c9SPeter Wemm #endif 5713d903220SDoug Rabson 572b5d5c0c9SPeter Wemm int 573cb226aaaSPoul-Henning Kamp msgsnd(p, uap) 5743d903220SDoug Rabson struct proc *p; 5753d903220SDoug Rabson register struct msgsnd_args *uap; 5763d903220SDoug Rabson { 5773d903220SDoug Rabson int msqid = uap->msqid; 578b5d5c0c9SPeter Wemm void *user_msgp = uap->msgp; 5793d903220SDoug Rabson size_t msgsz = uap->msgsz; 5803d903220SDoug Rabson int msgflg = uap->msgflg; 5813d903220SDoug Rabson int segs_needed, eval; 5823d903220SDoug Rabson register struct msqid_ds *msqptr; 5833d903220SDoug Rabson register struct msg *msghdr; 5843d903220SDoug Rabson short next; 5853d903220SDoug Rabson 5863d903220SDoug Rabson #ifdef MSG_DEBUG_OK 5873d903220SDoug Rabson printf("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz, 5883d903220SDoug Rabson msgflg); 5893d903220SDoug Rabson #endif 5903d903220SDoug Rabson 591cb1f0db9SRobert Watson if (!jail_sysvipc_allowed && p->p_prison != NULL) 592cb1f0db9SRobert Watson return (ENOSYS); 593cb1f0db9SRobert Watson 5943d903220SDoug Rabson msqid = IPCID_TO_IX(msqid); 5953d903220SDoug Rabson 5963d903220SDoug Rabson if (msqid < 0 || msqid >= msginfo.msgmni) { 5973d903220SDoug Rabson #ifdef MSG_DEBUG_OK 5983d903220SDoug Rabson printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid, 5993d903220SDoug Rabson msginfo.msgmni); 6003d903220SDoug Rabson #endif 6013d903220SDoug Rabson return(EINVAL); 6023d903220SDoug Rabson } 6033d903220SDoug Rabson 6043d903220SDoug Rabson msqptr = &msqids[msqid]; 6053d903220SDoug Rabson if (msqptr->msg_qbytes == 0) { 6063d903220SDoug Rabson #ifdef MSG_DEBUG_OK 6073d903220SDoug Rabson printf("no such message queue id\n"); 6083d903220SDoug Rabson #endif 6093d903220SDoug Rabson return(EINVAL); 6103d903220SDoug Rabson } 6113d903220SDoug Rabson if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) { 6123d903220SDoug Rabson #ifdef MSG_DEBUG_OK 6133d903220SDoug Rabson printf("wrong sequence number\n"); 6143d903220SDoug Rabson #endif 6153d903220SDoug Rabson return(EINVAL); 6163d903220SDoug Rabson } 6173d903220SDoug Rabson 6181c308b81SPoul-Henning Kamp if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_W))) { 6193d903220SDoug Rabson #ifdef MSG_DEBUG_OK 6203d903220SDoug Rabson printf("requester doesn't have write access\n"); 6213d903220SDoug Rabson #endif 6223d903220SDoug Rabson return(eval); 6233d903220SDoug Rabson } 6243d903220SDoug Rabson 6253d903220SDoug Rabson segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz; 6263d903220SDoug Rabson #ifdef MSG_DEBUG_OK 6273d903220SDoug Rabson printf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz, 6283d903220SDoug Rabson segs_needed); 6293d903220SDoug Rabson #endif 6303d903220SDoug Rabson for (;;) { 6313d903220SDoug Rabson int need_more_resources = 0; 6323d903220SDoug Rabson 6333d903220SDoug Rabson /* 6343d903220SDoug Rabson * check msgsz 6353d903220SDoug Rabson * (inside this loop in case msg_qbytes changes while we sleep) 6363d903220SDoug Rabson */ 6373d903220SDoug Rabson 638789668e2SDavid Greenman if (msgsz > msqptr->msg_qbytes) { 6393d903220SDoug Rabson #ifdef MSG_DEBUG_OK 6403d903220SDoug Rabson printf("msgsz > msqptr->msg_qbytes\n"); 6413d903220SDoug Rabson #endif 6423d903220SDoug Rabson return(EINVAL); 6433d903220SDoug Rabson } 6443d903220SDoug Rabson 6453d903220SDoug Rabson if (msqptr->msg_perm.mode & MSG_LOCKED) { 6463d903220SDoug Rabson #ifdef MSG_DEBUG_OK 6473d903220SDoug Rabson printf("msqid is locked\n"); 6483d903220SDoug Rabson #endif 6493d903220SDoug Rabson need_more_resources = 1; 6503d903220SDoug Rabson } 6513d903220SDoug Rabson if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) { 6523d903220SDoug Rabson #ifdef MSG_DEBUG_OK 6533d903220SDoug Rabson printf("msgsz + msg_cbytes > msg_qbytes\n"); 6543d903220SDoug Rabson #endif 6553d903220SDoug Rabson need_more_resources = 1; 6563d903220SDoug Rabson } 6573d903220SDoug Rabson if (segs_needed > nfree_msgmaps) { 6583d903220SDoug Rabson #ifdef MSG_DEBUG_OK 6593d903220SDoug Rabson printf("segs_needed > nfree_msgmaps\n"); 6603d903220SDoug Rabson #endif 6613d903220SDoug Rabson need_more_resources = 1; 6623d903220SDoug Rabson } 6633d903220SDoug Rabson if (free_msghdrs == NULL) { 6643d903220SDoug Rabson #ifdef MSG_DEBUG_OK 6653d903220SDoug Rabson printf("no more msghdrs\n"); 6663d903220SDoug Rabson #endif 6673d903220SDoug Rabson need_more_resources = 1; 6683d903220SDoug Rabson } 6693d903220SDoug Rabson 6703d903220SDoug Rabson if (need_more_resources) { 6713d903220SDoug Rabson int we_own_it; 6723d903220SDoug Rabson 6733d903220SDoug Rabson if ((msgflg & IPC_NOWAIT) != 0) { 6743d903220SDoug Rabson #ifdef MSG_DEBUG_OK 6753d903220SDoug Rabson printf("need more resources but caller doesn't want to wait\n"); 6763d903220SDoug Rabson #endif 6773d903220SDoug Rabson return(EAGAIN); 6783d903220SDoug Rabson } 6793d903220SDoug Rabson 6803d903220SDoug Rabson if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) { 6813d903220SDoug Rabson #ifdef MSG_DEBUG_OK 6823d903220SDoug Rabson printf("we don't own the msqid_ds\n"); 6833d903220SDoug Rabson #endif 6843d903220SDoug Rabson we_own_it = 0; 6853d903220SDoug Rabson } else { 6863d903220SDoug Rabson /* Force later arrivals to wait for our 6873d903220SDoug Rabson request */ 6883d903220SDoug Rabson #ifdef MSG_DEBUG_OK 6893d903220SDoug Rabson printf("we own the msqid_ds\n"); 6903d903220SDoug Rabson #endif 6913d903220SDoug Rabson msqptr->msg_perm.mode |= MSG_LOCKED; 6923d903220SDoug Rabson we_own_it = 1; 6933d903220SDoug Rabson } 6943d903220SDoug Rabson #ifdef MSG_DEBUG_OK 6953d903220SDoug Rabson printf("goodnight\n"); 6963d903220SDoug Rabson #endif 6973d903220SDoug Rabson eval = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH, 6983d903220SDoug Rabson "msgwait", 0); 6993d903220SDoug Rabson #ifdef MSG_DEBUG_OK 7003d903220SDoug Rabson printf("good morning, eval=%d\n", eval); 7013d903220SDoug Rabson #endif 7023d903220SDoug Rabson if (we_own_it) 7033d903220SDoug Rabson msqptr->msg_perm.mode &= ~MSG_LOCKED; 7043d903220SDoug Rabson if (eval != 0) { 7053d903220SDoug Rabson #ifdef MSG_DEBUG_OK 7063d903220SDoug Rabson printf("msgsnd: interrupted system call\n"); 7073d903220SDoug Rabson #endif 7083d903220SDoug Rabson return(EINTR); 7093d903220SDoug Rabson } 7103d903220SDoug Rabson 7113d903220SDoug Rabson /* 7123d903220SDoug Rabson * Make sure that the msq queue still exists 7133d903220SDoug Rabson */ 7143d903220SDoug Rabson 7153d903220SDoug Rabson if (msqptr->msg_qbytes == 0) { 7163d903220SDoug Rabson #ifdef MSG_DEBUG_OK 7173d903220SDoug Rabson printf("msqid deleted\n"); 7183d903220SDoug Rabson #endif 7193d903220SDoug Rabson return(EIDRM); 7203d903220SDoug Rabson } 7213d903220SDoug Rabson 7223d903220SDoug Rabson } else { 7233d903220SDoug Rabson #ifdef MSG_DEBUG_OK 7243d903220SDoug Rabson printf("got all the resources that we need\n"); 7253d903220SDoug Rabson #endif 7263d903220SDoug Rabson break; 7273d903220SDoug Rabson } 7283d903220SDoug Rabson } 7293d903220SDoug Rabson 7303d903220SDoug Rabson /* 7313d903220SDoug Rabson * We have the resources that we need. 7323d903220SDoug Rabson * Make sure! 7333d903220SDoug Rabson */ 7343d903220SDoug Rabson 7353d903220SDoug Rabson if (msqptr->msg_perm.mode & MSG_LOCKED) 7363d903220SDoug Rabson panic("msg_perm.mode & MSG_LOCKED"); 7373d903220SDoug Rabson if (segs_needed > nfree_msgmaps) 7383d903220SDoug Rabson panic("segs_needed > nfree_msgmaps"); 7393d903220SDoug Rabson if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) 7403d903220SDoug Rabson panic("msgsz + msg_cbytes > msg_qbytes"); 7413d903220SDoug Rabson if (free_msghdrs == NULL) 7423d903220SDoug Rabson panic("no more msghdrs"); 7433d903220SDoug Rabson 7443d903220SDoug Rabson /* 7453d903220SDoug Rabson * Re-lock the msqid_ds in case we page-fault when copying in the 7463d903220SDoug Rabson * message 7473d903220SDoug Rabson */ 7483d903220SDoug Rabson 7493d903220SDoug Rabson if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) 7503d903220SDoug Rabson panic("msqid_ds is already locked"); 7513d903220SDoug Rabson msqptr->msg_perm.mode |= MSG_LOCKED; 7523d903220SDoug Rabson 7533d903220SDoug Rabson /* 7543d903220SDoug Rabson * Allocate a message header 7553d903220SDoug Rabson */ 7563d903220SDoug Rabson 7573d903220SDoug Rabson msghdr = free_msghdrs; 7583d903220SDoug Rabson free_msghdrs = msghdr->msg_next; 7593d903220SDoug Rabson msghdr->msg_spot = -1; 7603d903220SDoug Rabson msghdr->msg_ts = msgsz; 7613d903220SDoug Rabson 7623d903220SDoug Rabson /* 7633d903220SDoug Rabson * Allocate space for the message 7643d903220SDoug Rabson */ 7653d903220SDoug Rabson 7663d903220SDoug Rabson while (segs_needed > 0) { 7673d903220SDoug Rabson if (nfree_msgmaps <= 0) 7683d903220SDoug Rabson panic("not enough msgmaps"); 7693d903220SDoug Rabson if (free_msgmaps == -1) 7703d903220SDoug Rabson panic("nil free_msgmaps"); 7713d903220SDoug Rabson next = free_msgmaps; 7723d903220SDoug Rabson if (next <= -1) 7733d903220SDoug Rabson panic("next too low #1"); 7743d903220SDoug Rabson if (next >= msginfo.msgseg) 7753d903220SDoug Rabson panic("next out of range #1"); 7763d903220SDoug Rabson #ifdef MSG_DEBUG_OK 7773d903220SDoug Rabson printf("allocating segment %d to message\n", next); 7783d903220SDoug Rabson #endif 7793d903220SDoug Rabson free_msgmaps = msgmaps[next].next; 7803d903220SDoug Rabson nfree_msgmaps--; 7813d903220SDoug Rabson msgmaps[next].next = msghdr->msg_spot; 7823d903220SDoug Rabson msghdr->msg_spot = next; 7833d903220SDoug Rabson segs_needed--; 7843d903220SDoug Rabson } 7853d903220SDoug Rabson 7863d903220SDoug Rabson /* 7873d903220SDoug Rabson * Copy in the message type 7883d903220SDoug Rabson */ 7893d903220SDoug Rabson 7903d903220SDoug Rabson if ((eval = copyin(user_msgp, &msghdr->msg_type, 7913d903220SDoug Rabson sizeof(msghdr->msg_type))) != 0) { 7923d903220SDoug Rabson #ifdef MSG_DEBUG_OK 7933d903220SDoug Rabson printf("error %d copying the message type\n", eval); 7943d903220SDoug Rabson #endif 7953d903220SDoug Rabson msg_freehdr(msghdr); 7963d903220SDoug Rabson msqptr->msg_perm.mode &= ~MSG_LOCKED; 7973d903220SDoug Rabson wakeup((caddr_t)msqptr); 7983d903220SDoug Rabson return(eval); 7993d903220SDoug Rabson } 80009a8dfa2SBruce Evans user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type); 8013d903220SDoug Rabson 8023d903220SDoug Rabson /* 8033d903220SDoug Rabson * Validate the message type 8043d903220SDoug Rabson */ 8053d903220SDoug Rabson 8063d903220SDoug Rabson if (msghdr->msg_type < 1) { 8073d903220SDoug Rabson msg_freehdr(msghdr); 8083d903220SDoug Rabson msqptr->msg_perm.mode &= ~MSG_LOCKED; 8093d903220SDoug Rabson wakeup((caddr_t)msqptr); 8103d903220SDoug Rabson #ifdef MSG_DEBUG_OK 8113d903220SDoug Rabson printf("mtype (%d) < 1\n", msghdr->msg_type); 8123d903220SDoug Rabson #endif 8133d903220SDoug Rabson return(EINVAL); 8143d903220SDoug Rabson } 8153d903220SDoug Rabson 8163d903220SDoug Rabson /* 8173d903220SDoug Rabson * Copy in the message body 8183d903220SDoug Rabson */ 8193d903220SDoug Rabson 8203d903220SDoug Rabson next = msghdr->msg_spot; 8213d903220SDoug Rabson while (msgsz > 0) { 8223d903220SDoug Rabson size_t tlen; 8233d903220SDoug Rabson if (msgsz > msginfo.msgssz) 8243d903220SDoug Rabson tlen = msginfo.msgssz; 8253d903220SDoug Rabson else 8263d903220SDoug Rabson tlen = msgsz; 8273d903220SDoug Rabson if (next <= -1) 8283d903220SDoug Rabson panic("next too low #2"); 8293d903220SDoug Rabson if (next >= msginfo.msgseg) 8303d903220SDoug Rabson panic("next out of range #2"); 8313d903220SDoug Rabson if ((eval = copyin(user_msgp, &msgpool[next * msginfo.msgssz], 8323d903220SDoug Rabson tlen)) != 0) { 8333d903220SDoug Rabson #ifdef MSG_DEBUG_OK 8343d903220SDoug Rabson printf("error %d copying in message segment\n", eval); 8353d903220SDoug Rabson #endif 8363d903220SDoug Rabson msg_freehdr(msghdr); 8373d903220SDoug Rabson msqptr->msg_perm.mode &= ~MSG_LOCKED; 8383d903220SDoug Rabson wakeup((caddr_t)msqptr); 8393d903220SDoug Rabson return(eval); 8403d903220SDoug Rabson } 8413d903220SDoug Rabson msgsz -= tlen; 84209a8dfa2SBruce Evans user_msgp = (char *)user_msgp + tlen; 8433d903220SDoug Rabson next = msgmaps[next].next; 8443d903220SDoug Rabson } 8453d903220SDoug Rabson if (next != -1) 8463d903220SDoug Rabson panic("didn't use all the msg segments"); 8473d903220SDoug Rabson 8483d903220SDoug Rabson /* 8493d903220SDoug Rabson * We've got the message. Unlock the msqid_ds. 8503d903220SDoug Rabson */ 8513d903220SDoug Rabson 8523d903220SDoug Rabson msqptr->msg_perm.mode &= ~MSG_LOCKED; 8533d903220SDoug Rabson 8543d903220SDoug Rabson /* 8553d903220SDoug Rabson * Make sure that the msqid_ds is still allocated. 8563d903220SDoug Rabson */ 8573d903220SDoug Rabson 8583d903220SDoug Rabson if (msqptr->msg_qbytes == 0) { 8593d903220SDoug Rabson msg_freehdr(msghdr); 8603d903220SDoug Rabson wakeup((caddr_t)msqptr); 8613d903220SDoug Rabson return(EIDRM); 8623d903220SDoug Rabson } 8633d903220SDoug Rabson 8643d903220SDoug Rabson /* 8653d903220SDoug Rabson * Put the message into the queue 8663d903220SDoug Rabson */ 8673d903220SDoug Rabson 8683d903220SDoug Rabson if (msqptr->msg_first == NULL) { 8693d903220SDoug Rabson msqptr->msg_first = msghdr; 8703d903220SDoug Rabson msqptr->msg_last = msghdr; 8713d903220SDoug Rabson } else { 8723d903220SDoug Rabson msqptr->msg_last->msg_next = msghdr; 8733d903220SDoug Rabson msqptr->msg_last = msghdr; 8743d903220SDoug Rabson } 8753d903220SDoug Rabson msqptr->msg_last->msg_next = NULL; 8763d903220SDoug Rabson 8773d903220SDoug Rabson msqptr->msg_cbytes += msghdr->msg_ts; 8783d903220SDoug Rabson msqptr->msg_qnum++; 8793d903220SDoug Rabson msqptr->msg_lspid = p->p_pid; 880227ee8a1SPoul-Henning Kamp msqptr->msg_stime = time_second; 8813d903220SDoug Rabson 8823d903220SDoug Rabson wakeup((caddr_t)msqptr); 883cb226aaaSPoul-Henning Kamp p->p_retval[0] = 0; 8843d903220SDoug Rabson return(0); 8853d903220SDoug Rabson } 8863d903220SDoug Rabson 887b5d5c0c9SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 8883d903220SDoug Rabson struct msgrcv_args { 8893d903220SDoug Rabson int msqid; 8903d903220SDoug Rabson void *msgp; 8913d903220SDoug Rabson size_t msgsz; 8923d903220SDoug Rabson long msgtyp; 8933d903220SDoug Rabson int msgflg; 8943d903220SDoug Rabson }; 895b5d5c0c9SPeter Wemm #endif 8963d903220SDoug Rabson 897b5d5c0c9SPeter Wemm int 898cb226aaaSPoul-Henning Kamp msgrcv(p, uap) 8993d903220SDoug Rabson struct proc *p; 9003d903220SDoug Rabson register struct msgrcv_args *uap; 9013d903220SDoug Rabson { 9023d903220SDoug Rabson int msqid = uap->msqid; 9033d903220SDoug Rabson void *user_msgp = uap->msgp; 9043d903220SDoug Rabson size_t msgsz = uap->msgsz; 9053d903220SDoug Rabson long msgtyp = uap->msgtyp; 9063d903220SDoug Rabson int msgflg = uap->msgflg; 9073d903220SDoug Rabson size_t len; 9083d903220SDoug Rabson register struct msqid_ds *msqptr; 9093d903220SDoug Rabson register struct msg *msghdr; 9103d903220SDoug Rabson int eval; 9113d903220SDoug Rabson short next; 9123d903220SDoug Rabson 9133d903220SDoug Rabson #ifdef MSG_DEBUG_OK 9143d903220SDoug Rabson printf("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid, user_msgp, 9153d903220SDoug Rabson msgsz, msgtyp, msgflg); 9163d903220SDoug Rabson #endif 9173d903220SDoug Rabson 918cb1f0db9SRobert Watson if (!jail_sysvipc_allowed && p->p_prison != NULL) 919cb1f0db9SRobert Watson return (ENOSYS); 920cb1f0db9SRobert Watson 9213d903220SDoug Rabson msqid = IPCID_TO_IX(msqid); 9223d903220SDoug Rabson 9233d903220SDoug Rabson if (msqid < 0 || msqid >= msginfo.msgmni) { 9243d903220SDoug Rabson #ifdef MSG_DEBUG_OK 9253d903220SDoug Rabson printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid, 9263d903220SDoug Rabson msginfo.msgmni); 9273d903220SDoug Rabson #endif 9283d903220SDoug Rabson return(EINVAL); 9293d903220SDoug Rabson } 9303d903220SDoug Rabson 9313d903220SDoug Rabson msqptr = &msqids[msqid]; 9323d903220SDoug Rabson if (msqptr->msg_qbytes == 0) { 9333d903220SDoug Rabson #ifdef MSG_DEBUG_OK 9343d903220SDoug Rabson printf("no such message queue id\n"); 9353d903220SDoug Rabson #endif 9363d903220SDoug Rabson return(EINVAL); 9373d903220SDoug Rabson } 9383d903220SDoug Rabson if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) { 9393d903220SDoug Rabson #ifdef MSG_DEBUG_OK 9403d903220SDoug Rabson printf("wrong sequence number\n"); 9413d903220SDoug Rabson #endif 9423d903220SDoug Rabson return(EINVAL); 9433d903220SDoug Rabson } 9443d903220SDoug Rabson 9451c308b81SPoul-Henning Kamp if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_R))) { 9463d903220SDoug Rabson #ifdef MSG_DEBUG_OK 9473d903220SDoug Rabson printf("requester doesn't have read access\n"); 9483d903220SDoug Rabson #endif 9493d903220SDoug Rabson return(eval); 9503d903220SDoug Rabson } 9513d903220SDoug Rabson 9523d903220SDoug Rabson msghdr = NULL; 9533d903220SDoug Rabson while (msghdr == NULL) { 9543d903220SDoug Rabson if (msgtyp == 0) { 9553d903220SDoug Rabson msghdr = msqptr->msg_first; 9563d903220SDoug Rabson if (msghdr != NULL) { 9573d903220SDoug Rabson if (msgsz < msghdr->msg_ts && 9583d903220SDoug Rabson (msgflg & MSG_NOERROR) == 0) { 9593d903220SDoug Rabson #ifdef MSG_DEBUG_OK 9603d903220SDoug Rabson printf("first message on the queue is too big (want %d, got %d)\n", 9613d903220SDoug Rabson msgsz, msghdr->msg_ts); 9623d903220SDoug Rabson #endif 9633d903220SDoug Rabson return(E2BIG); 9643d903220SDoug Rabson } 9653d903220SDoug Rabson if (msqptr->msg_first == msqptr->msg_last) { 9663d903220SDoug Rabson msqptr->msg_first = NULL; 9673d903220SDoug Rabson msqptr->msg_last = NULL; 9683d903220SDoug Rabson } else { 9693d903220SDoug Rabson msqptr->msg_first = msghdr->msg_next; 9703d903220SDoug Rabson if (msqptr->msg_first == NULL) 9713d903220SDoug Rabson panic("msg_first/last screwed up #1"); 9723d903220SDoug Rabson } 9733d903220SDoug Rabson } 9743d903220SDoug Rabson } else { 9753d903220SDoug Rabson struct msg *previous; 9763d903220SDoug Rabson struct msg **prev; 9773d903220SDoug Rabson 9783d903220SDoug Rabson previous = NULL; 9793d903220SDoug Rabson prev = &(msqptr->msg_first); 9803d903220SDoug Rabson while ((msghdr = *prev) != NULL) { 9813d903220SDoug Rabson /* 9823d903220SDoug Rabson * Is this message's type an exact match or is 9833d903220SDoug Rabson * this message's type less than or equal to 9843d903220SDoug Rabson * the absolute value of a negative msgtyp? 9853d903220SDoug Rabson * Note that the second half of this test can 9863d903220SDoug Rabson * NEVER be true if msgtyp is positive since 9873d903220SDoug Rabson * msg_type is always positive! 9883d903220SDoug Rabson */ 9893d903220SDoug Rabson 9903d903220SDoug Rabson if (msgtyp == msghdr->msg_type || 9913d903220SDoug Rabson msghdr->msg_type <= -msgtyp) { 9923d903220SDoug Rabson #ifdef MSG_DEBUG_OK 9933d903220SDoug Rabson printf("found message type %d, requested %d\n", 9943d903220SDoug Rabson msghdr->msg_type, msgtyp); 9953d903220SDoug Rabson #endif 9963d903220SDoug Rabson if (msgsz < msghdr->msg_ts && 9973d903220SDoug Rabson (msgflg & MSG_NOERROR) == 0) { 9983d903220SDoug Rabson #ifdef MSG_DEBUG_OK 9993d903220SDoug Rabson printf("requested message on the queue is too big (want %d, got %d)\n", 10003d903220SDoug Rabson msgsz, msghdr->msg_ts); 10013d903220SDoug Rabson #endif 10023d903220SDoug Rabson return(E2BIG); 10033d903220SDoug Rabson } 10043d903220SDoug Rabson *prev = msghdr->msg_next; 10053d903220SDoug Rabson if (msghdr == msqptr->msg_last) { 10063d903220SDoug Rabson if (previous == NULL) { 10073d903220SDoug Rabson if (prev != 10083d903220SDoug Rabson &msqptr->msg_first) 10093d903220SDoug Rabson panic("msg_first/last screwed up #2"); 10103d903220SDoug Rabson msqptr->msg_first = 10113d903220SDoug Rabson NULL; 10123d903220SDoug Rabson msqptr->msg_last = 10133d903220SDoug Rabson NULL; 10143d903220SDoug Rabson } else { 10153d903220SDoug Rabson if (prev == 10163d903220SDoug Rabson &msqptr->msg_first) 10173d903220SDoug Rabson panic("msg_first/last screwed up #3"); 10183d903220SDoug Rabson msqptr->msg_last = 10193d903220SDoug Rabson previous; 10203d903220SDoug Rabson } 10213d903220SDoug Rabson } 10223d903220SDoug Rabson break; 10233d903220SDoug Rabson } 10243d903220SDoug Rabson previous = msghdr; 10253d903220SDoug Rabson prev = &(msghdr->msg_next); 10263d903220SDoug Rabson } 10273d903220SDoug Rabson } 10283d903220SDoug Rabson 10293d903220SDoug Rabson /* 10303d903220SDoug Rabson * We've either extracted the msghdr for the appropriate 10313d903220SDoug Rabson * message or there isn't one. 10323d903220SDoug Rabson * If there is one then bail out of this loop. 10333d903220SDoug Rabson */ 10343d903220SDoug Rabson 10353d903220SDoug Rabson if (msghdr != NULL) 10363d903220SDoug Rabson break; 10373d903220SDoug Rabson 10383d903220SDoug Rabson /* 10393d903220SDoug Rabson * Hmph! No message found. Does the user want to wait? 10403d903220SDoug Rabson */ 10413d903220SDoug Rabson 10423d903220SDoug Rabson if ((msgflg & IPC_NOWAIT) != 0) { 10433d903220SDoug Rabson #ifdef MSG_DEBUG_OK 10443d903220SDoug Rabson printf("no appropriate message found (msgtyp=%d)\n", 10453d903220SDoug Rabson msgtyp); 10463d903220SDoug Rabson #endif 10473d903220SDoug Rabson /* The SVID says to return ENOMSG. */ 10483d903220SDoug Rabson #ifdef ENOMSG 10493d903220SDoug Rabson return(ENOMSG); 10503d903220SDoug Rabson #else 10513d903220SDoug Rabson /* Unfortunately, BSD doesn't define that code yet! */ 10523d903220SDoug Rabson return(EAGAIN); 10533d903220SDoug Rabson #endif 10543d903220SDoug Rabson } 10553d903220SDoug Rabson 10563d903220SDoug Rabson /* 10573d903220SDoug Rabson * Wait for something to happen 10583d903220SDoug Rabson */ 10593d903220SDoug Rabson 10603d903220SDoug Rabson #ifdef MSG_DEBUG_OK 10613d903220SDoug Rabson printf("msgrcv: goodnight\n"); 10623d903220SDoug Rabson #endif 10633d903220SDoug Rabson eval = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH, "msgwait", 10643d903220SDoug Rabson 0); 10653d903220SDoug Rabson #ifdef MSG_DEBUG_OK 10663d903220SDoug Rabson printf("msgrcv: good morning (eval=%d)\n", eval); 10673d903220SDoug Rabson #endif 10683d903220SDoug Rabson 10693d903220SDoug Rabson if (eval != 0) { 10703d903220SDoug Rabson #ifdef MSG_DEBUG_OK 10713d903220SDoug Rabson printf("msgsnd: interrupted system call\n"); 10723d903220SDoug Rabson #endif 10733d903220SDoug Rabson return(EINTR); 10743d903220SDoug Rabson } 10753d903220SDoug Rabson 10763d903220SDoug Rabson /* 10773d903220SDoug Rabson * Make sure that the msq queue still exists 10783d903220SDoug Rabson */ 10793d903220SDoug Rabson 10803d903220SDoug Rabson if (msqptr->msg_qbytes == 0 || 10813d903220SDoug Rabson msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) { 10823d903220SDoug Rabson #ifdef MSG_DEBUG_OK 10833d903220SDoug Rabson printf("msqid deleted\n"); 10843d903220SDoug Rabson #endif 10853d903220SDoug Rabson return(EIDRM); 10863d903220SDoug Rabson } 10873d903220SDoug Rabson } 10883d903220SDoug Rabson 10893d903220SDoug Rabson /* 10903d903220SDoug Rabson * Return the message to the user. 10913d903220SDoug Rabson * 10923d903220SDoug Rabson * First, do the bookkeeping (before we risk being interrupted). 10933d903220SDoug Rabson */ 10943d903220SDoug Rabson 10953d903220SDoug Rabson msqptr->msg_cbytes -= msghdr->msg_ts; 10963d903220SDoug Rabson msqptr->msg_qnum--; 10973d903220SDoug Rabson msqptr->msg_lrpid = p->p_pid; 1098227ee8a1SPoul-Henning Kamp msqptr->msg_rtime = time_second; 10993d903220SDoug Rabson 11003d903220SDoug Rabson /* 11013d903220SDoug Rabson * Make msgsz the actual amount that we'll be returning. 11023d903220SDoug Rabson * Note that this effectively truncates the message if it is too long 11033d903220SDoug Rabson * (since msgsz is never increased). 11043d903220SDoug Rabson */ 11053d903220SDoug Rabson 11063d903220SDoug Rabson #ifdef MSG_DEBUG_OK 11073d903220SDoug Rabson printf("found a message, msgsz=%d, msg_ts=%d\n", msgsz, 11083d903220SDoug Rabson msghdr->msg_ts); 11093d903220SDoug Rabson #endif 11103d903220SDoug Rabson if (msgsz > msghdr->msg_ts) 11113d903220SDoug Rabson msgsz = msghdr->msg_ts; 11123d903220SDoug Rabson 11133d903220SDoug Rabson /* 11143d903220SDoug Rabson * Return the type to the user. 11153d903220SDoug Rabson */ 11163d903220SDoug Rabson 11173d903220SDoug Rabson eval = copyout((caddr_t)&(msghdr->msg_type), user_msgp, 11183d903220SDoug Rabson sizeof(msghdr->msg_type)); 11193d903220SDoug Rabson if (eval != 0) { 11203d903220SDoug Rabson #ifdef MSG_DEBUG_OK 11213d903220SDoug Rabson printf("error (%d) copying out message type\n", eval); 11223d903220SDoug Rabson #endif 11233d903220SDoug Rabson msg_freehdr(msghdr); 11243d903220SDoug Rabson wakeup((caddr_t)msqptr); 11253d903220SDoug Rabson return(eval); 11263d903220SDoug Rabson } 112709a8dfa2SBruce Evans user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type); 11283d903220SDoug Rabson 11293d903220SDoug Rabson /* 11303d903220SDoug Rabson * Return the segments to the user 11313d903220SDoug Rabson */ 11323d903220SDoug Rabson 11333d903220SDoug Rabson next = msghdr->msg_spot; 11343d903220SDoug Rabson for (len = 0; len < msgsz; len += msginfo.msgssz) { 11353d903220SDoug Rabson size_t tlen; 11363d903220SDoug Rabson 1137565592bdSSADA Kenji if (msgsz - len > msginfo.msgssz) 11383d903220SDoug Rabson tlen = msginfo.msgssz; 11393d903220SDoug Rabson else 1140565592bdSSADA Kenji tlen = msgsz - len; 11413d903220SDoug Rabson if (next <= -1) 11423d903220SDoug Rabson panic("next too low #3"); 11433d903220SDoug Rabson if (next >= msginfo.msgseg) 11443d903220SDoug Rabson panic("next out of range #3"); 11453d903220SDoug Rabson eval = copyout((caddr_t)&msgpool[next * msginfo.msgssz], 11463d903220SDoug Rabson user_msgp, tlen); 11473d903220SDoug Rabson if (eval != 0) { 11483d903220SDoug Rabson #ifdef MSG_DEBUG_OK 11493d903220SDoug Rabson printf("error (%d) copying out message segment\n", 11503d903220SDoug Rabson eval); 11513d903220SDoug Rabson #endif 11523d903220SDoug Rabson msg_freehdr(msghdr); 11533d903220SDoug Rabson wakeup((caddr_t)msqptr); 11543d903220SDoug Rabson return(eval); 11553d903220SDoug Rabson } 115609a8dfa2SBruce Evans user_msgp = (char *)user_msgp + tlen; 11573d903220SDoug Rabson next = msgmaps[next].next; 11583d903220SDoug Rabson } 11593d903220SDoug Rabson 11603d903220SDoug Rabson /* 11613d903220SDoug Rabson * Done, return the actual number of bytes copied out. 11623d903220SDoug Rabson */ 11633d903220SDoug Rabson 11643d903220SDoug Rabson msg_freehdr(msghdr); 11653d903220SDoug Rabson wakeup((caddr_t)msqptr); 1166cb226aaaSPoul-Henning Kamp p->p_retval[0] = msgsz; 11673d903220SDoug Rabson return(0); 11683d903220SDoug Rabson } 1169