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