unwind.h: Move to libc/sysdeps from nptl sysdeps
[uclibc-ng.git] / test / librt / shmtest.c
bloba14302d09028abe46de10977e6e7775522009f60
1 /* Copyright (C) 2009 Mikael Lund Jepsen <mlj@iccc.dk>
3 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
4 */
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <sys/mman.h>
14 #include <sys/stat.h>
15 #include <sys/wait.h>
17 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
19 char shared_name[] = "/sharetest";
20 int test_data[11] = {0,1,2,3,4,5,6,7,8,9,10};
22 int main(void) {
23 int pfds[2];
24 pid_t pid;
25 int fd;
26 int test_data_fails = 0;
27 char *ptest_data;
28 unsigned int i;
29 char buf[30];
30 int rv;
32 pipe(pfds);
34 switch(pid = fork()) {
35 case -1:
36 perror("fork");
37 exit(1); /* parent exits */
39 case 0:
40 /* Child */
42 /* wait for parent */
43 read(pfds[0], buf, 5);
45 fd = shm_open(shared_name, O_RDWR, DEFFILEMODE);
46 if (fd == -1) {
47 perror("CHILD - shm_open(existing):");
48 exit(1);
49 } else {
50 ptest_data = mmap(0, sizeof(test_data), PROT_READ + PROT_WRITE, MAP_SHARED, fd, 0);
51 if (ptest_data != MAP_FAILED) {
52 for (i=0; i < ARRAY_SIZE(test_data); i++) {
53 if (ptest_data[i] != test_data[i]) {
54 printf("%-40s: Offset %d, local %d, shm %d\n", "Compare memory error", i, test_data[i], ptest_data[i]);
55 test_data_fails++;
58 if (test_data_fails == 0)
59 printf("%-40s: %s\n", "Compare memory", "Success");
61 munmap(ptest_data, sizeof(test_data));
64 exit(0);
66 default:
67 /* Parent */
68 fd = shm_open(shared_name, O_RDWR+O_CREAT+O_EXCL, DEFFILEMODE );
69 if (fd == -1) {
70 perror("PARENT - shm_open(create):");
71 } else {
72 if ((ftruncate(fd, sizeof(test_data))) == -1)
74 printf("%-40s: %s", "ftruncate", strerror(errno));
75 shm_unlink(shared_name);
76 return 0;
79 ptest_data = mmap(0, sizeof(test_data), PROT_READ + PROT_WRITE, MAP_SHARED, fd, 0);
80 if (ptest_data == MAP_FAILED)
82 perror("PARENT - mmap:");
83 if (shm_unlink(shared_name) == -1) {
84 perror("PARENT - shm_unlink:");
86 return 0;
88 for (i=0; i < ARRAY_SIZE(test_data); i++)
89 ptest_data[i] = test_data[i];
91 /* signal child */
92 write(pfds[1], "rdy", 5);
93 /* wait for child */
94 wait(&rv);
96 /* Cleanup */
97 munmap(ptest_data, sizeof(test_data));
98 if (shm_unlink(shared_name) == -1) {
99 perror("PARENT - shm_unlink:");
103 return 0;