]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysvipc/test-sysvshm.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / sysvipc / test-sysvshm.c
1 /* Basic tests for SYSV shared memory functions.
2 Copyright (C) 2016-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/ipc.h>
26 #include <sys/shm.h>
27
28 #include <support/support.h>
29 #include <support/check.h>
30 #include <support/temp_file.h>
31
32 /* These are for the temporary file we generate. */
33 static char *name;
34 static int shmid;
35
36 static void
37 remove_shm (void)
38 {
39 /* Enforce message queue removal in case of early test failure.
40 Ignore error since the shm may already have being removed. */
41 shmctl (shmid, IPC_RMID, 0);
42 }
43
44 static void
45 do_prepare (int argc, char *argv[])
46 {
47 int fd = create_temp_file ("tst-sysvshm.", &name);
48 if (fd == -1)
49 FAIL_EXIT1 ("cannot create temporary file (errno=%d)", errno);
50 }
51
52 #define PREPARE do_prepare
53
54 /* It is not an extensive test, but rather a functional one aimed to check
55 correct parameter passing on kernel. */
56
57 #define CHECK_EQ(v, k) \
58 if ((v) != (k)) \
59 FAIL_EXIT1("%d != %d", v, k)
60
61 #define SHM_MODE 0666
62
63 static int
64 do_test (void)
65 {
66 atexit (remove_shm);
67
68 key_t key = ftok (name, 'G');
69 if (key == -1)
70 FAIL_EXIT1 ("ftok failed");
71
72 long int pgsz = sysconf (_SC_PAGESIZE);
73 if (pgsz == -1)
74 FAIL_EXIT1 ("sysconf (_SC_PAGESIZE) failed (errno = %d)", errno);
75
76 shmid = shmget(key, pgsz, IPC_CREAT | IPC_EXCL | SHM_MODE);
77 if (shmid == -1)
78 {
79 if (errno == ENOSYS)
80 FAIL_UNSUPPORTED ("shmget not supported");
81 FAIL_EXIT1 ("shmget failed (errno=%d)", errno);
82 }
83
84 /* Get shared memory kernel information and do some sanity checks. */
85 struct shmid_ds shminfo;
86 if (shmctl (shmid, IPC_STAT, &shminfo) == -1)
87 FAIL_EXIT1 ("shmctl with IPC_STAT failed (errno=%d)", errno);
88
89 if (shminfo.shm_perm.__key != key)
90 FAIL_EXIT1 ("shmid_ds::shm_perm::key (%d) != %d",
91 (int) shminfo.shm_perm.__key, (int) key);
92 if (shminfo.shm_perm.mode != SHM_MODE)
93 FAIL_EXIT1 ("shmid_ds::shm_perm::mode (%o) != %o",
94 shminfo.shm_perm.mode, SHM_MODE);
95 if (shminfo.shm_segsz != pgsz)
96 FAIL_EXIT1 ("shmid_ds::shm_segsz (%lu) != %lu",
97 (long unsigned) shminfo.shm_segsz, pgsz);
98
99 /* Attach on shared memory and realize some operations. */
100 int *shmem = shmat (shmid, NULL, 0);
101 if (shmem == (void*) -1)
102 FAIL_EXIT1 ("shmem failed (errno=%d)", errno);
103
104 shmem[0] = 0x55555555;
105 shmem[32] = 0x44444444;
106 shmem[64] = 0x33333333;
107 shmem[128] = 0x22222222;
108
109 if (shmdt (shmem) == -1)
110 FAIL_EXIT1 ("shmem failed (errno=%d)", errno);
111
112 shmem = shmat (shmid, NULL, SHM_RDONLY);
113 if (shmem == (void*) -1)
114 FAIL_EXIT1 ("shmem failed (errno=%d)", errno);
115
116 CHECK_EQ (shmem[0], 0x55555555);
117 CHECK_EQ (shmem[32], 0x44444444);
118 CHECK_EQ (shmem[64], 0x33333333);
119 CHECK_EQ (shmem[128], 0x22222222);
120
121 if (shmdt (shmem) == -1)
122 FAIL_EXIT1 ("shmem failed (errno=%d)", errno);
123
124 /* Finally free up the semnaphore resource. */
125 if (shmctl (shmid, IPC_RMID, 0) == -1)
126 FAIL_EXIT1 ("semctl failed (errno=%d)", errno);
127
128 return 0;
129 }
130
131 #include <support/test-driver.c>