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