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