]> granicus.if.org Git - strace/blob - ipc.c
Enhance semop()/semtimedop() sembuf parser
[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 static const struct xlat semop_flags[] = {
156         { SEM_UNDO,     "SEM_UNDO"      },
157         { IPC_NOWAIT,   "IPC_NOWAIT"    },
158         { 0,            NULL            },
159 };
160
161 int sys_msgget(tcp)
162 struct tcb *tcp;
163 {
164         if (entering(tcp)) {
165                 if (tcp->u_arg[0])
166                         tprintf("%#lx", tcp->u_arg[0]);
167                 else
168                         tprintf("IPC_PRIVATE");
169                 tprintf(", ");
170                 if (printflags(resource_flags, tcp->u_arg[1] & ~0777, NULL) != 0)
171                         tprintf("|");
172                 tprintf("%#lo", tcp->u_arg[1] & 0777);
173         }
174         return 0;
175 }
176
177 #ifdef IPC_64
178 # define PRINTCTL(flagset, arg, dflt) \
179         if ((arg) & IPC_64) tprintf("IPC_64|"); \
180         printxval((flagset), (arg) &~ IPC_64, dflt)
181 #else
182 # define PRINTCTL printxval
183 #endif
184
185 static int
186 indirect_ipccall(tcp)
187 struct tcb *tcp;
188 {
189 #ifdef LINUX
190 #ifdef X86_64
191         return current_personality > 0;
192 #endif
193 #if defined IA64
194         return tcp->scno < 1024; /* ia32 emulation syscalls are low */
195 #endif
196 #if !defined MIPS && !defined HPPA
197         return 1;
198 #endif
199 #endif  /* LINUX */
200         return 0;
201 }
202
203 int sys_msgctl(tcp)
204 struct tcb *tcp;
205 {
206         if (entering(tcp)) {
207                 tprintf("%lu, ", tcp->u_arg[0]);
208                 PRINTCTL(msgctl_flags, tcp->u_arg[1], "MSG_???");
209                 tprintf(", %#lx", tcp->u_arg[indirect_ipccall(tcp) ? 3 : 2]);
210         }
211         return 0;
212 }
213
214 int sys_msgsnd(tcp)
215 struct tcb *tcp;
216 {
217         long mtype;
218
219         if (entering(tcp)) {
220                 tprintf("%lu", tcp->u_arg[0]);
221                 if (indirect_ipccall(tcp)) {
222                         umove(tcp, tcp->u_arg[3], &mtype);
223                         tprintf(", {%lu, ", mtype);
224                         printstr(tcp, tcp->u_arg[3] + sizeof(long),
225                                  tcp->u_arg[1]);
226                         tprintf("}, %lu", tcp->u_arg[1]);
227                         tprintf(", ");
228                         printflags(msg_flags, tcp->u_arg[2], "MSG_???");
229                 } else {
230                         umove(tcp, tcp->u_arg[1], &mtype);
231                         tprintf(", {%lu, ", mtype);
232                         printstr(tcp, tcp->u_arg[1] + sizeof(long),
233                                  tcp->u_arg[2]);
234                         tprintf("}, %lu", tcp->u_arg[2]);
235                         tprintf(", ");
236                         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
237                 }
238         }
239         return 0;
240 }
241
242 int sys_msgrcv(tcp)
243 struct tcb *tcp;
244 {
245         long mtype;
246
247         if (entering(tcp)) {
248                 tprintf("%lu, ", tcp->u_arg[0]);
249         } else {
250                 tprintf("%lu", tcp->u_arg[0]);
251                 if (indirect_ipccall(tcp)) {
252                         struct ipc_wrapper {
253                                 struct msgbuf *msgp;
254                                 long msgtyp;
255                         } tmp;
256                         umove(tcp, tcp->u_arg[3], &tmp);
257                         umove(tcp, (long) tmp.msgp, &mtype);
258                         tprintf(", {%lu, ", mtype);
259                         printstr(tcp, (long) (tmp.msgp) + sizeof(long),
260                                  tcp->u_arg[1]);
261                         tprintf("}, %lu", tcp->u_arg[1]);
262                         tprintf(", %ld", tmp.msgtyp);
263                         tprintf(", ");
264                         printflags(msg_flags, tcp->u_arg[2], "MSG_???");
265                 } else {
266                         umove(tcp, tcp->u_arg[1], &mtype);
267                         tprintf("{%lu, ", mtype);
268                         printstr(tcp, tcp->u_arg[1] + sizeof(long),
269                                  tcp->u_arg[2]);
270                         tprintf("}, %lu", tcp->u_arg[2]);
271                         tprintf(", %ld", tcp->u_arg[3]);
272                         tprintf(", ");
273                         printflags(msg_flags, tcp->u_arg[4], "MSG_???");
274                 }
275         }
276         return 0;
277 }
278
279 static void
280 tprint_sembuf(struct tcb *tcp, long addr, unsigned long count)
281 {
282         unsigned long i, max_count;
283
284         if (abbrev(tcp))
285                 max_count = (max_strlen < count) ? max_strlen : count;
286         else
287                 max_count = count;
288
289         if (!max_count) {
290                 tprintf("%#lx, %lu", addr, count);
291                 return;
292         }
293
294         for(i = 0; i < max_count; ++i) {
295                 struct sembuf sb;
296                 if (i)
297                         tprintf(", ");
298                 if (umove(tcp, addr + i * sizeof(struct sembuf), &sb) < 0) {
299                         if (i) {
300                                 tprintf("{???}");
301                                 break;
302                         } else {
303                                 tprintf("%#lx, %lu", addr, count);
304                                 return;
305                         }
306                 } else {
307                         if (!i)
308                                 tprintf("{");
309                         tprintf("{%u, %d, ", sb.sem_num, sb.sem_op);
310                         printflags(semop_flags, sb.sem_flg, "SEM_???");
311                         tprintf("}");
312                 }
313         }
314
315         if (i < max_count || max_count < count)
316                 tprintf(", ...");
317
318         tprintf("}, %lu", count);
319 }
320
321 int sys_semop(struct tcb *tcp)
322 {
323         if (entering(tcp)) {
324                 tprintf("%lu, ", tcp->u_arg[0]);
325                 if (indirect_ipccall(tcp)) {
326                         tprint_sembuf(tcp, tcp->u_arg[3], tcp->u_arg[1]);
327                 } else {
328                         tprint_sembuf(tcp, tcp->u_arg[1], tcp->u_arg[2]);
329                 }
330         }
331         return 0;
332 }
333
334 #ifdef LINUX
335 int sys_semtimedop(struct tcb *tcp)
336 {
337         if (entering(tcp)) {
338                 tprintf("%lu, ", tcp->u_arg[0]);
339                 if (indirect_ipccall(tcp)) {
340                         tprint_sembuf(tcp, tcp->u_arg[3], tcp->u_arg[1]);
341                         tprintf(", ");
342                         printtv(tcp, tcp->u_arg[5]);
343                 } else {
344                         tprint_sembuf(tcp, tcp->u_arg[1], tcp->u_arg[2]);
345                         tprintf(", ");
346                         printtv(tcp, tcp->u_arg[3]);
347                 }
348         }
349         return 0;
350 }
351 #endif
352
353 int sys_semget(tcp)
354 struct tcb *tcp;
355 {
356         if (entering(tcp)) {
357                 if (tcp->u_arg[0])
358                         tprintf("%#lx", tcp->u_arg[0]);
359                 else
360                         tprintf("IPC_PRIVATE");
361                 tprintf(", %lu", tcp->u_arg[1]);
362                 tprintf(", ");
363                 if (printflags(resource_flags, tcp->u_arg[2] & ~0777, NULL) != 0)
364                         tprintf("|");
365                 tprintf("%#lo", tcp->u_arg[2] & 0777);
366         }
367         return 0;
368 }
369
370 int sys_semctl(tcp)
371 struct tcb *tcp;
372 {
373         if (entering(tcp)) {
374                 tprintf("%lu", tcp->u_arg[0]);
375                 tprintf(", %lu, ", tcp->u_arg[1]);
376                 PRINTCTL(semctl_flags, tcp->u_arg[2], "SEM_???");
377                 tprintf(", %#lx", tcp->u_arg[3]);
378         }
379         return 0;
380 }
381
382 int sys_shmget(tcp)
383 struct tcb *tcp;
384 {
385         if (entering(tcp)) {
386                 if (tcp->u_arg[0])
387                         tprintf("%#lx", tcp->u_arg[0]);
388                 else
389                         tprintf("IPC_PRIVATE");
390                 tprintf(", %lu", tcp->u_arg[1]);
391                 tprintf(", ");
392                 if (printflags(shm_resource_flags, tcp->u_arg[2] & ~0777, NULL) != 0)
393                         tprintf("|");
394                 tprintf("%#lo", tcp->u_arg[2] & 0777);
395         }
396         return 0;
397 }
398
399 int sys_shmctl(tcp)
400 struct tcb *tcp;
401 {
402         if (entering(tcp)) {
403                 tprintf("%lu, ", tcp->u_arg[0]);
404                 PRINTCTL(shmctl_flags, tcp->u_arg[1], "SHM_???");
405                 if (indirect_ipccall(tcp)) {
406                         tprintf(", %#lx", tcp->u_arg[3]);
407                 } else {
408                         tprintf(", %#lx", tcp->u_arg[2]);
409                 }
410         }
411         return 0;
412 }
413
414 int sys_shmat(tcp)
415 struct tcb *tcp;
416 {
417 #ifdef LINUX
418         unsigned long raddr;
419 #endif /* LINUX */
420
421         if (exiting(tcp)) {
422                 tprintf("%lu", tcp->u_arg[0]);
423                 if (indirect_ipccall(tcp)) {
424                         tprintf(", %#lx", tcp->u_arg[3]);
425                         tprintf(", ");
426                         printflags(shm_flags, tcp->u_arg[1], "SHM_???");
427                 } else {
428                         tprintf(", %#lx", tcp->u_arg[1]);
429                         tprintf(", ");
430                         printflags(shm_flags, tcp->u_arg[2], "SHM_???");
431                 }
432                 if (syserror(tcp))
433                         return 0;
434 /* HPPA does not use an IPC multiplexer on Linux.  */
435 #if defined(LINUX) && !defined(HPPA)
436                 if (umove(tcp, tcp->u_arg[2], &raddr) < 0)
437                         return RVAL_NONE;
438                 tcp->u_rval = raddr;
439 #endif /* LINUX */
440                 return RVAL_HEX;
441         }
442         return 0;
443 }
444
445 int sys_shmdt(tcp)
446 struct tcb *tcp;
447 {
448         if (entering(tcp)) {
449                 if (indirect_ipccall(tcp)) {
450                         tprintf("%#lx", tcp->u_arg[3]);
451                 } else {
452                         tprintf("%#lx", tcp->u_arg[0]);
453                 }
454         }
455         return 0;
456 }
457
458 #endif /* defined(LINUX) || defined(SUNOS4) || defined(FREEBSD) */
459
460 #ifdef LINUX
461 int
462 sys_mq_open(struct tcb *tcp)
463 {
464         if (entering(tcp)) {
465                 printpath(tcp, tcp->u_arg[0]);
466                 tprintf(", ");
467                 /* flags */
468                 tprint_open_modes(tcp->u_arg[1]);
469                 if (tcp->u_arg[1] & O_CREAT) {
470 # ifndef HAVE_MQUEUE_H
471                         tprintf(", %lx", tcp->u_arg[2]);
472 # else
473                         struct mq_attr attr;
474                         /* mode */
475                         tprintf(", %#lo, ", tcp->u_arg[2]);
476                         if (umove(tcp, tcp->u_arg[3], &attr) < 0)
477                                 tprintf("{ ??? }");
478                         else
479                                 tprintf("{mq_maxmsg=%ld, mq_msgsize=%ld}",
480                                         attr.mq_maxmsg, attr.mq_msgsize);
481 # endif
482                 }
483         }
484         return 0;
485 }
486
487 int
488 sys_mq_timedsend(struct tcb *tcp)
489 {
490         if (entering(tcp)) {
491                 tprintf("%ld, ", tcp->u_arg[0]);
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_timedreceive(struct tcb *tcp)
501 {
502         if (entering(tcp))
503                 tprintf("%ld, ", tcp->u_arg[0]);
504         else {
505                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
506                 tprintf(", %lu, %ld, ", tcp->u_arg[2], tcp->u_arg[3]);
507                 printtv(tcp, tcp->u_arg[4]);
508         }
509         return 0;
510 }
511
512 int
513 sys_mq_notify(struct tcb *tcp)
514 {
515         if (entering(tcp)) {
516                 tprintf("%ld, ", tcp->u_arg[0]);
517                 printsigevent(tcp, tcp->u_arg[1]);
518         }
519         return 0;
520 }
521
522 static void
523 printmqattr(struct tcb *tcp, long addr)
524 {
525         if (addr == 0)
526                 tprintf("NULL");
527         else {
528 # ifndef HAVE_MQUEUE_H
529                 tprintf("%#lx", addr);
530 # else
531                 struct mq_attr attr;
532                 if (umove(tcp, addr, &attr) < 0) {
533                         tprintf("{...}");
534                         return;
535                 }
536                 tprintf("{mq_flags=");
537                 tprint_open_modes(attr.mq_flags);
538                 tprintf(", mq_maxmsg=%ld, mq_msgsize=%ld, mq_curmsg=%ld}",
539                         attr.mq_maxmsg, attr.mq_msgsize, attr.mq_curmsgs);
540 # endif
541         }
542 }
543
544 int
545 sys_mq_getsetattr(struct tcb *tcp)
546 {
547         if (entering(tcp)) {
548                 tprintf("%ld, ", tcp->u_arg[0]);
549                 printmqattr(tcp, tcp->u_arg[1]);
550                 tprintf(", ");
551         } else
552                 printmqattr(tcp, tcp->u_arg[2]);
553         return 0;
554 }
555 #endif