Commit 5a06a363 authored by Ingo Molnar's avatar Ingo Molnar Committed by Linus Torvalds

[PATCH] ipc/msg.c: clean up coding style

Clean up ipc/msg.c to conform to Documentation/CodingStyle.  (before it was
an inconsistent hodepodge of various coding styles)

Verified that the before/after .o's are identical.
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 873302c7
/* /*
* linux/ipc/msg.c * linux/ipc/msg.c
* Copyright (C) 1992 Krishna Balasubramanian * Copyright (C) 1992 Krishna Balasubramanian
* *
* Removed all the remaining kerneld mess * Removed all the remaining kerneld mess
* Catch the -EFAULT stuff properly * Catch the -EFAULT stuff properly
...@@ -41,22 +41,24 @@ int msg_ctlmax = MSGMAX; ...@@ -41,22 +41,24 @@ int msg_ctlmax = MSGMAX;
int msg_ctlmnb = MSGMNB; int msg_ctlmnb = MSGMNB;
int msg_ctlmni = MSGMNI; int msg_ctlmni = MSGMNI;
/* one msg_receiver structure for each sleeping receiver */ /*
* one msg_receiver structure for each sleeping receiver:
*/
struct msg_receiver { struct msg_receiver {
struct list_head r_list; struct list_head r_list;
struct task_struct* r_tsk; struct task_struct *r_tsk;
int r_mode; int r_mode;
long r_msgtype; long r_msgtype;
long r_maxsize; long r_maxsize;
struct msg_msg* volatile r_msg; volatile struct msg_msg *r_msg;
}; };
/* one msg_sender for each sleeping sender */ /* one msg_sender for each sleeping sender */
struct msg_sender { struct msg_sender {
struct list_head list; struct list_head list;
struct task_struct* tsk; struct task_struct *tsk;
}; };
#define SEARCH_ANY 1 #define SEARCH_ANY 1
...@@ -64,45 +66,42 @@ struct msg_sender { ...@@ -64,45 +66,42 @@ struct msg_sender {
#define SEARCH_NOTEQUAL 3 #define SEARCH_NOTEQUAL 3
#define SEARCH_LESSEQUAL 4 #define SEARCH_LESSEQUAL 4
static atomic_t msg_bytes = ATOMIC_INIT(0); static atomic_t msg_bytes = ATOMIC_INIT(0);
static atomic_t msg_hdrs = ATOMIC_INIT(0); static atomic_t msg_hdrs = ATOMIC_INIT(0);
static struct ipc_ids msg_ids; static struct ipc_ids msg_ids;
#define msg_lock(id) ((struct msg_queue*)ipc_lock(&msg_ids,id)) #define msg_lock(id) ((struct msg_queue *)ipc_lock(&msg_ids, id))
#define msg_unlock(msq) ipc_unlock(&(msq)->q_perm) #define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
#define msg_rmid(id) ((struct msg_queue*)ipc_rmid(&msg_ids,id)) #define msg_rmid(id) ((struct msg_queue *)ipc_rmid(&msg_ids, id))
#define msg_checkid(msq, msgid) \ #define msg_checkid(msq, msgid) ipc_checkid(&msg_ids, &msq->q_perm, msgid)
ipc_checkid(&msg_ids,&msq->q_perm,msgid) #define msg_buildid(id, seq) ipc_buildid(&msg_ids, id, seq)
#define msg_buildid(id, seq) \
ipc_buildid(&msg_ids, id, seq)
static void freeque (struct msg_queue *msq, int id); static void freeque(struct msg_queue *msq, int id);
static int newque (key_t key, int msgflg); static int newque(key_t key, int msgflg);
#ifdef CONFIG_PROC_FS #ifdef CONFIG_PROC_FS
static int sysvipc_msg_proc_show(struct seq_file *s, void *it); static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
#endif #endif
void __init msg_init (void) void __init msg_init(void)
{ {
ipc_init_ids(&msg_ids,msg_ctlmni); ipc_init_ids(&msg_ids, msg_ctlmni);
ipc_init_proc_interface("sysvipc/msg", ipc_init_proc_interface("sysvipc/msg",
" key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n", " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
&msg_ids, &msg_ids,
sysvipc_msg_proc_show); sysvipc_msg_proc_show);
} }
static int newque (key_t key, int msgflg) static int newque(key_t key, int msgflg)
{ {
int id;
int retval;
struct msg_queue *msq; struct msg_queue *msq;
int id, retval;
msq = ipc_rcu_alloc(sizeof(*msq)); msq = ipc_rcu_alloc(sizeof(*msq));
if (!msq) if (!msq)
return -ENOMEM; return -ENOMEM;
msq->q_perm.mode = (msgflg & S_IRWXUGO); msq->q_perm.mode = msgflg & S_IRWXUGO;
msq->q_perm.key = key; msq->q_perm.key = key;
msq->q_perm.security = NULL; msq->q_perm.security = NULL;
...@@ -113,13 +112,13 @@ static int newque (key_t key, int msgflg) ...@@ -113,13 +112,13 @@ static int newque (key_t key, int msgflg)
} }
id = ipc_addid(&msg_ids, &msq->q_perm, msg_ctlmni); id = ipc_addid(&msg_ids, &msq->q_perm, msg_ctlmni);
if(id == -1) { if (id == -1) {
security_msg_queue_free(msq); security_msg_queue_free(msq);
ipc_rcu_putref(msq); ipc_rcu_putref(msq);
return -ENOSPC; return -ENOSPC;
} }
msq->q_id = msg_buildid(id,msq->q_perm.seq); msq->q_id = msg_buildid(id, msq->q_perm.seq);
msq->q_stime = msq->q_rtime = 0; msq->q_stime = msq->q_rtime = 0;
msq->q_ctime = get_seconds(); msq->q_ctime = get_seconds();
msq->q_cbytes = msq->q_qnum = 0; msq->q_cbytes = msq->q_qnum = 0;
...@@ -133,44 +132,44 @@ static int newque (key_t key, int msgflg) ...@@ -133,44 +132,44 @@ static int newque (key_t key, int msgflg)
return msq->q_id; return msq->q_id;
} }
static inline void ss_add(struct msg_queue* msq, struct msg_sender* mss) static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
{ {
mss->tsk=current; mss->tsk = current;
current->state=TASK_INTERRUPTIBLE; current->state = TASK_INTERRUPTIBLE;
list_add_tail(&mss->list,&msq->q_senders); list_add_tail(&mss->list, &msq->q_senders);
} }
static inline void ss_del(struct msg_sender* mss) static inline void ss_del(struct msg_sender *mss)
{ {
if(mss->list.next != NULL) if (mss->list.next != NULL)
list_del(&mss->list); list_del(&mss->list);
} }
static void ss_wakeup(struct list_head* h, int kill) static void ss_wakeup(struct list_head *h, int kill)
{ {
struct list_head *tmp; struct list_head *tmp;
tmp = h->next; tmp = h->next;
while (tmp != h) { while (tmp != h) {
struct msg_sender* mss; struct msg_sender *mss;
mss = list_entry(tmp,struct msg_sender,list); mss = list_entry(tmp, struct msg_sender, list);
tmp = tmp->next; tmp = tmp->next;
if(kill) if (kill)
mss->list.next=NULL; mss->list.next = NULL;
wake_up_process(mss->tsk); wake_up_process(mss->tsk);
} }
} }
static void expunge_all(struct msg_queue* msq, int res) static void expunge_all(struct msg_queue *msq, int res)
{ {
struct list_head *tmp; struct list_head *tmp;
tmp = msq->q_receivers.next; tmp = msq->q_receivers.next;
while (tmp != &msq->q_receivers) { while (tmp != &msq->q_receivers) {
struct msg_receiver* msr; struct msg_receiver *msr;
msr = list_entry(tmp,struct msg_receiver,r_list); msr = list_entry(tmp, struct msg_receiver, r_list);
tmp = tmp->next; tmp = tmp->next;
msr->r_msg = NULL; msr->r_msg = NULL;
wake_up_process(msr->r_tsk); wake_up_process(msr->r_tsk);
...@@ -178,26 +177,28 @@ static void expunge_all(struct msg_queue* msq, int res) ...@@ -178,26 +177,28 @@ static void expunge_all(struct msg_queue* msq, int res)
msr->r_msg = ERR_PTR(res); msr->r_msg = ERR_PTR(res);
} }
} }
/*
* freeque() wakes up waiters on the sender and receiver waiting queue, /*
* removes the message queue from message queue ID * freeque() wakes up waiters on the sender and receiver waiting queue,
* removes the message queue from message queue ID
* array, and cleans up all the messages associated with this queue. * array, and cleans up all the messages associated with this queue.
* *
* msg_ids.mutex and the spinlock for this message queue is hold * msg_ids.mutex and the spinlock for this message queue is hold
* before freeque() is called. msg_ids.mutex remains locked on exit. * before freeque() is called. msg_ids.mutex remains locked on exit.
*/ */
static void freeque (struct msg_queue *msq, int id) static void freeque(struct msg_queue *msq, int id)
{ {
struct list_head *tmp; struct list_head *tmp;
expunge_all(msq,-EIDRM); expunge_all(msq, -EIDRM);
ss_wakeup(&msq->q_senders,1); ss_wakeup(&msq->q_senders, 1);
msq = msg_rmid(id); msq = msg_rmid(id);
msg_unlock(msq); msg_unlock(msq);
tmp = msq->q_messages.next; tmp = msq->q_messages.next;
while(tmp != &msq->q_messages) { while (tmp != &msq->q_messages) {
struct msg_msg* msg = list_entry(tmp,struct msg_msg,m_list); struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
tmp = tmp->next; tmp = tmp->next;
atomic_dec(&msg_hdrs); atomic_dec(&msg_hdrs);
free_msg(msg); free_msg(msg);
...@@ -207,10 +208,10 @@ static void freeque (struct msg_queue *msq, int id) ...@@ -207,10 +208,10 @@ static void freeque (struct msg_queue *msq, int id)
ipc_rcu_putref(msq); ipc_rcu_putref(msq);
} }
asmlinkage long sys_msgget (key_t key, int msgflg) asmlinkage long sys_msgget(key_t key, int msgflg)
{ {
int id, ret = -EPERM;
struct msg_queue *msq; struct msg_queue *msq;
int id, ret = -EPERM;
mutex_lock(&msg_ids.mutex); mutex_lock(&msg_ids.mutex);
if (key == IPC_PRIVATE) if (key == IPC_PRIVATE)
...@@ -224,31 +225,34 @@ asmlinkage long sys_msgget (key_t key, int msgflg) ...@@ -224,31 +225,34 @@ asmlinkage long sys_msgget (key_t key, int msgflg)
ret = -EEXIST; ret = -EEXIST;
} else { } else {
msq = msg_lock(id); msq = msg_lock(id);
BUG_ON(msq==NULL); BUG_ON(msq == NULL);
if (ipcperms(&msq->q_perm, msgflg)) if (ipcperms(&msq->q_perm, msgflg))
ret = -EACCES; ret = -EACCES;
else { else {
int qid = msg_buildid(id, msq->q_perm.seq); int qid = msg_buildid(id, msq->q_perm.seq);
ret = security_msg_queue_associate(msq, msgflg);
ret = security_msg_queue_associate(msq, msgflg);
if (!ret) if (!ret)
ret = qid; ret = qid;
} }
msg_unlock(msq); msg_unlock(msq);
} }
mutex_unlock(&msg_ids.mutex); mutex_unlock(&msg_ids.mutex);
return ret; return ret;
} }
static inline unsigned long copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version) static inline unsigned long
copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
{ {
switch(version) { switch(version) {
case IPC_64: case IPC_64:
return copy_to_user (buf, in, sizeof(*in)); return copy_to_user(buf, in, sizeof(*in));
case IPC_OLD: case IPC_OLD:
{ {
struct msqid_ds out; struct msqid_ds out;
memset(&out,0,sizeof(out)); memset(&out, 0, sizeof(out));
ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm); ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
...@@ -256,18 +260,18 @@ static inline unsigned long copy_msqid_to_user(void __user *buf, struct msqid64_ ...@@ -256,18 +260,18 @@ static inline unsigned long copy_msqid_to_user(void __user *buf, struct msqid64_
out.msg_rtime = in->msg_rtime; out.msg_rtime = in->msg_rtime;
out.msg_ctime = in->msg_ctime; out.msg_ctime = in->msg_ctime;
if(in->msg_cbytes > USHRT_MAX) if (in->msg_cbytes > USHRT_MAX)
out.msg_cbytes = USHRT_MAX; out.msg_cbytes = USHRT_MAX;
else else
out.msg_cbytes = in->msg_cbytes; out.msg_cbytes = in->msg_cbytes;
out.msg_lcbytes = in->msg_cbytes; out.msg_lcbytes = in->msg_cbytes;
if(in->msg_qnum > USHRT_MAX) if (in->msg_qnum > USHRT_MAX)
out.msg_qnum = USHRT_MAX; out.msg_qnum = USHRT_MAX;
else else
out.msg_qnum = in->msg_qnum; out.msg_qnum = in->msg_qnum;
if(in->msg_qbytes > USHRT_MAX) if (in->msg_qbytes > USHRT_MAX)
out.msg_qbytes = USHRT_MAX; out.msg_qbytes = USHRT_MAX;
else else
out.msg_qbytes = in->msg_qbytes; out.msg_qbytes = in->msg_qbytes;
...@@ -276,8 +280,8 @@ static inline unsigned long copy_msqid_to_user(void __user *buf, struct msqid64_ ...@@ -276,8 +280,8 @@ static inline unsigned long copy_msqid_to_user(void __user *buf, struct msqid64_
out.msg_lspid = in->msg_lspid; out.msg_lspid = in->msg_lspid;
out.msg_lrpid = in->msg_lrpid; out.msg_lrpid = in->msg_lrpid;
return copy_to_user (buf, &out, sizeof(out)); return copy_to_user(buf, &out, sizeof(out));
} }
default: default:
return -EINVAL; return -EINVAL;
} }
...@@ -290,14 +294,15 @@ struct msq_setbuf { ...@@ -290,14 +294,15 @@ struct msq_setbuf {
mode_t mode; mode_t mode;
}; };
static inline unsigned long copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version) static inline unsigned long
copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
{ {
switch(version) { switch(version) {
case IPC_64: case IPC_64:
{ {
struct msqid64_ds tbuf; struct msqid64_ds tbuf;
if (copy_from_user (&tbuf, buf, sizeof (tbuf))) if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
return -EFAULT; return -EFAULT;
out->qbytes = tbuf.msg_qbytes; out->qbytes = tbuf.msg_qbytes;
...@@ -306,60 +311,61 @@ static inline unsigned long copy_msqid_from_user(struct msq_setbuf *out, void __ ...@@ -306,60 +311,61 @@ static inline unsigned long copy_msqid_from_user(struct msq_setbuf *out, void __
out->mode = tbuf.msg_perm.mode; out->mode = tbuf.msg_perm.mode;
return 0; return 0;
} }
case IPC_OLD: case IPC_OLD:
{ {
struct msqid_ds tbuf_old; struct msqid_ds tbuf_old;
if (copy_from_user (&tbuf_old, buf, sizeof (tbuf_old))) if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
return -EFAULT; return -EFAULT;
out->uid = tbuf_old.msg_perm.uid; out->uid = tbuf_old.msg_perm.uid;
out->gid = tbuf_old.msg_perm.gid; out->gid = tbuf_old.msg_perm.gid;
out->mode = tbuf_old.msg_perm.mode; out->mode = tbuf_old.msg_perm.mode;
if(tbuf_old.msg_qbytes == 0) if (tbuf_old.msg_qbytes == 0)
out->qbytes = tbuf_old.msg_lqbytes; out->qbytes = tbuf_old.msg_lqbytes;
else else
out->qbytes = tbuf_old.msg_qbytes; out->qbytes = tbuf_old.msg_qbytes;
return 0; return 0;
} }
default: default:
return -EINVAL; return -EINVAL;
} }
} }
asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds __user *buf) asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
{ {
int err, version;
struct msg_queue *msq;
struct msq_setbuf setbuf;
struct kern_ipc_perm *ipcp; struct kern_ipc_perm *ipcp;
struct msq_setbuf setbuf;
struct msg_queue *msq;
int err, version;
if (msqid < 0 || cmd < 0) if (msqid < 0 || cmd < 0)
return -EINVAL; return -EINVAL;
version = ipc_parse_version(&cmd); version = ipc_parse_version(&cmd);
switch (cmd) { switch (cmd) {
case IPC_INFO: case IPC_INFO:
case MSG_INFO: case MSG_INFO:
{ {
struct msginfo msginfo; struct msginfo msginfo;
int max_id; int max_id;
if (!buf) if (!buf)
return -EFAULT; return -EFAULT;
/* We must not return kernel stack data. /*
* We must not return kernel stack data.
* due to padding, it's not enough * due to padding, it's not enough
* to set all member fields. * to set all member fields.
*/ */
err = security_msg_queue_msgctl(NULL, cmd); err = security_msg_queue_msgctl(NULL, cmd);
if (err) if (err)
return err; return err;
memset(&msginfo,0,sizeof(msginfo)); memset(&msginfo, 0, sizeof(msginfo));
msginfo.msgmni = msg_ctlmni; msginfo.msgmni = msg_ctlmni;
msginfo.msgmax = msg_ctlmax; msginfo.msgmax = msg_ctlmax;
msginfo.msgmnb = msg_ctlmnb; msginfo.msgmnb = msg_ctlmnb;
...@@ -377,36 +383,37 @@ asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds __user *buf) ...@@ -377,36 +383,37 @@ asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds __user *buf)
} }
max_id = msg_ids.max_id; max_id = msg_ids.max_id;
mutex_unlock(&msg_ids.mutex); mutex_unlock(&msg_ids.mutex);
if (copy_to_user (buf, &msginfo, sizeof(struct msginfo))) if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
return -EFAULT; return -EFAULT;
return (max_id < 0) ? 0: max_id; return (max_id < 0) ? 0 : max_id;
} }
case MSG_STAT: case MSG_STAT:
case IPC_STAT: case IPC_STAT:
{ {
struct msqid64_ds tbuf; struct msqid64_ds tbuf;
int success_return; int success_return;
if (!buf) if (!buf)
return -EFAULT; return -EFAULT;
if(cmd == MSG_STAT && msqid >= msg_ids.entries->size) if (cmd == MSG_STAT && msqid >= msg_ids.entries->size)
return -EINVAL; return -EINVAL;
memset(&tbuf,0,sizeof(tbuf)); memset(&tbuf, 0, sizeof(tbuf));
msq = msg_lock(msqid); msq = msg_lock(msqid);
if (msq == NULL) if (msq == NULL)
return -EINVAL; return -EINVAL;
if(cmd == MSG_STAT) { if (cmd == MSG_STAT) {
success_return = msg_buildid(msqid, msq->q_perm.seq); success_return = msg_buildid(msqid, msq->q_perm.seq);
} else { } else {
err = -EIDRM; err = -EIDRM;
if (msg_checkid(msq,msqid)) if (msg_checkid(msq, msqid))
goto out_unlock; goto out_unlock;
success_return = 0; success_return = 0;
} }
err = -EACCES; err = -EACCES;
if (ipcperms (&msq->q_perm, S_IRUGO)) if (ipcperms(&msq->q_perm, S_IRUGO))
goto out_unlock; goto out_unlock;
err = security_msg_queue_msgctl(msq, cmd); err = security_msg_queue_msgctl(msq, cmd);
...@@ -430,7 +437,7 @@ asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds __user *buf) ...@@ -430,7 +437,7 @@ asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds __user *buf)
case IPC_SET: case IPC_SET:
if (!buf) if (!buf)
return -EFAULT; return -EFAULT;
if (copy_msqid_from_user (&setbuf, buf, version)) if (copy_msqid_from_user(&setbuf, buf, version))
return -EFAULT; return -EFAULT;
break; break;
case IPC_RMID: case IPC_RMID:
...@@ -441,12 +448,12 @@ asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds __user *buf) ...@@ -441,12 +448,12 @@ asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds __user *buf)
mutex_lock(&msg_ids.mutex); mutex_lock(&msg_ids.mutex);
msq = msg_lock(msqid); msq = msg_lock(msqid);
err=-EINVAL; err = -EINVAL;
if (msq == NULL) if (msq == NULL)
goto out_up; goto out_up;
err = -EIDRM; err = -EIDRM;
if (msg_checkid(msq,msqid)) if (msg_checkid(msq, msqid))
goto out_unlock_up; goto out_unlock_up;
ipcp = &msq->q_perm; ipcp = &msq->q_perm;
...@@ -454,15 +461,16 @@ asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds __user *buf) ...@@ -454,15 +461,16 @@ asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds __user *buf)
if (err) if (err)
goto out_unlock_up; goto out_unlock_up;
if (cmd==IPC_SET) { if (cmd==IPC_SET) {
err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid, setbuf.mode); err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid,
setbuf.mode);
if (err) if (err)
goto out_unlock_up; goto out_unlock_up;
} }
err = -EPERM; err = -EPERM;
if (current->euid != ipcp->cuid && if (current->euid != ipcp->cuid &&
current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
/* We _could_ check for CAP_CHOWN above, but we don't */ /* We _could_ check for CAP_CHOWN above, but we don't */
goto out_unlock_up; goto out_unlock_up;
err = security_msg_queue_msgctl(msq, cmd); err = security_msg_queue_msgctl(msq, cmd);
...@@ -480,22 +488,22 @@ asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds __user *buf) ...@@ -480,22 +488,22 @@ asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds __user *buf)
ipcp->uid = setbuf.uid; ipcp->uid = setbuf.uid;
ipcp->gid = setbuf.gid; ipcp->gid = setbuf.gid;
ipcp->mode = (ipcp->mode & ~S_IRWXUGO) | ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
(S_IRWXUGO & setbuf.mode); (S_IRWXUGO & setbuf.mode);
msq->q_ctime = get_seconds(); msq->q_ctime = get_seconds();
/* sleeping receivers might be excluded by /* sleeping receivers might be excluded by
* stricter permissions. * stricter permissions.
*/ */
expunge_all(msq,-EAGAIN); expunge_all(msq, -EAGAIN);
/* sleeping senders might be able to send /* sleeping senders might be able to send
* due to a larger queue size. * due to a larger queue size.
*/ */
ss_wakeup(&msq->q_senders,0); ss_wakeup(&msq->q_senders, 0);
msg_unlock(msq); msg_unlock(msq);
break; break;
} }
case IPC_RMID: case IPC_RMID:
freeque (msq, msqid); freeque(msq, msqid);
break; break;
} }
err = 0; err = 0;
...@@ -510,41 +518,44 @@ out_unlock: ...@@ -510,41 +518,44 @@ out_unlock:
return err; return err;
} }
static int testmsg(struct msg_msg* msg,long type,int mode) static int testmsg(struct msg_msg *msg, long type, int mode)
{ {
switch(mode) switch(mode)
{ {
case SEARCH_ANY: case SEARCH_ANY:
return 1; return 1;
case SEARCH_LESSEQUAL: case SEARCH_LESSEQUAL:
if(msg->m_type <=type) if (msg->m_type <=type)
return 1; return 1;
break; break;
case SEARCH_EQUAL: case SEARCH_EQUAL:
if(msg->m_type == type) if (msg->m_type == type)
return 1; return 1;
break; break;
case SEARCH_NOTEQUAL: case SEARCH_NOTEQUAL:
if(msg->m_type != type) if (msg->m_type != type)
return 1; return 1;
break; break;
} }
return 0; return 0;
} }
static inline int pipelined_send(struct msg_queue* msq, struct msg_msg* msg) static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
{ {
struct list_head* tmp; struct list_head *tmp;
tmp = msq->q_receivers.next; tmp = msq->q_receivers.next;
while (tmp != &msq->q_receivers) { while (tmp != &msq->q_receivers) {
struct msg_receiver* msr; struct msg_receiver *msr;
msr = list_entry(tmp,struct msg_receiver,r_list);
msr = list_entry(tmp, struct msg_receiver, r_list);
tmp = tmp->next; tmp = tmp->next;
if(testmsg(msg,msr->r_msgtype,msr->r_mode) && if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
!security_msg_queue_msgrcv(msq, msg, msr->r_tsk, msr->r_msgtype, msr->r_mode)) { !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
msr->r_msgtype, msr->r_mode)) {
list_del(&msr->r_list); list_del(&msr->r_list);
if(msr->r_maxsize < msg->m_ts) { if (msr->r_maxsize < msg->m_ts) {
msr->r_msg = NULL; msr->r_msg = NULL;
wake_up_process(msr->r_tsk); wake_up_process(msr->r_tsk);
smp_mb(); smp_mb();
...@@ -556,6 +567,7 @@ static inline int pipelined_send(struct msg_queue* msq, struct msg_msg* msg) ...@@ -556,6 +567,7 @@ static inline int pipelined_send(struct msg_queue* msq, struct msg_msg* msg)
wake_up_process(msr->r_tsk); wake_up_process(msr->r_tsk);
smp_mb(); smp_mb();
msr->r_msg = msg; msr->r_msg = msg;
return 1; return 1;
} }
} }
...@@ -563,40 +575,41 @@ static inline int pipelined_send(struct msg_queue* msq, struct msg_msg* msg) ...@@ -563,40 +575,41 @@ static inline int pipelined_send(struct msg_queue* msq, struct msg_msg* msg)
return 0; return 0;
} }
asmlinkage long sys_msgsnd (int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg) asmlinkage long
sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
{ {
struct msg_queue *msq; struct msg_queue *msq;
struct msg_msg *msg; struct msg_msg *msg;
long mtype; long mtype;
int err; int err;
if (msgsz > msg_ctlmax || (long) msgsz < 0 || msqid < 0) if (msgsz > msg_ctlmax || (long) msgsz < 0 || msqid < 0)
return -EINVAL; return -EINVAL;
if (get_user(mtype, &msgp->mtype)) if (get_user(mtype, &msgp->mtype))
return -EFAULT; return -EFAULT;
if (mtype < 1) if (mtype < 1)
return -EINVAL; return -EINVAL;
msg = load_msg(msgp->mtext, msgsz); msg = load_msg(msgp->mtext, msgsz);
if(IS_ERR(msg)) if (IS_ERR(msg))
return PTR_ERR(msg); return PTR_ERR(msg);
msg->m_type = mtype; msg->m_type = mtype;
msg->m_ts = msgsz; msg->m_ts = msgsz;
msq = msg_lock(msqid); msq = msg_lock(msqid);
err=-EINVAL; err = -EINVAL;
if(msq==NULL) if (msq == NULL)
goto out_free; goto out_free;
err= -EIDRM; err= -EIDRM;
if (msg_checkid(msq,msqid)) if (msg_checkid(msq, msqid))
goto out_unlock_free; goto out_unlock_free;
for (;;) { for (;;) {
struct msg_sender s; struct msg_sender s;
err=-EACCES; err = -EACCES;
if (ipcperms(&msq->q_perm, S_IWUGO)) if (ipcperms(&msq->q_perm, S_IWUGO))
goto out_unlock_free; goto out_unlock_free;
...@@ -604,14 +617,14 @@ asmlinkage long sys_msgsnd (int msqid, struct msgbuf __user *msgp, size_t msgsz, ...@@ -604,14 +617,14 @@ asmlinkage long sys_msgsnd (int msqid, struct msgbuf __user *msgp, size_t msgsz,
if (err) if (err)
goto out_unlock_free; goto out_unlock_free;
if(msgsz + msq->q_cbytes <= msq->q_qbytes && if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
1 + msq->q_qnum <= msq->q_qbytes) { 1 + msq->q_qnum <= msq->q_qbytes) {
break; break;
} }
/* queue full, wait: */ /* queue full, wait: */
if(msgflg&IPC_NOWAIT) { if (msgflg & IPC_NOWAIT) {
err=-EAGAIN; err = -EAGAIN;
goto out_unlock_free; goto out_unlock_free;
} }
ss_add(msq, &s); ss_add(msq, &s);
...@@ -626,9 +639,9 @@ asmlinkage long sys_msgsnd (int msqid, struct msgbuf __user *msgp, size_t msgsz, ...@@ -626,9 +639,9 @@ asmlinkage long sys_msgsnd (int msqid, struct msgbuf __user *msgp, size_t msgsz,
goto out_unlock_free; goto out_unlock_free;
} }
ss_del(&s); ss_del(&s);
if (signal_pending(current)) { if (signal_pending(current)) {
err=-ERESTARTNOHAND; err = -ERESTARTNOHAND;
goto out_unlock_free; goto out_unlock_free;
} }
} }
...@@ -636,47 +649,47 @@ asmlinkage long sys_msgsnd (int msqid, struct msgbuf __user *msgp, size_t msgsz, ...@@ -636,47 +649,47 @@ asmlinkage long sys_msgsnd (int msqid, struct msgbuf __user *msgp, size_t msgsz,
msq->q_lspid = current->tgid; msq->q_lspid = current->tgid;
msq->q_stime = get_seconds(); msq->q_stime = get_seconds();
if(!pipelined_send(msq,msg)) { if (!pipelined_send(msq, msg)) {
/* noone is waiting for this message, enqueue it */ /* noone is waiting for this message, enqueue it */
list_add_tail(&msg->m_list,&msq->q_messages); list_add_tail(&msg->m_list, &msq->q_messages);
msq->q_cbytes += msgsz; msq->q_cbytes += msgsz;
msq->q_qnum++; msq->q_qnum++;
atomic_add(msgsz,&msg_bytes); atomic_add(msgsz, &msg_bytes);
atomic_inc(&msg_hdrs); atomic_inc(&msg_hdrs);
} }
err = 0; err = 0;
msg = NULL; msg = NULL;
out_unlock_free: out_unlock_free:
msg_unlock(msq); msg_unlock(msq);
out_free: out_free:
if(msg!=NULL) if (msg != NULL)
free_msg(msg); free_msg(msg);
return err; return err;
} }
static inline int convert_mode(long* msgtyp, int msgflg) static inline int convert_mode(long *msgtyp, int msgflg)
{ {
/* /*
* find message of correct type. * find message of correct type.
* msgtyp = 0 => get first. * msgtyp = 0 => get first.
* msgtyp > 0 => get first message of matching type. * msgtyp > 0 => get first message of matching type.
* msgtyp < 0 => get message with least type must be < abs(msgtype). * msgtyp < 0 => get message with least type must be < abs(msgtype).
*/ */
if(*msgtyp==0) if (*msgtyp == 0)
return SEARCH_ANY; return SEARCH_ANY;
if(*msgtyp<0) { if (*msgtyp < 0) {
*msgtyp=-(*msgtyp); *msgtyp = -*msgtyp;
return SEARCH_LESSEQUAL; return SEARCH_LESSEQUAL;
} }
if(msgflg & MSG_EXCEPT) if (msgflg & MSG_EXCEPT)
return SEARCH_NOTEQUAL; return SEARCH_NOTEQUAL;
return SEARCH_EQUAL; return SEARCH_EQUAL;
} }
asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz, asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
long msgtyp, int msgflg) long msgtyp, int msgflg)
{ {
struct msg_queue *msq; struct msg_queue *msq;
struct msg_msg *msg; struct msg_msg *msg;
...@@ -684,44 +697,51 @@ asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz, ...@@ -684,44 +697,51 @@ asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz,
if (msqid < 0 || (long) msgsz < 0) if (msqid < 0 || (long) msgsz < 0)
return -EINVAL; return -EINVAL;
mode = convert_mode(&msgtyp,msgflg); mode = convert_mode(&msgtyp, msgflg);
msq = msg_lock(msqid); msq = msg_lock(msqid);
if(msq==NULL) if (msq == NULL)
return -EINVAL; return -EINVAL;
msg = ERR_PTR(-EIDRM); msg = ERR_PTR(-EIDRM);
if (msg_checkid(msq,msqid)) if (msg_checkid(msq, msqid))
goto out_unlock; goto out_unlock;
for (;;) { for (;;) {
struct msg_receiver msr_d; struct msg_receiver msr_d;
struct list_head* tmp; struct list_head *tmp;
msg = ERR_PTR(-EACCES); msg = ERR_PTR(-EACCES);
if (ipcperms (&msq->q_perm, S_IRUGO)) if (ipcperms(&msq->q_perm, S_IRUGO))
goto out_unlock; goto out_unlock;
msg = ERR_PTR(-EAGAIN); msg = ERR_PTR(-EAGAIN);
tmp = msq->q_messages.next; tmp = msq->q_messages.next;
while (tmp != &msq->q_messages) { while (tmp != &msq->q_messages) {
struct msg_msg *walk_msg; struct msg_msg *walk_msg;
walk_msg = list_entry(tmp,struct msg_msg,m_list);
if(testmsg(walk_msg,msgtyp,mode) && walk_msg = list_entry(tmp, struct msg_msg, m_list);
!security_msg_queue_msgrcv(msq, walk_msg, current, msgtyp, mode)) { if (testmsg(walk_msg, msgtyp, mode) &&
!security_msg_queue_msgrcv(msq, walk_msg, current,
msgtyp, mode)) {
msg = walk_msg; msg = walk_msg;
if(mode == SEARCH_LESSEQUAL && walk_msg->m_type != 1) { if (mode == SEARCH_LESSEQUAL &&
msg=walk_msg; walk_msg->m_type != 1) {
msgtyp=walk_msg->m_type-1; msg = walk_msg;
msgtyp = walk_msg->m_type - 1;
} else { } else {
msg=walk_msg; msg = walk_msg;
break; break;
} }
} }
tmp = tmp->next; tmp = tmp->next;
} }
if(!IS_ERR(msg)) { if (!IS_ERR(msg)) {
/* Found a suitable message. Unlink it from the queue. */ /*
* Found a suitable message.
* Unlink it from the queue.
*/
if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) { if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
msg = ERR_PTR(-E2BIG); msg = ERR_PTR(-E2BIG);
goto out_unlock; goto out_unlock;
...@@ -731,9 +751,9 @@ asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz, ...@@ -731,9 +751,9 @@ asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz,
msq->q_rtime = get_seconds(); msq->q_rtime = get_seconds();
msq->q_lrpid = current->tgid; msq->q_lrpid = current->tgid;
msq->q_cbytes -= msg->m_ts; msq->q_cbytes -= msg->m_ts;
atomic_sub(msg->m_ts,&msg_bytes); atomic_sub(msg->m_ts, &msg_bytes);
atomic_dec(&msg_hdrs); atomic_dec(&msg_hdrs);
ss_wakeup(&msq->q_senders,0); ss_wakeup(&msq->q_senders, 0);
msg_unlock(msq); msg_unlock(msq);
break; break;
} }
...@@ -742,13 +762,13 @@ asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz, ...@@ -742,13 +762,13 @@ asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz,
msg = ERR_PTR(-ENOMSG); msg = ERR_PTR(-ENOMSG);
goto out_unlock; goto out_unlock;
} }
list_add_tail(&msr_d.r_list,&msq->q_receivers); list_add_tail(&msr_d.r_list, &msq->q_receivers);
msr_d.r_tsk = current; msr_d.r_tsk = current;
msr_d.r_msgtype = msgtyp; msr_d.r_msgtype = msgtyp;
msr_d.r_mode = mode; msr_d.r_mode = mode;
if(msgflg & MSG_NOERROR) if (msgflg & MSG_NOERROR)
msr_d.r_maxsize = INT_MAX; msr_d.r_maxsize = INT_MAX;
else else
msr_d.r_maxsize = msgsz; msr_d.r_maxsize = msgsz;
msr_d.r_msg = ERR_PTR(-EAGAIN); msr_d.r_msg = ERR_PTR(-EAGAIN);
current->state = TASK_INTERRUPTIBLE; current->state = TASK_INTERRUPTIBLE;
...@@ -773,17 +793,17 @@ asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz, ...@@ -773,17 +793,17 @@ asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz,
* wake_up_process(). There is a race with exit(), see * wake_up_process(). There is a race with exit(), see
* ipc/mqueue.c for the details. * ipc/mqueue.c for the details.
*/ */
msg = (struct msg_msg*) msr_d.r_msg; msg = (struct msg_msg*)msr_d.r_msg;
while (msg == NULL) { while (msg == NULL) {
cpu_relax(); cpu_relax();
msg = (struct msg_msg*) msr_d.r_msg; msg = (struct msg_msg *)msr_d.r_msg;
} }
/* Lockless receive, part 3: /* Lockless receive, part 3:
* If there is a message or an error then accept it without * If there is a message or an error then accept it without
* locking. * locking.
*/ */
if(msg != ERR_PTR(-EAGAIN)) { if (msg != ERR_PTR(-EAGAIN)) {
rcu_read_unlock(); rcu_read_unlock();
break; break;
} }
...@@ -798,7 +818,7 @@ asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz, ...@@ -798,7 +818,7 @@ asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz,
* Repeat test after acquiring the spinlock. * Repeat test after acquiring the spinlock.
*/ */
msg = (struct msg_msg*)msr_d.r_msg; msg = (struct msg_msg*)msr_d.r_msg;
if(msg != ERR_PTR(-EAGAIN)) if (msg != ERR_PTR(-EAGAIN))
goto out_unlock; goto out_unlock;
list_del(&msr_d.r_list); list_del(&msr_d.r_list);
...@@ -810,14 +830,15 @@ out_unlock: ...@@ -810,14 +830,15 @@ out_unlock:
} }
} }
if (IS_ERR(msg)) if (IS_ERR(msg))
return PTR_ERR(msg); return PTR_ERR(msg);
msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz; msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
if (put_user (msg->m_type, &msgp->mtype) || if (put_user (msg->m_type, &msgp->mtype) ||
store_msg(msgp->mtext, msg, msgsz)) { store_msg(msgp->mtext, msg, msgsz)) {
msgsz = -EFAULT; msgsz = -EFAULT;
} }
free_msg(msg); free_msg(msg);
return msgsz; return msgsz;
} }
...@@ -827,20 +848,20 @@ static int sysvipc_msg_proc_show(struct seq_file *s, void *it) ...@@ -827,20 +848,20 @@ static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
struct msg_queue *msq = it; struct msg_queue *msq = it;
return seq_printf(s, return seq_printf(s,
"%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n", "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
msq->q_perm.key, msq->q_perm.key,
msq->q_id, msq->q_id,
msq->q_perm.mode, msq->q_perm.mode,
msq->q_cbytes, msq->q_cbytes,
msq->q_qnum, msq->q_qnum,
msq->q_lspid, msq->q_lspid,
msq->q_lrpid, msq->q_lrpid,
msq->q_perm.uid, msq->q_perm.uid,
msq->q_perm.gid, msq->q_perm.gid,
msq->q_perm.cuid, msq->q_perm.cuid,
msq->q_perm.cgid, msq->q_perm.cgid,
msq->q_stime, msq->q_stime,
msq->q_rtime, msq->q_rtime,
msq->q_ctime); msq->q_ctime);
} }
#endif #endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment