OSDN Git Service

dl-elf.h: remove unused prototype
[uclinux-h8/uClibc.git] / test / librt / shmtest.c
1 /* Copyright (C) 2009 Mikael Lund Jepsen <mlj@iccc.dk>
2  *
3  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
4  */
5
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>
16
17 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
18
19 char shared_name[] = "/sharetest";
20 int test_data[11] = {0,1,2,3,4,5,6,7,8,9,10};
21
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;
31
32         pipe(pfds);
33
34         switch(pid = fork()) {
35         case -1:
36                 perror("fork");
37                 exit(1);        /* parent exits */
38
39         case 0:
40                 /* Child */
41
42                 /* wait for parent */
43                 read(pfds[0], buf, 5);
44
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++;
56                                         }
57                                 }
58                                 if (test_data_fails == 0)
59                                         printf("%-40s: %s\n", "Compare memory", "Success");
60
61                                 munmap(ptest_data, sizeof(test_data));
62                         }
63                 }
64                 exit(0);
65
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)
73                         {
74                                 printf("%-40s: %s", "ftruncate", strerror(errno));
75                                 shm_unlink(shared_name);
76                                 return 0;
77                         }
78
79                         ptest_data = mmap(0, sizeof(test_data), PROT_READ + PROT_WRITE, MAP_SHARED, fd, 0);
80                         if (ptest_data == MAP_FAILED)
81                         {
82                                 perror("PARENT - mmap:");
83                                 if (shm_unlink(shared_name) == -1) {
84                                         perror("PARENT - shm_unlink:");
85                                 }
86                                 return 0;
87                         }
88                         for (i=0; i < ARRAY_SIZE(test_data); i++)
89                                 ptest_data[i] = test_data[i];
90
91                         /* signal child */
92                         write(pfds[1], "rdy", 5);
93                         /* wait for child */
94                         wait(&rv);
95
96                         /* Cleanup */
97                         munmap(ptest_data, sizeof(test_data));
98                         if (shm_unlink(shared_name) == -1) {
99                                 perror("PARENT - shm_unlink:");
100                         }
101                 }
102         }
103         return 0;
104 }