]> granicus.if.org Git - strace/blob - ipc.c
d8757c1f1d6fd1c1fb62e011ec3ef685fb62a3ad
[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  *      $Id$
31  */
32
33 #include "defs.h"
34
35
36 # ifdef HAVE_MQUEUE_H
37 #  include <mqueue.h>
38 # endif
39
40 #include <fcntl.h>
41 #include <sys/ipc.h>
42 #include <sys/sem.h>
43 #include <sys/msg.h>
44 #include <sys/shm.h>
45
46 #ifndef MSG_STAT
47 #define MSG_STAT 11
48 #endif
49 #ifndef MSG_INFO
50 #define MSG_INFO 12
51 #endif
52 #ifndef SHM_STAT
53 #define SHM_STAT 13
54 #endif
55 #ifndef SHM_INFO
56 #define SHM_INFO 14
57 #endif
58 #ifndef SEM_STAT
59 #define SEM_STAT 18
60 #endif
61 #ifndef SEM_INFO
62 #define SEM_INFO 19
63 #endif
64
65 #if !defined IPC_64
66 # define IPC_64 0x100
67 #endif
68
69 extern void printsigevent(struct tcb *tcp, long arg);
70
71 static const struct xlat msgctl_flags[] = {
72         { IPC_RMID,     "IPC_RMID"      },
73         { IPC_SET,      "IPC_SET"       },
74         { IPC_STAT,     "IPC_STAT"      },
75         { IPC_INFO,     "IPC_INFO"      },
76         { MSG_STAT,     "MSG_STAT"      },
77         { MSG_INFO,     "MSG_INFO"      },
78         { 0,            NULL            },
79 };
80
81 static const struct xlat semctl_flags[] = {
82         { IPC_RMID,     "IPC_RMID"      },
83         { IPC_SET,      "IPC_SET"       },
84         { IPC_STAT,     "IPC_STAT"      },
85         { IPC_INFO,     "IPC_INFO"      },
86         { SEM_STAT,     "SEM_STAT"      },
87         { SEM_INFO,     "SEM_INFO"      },
88         { GETPID,       "GETPID"        },
89         { GETVAL,       "GETVAL"        },
90         { GETALL,       "GETALL"        },
91         { GETNCNT,      "GETNCNT"       },
92         { GETZCNT,      "GETZCNT"       },
93         { SETVAL,       "SETVAL"        },
94         { SETALL,       "SETALL"        },
95         { 0,            NULL            },
96 };
97
98 static const struct xlat shmctl_flags[] = {
99         { IPC_RMID,     "IPC_RMID"      },
100         { IPC_SET,      "IPC_SET"       },
101         { IPC_STAT,     "IPC_STAT"      },
102         { IPC_INFO,     "IPC_INFO"      },
103         { SHM_STAT,     "SHM_STAT"      },
104         { SHM_INFO,     "SHM_INFO"      },
105 #ifdef SHM_LOCK
106         { SHM_LOCK,     "SHM_LOCK"      },
107 #endif
108 #ifdef SHM_UNLOCK
109         { SHM_UNLOCK,   "SHM_UNLOCK"    },
110 #endif
111         { 0,            NULL            },
112 };
113
114 static const struct xlat resource_flags[] = {
115         { IPC_CREAT,    "IPC_CREAT"     },
116         { IPC_EXCL,     "IPC_EXCL"      },
117         { IPC_NOWAIT,   "IPC_NOWAIT"    },
118         { 0,            NULL            },
119 };
120
121 static const struct xlat shm_resource_flags[] = {
122         { IPC_CREAT,    "IPC_CREAT"     },
123         { IPC_EXCL,     "IPC_EXCL"      },
124 #ifdef SHM_HUGETLB
125         { SHM_HUGETLB,  "SHM_HUGETLB"   },
126 #endif
127         { 0,            NULL            },
128 };
129
130 static const struct xlat shm_flags[] = {
131         { SHM_REMAP,    "SHM_REMAP"     },
132         { SHM_RDONLY,   "SHM_RDONLY"    },
133         { SHM_RND,      "SHM_RND"       },
134         { 0,            NULL            },
135 };
136
137 static const struct xlat msg_flags[] = {
138         { MSG_NOERROR,  "MSG_NOERROR"   },
139         { MSG_EXCEPT,   "MSG_EXCEPT"    },
140         { IPC_NOWAIT,   "IPC_NOWAIT"    },
141         { 0,            NULL            },
142 };
143
144 static const struct xlat semop_flags[] = {
145         { SEM_UNDO,     "SEM_UNDO"      },
146         { IPC_NOWAIT,   "IPC_NOWAIT"    },
147         { 0,            NULL            },
148 };
149
150 int sys_msgget(struct tcb *tcp)
151 {
152         if (entering(tcp)) {
153                 if (tcp->u_arg[0])
154                         tprintf("%#lx", tcp->u_arg[0]);
155                 else
156                         tprints("IPC_PRIVATE");
157                 tprints(", ");
158                 if (printflags(resource_flags, tcp->u_arg[1] & ~0777, NULL) != 0)
159                         tprints("|");
160                 tprintf("%#lo", tcp->u_arg[1] & 0777);
161         }
162         return 0;
163 }
164
165 #ifdef IPC_64
166 # define PRINTCTL(flagset, arg, dflt) \
167         if ((arg) & IPC_64) tprints("IPC_64|"); \
168         printxval((flagset), (arg) &~ IPC_64, dflt)
169 #else
170 # define PRINTCTL printxval
171 #endif
172
173 static int
174 indirect_ipccall(struct tcb *tcp)
175 {
176 #ifdef X86_64
177         return current_personality > 0;
178 #endif
179 #if defined IA64
180         return tcp->scno < 1024; /* ia32 emulation syscalls are low */
181 #endif
182 #if !defined MIPS && !defined HPPA
183         return 1;
184 #endif
185         return 0;
186 }
187
188 int sys_msgctl(struct tcb *tcp)
189 {
190         if (entering(tcp)) {
191                 tprintf("%lu, ", tcp->u_arg[0]);
192                 PRINTCTL(msgctl_flags, tcp->u_arg[1], "MSG_???");
193                 tprintf(", %#lx", tcp->u_arg[indirect_ipccall(tcp) ? 3 : 2]);
194         }
195         return 0;
196 }
197
198 static void
199 tprint_msgsnd(struct tcb *tcp, long addr, unsigned long count,
200               unsigned long flags)
201 {
202         long mtype;
203
204         if (umove(tcp, addr, &mtype) < 0) {
205                 tprintf("%#lx", addr);
206         } else {
207                 tprintf("{%lu, ", mtype);
208                 printstr(tcp, addr + sizeof(mtype), count);
209                 tprints("}");
210         }
211         tprintf(", %lu, ", count);
212         printflags(msg_flags, flags, "MSG_???");
213 }
214
215 int sys_msgsnd(struct tcb *tcp)
216 {
217         if (entering(tcp)) {
218                 tprintf("%d, ", (int) tcp->u_arg[0]);
219                 if (indirect_ipccall(tcp)) {
220                         tprint_msgsnd(tcp, tcp->u_arg[3], tcp->u_arg[1],
221                                       tcp->u_arg[2]);
222                 } else {
223                         tprint_msgsnd(tcp, tcp->u_arg[1], tcp->u_arg[2],
224                                       tcp->u_arg[3]);
225                 }
226         }
227         return 0;
228 }
229
230 static void
231 tprint_msgrcv(struct tcb *tcp, long addr, unsigned long count, long msgtyp)
232 {
233         long mtype;
234
235         if (syserror(tcp) || umove(tcp, addr, &mtype) < 0) {
236                 tprintf("%#lx", addr);
237         } else {
238                 tprintf("{%lu, ", mtype);
239                 printstr(tcp, addr + sizeof(mtype), count);
240                 tprints("}");
241         }
242         tprintf(", %lu, %ld, ", count, msgtyp);
243 }
244
245 int sys_msgrcv(struct tcb *tcp)
246 {
247         if (entering(tcp)) {
248                 tprintf("%d, ", (int) tcp->u_arg[0]);
249         } else {
250                 if (indirect_ipccall(tcp)) {
251                         struct ipc_wrapper {
252                                 struct msgbuf *msgp;
253                                 long msgtyp;
254                         } tmp;
255
256                         if (umove(tcp, tcp->u_arg[3], &tmp) < 0) {
257                                 tprintf("%#lx, %lu, ",
258                                         tcp->u_arg[3], tcp->u_arg[1]);
259                         } else {
260                                 tprint_msgrcv(tcp, (long) tmp.msgp,
261                                         tcp->u_arg[1], tmp.msgtyp);
262                         }
263                         printflags(msg_flags, tcp->u_arg[2], "MSG_???");
264                 } else {
265                         tprint_msgrcv(tcp, tcp->u_arg[1],
266                                 tcp->u_arg[2], tcp->u_arg[3]);
267                         printflags(msg_flags, tcp->u_arg[4], "MSG_???");
268                 }
269         }
270         return 0;
271 }
272
273 static void
274 tprint_sembuf(struct tcb *tcp, long addr, unsigned long count)
275 {
276         unsigned long i, max_count;
277
278         if (abbrev(tcp))
279                 max_count = (max_strlen < count) ? max_strlen : count;
280         else
281                 max_count = count;
282
283         if (!max_count) {
284                 tprintf("%#lx, %lu", addr, count);
285                 return;
286         }
287
288         for (i = 0; i < max_count; ++i) {
289                 struct sembuf sb;
290                 if (i)
291                         tprints(", ");
292                 if (umove(tcp, addr + i * sizeof(struct sembuf), &sb) < 0) {
293                         if (i) {
294                                 tprints("{???}");
295                                 break;
296                         } else {
297                                 tprintf("%#lx, %lu", addr, count);
298                                 return;
299                         }
300                 } else {
301                         if (!i)
302                                 tprints("{");
303                         tprintf("{%u, %d, ", sb.sem_num, sb.sem_op);
304                         printflags(semop_flags, sb.sem_flg, "SEM_???");
305                         tprints("}");
306                 }
307         }
308
309         if (i < max_count || max_count < count)
310                 tprints(", ...");
311
312         tprintf("}, %lu", count);
313 }
314
315 int sys_semop(struct tcb *tcp)
316 {
317         if (entering(tcp)) {
318                 tprintf("%lu, ", tcp->u_arg[0]);
319                 if (indirect_ipccall(tcp)) {
320                         tprint_sembuf(tcp, tcp->u_arg[3], tcp->u_arg[1]);
321                 } else {
322                         tprint_sembuf(tcp, tcp->u_arg[1], tcp->u_arg[2]);
323                 }
324         }
325         return 0;
326 }
327
328 int sys_semtimedop(struct tcb *tcp)
329 {
330         if (entering(tcp)) {
331                 tprintf("%lu, ", tcp->u_arg[0]);
332                 if (indirect_ipccall(tcp)) {
333                         tprint_sembuf(tcp, tcp->u_arg[3], tcp->u_arg[1]);
334                         tprints(", ");
335 #if defined(S390)
336                         printtv(tcp, tcp->u_arg[2]);
337 #else
338                         printtv(tcp, tcp->u_arg[5]);
339 #endif
340                 } else {
341                         tprint_sembuf(tcp, tcp->u_arg[1], tcp->u_arg[2]);
342                         tprints(", ");
343                         printtv(tcp, tcp->u_arg[3]);
344                 }
345         }
346         return 0;
347 }
348
349 int sys_semget(struct tcb *tcp)
350 {
351         if (entering(tcp)) {
352                 if (tcp->u_arg[0])
353                         tprintf("%#lx", tcp->u_arg[0]);
354                 else
355                         tprints("IPC_PRIVATE");
356                 tprintf(", %lu", tcp->u_arg[1]);
357                 tprints(", ");
358                 if (printflags(resource_flags, tcp->u_arg[2] & ~0777, NULL) != 0)
359                         tprints("|");
360                 tprintf("%#lo", tcp->u_arg[2] & 0777);
361         }
362         return 0;
363 }
364
365 int sys_semctl(struct tcb *tcp)
366 {
367         if (entering(tcp)) {
368                 tprintf("%lu", tcp->u_arg[0]);
369                 tprintf(", %lu, ", tcp->u_arg[1]);
370                 PRINTCTL(semctl_flags, tcp->u_arg[2], "SEM_???");
371                 tprintf(", %#lx", tcp->u_arg[3]);
372         }
373         return 0;
374 }
375
376 int sys_shmget(struct tcb *tcp)
377 {
378         if (entering(tcp)) {
379                 if (tcp->u_arg[0])
380                         tprintf("%#lx", tcp->u_arg[0]);
381                 else
382                         tprints("IPC_PRIVATE");
383                 tprintf(", %lu", tcp->u_arg[1]);
384                 tprints(", ");
385                 if (printflags(shm_resource_flags, tcp->u_arg[2] & ~0777, NULL) != 0)
386                         tprints("|");
387                 tprintf("%#lo", tcp->u_arg[2] & 0777);
388         }
389         return 0;
390 }
391
392 int sys_shmctl(struct tcb *tcp)
393 {
394         if (entering(tcp)) {
395                 tprintf("%lu, ", tcp->u_arg[0]);
396                 PRINTCTL(shmctl_flags, tcp->u_arg[1], "SHM_???");
397                 if (indirect_ipccall(tcp)) {
398                         tprintf(", %#lx", tcp->u_arg[3]);
399                 } else {
400                         tprintf(", %#lx", tcp->u_arg[2]);
401                 }
402         }
403         return 0;
404 }
405
406 int sys_shmat(struct tcb *tcp)
407 {
408         unsigned long raddr;
409
410         if (exiting(tcp)) {
411                 tprintf("%lu", tcp->u_arg[0]);
412                 if (indirect_ipccall(tcp)) {
413                         tprintf(", %#lx", tcp->u_arg[3]);
414                         tprints(", ");
415                         printflags(shm_flags, tcp->u_arg[1], "SHM_???");
416                 } else {
417                         tprintf(", %#lx", tcp->u_arg[1]);
418                         tprints(", ");
419                         printflags(shm_flags, tcp->u_arg[2], "SHM_???");
420                 }
421                 if (syserror(tcp))
422                         return 0;
423 /* HPPA does not use an IPC multiplexer on Linux.  */
424 #if !defined(HPPA)
425                 if (umove(tcp, tcp->u_arg[2], &raddr) < 0)
426                         return RVAL_NONE;
427                 tcp->u_rval = raddr;
428 #endif
429                 return RVAL_HEX;
430         }
431         return 0;
432 }
433
434 int sys_shmdt(struct tcb *tcp)
435 {
436         if (entering(tcp)) {
437                 if (indirect_ipccall(tcp)) {
438                         tprintf("%#lx", tcp->u_arg[3]);
439                 } else {
440                         tprintf("%#lx", tcp->u_arg[0]);
441                 }
442         }
443         return 0;
444 }
445
446
447 int
448 sys_mq_open(struct tcb *tcp)
449 {
450         if (entering(tcp)) {
451                 printpath(tcp, tcp->u_arg[0]);
452                 tprints(", ");
453                 /* flags */
454                 tprint_open_modes(tcp->u_arg[1]);
455                 if (tcp->u_arg[1] & O_CREAT) {
456 # ifndef HAVE_MQUEUE_H
457                         tprintf(", %lx", tcp->u_arg[2]);
458 # else
459                         struct mq_attr attr;
460                         /* mode */
461                         tprintf(", %#lo, ", tcp->u_arg[2]);
462                         if (umove(tcp, tcp->u_arg[3], &attr) < 0)
463                                 tprints("{ ??? }");
464                         else
465                                 tprintf("{mq_maxmsg=%ld, mq_msgsize=%ld}",
466                                         (long) attr.mq_maxmsg,
467                                         (long) attr.mq_msgsize);
468 # endif
469                 }
470         }
471         return 0;
472 }
473
474 int
475 sys_mq_timedsend(struct tcb *tcp)
476 {
477         if (entering(tcp)) {
478                 tprintf("%ld, ", tcp->u_arg[0]);
479                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
480                 tprintf(", %lu, %ld, ", tcp->u_arg[2], tcp->u_arg[3]);
481                 printtv(tcp, tcp->u_arg[4]);
482         }
483         return 0;
484 }
485
486 int
487 sys_mq_timedreceive(struct tcb *tcp)
488 {
489         if (entering(tcp))
490                 tprintf("%ld, ", tcp->u_arg[0]);
491         else {
492                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
493                 tprintf(", %lu, %ld, ", tcp->u_arg[2], tcp->u_arg[3]);
494                 printtv(tcp, tcp->u_arg[4]);
495         }
496         return 0;
497 }
498
499 int
500 sys_mq_notify(struct tcb *tcp)
501 {
502         if (entering(tcp)) {
503                 tprintf("%ld, ", tcp->u_arg[0]);
504                 printsigevent(tcp, tcp->u_arg[1]);
505         }
506         return 0;
507 }
508
509 static void
510 printmqattr(struct tcb *tcp, long addr)
511 {
512         if (addr == 0)
513                 tprints("NULL");
514         else {
515 # ifndef HAVE_MQUEUE_H
516                 tprintf("%#lx", addr);
517 # else
518                 struct mq_attr attr;
519                 if (umove(tcp, addr, &attr) < 0) {
520                         tprints("{...}");
521                         return;
522                 }
523                 tprints("{mq_flags=");
524                 tprint_open_modes(attr.mq_flags);
525                 tprintf(", mq_maxmsg=%ld, mq_msgsize=%ld, mq_curmsg=%ld}",
526                         (long) attr.mq_maxmsg, (long) attr.mq_msgsize,
527                         (long) attr.mq_curmsgs);
528 # endif
529         }
530 }
531
532 int
533 sys_mq_getsetattr(struct tcb *tcp)
534 {
535         if (entering(tcp)) {
536                 tprintf("%ld, ", tcp->u_arg[0]);
537                 printmqattr(tcp, tcp->u_arg[1]);
538                 tprints(", ");
539         } else
540                 printmqattr(tcp, tcp->u_arg[2]);
541         return 0;
542 }
543
544 int
545 sys_ipc(struct tcb *tcp)
546 {
547         return printargs(tcp);
548 }