1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2020 Cyril Hrubis <chrubis@suse.cz>
4 */
5
6 /*\
7 * [Description]
8 *
9 * Test for a SHM_LOCK and SHM_UNLOCK.
10 */
11
12 #define _GNU_SOURCE
13 #include <stdio.h>
14 #include "tst_test.h"
15 #include "tst_safe_sysv_ipc.h"
16 #include "libnewipc.h"
17
18 #define SHM_SIZE 2048
19
20 static int shm_id = -1;
21
verify_shmlock(void)22 static void verify_shmlock(void)
23 {
24 struct shmid_ds ds;
25
26 TEST(shmctl(shm_id, SHM_LOCK, NULL));
27
28 if (TST_RET != 0)
29 tst_res(TFAIL | TTERRNO, "shmctl(%i, SHM_LOCK, NULL)", shm_id);
30 else
31 tst_res(TPASS, "shmctl(%i, SHM_LOCK, NULL)", shm_id);
32
33
34 SAFE_SHMCTL(shm_id, IPC_STAT, &ds);
35
36 if (ds.shm_perm.mode & SHM_LOCKED)
37 tst_res(TPASS, "SMH_LOCKED bit is on in shm_perm.mode");
38 else
39 tst_res(TFAIL, "SHM_LOCKED bit is off in shm_perm.mode");
40
41 TEST(shmctl(shm_id, SHM_UNLOCK, NULL));
42
43 if (TST_RET != 0)
44 tst_res(TFAIL | TTERRNO, "shmctl(%i, SHM_UNLOCK, NULL)", shm_id);
45 else
46 tst_res(TPASS, "shmctl(%i, SHM_UNLOCK, NULL)", shm_id);
47
48 SAFE_SHMCTL(shm_id, IPC_STAT, &ds);
49
50 if (ds.shm_perm.mode & SHM_LOCKED)
51 tst_res(TFAIL, "SMH_LOCKED bit is on in shm_perm.mode");
52 else
53 tst_res(TPASS, "SHM_LOCKED bit is off in shm_perm.mode");
54 }
55
setup(void)56 static void setup(void)
57 {
58 shm_id = SAFE_SHMGET(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | SHM_RW);
59 }
60
cleanup(void)61 static void cleanup(void)
62 {
63 if (shm_id >= 0)
64 SAFE_SHMCTL(shm_id, IPC_RMID, NULL);
65 }
66
67 static struct tst_test test = {
68 .setup = setup,
69 .cleanup = cleanup,
70 .test_all = verify_shmlock,
71 };
72