]> granicus.if.org Git - strace/blob - ipc.c
ipc.c: move fallback definitions of msg, sem, and shm constants to xlat/
[strace] / ipc.c
1 /*
2  * Copyright (c) 1993 Ulrich Pegelow <pegelow@moorea.uni-muenster.de>
3  * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "defs.h"
32 #ifdef HAVE_MQUEUE_H
33 # include <mqueue.h>
34 #endif
35 #include <fcntl.h>
36 #include <sys/ipc.h>
37 #include <sys/sem.h>
38 #include <sys/msg.h>
39 #include <sys/shm.h>
40
41 #if !defined IPC_64
42 # define IPC_64 0x100
43 #endif
44
45 extern void printsigevent(struct tcb *tcp, long arg);
46
47 #include "xlat/msgctl_flags.h"
48 #include "xlat/semctl_flags.h"
49 #include "xlat/shmctl_flags.h"
50 #include "xlat/resource_flags.h"
51 #include "xlat/shm_resource_flags.h"
52 #include "xlat/shm_flags.h"
53 #include "xlat/ipc_msg_flags.h"
54 #include "xlat/semop_flags.h"
55
56 SYS_FUNC(msgget)
57 {
58         if (tcp->u_arg[0])
59                 tprintf("%#lx, ", tcp->u_arg[0]);
60         else
61                 tprints("IPC_PRIVATE, ");
62         if (printflags(resource_flags, tcp->u_arg[1] & ~0777, NULL) != 0)
63                 tprints("|");
64         tprintf("%#lo", tcp->u_arg[1] & 0777);
65         return RVAL_DECODED;
66 }
67
68 #ifdef IPC_64
69 # define PRINTCTL(flagset, arg, dflt) \
70         if ((arg) & IPC_64) tprints("IPC_64|"); \
71         printxval((flagset), (arg) &~ IPC_64, dflt)
72 #else
73 # define PRINTCTL printxval
74 #endif
75
76 static int
77 indirect_ipccall(struct tcb *tcp)
78 {
79         return tcp->s_ent->sys_flags & TRACE_INDIRECT_SUBCALL;
80 }
81
82 SYS_FUNC(msgctl)
83 {
84         tprintf("%lu, ", tcp->u_arg[0]);
85         PRINTCTL(msgctl_flags, tcp->u_arg[1], "MSG_???");
86         tprints(", ");
87         printaddr(tcp->u_arg[indirect_ipccall(tcp) ? 3 : 2]);
88         return RVAL_DECODED;
89 }
90
91 static void
92 tprint_msgbuf(struct tcb *tcp, const long addr, const unsigned long count)
93 {
94         long mtype;
95
96         if (!umove_or_printaddr(tcp, addr, &mtype)) {
97                 tprintf("{%lu, ", mtype);
98                 printstr(tcp, addr + sizeof(mtype), count);
99                 tprints("}");
100         }
101         tprintf(", %lu, ", count);
102 }
103
104 static void
105 tprint_msgsnd(struct tcb *tcp, const long addr, const unsigned long count,
106               const unsigned long flags)
107 {
108         tprint_msgbuf(tcp, addr, count);
109         printflags(ipc_msg_flags, flags, "MSG_???");
110 }
111
112 SYS_FUNC(msgsnd)
113 {
114         tprintf("%d, ", (int) tcp->u_arg[0]);
115         if (indirect_ipccall(tcp)) {
116                 tprint_msgsnd(tcp, tcp->u_arg[3], tcp->u_arg[1],
117                               tcp->u_arg[2]);
118         } else {
119                 tprint_msgsnd(tcp, tcp->u_arg[1], tcp->u_arg[2],
120                               tcp->u_arg[3]);
121         }
122         return RVAL_DECODED;
123 }
124
125 static void
126 tprint_msgrcv(struct tcb *tcp, const long addr, const unsigned long count,
127               const long msgtyp)
128 {
129         tprint_msgbuf(tcp, addr, count);
130         tprintf("%ld, ", msgtyp);
131 }
132
133 SYS_FUNC(msgrcv)
134 {
135         if (entering(tcp)) {
136                 tprintf("%d, ", (int) tcp->u_arg[0]);
137         } else {
138                 if (indirect_ipccall(tcp)) {
139                         struct ipc_wrapper {
140                                 struct msgbuf *msgp;
141                                 long msgtyp;
142                         } tmp;
143
144                         if (umove_or_printaddr(tcp, tcp->u_arg[3], &tmp))
145                                 tprintf(", %lu, ", tcp->u_arg[1]);
146                         else
147                                 tprint_msgrcv(tcp, (long) tmp.msgp,
148                                         tcp->u_arg[1], tmp.msgtyp);
149                         printflags(ipc_msg_flags, tcp->u_arg[2], "MSG_???");
150                 } else {
151                         tprint_msgrcv(tcp, tcp->u_arg[1],
152                                 tcp->u_arg[2], tcp->u_arg[3]);
153                         printflags(ipc_msg_flags, tcp->u_arg[4], "MSG_???");
154                 }
155         }
156         return 0;
157 }
158
159 static void
160 tprint_sembuf(const struct sembuf *sb)
161 {
162         tprintf("{%u, %d, ", sb->sem_num, sb->sem_op);
163         printflags(semop_flags, sb->sem_flg, "SEM_???");
164         tprints("}");
165 }
166
167 static void
168 tprint_sembuf_array(struct tcb *tcp, const long addr, const unsigned long count)
169 {
170         unsigned long max_count;
171         struct sembuf sb;
172
173         if (abbrev(tcp))
174                 max_count = (max_strlen < count) ? max_strlen : count;
175         else
176                 max_count = count;
177
178         if (!max_count)
179                 printaddr(addr);
180         else if (!umove_or_printaddr(tcp, addr, &sb)) {
181                 unsigned long i;
182
183                 tprints("[");
184                 tprint_sembuf(&sb);
185
186                 for (i = 1; i < max_count; ++i) {
187                         tprints(", ");
188                         if (umove_or_printaddr(tcp, addr + i * sizeof(sb), &sb))
189                                 break;
190                         else
191                                 tprint_sembuf(&sb);
192                 }
193
194                 if (i < max_count || max_count < count)
195                         tprints(", ...");
196
197                 tprints("]");
198         }
199         tprintf(", %lu", count);
200 }
201
202 SYS_FUNC(semop)
203 {
204         tprintf("%lu, ", tcp->u_arg[0]);
205         if (indirect_ipccall(tcp)) {
206                 tprint_sembuf_array(tcp, tcp->u_arg[3], tcp->u_arg[1]);
207         } else {
208                 tprint_sembuf_array(tcp, tcp->u_arg[1], tcp->u_arg[2]);
209         }
210         return RVAL_DECODED;
211 }
212
213 SYS_FUNC(semtimedop)
214 {
215         tprintf("%lu, ", tcp->u_arg[0]);
216         if (indirect_ipccall(tcp)) {
217                 tprint_sembuf_array(tcp, tcp->u_arg[3], tcp->u_arg[1]);
218                 tprints(", ");
219 #if defined(S390) || defined(S390X)
220                 printtv(tcp, tcp->u_arg[2]);
221 #else
222                 printtv(tcp, tcp->u_arg[4]);
223 #endif
224         } else {
225                 tprint_sembuf_array(tcp, tcp->u_arg[1], tcp->u_arg[2]);
226                 tprints(", ");
227                 printtv(tcp, tcp->u_arg[3]);
228         }
229         return RVAL_DECODED;
230 }
231
232 SYS_FUNC(semget)
233 {
234         if (tcp->u_arg[0])
235                 tprintf("%#lx", tcp->u_arg[0]);
236         else
237                 tprints("IPC_PRIVATE");
238         tprintf(", %lu, ", tcp->u_arg[1]);
239         if (printflags(resource_flags, tcp->u_arg[2] & ~0777, NULL) != 0)
240                 tprints("|");
241         tprintf("%#lo", tcp->u_arg[2] & 0777);
242         return RVAL_DECODED;
243 }
244
245 SYS_FUNC(semctl)
246 {
247         tprintf("%lu, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
248         PRINTCTL(semctl_flags, tcp->u_arg[2], "SEM_???");
249         tprints(", ");
250         if (indirect_ipccall(tcp)) {
251                 if (current_wordsize == sizeof(int))
252                         printnum_int(tcp, tcp->u_arg[3], "%#x");
253                 else
254                         printnum_long(tcp, tcp->u_arg[3], "%#lx");
255         } else {
256                 tprintf("%#lx", tcp->u_arg[3]);
257         }
258         return RVAL_DECODED;
259 }
260
261 SYS_FUNC(shmget)
262 {
263         if (tcp->u_arg[0])
264                 tprintf("%#lx", tcp->u_arg[0]);
265         else
266                 tprints("IPC_PRIVATE");
267         tprintf(", %lu, ", tcp->u_arg[1]);
268         if (printflags(shm_resource_flags, tcp->u_arg[2] & ~0777, NULL) != 0)
269                 tprints("|");
270         tprintf("%#lo", tcp->u_arg[2] & 0777);
271         return RVAL_DECODED;
272 }
273
274 SYS_FUNC(shmctl)
275 {
276         tprintf("%lu, ", tcp->u_arg[0]);
277         PRINTCTL(shmctl_flags, tcp->u_arg[1], "SHM_???");
278         tprints(", ");
279         printaddr(tcp->u_arg[indirect_ipccall(tcp) ? 3 : 2]);
280         return RVAL_DECODED;
281 }
282
283 SYS_FUNC(shmat)
284 {
285         if (entering(tcp)) {
286                 tprintf("%lu, ", tcp->u_arg[0]);
287                 if (indirect_ipccall(tcp)) {
288                         printaddr(tcp->u_arg[3]);
289                         tprints(", ");
290                         printflags(shm_flags, tcp->u_arg[1], "SHM_???");
291                 } else {
292                         printaddr(tcp->u_arg[1]);
293                         tprints(", ");
294                         printflags(shm_flags, tcp->u_arg[2], "SHM_???");
295                 }
296                 return 0;
297         } else {
298                 if (syserror(tcp))
299                         return 0;
300                 if (indirect_ipccall(tcp)) {
301                         unsigned long raddr;
302                         if (umove(tcp, tcp->u_arg[2], &raddr) < 0)
303                                 return RVAL_NONE;
304                         tcp->u_rval = raddr;
305                 }
306                 return RVAL_HEX;
307         }
308 }
309
310 SYS_FUNC(shmdt)
311 {
312         printaddr(tcp->u_arg[indirect_ipccall(tcp) ? 3 : 0]);
313         return RVAL_DECODED;
314 }
315
316 SYS_FUNC(mq_open)
317 {
318         printpath(tcp, tcp->u_arg[0]);
319         tprints(", ");
320         /* flags */
321         tprint_open_modes(tcp->u_arg[1]);
322         if (tcp->u_arg[1] & O_CREAT) {
323                 /* mode */
324                 tprintf(", %#lo, ", tcp->u_arg[2]);
325 # ifndef HAVE_MQUEUE_H
326                 printaddr(tcp->u_arg[3]);
327 # else
328                 struct mq_attr attr;
329
330                 if (!umove_or_printaddr(tcp, tcp->u_arg[3], &attr))
331                         tprintf("{mq_maxmsg=%ld, mq_msgsize=%ld}",
332                                 (long) attr.mq_maxmsg,
333                                 (long) attr.mq_msgsize);
334 # endif
335         }
336         return RVAL_DECODED;
337 }
338
339 SYS_FUNC(mq_timedsend)
340 {
341         tprintf("%ld, ", tcp->u_arg[0]);
342         printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
343         tprintf(", %lu, %ld, ", tcp->u_arg[2], tcp->u_arg[3]);
344         printtv(tcp, tcp->u_arg[4]);
345         return RVAL_DECODED;
346 }
347
348 SYS_FUNC(mq_timedreceive)
349 {
350         if (entering(tcp))
351                 tprintf("%ld, ", tcp->u_arg[0]);
352         else {
353                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
354                 tprintf(", %lu, %ld, ", tcp->u_arg[2], tcp->u_arg[3]);
355                 printtv(tcp, tcp->u_arg[4]);
356         }
357         return 0;
358 }
359
360 SYS_FUNC(mq_notify)
361 {
362         tprintf("%ld, ", tcp->u_arg[0]);
363         printsigevent(tcp, tcp->u_arg[1]);
364         return RVAL_DECODED;
365 }
366
367 static void
368 printmqattr(struct tcb *tcp, const long addr)
369 {
370 # ifndef HAVE_MQUEUE_H
371         printaddr(addr);
372 # else
373         struct mq_attr attr;
374         if (umove_or_printaddr(tcp, addr, &attr))
375                 return;
376         tprints("{mq_flags=");
377         tprint_open_modes(attr.mq_flags);
378         tprintf(", mq_maxmsg=%ld, mq_msgsize=%ld, mq_curmsg=%ld}",
379                 (long) attr.mq_maxmsg, (long) attr.mq_msgsize,
380                 (long) attr.mq_curmsgs);
381 # endif
382 }
383
384 SYS_FUNC(mq_getsetattr)
385 {
386         if (entering(tcp)) {
387                 tprintf("%ld, ", tcp->u_arg[0]);
388                 printmqattr(tcp, tcp->u_arg[1]);
389                 tprints(", ");
390         } else
391                 printmqattr(tcp, tcp->u_arg[2]);
392         return 0;
393 }