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 #ifndef SHM_EXEC
22 # define SHM_EXEC 0100000
23 #endif
24
25 int
main(void)26 main(void)
27 {
28 static const int bogus_shmid = 0xfdb97531;
29 static const void * const bogus_shmaddr =
30 (void *) (unsigned long) 0xdec0ded1dec0ded2ULL;
31 static const int bogus_shmflg = 0xffffface;
32
33 long rc;
34
35 id = shmget(IPC_PRIVATE, 1, 0600);
36 if (id < 0)
37 perror_msg_and_skip("shmget");
38 atexit(cleanup);
39
40 rc = (long) shmat(bogus_shmid, bogus_shmaddr, bogus_shmflg);
41 printf("%s(%d, %p, SHM_RDONLY|SHM_RND|SHM_REMAP|SHM_EXEC|%#x) = %s\n",
42 SHMAT, bogus_shmid, bogus_shmaddr, bogus_shmflg & ~0xf000,
43 sprintrc(rc));
44
45 shmat(id, NULL, SHM_REMAP);
46 printf("%s(%d, NULL, SHM_REMAP) = -1 %s (%m)\n",
47 SHMAT, id, errno2name());
48
49 void *shmaddr = shmat(id, NULL, SHM_RDONLY);
50 if (shmaddr == (void *)(-1))
51 perror_msg_and_skip("shmat SHM_RDONLY");
52 printf("%s(%d, NULL, SHM_RDONLY) = %p\n", SHMAT, id, shmaddr);
53
54 rc = shmdt(NULL);
55 printf("shmdt(NULL) = %s\n", sprintrc(rc));
56
57 rc = shmdt(shmaddr);
58 printf("shmdt(%p) = %s\n", shmaddr, sprintrc(rc));
59
60 ++shmaddr;
61 void *shmaddr2 = shmat(id, shmaddr, SHM_RND);
62 if (shmaddr2 == (void *)(-1))
63 printf("%s(%d, %p, SHM_RND) = -1 %s (%m)\n",
64 SHMAT, id, shmaddr, errno2name());
65 else {
66 printf("%s(%d, %p, SHM_RND) = %p\n",
67 SHMAT, id, shmaddr, shmaddr2);
68 rc = shmdt(shmaddr2);
69 printf("shmdt(%p) = %s\n", shmaddr2, sprintrc(rc));
70 }
71
72 shmaddr = shmat(id, NULL, SHM_RDONLY|SHM_EXEC);
73 if (shmaddr == (void *)(-1))
74 printf("%s(%d, NULL, SHM_RDONLY|SHM_EXEC) = %s\n",
75 SHMAT, id, sprintrc(-1));
76 else {
77 printf("%s(%d, NULL, SHM_RDONLY|SHM_EXEC) = %p\n",
78 SHMAT, id, shmaddr);
79 rc = shmdt(shmaddr);
80 printf("shmdt(%p) = %s\n", shmaddr, sprintrc(rc));
81 }
82
83 puts("+++ exited with 0 +++");
84 return 0;
85 }
86