1 #include "tests.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <sys/shm.h>
5
6 static int id = -1;
7
8 static void
cleanup(void)9 cleanup(void)
10 {
11 shmctl(id, IPC_RMID, NULL);
12 id = -1;
13 }
14
15 #ifdef __alpha__
16 # define SHMAT "osf_shmat"
17 #else
18 # define SHMAT "shmat"
19 #endif
20
21 int
main(void)22 main(void)
23 {
24 static const int bogus_shmid = 0xfdb97531;
25 static const void * const bogus_shmaddr =
26 (void *) (unsigned long) 0xdec0ded1dec0ded2ULL;
27 static const int bogus_shmflg = 0xffffface;
28
29 long rc;
30
31 id = shmget(IPC_PRIVATE, 1, 0600);
32 if (id < 0)
33 perror_msg_and_skip("shmget");
34 atexit(cleanup);
35
36 rc = (long) shmat(bogus_shmid, bogus_shmaddr, bogus_shmflg);
37 printf("%s(%d, %p, SHM_REMAP|SHM_RDONLY|SHM_RND|%#x) = %s\n",
38 SHMAT, bogus_shmid, bogus_shmaddr, bogus_shmflg & ~0x7000,
39 sprintrc(rc));
40
41 shmat(id, NULL, SHM_REMAP);
42 printf("%s(%d, NULL, SHM_REMAP) = -1 %s (%m)\n",
43 SHMAT, id, errno2name());
44
45 void *shmaddr = shmat(id, NULL, SHM_RDONLY);
46 if (shmaddr == (void *)(-1))
47 perror_msg_and_skip("shmat SHM_RDONLY");
48 printf("%s(%d, NULL, SHM_RDONLY) = %p\n", SHMAT, id, shmaddr);
49
50 rc = shmdt(NULL);
51 printf("shmdt(NULL) = %s\n", sprintrc(rc));
52
53 if (shmdt(shmaddr))
54 perror_msg_and_skip("shmdt");
55 printf("shmdt(%p) = 0\n", shmaddr);
56
57 ++shmaddr;
58 void *shmaddr2 = shmat(id, shmaddr, SHM_RND);
59 if (shmaddr2 == (void *)(-1))
60 printf("%s(%d, %p, SHM_RND) = -1 %s (%m)\n",
61 SHMAT, id, shmaddr, errno2name());
62 else
63 printf("%s(%d, %p, SHM_RND) = %p\n",
64 SHMAT, id, shmaddr, shmaddr2);
65
66 puts("+++ exited with 0 +++");
67 return 0;
68 }
69