]> granicus.if.org Git - strace/blob - ipc.c
dc72151b9085012a42fa538a0a6c946158b0fe61
[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 #ifndef MSG_STAT
42 #define MSG_STAT 11
43 #endif
44 #ifndef MSG_INFO
45 #define MSG_INFO 12
46 #endif
47 #ifndef SHM_STAT
48 #define SHM_STAT 13
49 #endif
50 #ifndef SHM_INFO
51 #define SHM_INFO 14
52 #endif
53 #ifndef SEM_STAT
54 #define SEM_STAT 18
55 #endif
56 #ifndef SEM_INFO
57 #define SEM_INFO 19
58 #endif
59
60 #if !defined IPC_64
61 # define IPC_64 0x100
62 #endif
63
64 extern void printsigevent(struct tcb *tcp, long arg);
65
66 #include "xlat/msgctl_flags.h"
67 #include "xlat/semctl_flags.h"
68 #include "xlat/shmctl_flags.h"
69 #include "xlat/resource_flags.h"
70 #include "xlat/shm_resource_flags.h"
71 #include "xlat/shm_flags.h"
72 #include "xlat/ipc_msg_flags.h"
73 #include "xlat/semop_flags.h"
74
75 SYS_FUNC(msgget)
76 {
77         if (tcp->u_arg[0])
78                 tprintf("%#lx, ", tcp->u_arg[0]);
79         else
80                 tprints("IPC_PRIVATE, ");
81         if (printflags(resource_flags, tcp->u_arg[1] & ~0777, NULL) != 0)
82                 tprints("|");
83         tprintf("%#lo", tcp->u_arg[1] & 0777);
84         return RVAL_DECODED;
85 }
86
87 #ifdef IPC_64
88 # define PRINTCTL(flagset, arg, dflt) \
89         if ((arg) & IPC_64) tprints("IPC_64|"); \
90         printxval((flagset), (arg) &~ IPC_64, dflt)
91 #else
92 # define PRINTCTL printxval
93 #endif
94
95 static int
96 indirect_ipccall(struct tcb *tcp)
97 {
98         return tcp->s_ent->sys_flags & TRACE_INDIRECT_SUBCALL;
99 }
100
101 SYS_FUNC(msgctl)
102 {
103         tprintf("%lu, ", tcp->u_arg[0]);
104         PRINTCTL(msgctl_flags, tcp->u_arg[1], "MSG_???");
105         tprintf(", %#lx", tcp->u_arg[indirect_ipccall(tcp) ? 3 : 2]);
106         return RVAL_DECODED;
107 }
108
109 static void
110 tprint_msgsnd(struct tcb *tcp, long addr, unsigned long count,
111               unsigned long flags)
112 {
113         long mtype;
114
115         if (umove(tcp, addr, &mtype) < 0) {
116                 tprintf("%#lx", addr);
117         } else {
118                 tprintf("{%lu, ", mtype);
119                 printstr(tcp, addr + sizeof(mtype), count);
120                 tprints("}");
121         }
122         tprintf(", %lu, ", count);
123         printflags(ipc_msg_flags, flags, "MSG_???");
124 }
125
126 SYS_FUNC(msgsnd)
127 {
128         tprintf("%d, ", (int) tcp->u_arg[0]);
129         if (indirect_ipccall(tcp)) {
130                 tprint_msgsnd(tcp, tcp->u_arg[3], tcp->u_arg[1],
131                               tcp->u_arg[2]);
132         } else {
133                 tprint_msgsnd(tcp, tcp->u_arg[1], tcp->u_arg[2],
134                               tcp->u_arg[3]);
135         }
136         return RVAL_DECODED;
137 }
138
139 static void
140 tprint_msgrcv(struct tcb *tcp, long addr, unsigned long count, long msgtyp)
141 {
142         long mtype;
143
144         if (syserror(tcp) || umove(tcp, addr, &mtype) < 0) {
145                 tprintf("%#lx", addr);
146         } else {
147                 tprintf("{%lu, ", mtype);
148                 printstr(tcp, addr + sizeof(mtype), count);
149                 tprints("}");
150         }
151         tprintf(", %lu, %ld, ", count, msgtyp);
152 }
153
154 SYS_FUNC(msgrcv)
155 {
156         if (entering(tcp)) {
157                 tprintf("%d, ", (int) tcp->u_arg[0]);
158         } else {
159                 if (indirect_ipccall(tcp)) {
160                         struct ipc_wrapper {
161                                 struct msgbuf *msgp;
162                                 long msgtyp;
163                         } tmp;
164
165                         if (umove(tcp, tcp->u_arg[3], &tmp) < 0) {
166                                 tprintf("%#lx, %lu, ",
167                                         tcp->u_arg[3], tcp->u_arg[1]);
168                         } else {
169                                 tprint_msgrcv(tcp, (long) tmp.msgp,
170                                         tcp->u_arg[1], tmp.msgtyp);
171                         }
172                         printflags(ipc_msg_flags, tcp->u_arg[2], "MSG_???");
173                 } else {
174                         tprint_msgrcv(tcp, tcp->u_arg[1],
175                                 tcp->u_arg[2], tcp->u_arg[3]);
176                         printflags(ipc_msg_flags, tcp->u_arg[4], "MSG_???");
177                 }
178         }
179         return 0;
180 }
181
182 static void
183 tprint_sembuf(struct tcb *tcp, long addr, unsigned long count)
184 {
185         unsigned long i, max_count;
186
187         if (abbrev(tcp))
188                 max_count = (max_strlen < count) ? max_strlen : count;
189         else
190                 max_count = count;
191
192         if (!max_count) {
193                 tprintf("%#lx, %lu", addr, count);
194                 return;
195         }
196
197         for (i = 0; i < max_count; ++i) {
198                 struct sembuf sb;
199                 if (i)
200                         tprints(", ");
201                 if (umove(tcp, addr + i * sizeof(struct sembuf), &sb) < 0) {
202                         if (i) {
203                                 tprints("{???}");
204                                 break;
205                         } else {
206                                 tprintf("%#lx, %lu", addr, count);
207                                 return;
208                         }
209                 } else {
210                         if (!i)
211                                 tprints("{");
212                         tprintf("{%u, %d, ", sb.sem_num, sb.sem_op);
213                         printflags(semop_flags, sb.sem_flg, "SEM_???");
214                         tprints("}");
215                 }
216         }
217
218         if (i < max_count || max_count < count)
219                 tprints(", ...");
220
221         tprintf("}, %lu", count);
222 }
223
224 SYS_FUNC(semop)
225 {
226         tprintf("%lu, ", tcp->u_arg[0]);
227         if (indirect_ipccall(tcp)) {
228                 tprint_sembuf(tcp, tcp->u_arg[3], tcp->u_arg[1]);
229         } else {
230                 tprint_sembuf(tcp, tcp->u_arg[1], tcp->u_arg[2]);
231         }
232         return RVAL_DECODED;
233 }
234
235 SYS_FUNC(semtimedop)
236 {
237         tprintf("%lu, ", tcp->u_arg[0]);
238         if (indirect_ipccall(tcp)) {
239                 tprint_sembuf(tcp, tcp->u_arg[3], tcp->u_arg[1]);
240                 tprints(", ");
241 #if defined(S390) || defined(S390X)
242                 printtv(tcp, tcp->u_arg[2]);
243 #else
244                 printtv(tcp, tcp->u_arg[4]);
245 #endif
246         } else {
247                 tprint_sembuf(tcp, tcp->u_arg[1], tcp->u_arg[2]);
248                 tprints(", ");
249                 printtv(tcp, tcp->u_arg[3]);
250         }
251         return RVAL_DECODED;
252 }
253
254 SYS_FUNC(semget)
255 {
256         if (tcp->u_arg[0])
257                 tprintf("%#lx", tcp->u_arg[0]);
258         else
259                 tprints("IPC_PRIVATE");
260         tprintf(", %lu, ", tcp->u_arg[1]);
261         if (printflags(resource_flags, tcp->u_arg[2] & ~0777, NULL) != 0)
262                 tprints("|");
263         tprintf("%#lo", tcp->u_arg[2] & 0777);
264         return RVAL_DECODED;
265 }
266
267 SYS_FUNC(semctl)
268 {
269         tprintf("%lu, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
270         PRINTCTL(semctl_flags, tcp->u_arg[2], "SEM_???");
271         tprints(", ");
272         if (indirect_ipccall(tcp)) {
273                 if (current_wordsize == sizeof(int)) {
274                         printnum_int(tcp, tcp->u_arg[3], "%#x");
275                 } else {
276                         printnum_long(tcp, tcp->u_arg[3], "%#lx");
277                 }
278         } else {
279                 tprintf("%#lx", tcp->u_arg[3]);
280         }
281         return RVAL_DECODED;
282 }
283
284 SYS_FUNC(shmget)
285 {
286         if (tcp->u_arg[0])
287                 tprintf("%#lx", tcp->u_arg[0]);
288         else
289                 tprints("IPC_PRIVATE");
290         tprintf(", %lu, ", tcp->u_arg[1]);
291         if (printflags(shm_resource_flags, tcp->u_arg[2] & ~0777, NULL) != 0)
292                 tprints("|");
293         tprintf("%#lo", tcp->u_arg[2] & 0777);
294         return RVAL_DECODED;
295 }
296
297 SYS_FUNC(shmctl)
298 {
299         tprintf("%lu, ", tcp->u_arg[0]);
300         PRINTCTL(shmctl_flags, tcp->u_arg[1], "SHM_???");
301         tprintf(", %#lx", tcp->u_arg[indirect_ipccall(tcp) ? 3 : 2]);
302         return RVAL_DECODED;
303 }
304
305 SYS_FUNC(shmat)
306 {
307         if (entering(tcp)) {
308                 tprintf("%lu", tcp->u_arg[0]);
309                 if (indirect_ipccall(tcp)) {
310                         tprintf(", %#lx, ", tcp->u_arg[3]);
311                         printflags(shm_flags, tcp->u_arg[1], "SHM_???");
312                 } else {
313                         tprintf(", %#lx, ", tcp->u_arg[1]);
314                         printflags(shm_flags, tcp->u_arg[2], "SHM_???");
315                 }
316                 return 0;
317         } else {
318                 if (syserror(tcp))
319                         return 0;
320                 if (indirect_ipccall(tcp)) {
321                         unsigned long raddr;
322                         if (umove(tcp, tcp->u_arg[2], &raddr) < 0)
323                                 return RVAL_NONE;
324                         tcp->u_rval = raddr;
325                 }
326                 return RVAL_HEX;
327         }
328 }
329
330 SYS_FUNC(shmdt)
331 {
332         tprintf("%#lx", tcp->u_arg[indirect_ipccall(tcp) ? 3 : 0]);
333         return RVAL_DECODED;
334 }
335
336 SYS_FUNC(mq_open)
337 {
338         printpath(tcp, tcp->u_arg[0]);
339         tprints(", ");
340         /* flags */
341         tprint_open_modes(tcp->u_arg[1]);
342         if (tcp->u_arg[1] & O_CREAT) {
343 # ifndef HAVE_MQUEUE_H
344                 tprintf(", %lx", tcp->u_arg[2]);
345 # else
346                 struct mq_attr attr;
347                 /* mode */
348                 tprintf(", %#lo, ", tcp->u_arg[2]);
349                 if (umove(tcp, tcp->u_arg[3], &attr) < 0)
350                         tprints("{???}");
351                 else
352                         tprintf("{mq_maxmsg=%ld, mq_msgsize=%ld}",
353                                 (long) attr.mq_maxmsg,
354                                 (long) attr.mq_msgsize);
355 # endif
356         }
357         return RVAL_DECODED;
358 }
359
360 SYS_FUNC(mq_timedsend)
361 {
362         tprintf("%ld, ", tcp->u_arg[0]);
363         printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
364         tprintf(", %lu, %ld, ", tcp->u_arg[2], tcp->u_arg[3]);
365         printtv(tcp, tcp->u_arg[4]);
366         return RVAL_DECODED;
367 }
368
369 SYS_FUNC(mq_timedreceive)
370 {
371         if (entering(tcp))
372                 tprintf("%ld, ", tcp->u_arg[0]);
373         else {
374                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
375                 tprintf(", %lu, %ld, ", tcp->u_arg[2], tcp->u_arg[3]);
376                 printtv(tcp, tcp->u_arg[4]);
377         }
378         return 0;
379 }
380
381 SYS_FUNC(mq_notify)
382 {
383         tprintf("%ld, ", tcp->u_arg[0]);
384         printsigevent(tcp, tcp->u_arg[1]);
385         return RVAL_DECODED;
386 }
387
388 static void
389 printmqattr(struct tcb *tcp, long addr)
390 {
391         if (addr == 0)
392                 tprints("NULL");
393         else {
394 # ifndef HAVE_MQUEUE_H
395                 tprintf("%#lx", addr);
396 # else
397                 struct mq_attr attr;
398                 if (umove(tcp, addr, &attr) < 0) {
399                         tprints("{...}");
400                         return;
401                 }
402                 tprints("{mq_flags=");
403                 tprint_open_modes(attr.mq_flags);
404                 tprintf(", mq_maxmsg=%ld, mq_msgsize=%ld, mq_curmsg=%ld}",
405                         (long) attr.mq_maxmsg, (long) attr.mq_msgsize,
406                         (long) attr.mq_curmsgs);
407 # endif
408         }
409 }
410
411 SYS_FUNC(mq_getsetattr)
412 {
413         if (entering(tcp)) {
414                 tprintf("%ld, ", tcp->u_arg[0]);
415                 printmqattr(tcp, tcp->u_arg[1]);
416                 tprints(", ");
417         } else
418                 printmqattr(tcp, tcp->u_arg[2]);
419         return 0;
420 }