• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-2018 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 #ifndef SEM_STAT_ANY
40 # define SEM_STAT_ANY 20
41 #endif
42 
43 #if XLAT_RAW
44 # define str_ipc_flags "0xface1e00"
45 # define str_ipc_private "0"
46 # define str_ipc_rmid "0"
47 # define str_ipc_stat "0x2"
48 # define str_sem_stat "0x12"
49 # define str_sem_info "0x13"
50 # define str_sem_stat_any "0x14"
51 # define str_ipc_64 "0x100"
52 # define str_bogus_cmd "0xdeadbeef"
53 #elif XLAT_VERBOSE
54 # define str_ipc_flags \
55 	"0xface1e00 /\\* IPC_CREAT\\|IPC_EXCL\\|IPC_NOWAIT\\|0xface1000 \\*/"
56 # define str_ipc_private "0 /\\* IPC_PRIVATE \\*/"
57 # define str_ipc_rmid "0 /\\* IPC_RMID \\*/"
58 # define str_ipc_stat "0x2 /\\* IPC_STAT \\*/"
59 # define str_sem_stat "0x12 /\\* SEM_STAT \\*/"
60 # define str_sem_info "0x13 /\\* SEM_INFO \\*/"
61 # define str_sem_stat_any "0x14 /\\* SEM_STAT_ANY \\*/"
62 # define str_ipc_64 "0x100 /\\* IPC_64 \\*/"
63 # define str_bogus_cmd "0xdeadbeef /\\* SEM_\\?\\?\\? \\*/"
64 #else
65 # define str_ipc_flags "IPC_CREAT\\|IPC_EXCL\\|IPC_NOWAIT\\|0xface1000"
66 # define str_ipc_private "IPC_PRIVATE"
67 # define str_ipc_rmid "IPC_RMID"
68 # define str_ipc_stat "IPC_STAT"
69 # define str_sem_stat "SEM_STAT"
70 # define str_sem_info "SEM_INFO"
71 # define str_sem_stat_any "SEM_STAT_ANY"
72 # define str_ipc_64 "IPC_64"
73 # define str_bogus_cmd "0xdeadbeef /\\* SEM_\\?\\?\\? \\*/"
74 #endif
75 
76 union semun {
77 	int		 val;    /* Value for SETVAL */
78 	struct semid_ds	*buf;    /* Buffer for IPC_STAT, IPC_SET */
79 	unsigned short	*array;  /* Array for GETALL, SETALL */
80 	struct seminfo	*__buf;  /* Buffer for IPC_INFO
81 				    (Linux-specific) */
82 };
83 
84 static int id = -1;
85 
86 static void
cleanup(void)87 cleanup(void)
88 {
89 	semctl(id, 0, IPC_RMID, 0);
90 	printf("semctl\\(%d, 0, (%s\\|)?%s, \\[?NULL\\]?\\) = 0\n",
91 	       id, str_ipc_64, str_ipc_rmid);
92 	id = -1;
93 }
94 
95 int
main(void)96 main(void)
97 {
98 	static const key_t private_key =
99 		(key_t) (0xffffffff00000000ULL | IPC_PRIVATE);
100 	static const key_t bogus_key = (key_t) 0xeca86420fdb97531ULL;
101 	static const int bogus_semid = 0xfdb97531;
102 	static const int bogus_semnum = 0xeca86420;
103 	static const int bogus_size = 0xdec0ded1;
104 	static const int bogus_flags = 0xface1e55;
105 	static const int bogus_cmd = 0xdeadbeef;
106 	static const unsigned long bogus_arg =
107 		(unsigned long) 0xbadc0dedfffffaceULL;
108 
109 	int rc;
110 	union semun un;
111 	struct semid_ds ds;
112 	struct seminfo info;
113 
114 	rc = semget(bogus_key, bogus_size, bogus_flags);
115 	printf("semget\\(%#llx, %d, %s\\|%#04o\\) = %s\n",
116 	       zero_extend_signed_to_ull(bogus_key), bogus_size,
117 	       str_ipc_flags, bogus_flags & 0777, sprintrc_grep(rc));
118 
119 	id = semget(private_key, 1, 0600);
120 	if (id < 0)
121 		perror_msg_and_skip("semget");
122 	printf("semget\\(%s, 1, 0600\\) = %d\n", str_ipc_private, id);
123 	atexit(cleanup);
124 
125 	rc = semctl(bogus_semid, bogus_semnum, bogus_cmd, bogus_arg);
126 #define SEMCTL_BOGUS_ARG_FMT "(%#lx|\\[(%#lx|NULL)\\]|NULL)"
127 	printf("semctl\\(%d, %d, (%s\\|)?%s, " SEMCTL_BOGUS_ARG_FMT "\\) = %s\n",
128 	       bogus_semid, bogus_semnum, str_ipc_64, str_bogus_cmd,
129 	       bogus_arg, bogus_arg, sprintrc_grep(rc));
130 
131 	un.buf = &ds;
132 	if (semctl(id, 0, IPC_STAT, un))
133 		perror_msg_and_skip("semctl IPC_STAT");
134 	printf("semctl\\(%d, 0, (%s\\|)?%s, \\[?%p\\]?\\) = 0\n",
135 	       id, str_ipc_64, str_ipc_stat, &ds);
136 
137 	un.__buf = &info;
138 	rc = semctl(0, 0, SEM_INFO, un);
139 	printf("semctl\\(0, 0, (%s\\|)?%s, \\[?%p\\]?\\) = %s\n",
140 	       str_ipc_64, str_sem_info, &info, sprintrc_grep(rc));
141 
142 	un.buf = &ds;
143 	rc = semctl(id, 0, SEM_STAT, un);
144 	printf("semctl\\(%d, 0, (%s\\|)?%s, \\[?%p\\]?\\) = %s\n",
145 	       id, str_ipc_64, str_sem_stat, &ds, sprintrc_grep(rc));
146 
147 	rc = semctl(id, 0, SEM_STAT_ANY, un);
148 	printf("semctl\\(%d, 0, (%s\\|)?%s, (%p|\\[(%p|NULL)\\]|NULL)\\) = %s\n",
149 	       id, str_ipc_64, str_sem_stat_any, &ds, &ds, sprintrc_grep(rc));
150 
151 	return 0;
152 }
153