ti_IN locale: Fix noexpr
[glibc.git] / sysvipc / test-sysvshm.c
blobb7ccd0e20e54b13efe1d62b72a1a8512146f3b11
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/>. */
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>
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. */
33 static char *name;
34 static int shmid;
36 static void
37 remove_shm (void)
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);
44 static void
45 do_prepare (int argc, char *argv[])
47 int fd = create_temp_file ("tst-sysvshm.", &name);
48 if (fd == -1)
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) \
58 if ((v) != (k)) \
59 FAIL_EXIT1("%d != %d", v, k)
61 #define SHM_MODE 0666
63 static int
64 do_test (void)
66 atexit (remove_shm);
68 key_t key = ftok (name, 'G');
69 if (key == -1)
70 FAIL_EXIT1 ("ftok failed");
72 long int pgsz = sysconf (_SC_PAGESIZE);
73 if (pgsz == -1)
74 FAIL_EXIT1 ("sysconf (_SC_PAGESIZE) failed (errno = %d)", errno);
76 shmid = shmget(key, pgsz, IPC_CREAT | IPC_EXCL | SHM_MODE);
77 if (shmid == -1)
79 if (errno == ENOSYS)
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);
128 return 0;
131 #include <support/test-driver.c>