diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2002-12-22 04:55:06 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2002-12-22 04:55:06 +0000 |
commit | b831d29aa408e1a2bcb40dbb947176ecffa33c40 (patch) | |
tree | d2250efe3370cbfa39fca4a26307c7412b510e09 | |
parent | 59e1c612a240cbd3e6e51cd0082ddf2353903b93 (diff) |
Cosmetic changes:
o Add a DPRINTF macro instead of using debugging #ifdefs all over the place
o KNF says to use "return (foo)" not "return(foo)"
o #define EIDRM and ENOMSG at the top instead of using #ifdefs to work
around the fact that we don't have them later on.
-rw-r--r-- | sys/kern/sysv_msg.c | 391 |
1 files changed, 136 insertions, 255 deletions
diff --git a/sys/kern/sysv_msg.c b/sys/kern/sysv_msg.c index 6126eabb83e..4f0736d6ab1 100644 --- a/sys/kern/sysv_msg.c +++ b/sys/kern/sysv_msg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sysv_msg.c,v 1.13 2002/12/22 04:52:10 millert Exp $ */ +/* $OpenBSD: sysv_msg.c,v 1.14 2002/12/22 04:55:05 millert Exp $ */ /* $NetBSD: sysv_msg.c,v 1.19 1996/02/09 19:00:18 christos Exp $ */ /* @@ -30,8 +30,19 @@ #include <sys/mount.h> #include <sys/syscallargs.h> -#define MSG_DEBUG -#undef MSG_DEBUG_OK +#ifdef MSG_DEBUG +#define DPRINTF(x) printf x +#else +#define DPRINTF(x) +#endif + +/* XXX - OpenBSD doesn't have EIDRM or ENOMSG */ +#ifndef EIDRM +#define EIDRM EINVAL +#endif +#ifndef ENOMSG +#define ENOMSG EAGAIN +#endif int nfree_msgmaps; /* # of free map entries */ short free_msgmaps; /* head of linked list of free map entries */ @@ -164,33 +175,25 @@ sys_msgctl(p, v, retval) struct msqid_ds msqbuf; register struct msqid_ds *msqptr; -#ifdef MSG_DEBUG_OK - printf("call to msgctl(%d, %d, %p)\n", msqid, cmd, user_msqptr); -#endif + DPRINTF(("call to msgctl(%d, %d, %p)\n", msqid, cmd, user_msqptr)); msqid = IPCID_TO_IX(msqid); if (msqid < 0 || msqid >= msginfo.msgmni) { -#ifdef MSG_DEBUG_OK - printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid, - msginfo.msgmni); -#endif - return(EINVAL); + DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid, + msginfo.msgmni)); + return (EINVAL); } msqptr = &msqids[msqid]; if (msqptr->msg_qbytes == 0) { -#ifdef MSG_DEBUG_OK - printf("no such msqid\n"); -#endif - return(EINVAL); + DPRINTF(("no such msqid\n")); + return (EINVAL); } if (msqptr->msg_perm.seq != IPCID_TO_SEQ(SCARG(uap, msqid))) { -#ifdef MSG_DEBUG_OK - printf("wrong sequence number\n"); -#endif - return(EINVAL); + DPRINTF(("wrong sequence number\n")); + return (EINVAL); } eval = 0; @@ -202,7 +205,7 @@ sys_msgctl(p, v, retval) { struct msg *msghdr; if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_M)) != 0) - return(eval); + return (eval); /* Free the message headers */ msghdr = msqptr->msg_first; while (msghdr != NULL) { @@ -232,26 +235,23 @@ sys_msgctl(p, v, retval) case IPC_SET: if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_M))) - return(eval); + return (eval); if ((eval = copyin(user_msqptr, &msqbuf, sizeof(msqbuf))) != 0) - return(eval); + return (eval); if (msqbuf.msg_qbytes > msqptr->msg_qbytes && cred->cr_uid != 0) - return(EPERM); + return (EPERM); if (msqbuf.msg_qbytes > msginfo.msgmnb) { -#ifdef MSG_DEBUG_OK - printf("can't increase msg_qbytes beyond %d (truncating)\n", - msginfo.msgmnb); -#endif - msqbuf.msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */ + DPRINTF(("can't increase msg_qbytes beyond %d " + "(truncating)\n", msginfo.msgmnb)); + /* silently restrict qbytes to system limit */ + msqbuf.msg_qbytes = msginfo.msgmnb; } if (msqbuf.msg_qbytes == 0) { -#ifdef MSG_DEBUG_OK - printf("can't reduce msg_qbytes to 0\n"); -#endif - return(EINVAL); /* non-standard errno! */ + DPRINTF(("can't reduce msg_qbytes to 0\n")); + return (EINVAL); /* non-standard errno! */ } - msqptr->msg_perm.uid = msqbuf.msg_perm.uid; /* change the owner */ - msqptr->msg_perm.gid = msqbuf.msg_perm.gid; /* change the owner */ + msqptr->msg_perm.uid = msqbuf.msg_perm.uid; /* change owner */ + msqptr->msg_perm.gid = msqbuf.msg_perm.gid; /* change owner */ msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) | (msqbuf.msg_perm.mode & 0777); msqptr->msg_qbytes = msqbuf.msg_qbytes; @@ -260,25 +260,21 @@ sys_msgctl(p, v, retval) case IPC_STAT: if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_R))) { -#ifdef MSG_DEBUG_OK - printf("requester doesn't have read access\n"); -#endif - return(eval); + DPRINTF(("requester doesn't have read access\n")); + return (eval); } eval = copyout((caddr_t)msqptr, user_msqptr, sizeof(struct msqid_ds)); break; default: -#ifdef MSG_DEBUG_OK - printf("invalid command %d\n", cmd); -#endif - return(EINVAL); + DPRINTF(("invalid command %d\n", cmd)); + return (EINVAL); } if (eval == 0) *retval = rval; - return(eval); + return (eval); } int @@ -297,9 +293,7 @@ sys_msgget(p, v, retval) struct ucred *cred = p->p_ucred; register struct msqid_ds *msqptr = NULL; -#ifdef MSG_DEBUG_OK - printf("msgget(0x%x, 0%o)\n", key, msgflg); -#endif + DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg)); if (key != IPC_PRIVATE) { for (msqid = 0; msqid < msginfo.msgmni; msqid++) { @@ -309,29 +303,21 @@ sys_msgget(p, v, retval) break; } if (msqid < msginfo.msgmni) { -#ifdef MSG_DEBUG_OK - printf("found public key\n"); -#endif + DPRINTF(("found public key\n")); if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) { -#ifdef MSG_DEBUG_OK - printf("not exclusive\n"); -#endif - return(EEXIST); + DPRINTF(("not exclusive\n")); + return (EEXIST); } if ((eval = ipcperm(cred, &msqptr->msg_perm, msgflg & 0700 ))) { -#ifdef MSG_DEBUG_OK - printf("requester doesn't have 0%o access\n", - msgflg & 0700); -#endif - return(eval); + DPRINTF(("requester doesn't have 0%o access\n", + msgflg & 0700)); + return (eval); } goto found; } } -#ifdef MSG_DEBUG_OK - printf("need to allocate the msqid_ds\n"); -#endif + DPRINTF(("need to allocate the msqid_ds\n")); if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) { for (msqid = 0; msqid < msginfo.msgmni; msqid++) { /* @@ -346,14 +332,10 @@ sys_msgget(p, v, retval) break; } if (msqid == msginfo.msgmni) { -#ifdef MSG_DEBUG_OK - printf("no more msqid_ds's available\n"); -#endif - return(ENOSPC); + DPRINTF(("no more msqid_ds's available\n")); + return (ENOSPC); } -#ifdef MSG_DEBUG_OK - printf("msqid %d is available\n", msqid); -#endif + DPRINTF(("msqid %d is available\n", msqid)); msqptr->msg_perm.key = key; msqptr->msg_perm.cuid = cred->cr_uid; msqptr->msg_perm.uid = cred->cr_uid; @@ -373,16 +355,14 @@ sys_msgget(p, v, retval) msqptr->msg_rtime = 0; msqptr->msg_ctime = time.tv_sec; } else { -#ifdef MSG_DEBUG_OK - printf("didn't find it and wasn't asked to create it\n"); -#endif - return(ENOENT); + DPRINTF(("didn't find it and wasn't asked to create it\n")); + return (ENOENT); } found: /* Construct the unique msqid */ *retval = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm); - return(0); + return (0); } int @@ -407,47 +387,35 @@ sys_msgsnd(p, v, retval) register struct msg *msghdr; short next; -#ifdef MSG_DEBUG_OK - printf("call to msgsnd(%d, %p, %d, %d)\n", msqid, user_msgp, msgsz, - msgflg); -#endif + DPRINTF(("call to msgsnd(%d, %p, %d, %d)\n", msqid, user_msgp, msgsz, + msgflg)); msqid = IPCID_TO_IX(msqid); if (msqid < 0 || msqid >= msginfo.msgmni) { -#ifdef MSG_DEBUG_OK - printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid, - msginfo.msgmni); -#endif - return(EINVAL); + DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid, + msginfo.msgmni)); + return (EINVAL); } msqptr = &msqids[msqid]; if (msqptr->msg_qbytes == 0) { -#ifdef MSG_DEBUG_OK - printf("no such message queue id\n"); -#endif - return(EINVAL); + DPRINTF(("no such message queue id\n")); + return (EINVAL); } if (msqptr->msg_perm.seq != IPCID_TO_SEQ(SCARG(uap, msqid))) { -#ifdef MSG_DEBUG_OK - printf("wrong sequence number\n"); -#endif - return(EINVAL); + DPRINTF(("wrong sequence number\n")); + return (EINVAL); } if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_W))) { -#ifdef MSG_DEBUG_OK - printf("requester doesn't have write access\n"); -#endif - return(eval); + DPRINTF(("requester doesn't have write access\n")); + return (eval); } segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz; -#ifdef MSG_DEBUG_OK - printf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz, - segs_needed); -#endif + DPRINTF(("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, + msginfo.msgssz, segs_needed)); for (;;) { int need_more_resources = 0; @@ -457,34 +425,24 @@ sys_msgsnd(p, v, retval) */ if (msgsz > msqptr->msg_qbytes) { -#ifdef MSG_DEBUG_OK - printf("msgsz > msqptr->msg_qbytes\n"); -#endif - return(EINVAL); + DPRINTF(("msgsz > msqptr->msg_qbytes\n")); + return (EINVAL); } if (msqptr->msg_perm.mode & MSG_LOCKED) { -#ifdef MSG_DEBUG_OK - printf("msqid is locked\n"); -#endif + DPRINTF(("msqid is locked\n")); need_more_resources = 1; } if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) { -#ifdef MSG_DEBUG_OK - printf("msgsz + msg_cbytes > msg_qbytes\n"); -#endif + DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n")); need_more_resources = 1; } if (segs_needed > nfree_msgmaps) { -#ifdef MSG_DEBUG_OK - printf("segs_needed > nfree_msgmaps\n"); -#endif + DPRINTF(("segs_needed > nfree_msgmaps\n")); need_more_resources = 1; } if (free_msghdrs == NULL) { -#ifdef MSG_DEBUG_OK - printf("no more msghdrs\n"); -#endif + DPRINTF(("no more msghdrs\n")); need_more_resources = 1; } @@ -492,41 +450,30 @@ sys_msgsnd(p, v, retval) int we_own_it; if ((msgflg & IPC_NOWAIT) != 0) { -#ifdef MSG_DEBUG_OK - printf("need more resources but caller doesn't want to wait\n"); -#endif - return(EAGAIN); + DPRINTF(("need more resources but caller " + "doesn't want to wait\n")); + return (EAGAIN); } if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) { -#ifdef MSG_DEBUG_OK - printf("we don't own the msqid_ds\n"); -#endif + DPRINTF(("we don't own the msqid_ds\n")); we_own_it = 0; } else { /* Force later arrivals to wait for our request */ -#ifdef MSG_DEBUG_OK - printf("we own the msqid_ds\n"); -#endif + DPRINTF(("we own the msqid_ds\n")); msqptr->msg_perm.mode |= MSG_LOCKED; we_own_it = 1; } -#ifdef MSG_DEBUG_OK - printf("goodnight\n"); -#endif + DPRINTF(("goodnight\n")); eval = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH, "msgwait", 0); -#ifdef MSG_DEBUG_OK - printf("good morning, eval=%d\n", eval); -#endif + DPRINTF(("good morning, eval=%d\n", eval)); if (we_own_it) msqptr->msg_perm.mode &= ~MSG_LOCKED; if (eval != 0) { -#ifdef MSG_DEBUG_OK - printf("msgsnd: interrupted system call\n"); -#endif - return(EINTR); + DPRINTF(("msgsnd: interrupted system call\n")); + return (EINTR); } /* @@ -534,23 +481,12 @@ sys_msgsnd(p, v, retval) */ if (msqptr->msg_qbytes == 0) { -#ifdef MSG_DEBUG_OK - printf("msqid deleted\n"); -#endif - /* The SVID says to return EIDRM. */ -#ifdef EIDRM - return(EIDRM); -#else - /* Unfortunately, BSD doesn't define that code - yet! */ - return(EINVAL); -#endif + DPRINTF(("msqid deleted\n")); + return (EIDRM); } } else { -#ifdef MSG_DEBUG_OK - printf("got all the resources that we need\n"); -#endif + DPRINTF(("got all the resources that we need\n")); break; } } @@ -609,9 +545,7 @@ sys_msgsnd(p, v, retval) if (next >= msginfo.msgseg) panic("next out of range #1"); #endif -#ifdef MSG_DEBUG_OK - printf("allocating segment %d to message\n", next); -#endif + DPRINTF(("allocating segment %d to message\n", next)); free_msgmaps = msgmaps[next].next; nfree_msgmaps--; msgmaps[next].next = msghdr->msg_spot; @@ -625,13 +559,11 @@ sys_msgsnd(p, v, retval) if ((eval = copyin(user_msgp, &msghdr->msg_type, sizeof(msghdr->msg_type))) != 0) { -#ifdef MSG_DEBUG_OK - printf("error %d copying the message type\n", eval); -#endif + DPRINTF(("error %d copying the message type\n", eval)); msg_freehdr(msghdr); msqptr->msg_perm.mode &= ~MSG_LOCKED; wakeup((caddr_t)msqptr); - return(eval); + return (eval); } user_msgp += sizeof(msghdr->msg_type); @@ -643,10 +575,8 @@ sys_msgsnd(p, v, retval) msg_freehdr(msghdr); msqptr->msg_perm.mode &= ~MSG_LOCKED; wakeup((caddr_t)msqptr); -#ifdef MSG_DEBUG_OK - printf("mtype (%d) < 1\n", msghdr->msg_type); -#endif - return(EINVAL); + DPRINTF(("mtype (%d) < 1\n", msghdr->msg_type)); + return (EINVAL); } /* @@ -668,13 +598,12 @@ sys_msgsnd(p, v, retval) #endif if ((eval = copyin(user_msgp, &msgpool[next * msginfo.msgssz], tlen)) != 0) { -#ifdef MSG_DEBUG_OK - printf("error %d copying in message segment\n", eval); -#endif + DPRINTF(("error %d copying in message segment\n", + eval)); msg_freehdr(msghdr); msqptr->msg_perm.mode &= ~MSG_LOCKED; wakeup((caddr_t)msqptr); - return(eval); + return (eval); } msgsz -= tlen; user_msgp += tlen; @@ -697,13 +626,7 @@ sys_msgsnd(p, v, retval) if (msqptr->msg_qbytes == 0) { msg_freehdr(msghdr); wakeup((caddr_t)msqptr); - /* The SVID says to return EIDRM. */ -#ifdef EIDRM - return(EIDRM); -#else - /* Unfortunately, BSD doesn't define that code yet! */ - return(EINVAL); -#endif + return (EIDRM); } /* @@ -726,7 +649,7 @@ sys_msgsnd(p, v, retval) wakeup((caddr_t)msqptr); *retval = 0; - return(0); + return (0); } int @@ -754,49 +677,37 @@ sys_msgrcv(p, v, retval) int eval; short next; -#ifdef MSG_DEBUG_OK - printf("call to msgrcv(%d, %p, %d, %ld, %d)\n", msqid, user_msgp, - msgsz, msgtyp, msgflg); -#endif + DPRINTF(("call to msgrcv(%d, %p, %d, %ld, %d)\n", msqid, user_msgp, + msgsz, msgtyp, msgflg)); msqid = IPCID_TO_IX(msqid); if (msqid < 0 || msqid >= msginfo.msgmni) { -#ifdef MSG_DEBUG_OK - printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid, - msginfo.msgmni); -#endif - return(EINVAL); + DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid, + msginfo.msgmni)); + return (EINVAL); } msqptr = &msqids[msqid]; if (msqptr->msg_qbytes == 0) { -#ifdef MSG_DEBUG_OK - printf("no such message queue id\n"); -#endif - return(EINVAL); + DPRINTF(("no such message queue id\n")); + return (EINVAL); } if (msqptr->msg_perm.seq != IPCID_TO_SEQ(SCARG(uap, msqid))) { -#ifdef MSG_DEBUG_OK - printf("wrong sequence number\n"); -#endif - return(EINVAL); + DPRINTF(("wrong sequence number\n")); + return (EINVAL); } if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_R))) { -#ifdef MSG_DEBUG_OK - printf("requester doesn't have read access\n"); -#endif - return(eval); + DPRINTF(("requester doesn't have read access\n")); + return (eval); } #if 0 /* cannot happen, msgsz is unsigned */ if (msgsz < 0) { -#ifdef MSG_DEBUG_OK - printf("msgsz < 0\n"); -#endif - return(EINVAL); + DPRINTF(("msgsz < 0\n")); + return (EINVAL); } #endif @@ -807,11 +718,10 @@ sys_msgrcv(p, v, retval) if (msghdr != NULL) { if (msgsz < msghdr->msg_ts && (msgflg & MSG_NOERROR) == 0) { -#ifdef MSG_DEBUG_OK - printf("first message on the queue is too big (want %d, got %d)\n", - msgsz, msghdr->msg_ts); -#endif - return(E2BIG); + DPRINTF(("first message on the queue " + "is too big (want %d, got %d)\n", + msgsz, msghdr->msg_ts)); + return (E2BIG); } if (msqptr->msg_first == msqptr->msg_last) { msqptr->msg_first = NULL; @@ -842,17 +752,16 @@ sys_msgrcv(p, v, retval) if (msgtyp == msghdr->msg_type || msghdr->msg_type <= -msgtyp) { -#ifdef MSG_DEBUG_OK - printf("found message type %d, requested %d\n", - msghdr->msg_type, msgtyp); -#endif + DPRINTF(("found message type %d, " + "requested %d\n", msghdr->msg_type, + msgtyp)); if (msgsz < msghdr->msg_ts && (msgflg & MSG_NOERROR) == 0) { -#ifdef MSG_DEBUG_OK - printf("requested message on the queue is too big (want %d, got %d)\n", - msgsz, msghdr->msg_ts); -#endif - return(E2BIG); + DPRINTF(("requested message on " + "the queue is too big " + "(want %d, got %d)\n", + msgsz, msghdr->msg_ts)); + return (E2BIG); } *prev = msghdr->msg_next; if (msghdr == msqptr->msg_last) { @@ -895,37 +804,23 @@ sys_msgrcv(p, v, retval) */ if ((msgflg & IPC_NOWAIT) != 0) { -#ifdef MSG_DEBUG_OK - printf("no appropriate message found (msgtyp=%d)\n", - msgtyp); -#endif - /* The SVID says to return ENOMSG. */ -#ifdef ENOMSG - return(ENOMSG); -#else - /* Unfortunately, BSD doesn't define that code yet! */ - return(EAGAIN); -#endif + DPRINTF(("no appropriate message found (msgtyp=%d)\n", + msgtyp)); + return (ENOMSG); } /* * Wait for something to happen */ -#ifdef MSG_DEBUG_OK - printf("msgrcv: goodnight\n"); -#endif + DPRINTF(("msgrcv: goodnight\n")); eval = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH, "msgwait", 0); -#ifdef MSG_DEBUG_OK - printf("msgrcv: good morning (eval=%d)\n", eval); -#endif + DPRINTF(("msgrcv: good morning (eval=%d)\n", eval)); if (eval != 0) { -#ifdef MSG_DEBUG_OK - printf("msgsnd: interrupted system call\n"); -#endif - return(EINTR); + DPRINTF(("msgsnd: interrupted system call\n")); + return (EINTR); } /* @@ -934,16 +829,8 @@ sys_msgrcv(p, v, retval) if (msqptr->msg_qbytes == 0 || msqptr->msg_perm.seq != IPCID_TO_SEQ(SCARG(uap, msqid))) { -#ifdef MSG_DEBUG_OK - printf("msqid deleted\n"); -#endif - /* The SVID says to return EIDRM. */ -#ifdef EIDRM - return(EIDRM); -#else - /* Unfortunately, BSD doesn't define that code yet! */ - return(EINVAL); -#endif + DPRINTF(("msqid deleted\n")); + return (EIDRM); } } @@ -964,10 +851,8 @@ sys_msgrcv(p, v, retval) * (since msgsz is never increased). */ -#ifdef MSG_DEBUG_OK - printf("found a message, msgsz=%d, msg_ts=%d\n", msgsz, - msghdr->msg_ts); -#endif + DPRINTF(("found a message, msgsz=%d, msg_ts=%d\n", msgsz, + msghdr->msg_ts)); if (msgsz > msghdr->msg_ts) msgsz = msghdr->msg_ts; @@ -978,12 +863,10 @@ sys_msgrcv(p, v, retval) eval = copyout((caddr_t)&msghdr->msg_type, user_msgp, sizeof(msghdr->msg_type)); if (eval != 0) { -#ifdef MSG_DEBUG_OK - printf("error (%d) copying out message type\n", eval); -#endif + DPRINTF(("error (%d) copying out message type\n", eval)); msg_freehdr(msghdr); wakeup((caddr_t)msqptr); - return(eval); + return (eval); } user_msgp += sizeof(msghdr->msg_type); @@ -1008,13 +891,11 @@ sys_msgrcv(p, v, retval) eval = copyout((caddr_t)&msgpool[next * msginfo.msgssz], user_msgp, tlen); if (eval != 0) { -#ifdef MSG_DEBUG_OK - printf("error (%d) copying out message segment\n", - eval); -#endif + DPRINTF(("error (%d) copying out message segment\n", + eval)); msg_freehdr(msghdr); wakeup((caddr_t)msqptr); - return(eval); + return (eval); } user_msgp += tlen; next = msgmaps[next].next; @@ -1027,5 +908,5 @@ sys_msgrcv(p, v, retval) msg_freehdr(msghdr); wakeup((caddr_t)msqptr); *retval = msgsz; - return(0); + return (0); } |