1 /* Basic tests for SYSV shared memory functions.
2 Copyright (C) 2016-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <http://www.gnu.org/licenses/>. */
24 #include <sys/types.h>
28 #include <support/support.h>
29 #include <support/check.h>
30 #include <support/temp_file.h>
32 /* These are for the temporary file we generate. */
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);
45 do_prepare (int argc
, char *argv
[])
47 int fd
= create_temp_file ("tst-sysvshm.", &name
);
49 FAIL_EXIT1 ("cannot create temporary file (errno=%d)", errno
);
52 #define PREPARE do_prepare
54 /* It is not an extensive test, but rather a functional one aimed to check
55 correct parameter passing on kernel. */
57 #define CHECK_EQ(v, k) \
59 FAIL_EXIT1("%d != %d", v, k)
68 key_t key
= ftok (name
, 'G');
70 FAIL_EXIT1 ("ftok failed");
72 long int pgsz
= sysconf (_SC_PAGESIZE
);
74 FAIL_EXIT1 ("sysconf (_SC_PAGESIZE) failed (errno = %d)", errno
);
76 shmid
= shmget(key
, pgsz
, IPC_CREAT
| IPC_EXCL
| SHM_MODE
);
80 FAIL_UNSUPPORTED ("shmget not supported");
81 FAIL_EXIT1 ("shmget failed (errno=%d)", errno
);
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
);
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
);
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
);
104 shmem
[0] = 0x55555555;
105 shmem
[32] = 0x44444444;
106 shmem
[64] = 0x33333333;
107 shmem
[128] = 0x22222222;
109 if (shmdt (shmem
) == -1)
110 FAIL_EXIT1 ("shmem failed (errno=%d)", errno
);
112 shmem
= shmat (shmid
, NULL
, SHM_RDONLY
);
113 if (shmem
== (void*) -1)
114 FAIL_EXIT1 ("shmem failed (errno=%d)", errno
);
116 CHECK_EQ (shmem
[0], 0x55555555);
117 CHECK_EQ (shmem
[32], 0x44444444);
118 CHECK_EQ (shmem
[64], 0x33333333);
119 CHECK_EQ (shmem
[128], 0x22222222);
121 if (shmdt (shmem
) == -1)
122 FAIL_EXIT1 ("shmem failed (errno=%d)", errno
);
124 /* Finally free up the semnaphore resource. */
125 if (shmctl (shmid
, IPC_RMID
, 0) == -1)
126 FAIL_EXIT1 ("semctl failed (errno=%d)", errno
);
131 #include <support/test-driver.c>