From 08e7d10bd8a7894a6f83396bad337396b7191de7 Mon Sep 17 00:00:00 2001 From: Ruediger Meier Date: Wed, 12 Mar 2014 14:53:20 +0100 Subject: ipcs: fix ipc_msg_get_info fallback case "ipcs -q" (case id < 0) was broken since v2.22-256-g35118df if /sys is not usable. The main issue was that the use of msqid argument did not cleanly distinguished between "queue identifier" and "index of kernel's internal array". Also now the fallback case and the regular case behave more equally regarding it's return value (introducing another counter j). Note that the case id >= 0 now performs a slower lookup. This could be avoided but then we would better handle both case differently like it was before the above mentioned cleanup commit. Signed-off-by: Ruediger Meier --- sys-utils/ipcutils.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/sys-utils/ipcutils.c b/sys-utils/ipcutils.c index c98c5d8b5..2491f7c91 100644 --- a/sys-utils/ipcutils.c +++ b/sys-utils/ipcutils.c @@ -423,26 +423,20 @@ int ipc_msg_get_info(int id, struct msg_data **msgds) /* Fallback; /proc or /sys file(s) missing. */ msg_fallback: - i = id < 0 ? 0 : id; - - maxid = msgctl(id, MSG_STAT, &dummy); + maxid = msgctl(0, MSG_INFO, &dummy); if (maxid < 0) return 0; - while (i <= maxid) { + for (int j = 0; j <= maxid; j++) { int msgid; struct ipc_perm *ipcp = &msgseg.msg_perm; - msgid = msgctl(i, MSG_STAT, &msgseg); - if (msgid < 0) { - if (-1 < id) { - free(*msgds); - return 0; - } - i++; + msgid = msgctl(j, MSG_STAT, &msgseg); + if (msgid < 0 || (id > -1 && msgid != id)) { continue; } + i++; p->msg_perm.key = ipcp->KEY; p->msg_perm.id = msgid; p->msg_perm.mode = ipcp->mode; @@ -463,11 +457,12 @@ msg_fallback: p->next = xcalloc(1, sizeof(struct msg_data)); p = p->next; p->next = NULL; - i++; } else - return 1; + break; } + if (i == 0) + free(*msgds); return i; } -- cgit v1.2.3-55-g7522