summaryrefslogtreecommitdiff
path: root/ipc.c
blob: 144c52bb0ab30d5559b3af61ded744e31ff241a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
 * Copyright (c) 1993 Ulrich Pegelow <pegelow@moorea.uni-muenster.de>
 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "defs.h"
#ifdef HAVE_MQUEUE_H
# include <mqueue.h>
#endif
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/msg.h>
#include <sys/shm.h>

#ifndef MSG_STAT
#define MSG_STAT 11
#endif
#ifndef MSG_INFO
#define MSG_INFO 12
#endif
#ifndef SHM_STAT
#define SHM_STAT 13
#endif
#ifndef SHM_INFO
#define SHM_INFO 14
#endif
#ifndef SEM_STAT
#define SEM_STAT 18
#endif
#ifndef SEM_INFO
#define SEM_INFO 19
#endif

#if !defined IPC_64
# define IPC_64 0x100
#endif

extern void printsigevent(struct tcb *tcp, long arg);

static const struct xlat msgctl_flags[] = {
	XLAT(IPC_RMID),
	XLAT(IPC_SET),
	XLAT(IPC_STAT),
	XLAT(IPC_INFO),
	XLAT(MSG_STAT),
	XLAT(MSG_INFO),
	XLAT_END
};

static const struct xlat semctl_flags[] = {
	XLAT(IPC_RMID),
	XLAT(IPC_SET),
	XLAT(IPC_STAT),
	XLAT(IPC_INFO),
	XLAT(SEM_STAT),
	XLAT(SEM_INFO),
	XLAT(GETPID),
	XLAT(GETVAL),
	XLAT(GETALL),
	XLAT(GETNCNT),
	XLAT(GETZCNT),
	XLAT(SETVAL),
	XLAT(SETALL),
	XLAT_END
};

static const struct xlat shmctl_flags[] = {
	XLAT(IPC_RMID),
	XLAT(IPC_SET),
	XLAT(IPC_STAT),
	XLAT(IPC_INFO),
	XLAT(SHM_STAT),
	XLAT(SHM_INFO),
#ifdef SHM_LOCK
	XLAT(SHM_LOCK),
#endif
#ifdef SHM_UNLOCK
	XLAT(SHM_UNLOCK),
#endif
	XLAT_END
};

static const struct xlat resource_flags[] = {
	XLAT(IPC_CREAT),
	XLAT(IPC_EXCL),
	XLAT(IPC_NOWAIT),
	XLAT_END
};

static const struct xlat shm_resource_flags[] = {
	XLAT(IPC_CREAT),
	XLAT(IPC_EXCL),
#ifdef SHM_HUGETLB
	XLAT(SHM_HUGETLB),
#endif
	XLAT_END
};

static const struct xlat shm_flags[] = {
	XLAT(SHM_REMAP),
	XLAT(SHM_RDONLY),
	XLAT(SHM_RND),
	XLAT_END
};

static const struct xlat msg_flags[] = {
	XLAT(MSG_NOERROR),
	XLAT(MSG_EXCEPT),
	XLAT(IPC_NOWAIT),
	XLAT_END
};

static const struct xlat semop_flags[] = {
	XLAT(SEM_UNDO),
	XLAT(IPC_NOWAIT),
	XLAT_END
};

int sys_msgget(struct tcb *tcp)
{
	if (entering(tcp)) {
		if (tcp->u_arg[0])
			tprintf("%#lx, ", tcp->u_arg[0]);
		else
			tprints("IPC_PRIVATE, ");
		if (printflags(resource_flags, tcp->u_arg[1] & ~0777, NULL) != 0)
			tprints("|");
		tprintf("%#lo", tcp->u_arg[1] & 0777);
	}
	return 0;
}

#ifdef IPC_64
# define PRINTCTL(flagset, arg, dflt) \
	if ((arg) & IPC_64) tprints("IPC_64|"); \
	printxval((flagset), (arg) &~ IPC_64, dflt)
#else
# define PRINTCTL printxval
#endif

static int
indirect_ipccall(struct tcb *tcp)
{
#ifdef X86_64
	return current_personality == 1;
#endif
#if defined IA64
	return tcp->scno < 1024; /* ia32 emulation syscalls are low */
#endif
#if defined(ALPHA) || defined(MIPS) || defined(HPPA) || defined(__ARM_EABI__) || defined(AARCH64)
	return 0;
#endif
	return 1;
}

int sys_msgctl(struct tcb *tcp)
{
	if (entering(tcp)) {
		tprintf("%lu, ", tcp->u_arg[0]);
		PRINTCTL(msgctl_flags, tcp->u_arg[1], "MSG_???");
		tprintf(", %#lx", tcp->u_arg[indirect_ipccall(tcp) ? 3 : 2]);
	}
	return 0;
}

static void
tprint_msgsnd(struct tcb *tcp, long addr, unsigned long count,
	      unsigned long flags)
{
	long mtype;

	if (umove(tcp, addr, &mtype) < 0) {
		tprintf("%#lx", addr);
	} else {
		tprintf("{%lu, ", mtype);
		printstr(tcp, addr + sizeof(mtype), count);
		tprints("}");
	}
	tprintf(", %lu, ", count);
	printflags(msg_flags, flags, "MSG_???");
}

int sys_msgsnd(struct tcb *tcp)
{
	if (entering(tcp)) {
		tprintf("%d, ", (int) tcp->u_arg[0]);
		if (indirect_ipccall(tcp)) {
			tprint_msgsnd(tcp, tcp->u_arg[3], tcp->u_arg[1],
				      tcp->u_arg[2]);
		} else {
			tprint_msgsnd(tcp, tcp->u_arg[1], tcp->u_arg[2],
				      tcp->u_arg[3]);
		}
	}
	return 0;
}

static void
tprint_msgrcv(struct tcb *tcp, long addr, unsigned long count, long msgtyp)
{
	long mtype;

	if (syserror(tcp) || umove(tcp, addr, &mtype) < 0) {
		tprintf("%#lx", addr);
	} else {
		tprintf("{%lu, ", mtype);
		printstr(tcp, addr + sizeof(mtype), count);
		tprints("}");
	}
	tprintf(", %lu, %ld, ", count, msgtyp);
}

int sys_msgrcv(struct tcb *tcp)
{
	if (entering(tcp)) {
		tprintf("%d, ", (int) tcp->u_arg[0]);
	} else {
		if (indirect_ipccall(tcp)) {
			struct ipc_wrapper {
				struct msgbuf *msgp;
				long msgtyp;
			} tmp;

			if (umove(tcp, tcp->u_arg[3], &tmp) < 0) {
				tprintf("%#lx, %lu, ",
					tcp->u_arg[3], tcp->u_arg[1]);
			} else {
				tprint_msgrcv(tcp, (long) tmp.msgp,
					tcp->u_arg[1], tmp.msgtyp);
			}
			printflags(msg_flags, tcp->u_arg[2], "MSG_???");
		} else {
			tprint_msgrcv(tcp, tcp->u_arg[1],
				tcp->u_arg[2], tcp->u_arg[3]);
			printflags(msg_flags, tcp->u_arg[4], "MSG_???");
		}
	}
	return 0;
}

static void
tprint_sembuf(struct tcb *tcp, long addr, unsigned long count)
{
	unsigned long i, max_count;

	if (abbrev(tcp))
		max_count = (max_strlen < count) ? max_strlen : count;
	else
		max_count = count;

	if (!max_count) {
		tprintf("%#lx, %lu", addr, count);
		return;
	}

	for (i = 0; i < max_count; ++i) {
		struct sembuf sb;
		if (i)
			tprints(", ");
		if (umove(tcp, addr + i * sizeof(struct sembuf), &sb) < 0) {
			if (i) {
				tprints("{???}");
				break;
			} else {
				tprintf("%#lx, %lu", addr, count);
				return;
			}
		} else {
			if (!i)
				tprints("{");
			tprintf("{%u, %d, ", sb.sem_num, sb.sem_op);
			printflags(semop_flags, sb.sem_flg, "SEM_???");
			tprints("}");
		}
	}

	if (i < max_count || max_count < count)
		tprints(", ...");

	tprintf("}, %lu", count);
}

int sys_semop(struct tcb *tcp)
{
	if (entering(tcp)) {
		tprintf("%lu, ", tcp->u_arg[0]);
		if (indirect_ipccall(tcp)) {
			tprint_sembuf(tcp, tcp->u_arg[3], tcp->u_arg[1]);
		} else {
			tprint_sembuf(tcp, tcp->u_arg[1], tcp->u_arg[2]);
		}
	}
	return 0;
}

int sys_semtimedop(struct tcb *tcp)
{
	if (entering(tcp)) {
		tprintf("%lu, ", tcp->u_arg[0]);
		if (indirect_ipccall(tcp)) {
			tprint_sembuf(tcp, tcp->u_arg[3], tcp->u_arg[1]);
			tprints(", ");
#if defined(S390) || defined(S390X)
			printtv(tcp, tcp->u_arg[2]);
#else
			printtv(tcp, tcp->u_arg[4]);
#endif
		} else {
			tprint_sembuf(tcp, tcp->u_arg[1], tcp->u_arg[2]);
			tprints(", ");
			printtv(tcp, tcp->u_arg[3]);
		}
	}
	return 0;
}

int sys_semget(struct tcb *tcp)
{
	if (entering(tcp)) {
		if (tcp->u_arg[0])
			tprintf("%#lx", tcp->u_arg[0]);
		else
			tprints("IPC_PRIVATE");
		tprintf(", %lu, ", tcp->u_arg[1]);
		if (printflags(resource_flags, tcp->u_arg[2] & ~0777, NULL) != 0)
			tprints("|");
		tprintf("%#lo", tcp->u_arg[2] & 0777);
	}
	return 0;
}

int sys_semctl(struct tcb *tcp)
{
	if (entering(tcp)) {
		tprintf("%lu, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
		PRINTCTL(semctl_flags, tcp->u_arg[2], "SEM_???");
		tprintf(", %#lx", tcp->u_arg[3]);
	}
	return 0;
}

int sys_shmget(struct tcb *tcp)
{
	if (entering(tcp)) {
		if (tcp->u_arg[0])
			tprintf("%#lx", tcp->u_arg[0]);
		else
			tprints("IPC_PRIVATE");
		tprintf(", %lu, ", tcp->u_arg[1]);
		if (printflags(shm_resource_flags, tcp->u_arg[2] & ~0777, NULL) != 0)
			tprints("|");
		tprintf("%#lo", tcp->u_arg[2] & 0777);
	}
	return 0;
}

int sys_shmctl(struct tcb *tcp)
{
	if (entering(tcp)) {
		tprintf("%lu, ", tcp->u_arg[0]);
		PRINTCTL(shmctl_flags, tcp->u_arg[1], "SHM_???");
		if (indirect_ipccall(tcp)) {
			tprintf(", %#lx", tcp->u_arg[3]);
		} else {
			tprintf(", %#lx", tcp->u_arg[2]);
		}
	}
	return 0;
}

int sys_shmat(struct tcb *tcp)
{
	if (exiting(tcp)) {
		tprintf("%lu", tcp->u_arg[0]);
		if (indirect_ipccall(tcp)) {
			tprintf(", %#lx, ", tcp->u_arg[3]);
			printflags(shm_flags, tcp->u_arg[1], "SHM_???");
		} else {
			tprintf(", %#lx, ", tcp->u_arg[1]);
			printflags(shm_flags, tcp->u_arg[2], "SHM_???");
		}
		if (syserror(tcp))
			return 0;
		if (indirect_ipccall(tcp)) {
			unsigned long raddr;
			if (umove(tcp, tcp->u_arg[2], &raddr) < 0)
				return RVAL_NONE;
			tcp->u_rval = raddr;
		}
		return RVAL_HEX;
	}
	return 0;
}

int sys_shmdt(struct tcb *tcp)
{
	if (entering(tcp)) {
		if (indirect_ipccall(tcp)) {
			tprintf("%#lx", tcp->u_arg[3]);
		} else {
			tprintf("%#lx", tcp->u_arg[0]);
		}
	}
	return 0;
}

int
sys_mq_open(struct tcb *tcp)
{
	if (entering(tcp)) {
		printpath(tcp, tcp->u_arg[0]);
		tprints(", ");
		/* flags */
		tprint_open_modes(tcp->u_arg[1]);
		if (tcp->u_arg[1] & O_CREAT) {
# ifndef HAVE_MQUEUE_H
			tprintf(", %lx", tcp->u_arg[2]);
# else
			struct mq_attr attr;
			/* mode */
			tprintf(", %#lo, ", tcp->u_arg[2]);
			if (umove(tcp, tcp->u_arg[3], &attr) < 0)
				tprints("{???}");
			else
				tprintf("{mq_maxmsg=%ld, mq_msgsize=%ld}",
					(long) attr.mq_maxmsg,
					(long) attr.mq_msgsize);
# endif
		}
	}
	return 0;
}

int
sys_mq_timedsend(struct tcb *tcp)
{
	if (entering(tcp)) {
		tprintf("%ld, ", tcp->u_arg[0]);
		printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
		tprintf(", %lu, %ld, ", tcp->u_arg[2], tcp->u_arg[3]);
		printtv(tcp, tcp->u_arg[4]);
	}
	return 0;
}

int
sys_mq_timedreceive(struct tcb *tcp)
{
	if (entering(tcp))
		tprintf("%ld, ", tcp->u_arg[0]);
	else {
		printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
		tprintf(", %lu, %ld, ", tcp->u_arg[2], tcp->u_arg[3]);
		printtv(tcp, tcp->u_arg[4]);
	}
	return 0;
}

int
sys_mq_notify(struct tcb *tcp)
{
	if (entering(tcp)) {
		tprintf("%ld, ", tcp->u_arg[0]);
		printsigevent(tcp, tcp->u_arg[1]);
	}
	return 0;
}

static void
printmqattr(struct tcb *tcp, long addr)
{
	if (addr == 0)
		tprints("NULL");
	else {
# ifndef HAVE_MQUEUE_H
		tprintf("%#lx", addr);
# else
		struct mq_attr attr;
		if (umove(tcp, addr, &attr) < 0) {
			tprints("{...}");
			return;
		}
		tprints("{mq_flags=");
		tprint_open_modes(attr.mq_flags);
		tprintf(", mq_maxmsg=%ld, mq_msgsize=%ld, mq_curmsg=%ld}",
			(long) attr.mq_maxmsg, (long) attr.mq_msgsize,
			(long) attr.mq_curmsgs);
# endif
	}
}

int
sys_mq_getsetattr(struct tcb *tcp)
{
	if (entering(tcp)) {
		tprintf("%ld, ", tcp->u_arg[0]);
		printmqattr(tcp, tcp->u_arg[1]);
		tprints(", ");
	} else
		printmqattr(tcp, tcp->u_arg[2]);
	return 0;
}

int
sys_ipc(struct tcb *tcp)
{
	return printargs(tcp);
}