Skip to content

Commit 934c12c

Browse files
xuyang0410metan-ucw
authored andcommitted
syscalls/msgget05: Add test when the id of msg_next_id has existed
When message queue identifier that msg_next_id has existed, msgget() with different key will return the another msg id. But kernel doesn't guarantee desired id, I just compare with existed id, if not equal, the test succeeded. Signed-off-by: Yang Xu <[email protected]> Reviewed-by: Cyril Hrubis <[email protected]>
1 parent e7f167b commit 934c12c

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

runtest/syscalls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ msgget01 msgget01
811811
msgget02 msgget02
812812
msgget03 msgget03
813813
msgget04 msgget04
814+
msgget05 msgget05
814815

815816
msgrcv01 msgrcv01
816817
msgrcv02 msgrcv02

runtest/syscalls-ipc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ msgget01 msgget01
1313
msgget02 msgget02
1414
msgget03 msgget03
1515
msgget04 msgget04
16+
msgget05 msgget05
1617

1718
msgrcv01 msgrcv01
1819
msgrcv02 msgrcv02

testcases/kernel/syscalls/ipc/msgget/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/msgget02
33
/msgget03
44
/msgget04
5+
/msgget05
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
4+
* Author: Yang Xu <[email protected]>
5+
*
6+
* It is a basic test about msg_next_id.
7+
* When the message queue identifier that msg_next_id stored has existed,
8+
* call msgget with different key just use another unused value in range
9+
* [0,INT_MAX]. kernel doesn't guarantee the desired id.
10+
*/
11+
12+
#include <errno.h>
13+
#include <string.h>
14+
#include <sys/types.h>
15+
#include <sys/ipc.h>
16+
#include <sys/msg.h>
17+
#include "tst_test.h"
18+
#include "tst_safe_sysv_ipc.h"
19+
#include "libnewipc.h"
20+
21+
#define NEXT_ID_PATH "/proc/sys/kernel/msg_next_id"
22+
23+
static int queue_id[2], pid;
24+
static key_t msgkey[2];
25+
26+
static void verify_msgget(void)
27+
{
28+
SAFE_FILE_PRINTF(NEXT_ID_PATH, "%d", queue_id[0]);
29+
30+
queue_id[1] = SAFE_MSGGET(msgkey[1], IPC_CREAT | MSG_RW);
31+
if (queue_id[1] == queue_id[0])
32+
tst_res(TFAIL, "msg id %d has existed, msgget() returns the"
33+
" same msg id unexpectedly", queue_id[0]);
34+
else
35+
tst_res(TPASS, "msg id %d has existed, msgget() returns the"
36+
" new msgid %d", queue_id[0], queue_id[1]);
37+
38+
SAFE_MSGCTL(queue_id[1], IPC_RMID, NULL);
39+
}
40+
41+
static void setup(void)
42+
{
43+
msgkey[0] = GETIPCKEY();
44+
msgkey[1] = GETIPCKEY();
45+
pid = getpid();
46+
SAFE_FILE_PRINTF(NEXT_ID_PATH, "%d", pid);
47+
queue_id[0] = SAFE_MSGGET(msgkey[0], IPC_CREAT | MSG_RW);
48+
tst_res(TINFO, "Test msg_next_id effects on msgget(different key) "
49+
"when this message queue identifier has existed");
50+
}
51+
52+
static void cleanup(void)
53+
{
54+
for (int i = 0; i < 2; i++) {
55+
if (queue_id[i] != -1)
56+
SAFE_MSGCTL(queue_id[i], IPC_RMID, NULL);
57+
}
58+
}
59+
60+
static struct tst_test test = {
61+
.setup = setup,
62+
.cleanup = cleanup,
63+
.test_all = verify_msgget,
64+
.needs_kconfigs = (const char *[]) {
65+
"CONFIG_CHECKPOINT_RESTORE=y",
66+
NULL
67+
},
68+
.needs_root = 1,
69+
};

0 commit comments

Comments
 (0)