1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2021 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 shm_next_id.
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * shm_next_id specifies desired id for next allocated IPC shared memory. By
13f08c3bdfSopenharmony_ci * default it's equal to -1, which means generic allocation logic.
14f08c3bdfSopenharmony_ci * Possible values to set are in range {0..INT_MAX}.
15f08c3bdfSopenharmony_ci * The value will be set back to -1 by kernel after successful IPC object
16f08c3bdfSopenharmony_ci * allocation.
17f08c3bdfSopenharmony_ci */
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci#include <errno.h>
20f08c3bdfSopenharmony_ci#include <string.h>
21f08c3bdfSopenharmony_ci#include <sys/types.h>
22f08c3bdfSopenharmony_ci#include <sys/ipc.h>
23f08c3bdfSopenharmony_ci#include <sys/shm.h>
24f08c3bdfSopenharmony_ci#include "tst_test.h"
25f08c3bdfSopenharmony_ci#include "tst_safe_sysv_ipc.h"
26f08c3bdfSopenharmony_ci#include "libnewipc.h"
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci#define NEXT_ID_PATH "/proc/sys/kernel/shm_next_id"
29f08c3bdfSopenharmony_cistatic int shm_id, pid;
30f08c3bdfSopenharmony_cistatic key_t shmkey;
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_cistatic void verify_shmget(void)
33f08c3bdfSopenharmony_ci{
34f08c3bdfSopenharmony_ci	SAFE_FILE_PRINTF(NEXT_ID_PATH, "%d", pid);
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	shm_id = SAFE_SHMGET(shmkey, SHM_SIZE, SHM_RW | IPC_CREAT);
37f08c3bdfSopenharmony_ci	if (shm_id == pid)
38f08c3bdfSopenharmony_ci		tst_res(TPASS, "shm_next_id succeeded, shm id %d", pid);
39f08c3bdfSopenharmony_ci	else
40f08c3bdfSopenharmony_ci		tst_res(TFAIL, "shm_next_id failed, expected id %d, but got %d", pid, shm_id);
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	TST_ASSERT_INT(NEXT_ID_PATH, -1);
43f08c3bdfSopenharmony_ci	SAFE_SHMCTL(shm_id, IPC_RMID, NULL);
44f08c3bdfSopenharmony_ci	pid++;
45f08c3bdfSopenharmony_ci}
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_cistatic void setup(void)
48f08c3bdfSopenharmony_ci{
49f08c3bdfSopenharmony_ci	shmkey = GETIPCKEY();
50f08c3bdfSopenharmony_ci	pid = getpid();
51f08c3bdfSopenharmony_ci}
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_cistatic void cleanup(void)
54f08c3bdfSopenharmony_ci{
55f08c3bdfSopenharmony_ci	if (shm_id != -1)
56f08c3bdfSopenharmony_ci		SAFE_SHMCTL(shm_id, IPC_RMID, NULL);
57f08c3bdfSopenharmony_ci}
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_cistatic struct tst_test test = {
60f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
61f08c3bdfSopenharmony_ci	.setup = setup,
62f08c3bdfSopenharmony_ci	.cleanup = cleanup,
63f08c3bdfSopenharmony_ci	.test_all = verify_shmget,
64f08c3bdfSopenharmony_ci	.needs_kconfigs = (const char *[]) {
65f08c3bdfSopenharmony_ci		"CONFIG_CHECKPOINT_RESTORE=y",
66f08c3bdfSopenharmony_ci		NULL
67f08c3bdfSopenharmony_ci	},
68f08c3bdfSopenharmony_ci	.needs_root = 1,
69f08c3bdfSopenharmony_ci};
70