Skip to contentSkip to conversation

syscalls/msgget04: Add test for /proc/sys/kernel/msg_next_id

PATCH New Yang Xu 2 participants 5 messages Jul 20, 2020 – Aug 13, 2020

lore
Share & tools
lore

Yang Xu

author
#0 lore raw

PATCH 1/1 syscalls/msgget04: Add test for /proc/sys/kernel/msg_next_id

---
 runtest/syscalls                              |  1 +
 runtest/syscalls-ipc                          |  1 +
 .../kernel/syscalls/ipc/msgget/.gitignore     |  1 +
 .../kernel/syscalls/ipc/msgget/msgget04.c     | 66 +++++++++++++++++++
 4 files changed, 69 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ipc/msgget/msgget04.c
runtest/syscalls +1 -0
index 9d927935b..4cdb123bd 100644
@@ -808,6 +808,7 @@ msgctl12 msgctl12
 msgget01 msgget01
 msgget02 msgget02
 msgget03 msgget03
+msgget04 msgget04
 
 msgrcv01 msgrcv01
 msgrcv02 msgrcv02
runtest/syscalls-ipc +1 -0
index 153e827cf..64d6b1c90 100644
@@ -12,6 +12,7 @@ msgctl12 msgctl12
 msgget01 msgget01
 msgget02 msgget02
 msgget03 msgget03
+msgget04 msgget04
 
 msgrcv01 msgrcv01
 msgrcv02 msgrcv02
testcases/kernel/syscalls/ipc/msgget/.gitignore +1 -0
index 3b2dfb518..7a9d5e340 100644
@@ -1,3 +1,4 @@
 /msgget01
 /msgget02
 /msgget03
+/msgget04
testcases/kernel/syscalls/ipc/msgget/msgget04.c new file +66 -0

66 lines not on this page · raw message

Signature · 1 line
2.23.0
#1 lore raw

PATCH 1/1 syscalls/msgget04: Add test for /proc/sys/kernel/msg_next_id

Hi!

Quoted 54 lines
testcases/kernel/syscalls/ipc/msgget/msgget04.c new file +48 -0
new file mode 100644
index 000000000..3791f331a
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <[email protected]>
+ *
+ * It is a basic test about msg_next_id.
+ * msg_next_id specifies desired id for next allocated IPC message. By default
+ * they are equal to -1, which means generic allocation logic. Possible values
+ * to set are in range {0..INT_MAX}.
+ * Toggle with non-default value will be set back to -1 by kernel after
+ * successful IPC object allocation.
+ */
+
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/ipc.h>
+#include <sys/msg.h>
+#include "tst_test.h"
+#include "tst_safe_sysv_ipc.h"
+#include "libnewipc.h"
+
+#define NEXT_ID_PATH "/proc/sys/kernel/msg_next_id"
+static int queue_id = 1, pid;
+static key_t msgkey;
+
+static void verify_msgget(void)
+{
+	SAFE_FILE_PRINTF(NEXT_ID_PATH, "%d", pid);
+
+	TEST(msgget(msgkey, IPC_CREAT | MSG_RW));
+	if (TST_RET == -1) {
+		tst_res(TFAIL | TTERRNO, "msgget() failed");
+		return;
+	}
+
+	queue_id = TST_RET;
+	if (queue_id == pid)
+		tst_res(TPASS, "msg_next_id succeeded, queue id %d", pid);
+	else
+		tst_res(TFAIL, "msg_next_id failed, expected id %d, but got %d", pid, queue_id);
+
+	TST_ASSERT_INT(NEXT_ID_PATH, -1);
+	SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
+	pid++;
+}
+
+static void setup(void)

testcases/kernel/syscalls/ipc/msgget/msgget04.c line 58

Quoted 10 lines
testcases/kernel/syscalls/ipc/msgget/msgget04.c new file +10 -0
+{
+	msgkey = GETIPCKEY();
+	pid = getpid();
+}
+
+static void cleanup(void)
+{
+	if (queue_id != -1)
+		SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
+	SAFE_FILE_SCANF(NEXT_ID_PATH, "-1");

Shouldn't this be SAFE_FILE_PRINTF() ?

Otherwise it looks fine, also I can fix this one before pushing.

Also what happens when we write id of existing id to that file? I guess that we can write another test that tries that...

Quoted 16 lines
testcases/kernel/syscalls/ipc/msgget/msgget04.c new file +8 -0
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_msgget,
+	.needs_root = 1,
+};
Signature · 7 lines
2.23.0




-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]
author
#2 lore raw

PATCH 1/1 syscalls/msgget04: Add test for /proc/sys/kernel/msg_next_id

HI Cyril

Quoted 59 lines

Hi!

Quoted 58 lines
testcases/kernel/syscalls/ipc/msgget/msgget04.c new file +52 -0
new file mode 100644
index 000000000..3791f331a
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <[email protected]>
+ *
+ * It is a basic test about msg_next_id.
+ * msg_next_id specifies desired id for next allocated IPC message. By default
+ * they are equal to -1, which means generic allocation logic. Possible values
+ * to set are in range {0..INT_MAX}.
+ * Toggle with non-default value will be set back to -1 by kernel after
+ * successful IPC object allocation.
+ */
+
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/ipc.h>
+#include <sys/msg.h>
+#include "tst_test.h"
+#include "tst_safe_sysv_ipc.h"
+#include "libnewipc.h"
+
+#define NEXT_ID_PATH "/proc/sys/kernel/msg_next_id"
+static int queue_id = 1, pid;
+static key_t msgkey;
+
+static void verify_msgget(void)
+{
+	SAFE_FILE_PRINTF(NEXT_ID_PATH, "%d", pid);
+
+	TEST(msgget(msgkey, IPC_CREAT | MSG_RW));
+	if (TST_RET == -1) {
+		tst_res(TFAIL | TTERRNO, "msgget() failed");
+		return;
+	}
+
+	queue_id = TST_RET;
+	if (queue_id == pid)
+		tst_res(TPASS, "msg_next_id succeeded, queue id %d", pid);
+	else
+		tst_res(TFAIL, "msg_next_id failed, expected id %d, but got %d", pid, queue_id);
+
+	TST_ASSERT_INT(NEXT_ID_PATH, -1);
+	SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
+	pid++;
+}
+
+static void setup(void)
+{
+	msgkey = GETIPCKEY();
+	pid = getpid();
+}
Quoted 8 lines

testcases/kernel/syscalls/ipc/msgget/msgget04.c line 58

Quoted 6 lines
testcases/kernel/syscalls/ipc/msgget/msgget04.c new file +6 -0
+
+static void cleanup(void)
+{
+	if (queue_id != -1)
+		SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
+	SAFE_FILE_SCANF(NEXT_ID_PATH, "-1");

Shouldn't this be SAFE_FILE_PRINTF() ?

Yes, sorry for the obvious mistake.
But if it will report EINVAL error when the value of NEXT_ID_PATH has been -1. I think we may need a flag.

Quoted 3 lines

Otherwise it looks fine, also I can fix this one before pushing.

I will send v2.

Quoted 4 lines

Also what happens when we write id of existing id to that file? I guess that we can write another test that tries that...

I will look kernel code firstly, then write a case about this.

Best Regards
Yang Xu

Quoted 18 lines
Quoted 17 lines
testcases/kernel/syscalls/ipc/msgget/msgget04.c new file +8 -0
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_msgget,
+	.needs_root = 1,
+};
Signature · 7 lines
2.23.0




-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#3 lore raw

PATCH v2 1/1 syscalls/msgget04: Add test for /proc/sys/kernel/msg_next_id

In reply to Cyril Hrubis

---
v1-v2:
1.queue_id doesn't need to init to 1
2.reset msg_next_id value to -1 in cleanup

 runtest/syscalls                              |  1 +
 runtest/syscalls-ipc                          |  1 +
 .../kernel/syscalls/ipc/msgget/.gitignore     |  1 +
 .../kernel/syscalls/ipc/msgget/msgget04.c     | 70 +++++++++++++++++++
 4 files changed, 73 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ipc/msgget/msgget04.c
runtest/syscalls +1 -0
index 98d79271d..41cd8a159 100644
@@ -810,6 +810,7 @@ msgctl12 msgctl12
 msgget01 msgget01
 msgget02 msgget02
 msgget03 msgget03
+msgget04 msgget04
 
 msgrcv01 msgrcv01
 msgrcv02 msgrcv02
runtest/syscalls-ipc +1 -0
index 41d54d939..d4baaa262 100644
@@ -12,6 +12,7 @@ msgctl12 msgctl12
 msgget01 msgget01
 msgget02 msgget02
 msgget03 msgget03
+msgget04 msgget04
 
 msgrcv01 msgrcv01
 msgrcv02 msgrcv02
testcases/kernel/syscalls/ipc/msgget/.gitignore +1 -0
index 3b2dfb518..7a9d5e340 100644
@@ -1,3 +1,4 @@
 /msgget01
 /msgget02
 /msgget03
+/msgget04
testcases/kernel/syscalls/ipc/msgget/msgget04.c new file +70 -0

70 lines not on this page · raw message

Signature · 1 line
2.23.0
author
#4 lore raw

PATCH v2 1/1 syscalls/msgget04: Add test for /proc/sys/kernel/msg_next_id

Hi Cyril

Quoted 77 lines

---
v1-v2:
1.queue_id doesn't need to init to 1
2.reset msg_next_id value to -1 in cleanup

  runtest/syscalls                              |  1 +
  runtest/syscalls-ipc                          |  1 +
  .../kernel/syscalls/ipc/msgget/.gitignore     |  1 +
  .../kernel/syscalls/ipc/msgget/msgget04.c     | 70 +++++++++++++++++++
  4 files changed, 73 insertions(+)

create mode 100644 testcases/kernel/syscalls/ipc/msgget/msgget04.c

runtest/syscalls +1 -0
index 98d79271d..41cd8a159 100644
@@ -810,6 +810,7 @@ msgctl12 msgctl12
  msgget01 msgget01
  msgget02 msgget02
  msgget03 msgget03
+msgget04 msgget04
 
  msgrcv01 msgrcv01
  msgrcv02 msgrcv02
runtest/syscalls-ipc +1 -0
index 41d54d939..d4baaa262 100644
@@ -12,6 +12,7 @@ msgctl12 msgctl12
  msgget01 msgget01
  msgget02 msgget02
  msgget03 msgget03
+msgget04 msgget04
 
  msgrcv01 msgrcv01
  msgrcv02 msgrcv02
testcases/kernel/syscalls/ipc/msgget/.gitignore +1 -0
index 3b2dfb518..7a9d5e340 100644
@@ -1,3 +1,4 @@
  /msgget01
  /msgget02
  /msgget03
+/msgget04
testcases/kernel/syscalls/ipc/msgget/msgget04.c new file +26 -0
new file mode 100644
index 000000000..d14aadd12
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <[email protected]>
+ *
+ * It is a basic test about msg_next_id.
+ * msg_next_id specifies desired id for next allocated IPC message. By default
+ * they are equal to -1, which means generic allocation logic. Possible values
+ * to set are in range {0..INT_MAX}.
+ * Toggle with non-default value will be set back to -1 by kernel after
+ * successful IPC object allocation.
+ */
+
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/ipc.h>
+#include <sys/msg.h>
+#include "tst_test.h"
+#include "tst_safe_sysv_ipc.h"
+#include "libnewipc.h"
+
+#define NEXT_ID_PATH "/proc/sys/kernel/msg_next_id"
+static int queue_id, pid;
+static key_t msgkey;
+

testcases/kernel/syscalls/ipc/msgget/msgget04.c line 36

Quoted 10 lines
testcases/kernel/syscalls/ipc/msgget/msgget04.c new file +10 -0
+static void verify_msgget(void)
+{
+	SAFE_FILE_PRINTF(NEXT_ID_PATH, "%d", pid);
+
+	TEST(msgget(msgkey, IPC_CREAT | MSG_RW));
+	if (TST_RET == -1) {
+		tst_res(TFAIL | TTERRNO, "msgget() failed");
+		return;
+	}
+

I forgot we have SAFE_MSGGET macro. we can directly use it in here.

Quoted 35 lines
testcases/kernel/syscalls/ipc/msgget/msgget04.c new file +34 -0
+	queue_id = TST_RET;
+	if (queue_id == pid)
+		tst_res(TPASS, "msg_next_id succeeded, queue id %d", pid);
+	else
+		tst_res(TFAIL, "msg_next_id failed, expected id %d, but got %d", pid, queue_id);
+
+	TST_ASSERT_INT(NEXT_ID_PATH, -1);
+	SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
+	pid++;
+}
+
+static void setup(void)
+{
+	msgkey = GETIPCKEY();
+	pid = getpid();
+}
+
+static void cleanup(void)
+{
+	if (queue_id != -1)
+		SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
+
+	/*avoid EINVAL error when path value has been -1*/
+	SAFE_FILE_SCANF(NEXT_ID_PATH, "%d", &queue_id);
+	if (queue_id != -1)
+		SAFE_FILE_PRINTF(NEXT_ID_PATH, "-1");
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_msgget,
+	.needs_root = 1,
+};