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