From fe71ea4bc155b70ac27e62e681e02f11313a6795 Mon Sep 17 00:00:00 2001 From: Sanchayan Maity Date: Sat, 19 Mar 2016 11:49:48 +0530 Subject: [PATCH] First round of assorted bug fixes Fixed the following bugs: 1. Message priority was not set at all at two places 2. Limits check was not correct while returning error values 3. We are suppose to allow multiple users operate on the same queue using a named descriptor. However any one user calling close before the other would result in closing the queue even when other users might be using it. Track number of users and close only if number of users is zero. Yet to do functional tests for this, so this is untested, but introduce it anyway at this juncture. --- minix/kernel/mqueue.c | 33 ++++++++++++++++++++------------- minix/kernel/mqueue.h | 1 + minix/lib/libsys/sys_mq_send.c | 1 + 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/minix/kernel/mqueue.c b/minix/kernel/mqueue.c index efe38f334..0093db282 100644 --- a/minix/kernel/mqueue.c +++ b/minix/kernel/mqueue.c @@ -21,6 +21,7 @@ int initialize_message_queues(void) for (int i = 0; i < MAX_QUEUES; i++) { mq.queue_slot_empty[i] = EMPTY; + mq.msg[i].num_users = 0; mq.msg[i].num_msgs = 0; } @@ -32,7 +33,8 @@ int deinitialize_message_queues(void) mq.num_queues = -1; for (int i = 0; i < MAX_QUEUES; i++) { mq.msg[i].num_msgs = -1; - mq.queue_slot_empty[i] = -1; + mq.msg[i].num_users = -1; + mq.queue_slot_empty[i] = EMPTY; } return 0; @@ -42,7 +44,7 @@ int mq_open(const char *name, int oflag) { mqd_t mqd; - if (mq.num_queues == MAX_QUEUES) + if (mq.num_queues >= MAX_QUEUES) return EMQUEUEFULL; if (strlen(name) > NAME_SIZE) @@ -50,18 +52,21 @@ int mq_open(const char *name, int oflag) for (int i = 0; i < MAX_QUEUES; i++) if (mq.queue_slot_empty[i] == NOT_EMPTY) - if (strcmp(mq.msg[i].name, name) == 0) + if (strcmp(mq.msg[i].name, name) == 0) { + mq.msg[i].num_users++; return (mqd_t) i; + } for (int i = 0; i < MAX_QUEUES; i++) { if (mq.queue_slot_empty[i] == EMPTY) { mqd = i; mq.queue_slot_empty[i] = NOT_EMPTY; mq.num_queues++; + mq.msg[i].num_users++; strncpy(mq.msg[i].name, name, strlen(name)); for (int j = 0; j < MAX_MESSAGES; j++) { - mq.msg[i].msg_slot_empty[j] = NOT_EMPTY; + mq.msg[i].msg_slot_empty[j] = EMPTY; mq.msg[i].msge[j].priority = DEFAULT_PRIO; for (int k = 0; k < MAX_RECEIVERS; k++) mq.msg[i].msge[j].dst[k] = EMPTY; @@ -81,9 +86,12 @@ int mq_close(mqd_t mqdes) if (mq.queue_slot_empty[mqdes] == EMPTY) return EMSGNOTFOUND; - mq.msg[mqdes].num_msgs = 0; - mq.queue_slot_empty[mqdes] = EMPTY; - mq.num_queues--; + mq.msg[mqdes].num_users--; + if (mq.msg[mqdes].num_users == 0) { + mq.msg[mqdes].num_msgs = 0; + mq.queue_slot_empty[mqdes] = EMPTY; + mq.num_queues--; + } return 0; } @@ -96,7 +104,7 @@ int mq_send(mqd_t mqdes, const char *msg_ptr, unsigned int msg_prio, endpoint_t if (mq.queue_slot_empty[mqdes] == EMPTY) return EMSGNOTFOUND; - if (mq.msg[mqdes].num_msgs >= MAX_MESSAGES) + if (mq.msg[mqdes].num_msgs > MAX_MESSAGES) return EMSGFULL; int empty_slot_pos; @@ -106,9 +114,11 @@ int mq_send(mqd_t mqdes, const char *msg_ptr, unsigned int msg_prio, endpoint_t break; } + memset(mq.msg[mqdes].msge[empty_slot_pos].msg, 0, MAX_PAYLOAD); memcpy(mq.msg[mqdes].msge[empty_slot_pos].msg, msg_ptr, MAX_PAYLOAD); mq.msg[mqdes].msge[empty_slot_pos].src = src; + mq.msg[mqdes].msge[empty_slot_pos].priority = msg_prio; mq.msg[mqdes].msg_slot_empty[empty_slot_pos] = NOT_EMPTY; for (int i = 0; i < MAX_RECEIVERS; i++) @@ -128,9 +138,6 @@ size_t mq_receive(mqd_t mqdes, char *msg_ptr, unsigned int msg_prio, endpoint_t if (mq.queue_slot_empty[mqdes] == EMPTY) return EMSGNOTFOUND; - if (mq.msg[mqdes].num_msgs >= MAX_MESSAGES) - return EMSGFULL; - if (mq.msg[mqdes].num_msgs == 0) return EMSGEMPTY; @@ -158,15 +165,15 @@ int find_max(int n1, int n2) return n2; } -int message_index_with_highprio(mqdes, dst) +int message_index_with_highprio(int mqdes, endpoint_t dst) { int prio; int max_prio = -1; int index = -1; for (int i = 0; i < MAX_MESSAGES; i++) { - prio = mq.msg[mqdes].msge[i].priority; if (mq.msg[mqdes].msg_slot_empty[i] == NOT_EMPTY) { + prio = mq.msg[mqdes].msge[i].priority; for (int j = 0; j < MAX_RECEIVERS; j++) { if (mq.msg[mqdes].msge[i].dst[j] == dst) { if (max_prio < find_max(max_prio, prio)) { diff --git a/minix/kernel/mqueue.h b/minix/kernel/mqueue.h index e85607191..180e7dc79 100644 --- a/minix/kernel/mqueue.h +++ b/minix/kernel/mqueue.h @@ -32,6 +32,7 @@ typedef struct message_entity { typedef struct messageq { char name[NAME_SIZE]; int num_msgs; + int num_users; message_entity msge[MAX_MESSAGES]; int msg_slot_empty[MAX_MESSAGES]; } messageq; diff --git a/minix/lib/libsys/sys_mq_send.c b/minix/lib/libsys/sys_mq_send.c index d4e0de237..2e0f8f087 100644 --- a/minix/lib/libsys/sys_mq_send.c +++ b/minix/lib/libsys/sys_mq_send.c @@ -13,6 +13,7 @@ int sys_mq_send(int mqdes, const char *msg_ptr, pid_t dst[], unsigned int msg_pr message m; m.m_lsys_krn_sys_mqueue_send.mqdes = mqdes; + m.m_lsys_krn_sys_mqueue_send.msg_prio = msg_prio; for (int i = 0; i < MAX_RECEIVERS; i++) { if (dst[i] == -1) { m.m_lsys_krn_sys_mqueue_send.dst[i] = dst[i];