]> granicus.if.org Git - strace/blob - tests/ipc_sem.c
Add copyright headers
[strace] / tests / ipc_sem.c
1 /*
2  * Copyright (c) 2015 Andreas Schwab <schwab@suse.de>
3  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
4  * Copyright (c) 2015-2017 The strace developers.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "tests.h"
31 #include <errno.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/sem.h>
35
36 #include "xlat.h"
37 #include "xlat/resource_flags.h"
38
39 union semun {
40         int              val;    /* Value for SETVAL */
41         struct semid_ds *buf;    /* Buffer for IPC_STAT, IPC_SET */
42         unsigned short  *array;  /* Array for GETALL, SETALL */
43         struct seminfo  *__buf;  /* Buffer for IPC_INFO
44                                     (Linux-specific) */
45 };
46
47 static int id = -1;
48
49 static void
50 cleanup(void)
51 {
52         semctl(id, 0, IPC_RMID, 0);
53         printf("semctl\\(%d, 0, (IPC_64\\|)?IPC_RMID, \\[?NULL\\]?\\) += 0\n",
54                id);
55         id = -1;
56 }
57
58 int
59 main(void)
60 {
61         static const key_t private_key =
62                 (key_t) (0xffffffff00000000ULL | IPC_PRIVATE);
63         static const key_t bogus_key = (key_t) 0xeca86420fdb97531ULL;
64         static const int bogus_semid = 0xfdb97531;
65         static const int bogus_semnum = 0xeca86420;
66         static const int bogus_size = 0xdec0ded1;
67         static const int bogus_flags = 0xface1e55;
68         static const int bogus_cmd = 0xdeadbeef;
69         static const unsigned long bogus_arg =
70                 (unsigned long) 0xbadc0dedfffffaceULL;
71
72         int rc;
73         union semun un;
74         struct semid_ds ds;
75         struct seminfo info;
76
77         rc = semget(bogus_key, bogus_size, bogus_flags);
78         printf("semget\\(%#llx, %d, %s%s%s%#x\\|%#04o\\) += %s\n",
79                zero_extend_signed_to_ull(bogus_key), bogus_size,
80                IPC_CREAT & bogus_flags ? "IPC_CREAT\\|" : "",
81                IPC_EXCL & bogus_flags ? "IPC_EXCL\\|" : "",
82                IPC_NOWAIT & bogus_flags ? "IPC_NOWAIT\\|" : "",
83                bogus_flags & ~(0777 | IPC_CREAT | IPC_EXCL | IPC_NOWAIT),
84                bogus_flags & 0777, sprintrc_grep(rc));
85
86         id = semget(private_key, 1, 0600);
87         if (id < 0)
88                 perror_msg_and_skip("semget");
89         printf("semget\\(IPC_PRIVATE, 1, 0600\\) += %d\n", id);
90         atexit(cleanup);
91
92         rc = semctl(bogus_semid, bogus_semnum, bogus_cmd, bogus_arg);
93 #define SEMCTL_BOGUS_ARG_FMT "(%#lx|\\[(%#lx|NULL)\\]|NULL)"
94         printf("semctl\\(%d, %d, (IPC_64\\|)?%#x /\\* SEM_\\?\\?\\? \\*/"
95                ", " SEMCTL_BOGUS_ARG_FMT "\\) += %s\n",
96                bogus_semid, bogus_semnum, bogus_cmd,
97                bogus_arg, bogus_arg, sprintrc_grep(rc));
98
99         un.buf = &ds;
100         if (semctl(id, 0, IPC_STAT, un))
101                 perror_msg_and_skip("semctl IPC_STAT");
102         printf("semctl\\(%d, 0, (IPC_64\\|)?IPC_STAT, \\[?%p\\]?\\) += 0\n",
103                id, &ds);
104
105         un.__buf = &info;
106         rc = semctl(0, 0, SEM_INFO, un);
107         printf("semctl\\(0, 0, (IPC_64\\|)?SEM_INFO, \\[?%p\\]?\\) += %s\n",
108                &info, sprintrc_grep(rc));
109
110         un.buf = &ds;
111         rc = semctl(id, 0, SEM_STAT, un);
112         printf("semctl\\(%d, 0, (IPC_64\\|)?SEM_STAT, \\[?%p\\]?\\) += %s\n",
113                id, &ds, sprintrc_grep(rc));
114
115         return 0;
116 }