1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
4f08c3bdfSopenharmony_ci * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * It is a basic test for msg_next_id.
11f08c3bdfSopenharmony_ci * msg_next_id specifies desired id for next allocated IPC message. By
12f08c3bdfSopenharmony_ci * default it's equal to -1, which means generic allocation logic.
13f08c3bdfSopenharmony_ci * Possible values to set are in range {0..INT_MAX}.
14f08c3bdfSopenharmony_ci * The value will be set back to -1 by kernel after successful IPC object
15f08c3bdfSopenharmony_ci * allocation.
16f08c3bdfSopenharmony_ci */
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#include <errno.h>
19f08c3bdfSopenharmony_ci#include <string.h>
20f08c3bdfSopenharmony_ci#include <sys/types.h>
21f08c3bdfSopenharmony_ci#include <sys/ipc.h>
22f08c3bdfSopenharmony_ci#include <sys/msg.h>
23f08c3bdfSopenharmony_ci#include "tst_test.h"
24f08c3bdfSopenharmony_ci#include "tst_safe_sysv_ipc.h"
25f08c3bdfSopenharmony_ci#include "libnewipc.h"
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci#define NEXT_ID_PATH "/proc/sys/kernel/msg_next_id"
28f08c3bdfSopenharmony_cistatic int queue_id, pid;
29f08c3bdfSopenharmony_cistatic key_t msgkey;
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_cistatic void verify_msgget(void)
32f08c3bdfSopenharmony_ci{
33f08c3bdfSopenharmony_ci	SAFE_FILE_PRINTF(NEXT_ID_PATH, "%d", pid);
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | MSG_RW);
36f08c3bdfSopenharmony_ci	if (queue_id == pid)
37f08c3bdfSopenharmony_ci		tst_res(TPASS, "msg_next_id succeeded, queue id %d", pid);
38f08c3bdfSopenharmony_ci	else
39f08c3bdfSopenharmony_ci		tst_res(TFAIL, "msg_next_id failed, expected id %d, but got %d", pid, queue_id);
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	TST_ASSERT_INT(NEXT_ID_PATH, -1);
42f08c3bdfSopenharmony_ci	SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
43f08c3bdfSopenharmony_ci	pid++;
44f08c3bdfSopenharmony_ci}
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_cistatic void setup(void)
47f08c3bdfSopenharmony_ci{
48f08c3bdfSopenharmony_ci	msgkey = GETIPCKEY();
49f08c3bdfSopenharmony_ci	pid = getpid();
50f08c3bdfSopenharmony_ci}
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_cistatic void cleanup(void)
53f08c3bdfSopenharmony_ci{
54f08c3bdfSopenharmony_ci	if (queue_id != -1)
55f08c3bdfSopenharmony_ci		SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
56f08c3bdfSopenharmony_ci}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_cistatic struct tst_test test = {
59f08c3bdfSopenharmony_ci	.setup = setup,
60f08c3bdfSopenharmony_ci	.cleanup = cleanup,
61f08c3bdfSopenharmony_ci	.test_all = verify_msgget,
62f08c3bdfSopenharmony_ci	.needs_kconfigs = (const char *[]) {
63f08c3bdfSopenharmony_ci		"CONFIG_CHECKPOINT_RESTORE=y",
64f08c3bdfSopenharmony_ci		NULL
65f08c3bdfSopenharmony_ci	},
66f08c3bdfSopenharmony_ci	.needs_root = 1,
67f08c3bdfSopenharmony_ci};
68