• 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  * msg_next_id specifies desired id for next allocated IPC message. By
12  * default it's equal to -1, which means generic allocation logic.
13  * Possible values to set are in range {0..INT_MAX}.
14  * The value will be set back to -1 by kernel after successful IPC object
15  * allocation.
16  */
17 
18 #include <errno.h>
19 #include <string.h>
20 #include <sys/types.h>
21 #include <sys/ipc.h>
22 #include <sys/msg.h>
23 #include "tst_test.h"
24 #include "tst_safe_sysv_ipc.h"
25 #include "libnewipc.h"
26 
27 #define NEXT_ID_PATH "/proc/sys/kernel/msg_next_id"
28 static int queue_id, pid;
29 static key_t msgkey;
30 
verify_msgget(void)31 static void verify_msgget(void)
32 {
33 	SAFE_FILE_PRINTF(NEXT_ID_PATH, "%d", pid);
34 
35 	queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | MSG_RW);
36 	if (queue_id == pid)
37 		tst_res(TPASS, "msg_next_id succeeded, queue id %d", pid);
38 	else
39 		tst_res(TFAIL, "msg_next_id failed, expected id %d, but got %d", pid, queue_id);
40 
41 	TST_ASSERT_INT(NEXT_ID_PATH, -1);
42 	SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
43 	pid++;
44 }
45 
setup(void)46 static void setup(void)
47 {
48 	msgkey = GETIPCKEY();
49 	pid = getpid();
50 }
51 
cleanup(void)52 static void cleanup(void)
53 {
54 	if (queue_id != -1)
55 		SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
56 }
57 
58 static struct tst_test test = {
59 	.setup = setup,
60 	.cleanup = cleanup,
61 	.test_all = verify_msgget,
62 	.needs_kconfigs = (const char *[]) {
63 		"CONFIG_CHECKPOINT_RESTORE=y",
64 		NULL
65 	},
66 	.needs_root = 1,
67 };
68