bfin: remove inline keyword
[xenomai-head.git] / src / testsuite / regression / posix / shm.c
blob52e0e0e3836d7f77e11e04319c1f69c116c3874f
1 /*
2 * Copyright (C) 2011-2013 Gilles Chanteperdrix <gch@xenomai.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <errno.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <sys/mman.h>
32 #include "check.h"
34 #define SHM_NAME "/shm"
35 #define SHM_SZ 16384
37 int main(void)
39 unsigned i;
40 void *shm;
41 int fd;
43 fprintf(stderr, "Checking posix skin shared memories\n");
45 fd = shm_open(SHM_NAME, O_RDWR | O_CREAT | O_EXCL, 0644);
46 if (fd == -1 && errno == EEXIST) {
47 fprintf(stderr, "Removing previous shared memory\n");
48 check_unix(shm_unlink(SHM_NAME));
49 check_unix(fd = shm_open(SHM_NAME,
50 O_RDWR | O_CREAT | O_EXCL, 0644));
52 check_unix(ftruncate(fd, SHM_SZ));
53 shm = mmap(NULL, SHM_SZ, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
54 check_unix(shm == MAP_FAILED ? -1 : 0);
55 for (i = 0; i < SHM_SZ; i++)
56 if (((unsigned char *)shm)[i] != 0) {
57 fprintf(stderr, "Test 1 failed at byte %u\n", i);
58 check_unix(shm_unlink(SHM_NAME));
59 exit(EXIT_FAILURE);
62 /* Fill the shared memory */
63 memset(shm, 0xA5, SHM_SZ);
64 check_unix(munmap(shm, SHM_SZ));
65 check_unix(close(fd));
67 check_unix(fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0644));
69 /* Resize it */
70 check_unix(ftruncate(fd, 2 * SHM_SZ));
71 shm = mmap(NULL, 2 * SHM_SZ, PROT_READ, MAP_SHARED, fd, 0);
72 check_unix(shm == MAP_FAILED ? -1 : 0);
74 /* Check contents */
75 for (i = 0; i < SHM_SZ; i++)
76 if (((unsigned char *)shm)[i] != 0xA5) {
77 fprintf(stderr, "Test 2 failed at byte %u (%x)\n",
78 i, ((unsigned char *)shm)[i]);
79 check_unix(shm_unlink(SHM_NAME));
80 exit(EXIT_FAILURE);
82 for (i = SHM_SZ; i < 2 * SHM_SZ; i++)
83 if (((unsigned char *)shm)[i] != 0) {
84 fprintf(stderr, "Test 2 failed at byte %u (%x)\n",
85 i, ((unsigned char *)shm)[i]);
86 check_unix(shm_unlink(SHM_NAME));
87 exit(EXIT_FAILURE);
89 check_unix(munmap(shm, 2 * SHM_SZ));
90 check_unix(close(fd));
91 check_unix(shm_unlink(SHM_NAME));
93 fprintf(stderr, "posix skin shared memories: success\n");
94 return EXIT_SUCCESS;