Skip to content

Commit daaf74c

Browse files
peterhurleytorvalds
authored andcommitted
ipc: refactor msg list search into separate function
[[email protected]: find_msg can be static] Signed-off-by: Peter Hurley <[email protected]> Cc: Fengguang Wu <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent d076ac9 commit daaf74c

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

ipc/msg.c

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,30 @@ static inline void free_copy(struct msg_msg *copy)
807807
}
808808
#endif
809809

810+
static struct msg_msg *find_msg(struct msg_queue *msq, long *msgtyp, int mode)
811+
{
812+
struct msg_msg *msg;
813+
long count = 0;
814+
815+
list_for_each_entry(msg, &msq->q_messages, m_list) {
816+
if (testmsg(msg, *msgtyp, mode) &&
817+
!security_msg_queue_msgrcv(msq, msg, current,
818+
*msgtyp, mode)) {
819+
if (mode == SEARCH_LESSEQUAL && msg->m_type != 1) {
820+
*msgtyp = msg->m_type - 1;
821+
} else if (mode == SEARCH_NUMBER) {
822+
if (*msgtyp == count)
823+
return msg;
824+
} else
825+
return msg;
826+
count++;
827+
}
828+
}
829+
830+
return ERR_PTR(-EAGAIN);
831+
}
832+
833+
810834
long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp,
811835
int msgflg,
812836
long (*msg_handler)(void __user *, struct msg_msg *, size_t))
@@ -836,33 +860,13 @@ long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp,
836860

837861
for (;;) {
838862
struct msg_receiver msr_d;
839-
struct msg_msg *walk_msg;
840-
long msg_counter = 0;
841863

842864
msg = ERR_PTR(-EACCES);
843865
if (ipcperms(ns, &msq->q_perm, S_IRUGO))
844866
goto out_unlock;
845867

846-
msg = ERR_PTR(-EAGAIN);
847-
list_for_each_entry(walk_msg, &msq->q_messages, m_list) {
848-
849-
if (testmsg(walk_msg, msgtyp, mode) &&
850-
!security_msg_queue_msgrcv(msq, walk_msg, current,
851-
msgtyp, mode)) {
852-
853-
msg = walk_msg;
854-
if (mode == SEARCH_LESSEQUAL &&
855-
walk_msg->m_type != 1) {
856-
msgtyp = walk_msg->m_type - 1;
857-
} else if (mode == SEARCH_NUMBER) {
858-
if (msgtyp == msg_counter)
859-
break;
860-
msg = ERR_PTR(-EAGAIN);
861-
} else
862-
break;
863-
msg_counter++;
864-
}
865-
}
868+
msg = find_msg(msq, &msgtyp, mode);
869+
866870
if (!IS_ERR(msg)) {
867871
/*
868872
* Found a suitable message.

0 commit comments

Comments
 (0)