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 * When the shared memory segment identifier that shm_next_id stored is already 13f08c3bdfSopenharmony_ci * in use, call shmget with different key just use another unused value in range 14f08c3bdfSopenharmony_ci * [0,INT_MAX]. Kernel doesn't guarantee the desired id. 15f08c3bdfSopenharmony_ci */ 16f08c3bdfSopenharmony_ci 17f08c3bdfSopenharmony_ci#include <errno.h> 18f08c3bdfSopenharmony_ci#include <string.h> 19f08c3bdfSopenharmony_ci#include <sys/types.h> 20f08c3bdfSopenharmony_ci#include <sys/ipc.h> 21f08c3bdfSopenharmony_ci#include <sys/shm.h> 22f08c3bdfSopenharmony_ci#include "tst_test.h" 23f08c3bdfSopenharmony_ci#include "tst_safe_sysv_ipc.h" 24f08c3bdfSopenharmony_ci#include "libnewipc.h" 25f08c3bdfSopenharmony_ci 26f08c3bdfSopenharmony_ci#define NEXT_ID_PATH "/proc/sys/kernel/shm_next_id" 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_cistatic int shm_id[2], pid; 29f08c3bdfSopenharmony_cistatic key_t shmkey[2]; 30f08c3bdfSopenharmony_ci 31f08c3bdfSopenharmony_cistatic void verify_shmget(void) 32f08c3bdfSopenharmony_ci{ 33f08c3bdfSopenharmony_ci SAFE_FILE_PRINTF(NEXT_ID_PATH, "%d", shm_id[0]); 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_ci shm_id[1] = SAFE_SHMGET(shmkey[1], SHM_SIZE, IPC_CREAT | SHM_RW); 36f08c3bdfSopenharmony_ci if (shm_id[1] == shm_id[0]) 37f08c3bdfSopenharmony_ci tst_res(TFAIL, "shm id %d has existed, shmget() returns the" 38f08c3bdfSopenharmony_ci " same shm id unexpectedly", shm_id[0]); 39f08c3bdfSopenharmony_ci else 40f08c3bdfSopenharmony_ci tst_res(TPASS, "shm id %d has existed, shmget() returns the" 41f08c3bdfSopenharmony_ci " new shm id %d", shm_id[0], shm_id[1]); 42f08c3bdfSopenharmony_ci 43f08c3bdfSopenharmony_ci SAFE_SHMCTL(shm_id[1], IPC_RMID, NULL); 44f08c3bdfSopenharmony_ci} 45f08c3bdfSopenharmony_ci 46f08c3bdfSopenharmony_cistatic void setup(void) 47f08c3bdfSopenharmony_ci{ 48f08c3bdfSopenharmony_ci shmkey[0] = GETIPCKEY(); 49f08c3bdfSopenharmony_ci shmkey[1] = GETIPCKEY(); 50f08c3bdfSopenharmony_ci pid = getpid(); 51f08c3bdfSopenharmony_ci SAFE_FILE_PRINTF(NEXT_ID_PATH, "%d", pid); 52f08c3bdfSopenharmony_ci shm_id[0] = SAFE_SHMGET(shmkey[0], SHM_SIZE, IPC_CREAT | SHM_RW); 53f08c3bdfSopenharmony_ci} 54f08c3bdfSopenharmony_ci 55f08c3bdfSopenharmony_cistatic void cleanup(void) 56f08c3bdfSopenharmony_ci{ 57f08c3bdfSopenharmony_ci int i; 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_ci for (i = 0; i < 2; i++) { 60f08c3bdfSopenharmony_ci if (shm_id[i] != -1) 61f08c3bdfSopenharmony_ci SAFE_SHMCTL(shm_id[i], IPC_RMID, NULL); 62f08c3bdfSopenharmony_ci } 63f08c3bdfSopenharmony_ci} 64f08c3bdfSopenharmony_ci 65f08c3bdfSopenharmony_cistatic struct tst_test test = { 66f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 67f08c3bdfSopenharmony_ci .setup = setup, 68f08c3bdfSopenharmony_ci .cleanup = cleanup, 69f08c3bdfSopenharmony_ci .test_all = verify_shmget, 70f08c3bdfSopenharmony_ci .needs_kconfigs = (const char *[]) { 71f08c3bdfSopenharmony_ci "CONFIG_CHECKPOINT_RESTORE=y", 72f08c3bdfSopenharmony_ci NULL 73f08c3bdfSopenharmony_ci }, 74f08c3bdfSopenharmony_ci .needs_root = 1, 75f08c3bdfSopenharmony_ci}; 76