]> granicus.if.org Git - strace/blob - tests/ipc_shm.c
Update SHM_* constants
[strace] / tests / ipc_shm.c
1 /*
2  * Copyright (c) 2015 Elvira Khabirova <lineprinter0@gmail.com>
3  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "tests.h"
30 #include <errno.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sys/shm.h>
34
35 #include "xlat.h"
36 #include "xlat/shm_resource_flags.h"
37
38 static int id = -1;
39
40 static void
41 cleanup(void)
42 {
43         shmctl(id, IPC_RMID, NULL);
44         printf("shmctl\\(%d, (IPC_64\\|)?IPC_RMID, NULL\\) += 0\n", id);
45         id = -1;
46 }
47
48 int
49 main(void)
50 {
51         static const key_t private_key =
52                 (key_t) (0xffffffff00000000ULL | IPC_PRIVATE);
53         static const key_t bogus_key = (key_t) 0xeca86420fdb97531ULL;
54         static const int bogus_id = 0xdefaced1;
55         static const int bogus_cmd = 0xdefaced2;
56         static void * const bogus_addr = (void *) -1L;
57         static const size_t bogus_size =
58         /*
59          * musl sets size to SIZE_MAX if size argument is greater than
60          * PTRDIFF_MAX - musl/src/ipc/shmget.c
61          */
62         #ifdef __GLIBC__
63                 (size_t) 0xdec0ded1dec0ded2ULL;
64         #else
65                 (size_t) 0x1e55c0de5dec0dedULL;
66         #endif
67         static const int bogus_flags = 0xface1e55;
68
69         int rc;
70         struct shmid_ds ds;
71
72         rc = shmget(bogus_key, bogus_size, bogus_flags);
73         printf("shmget\\(%#llx, %zu, %s%s%s%s%#x\\|%#04o\\) += %s\n",
74                zero_extend_signed_to_ull(bogus_key), bogus_size,
75                IPC_CREAT & bogus_flags ? "IPC_CREAT\\|" : "",
76                IPC_EXCL & bogus_flags ? "IPC_EXCL\\|" : "",
77                SHM_HUGETLB & bogus_flags ? "SHM_HUGETLB\\|" : "",
78                SHM_NORESERVE & bogus_flags ? "SHM_NORESERVE\\|" : "",
79                bogus_flags & ~(0777 | IPC_CREAT | IPC_EXCL
80                                     | SHM_HUGETLB | SHM_NORESERVE),
81                bogus_flags & 0777, sprintrc_grep(rc));
82
83         id = shmget(private_key, 1, 0600);
84         if (id < 0)
85                 perror_msg_and_skip("shmget");
86         printf("shmget\\(IPC_PRIVATE, 1, 0600\\) += %d\n", id);
87         atexit(cleanup);
88
89         rc = shmctl(bogus_id, bogus_cmd, NULL);
90         printf("shmctl\\(%d, (IPC_64\\|)?%#x /\\* SHM_\\?\\?\\? \\*/, NULL\\)"
91                " += %s\n", bogus_id, bogus_cmd, sprintrc_grep(rc));
92
93         rc = shmctl(bogus_id, IPC_STAT, bogus_addr);
94         printf("shmctl\\(%d, (IPC_64\\|)?IPC_STAT, %p\\) += %s\n",
95                bogus_id, bogus_addr, sprintrc_grep(rc));
96
97         if (shmctl(id, IPC_STAT, &ds))
98                 perror_msg_and_skip("shmctl IPC_STAT");
99         printf("shmctl\\(%d, (IPC_64\\|)?IPC_STAT, \\{shm_perm=\\{uid=%u, gid=%u, "
100                 "mode=%#o, key=%u, cuid=%u, cgid=%u\\}, shm_segsz=%u, shm_cpid=%u, "
101                 "shm_lpid=%u, shm_nattch=%u, shm_atime=%u, shm_dtime=%u, "
102                 "shm_ctime=%u\\}\\) += 0\n",
103                 id, (unsigned) ds.shm_perm.uid, (unsigned) ds.shm_perm.gid,
104                 (unsigned) ds.shm_perm.mode, (unsigned) ds.shm_perm.__key,
105                 (unsigned) ds.shm_perm.cuid, (unsigned) ds.shm_perm.cgid,
106                 (unsigned) ds.shm_segsz, (unsigned) ds.shm_cpid,
107                 (unsigned) ds.shm_lpid, (unsigned) ds.shm_nattch,
108                 (unsigned) ds.shm_atime, (unsigned) ds.shm_dtime,
109                 (unsigned) ds. shm_ctime);
110
111         if (shmctl(id, IPC_SET, &ds))
112                 perror_msg_and_skip("shmctl IPC_SET");
113         printf("shmctl\\(%d, (IPC_64\\|)?IPC_SET, \\{shm_perm=\\{uid=%u, gid=%u"
114                ", mode=%#o\\}, ...\\}\\) += 0\n",
115                id, (unsigned) ds.shm_perm.uid, (unsigned) ds.shm_perm.gid,
116                (unsigned) ds.shm_perm.mode);
117
118         rc = shmctl(0, SHM_INFO, &ds);
119         printf("shmctl\\(0, (IPC_64\\|)?SHM_INFO, %p\\) += %s\n",
120                &ds, sprintrc_grep(rc));
121
122         rc = shmctl(id, SHM_STAT, &ds);
123         printf("shmctl\\(%d, (IPC_64\\|)?SHM_STAT, %p\\) += %s\n",
124                id, &ds, sprintrc_grep(rc));
125
126         return 0;
127 }