1 /*
2  * Test shmat(NULL).
3  *
4  * SPDX-License-Identifier: GPL-2.0-or-later
5  */
6 #include <assert.h>
7 #include <stdlib.h>
8 #include <sys/ipc.h>
9 #include <sys/shm.h>
10 
11 int main(void)
12 {
13     int shmid;
14     char *p;
15     int err;
16 
17     /* Create, attach and intialize shared memory. */
18     shmid = shmget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
19     assert(shmid != -1);
20     p = shmat(shmid, NULL, 0);
21     assert(p != (void *)-1);
22     *p = 42;
23 
24     /* Reattach, check that the value is still there. */
25     err = shmdt(p);
26     assert(err == 0);
27     p = shmat(shmid, NULL, 0);
28     assert(p != (void *)-1);
29     assert(*p == 42);
30 
31     /* Detach. */
32     err = shmdt(p);
33     assert(err == 0);
34     err = shmctl(shmid, IPC_RMID, NULL);
35     assert(err == 0);
36 
37     return EXIT_SUCCESS;
38 }
39