summaryrefslogtreecommitdiffstats
path: root/src/kernel/tests/lib/tst_safe_sysv_ipc.c
blob: 30b5f6ec7ff5101a114a7830bd3c19210ef86db2 (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) 2017 Xiao yang <yangx.jy@cn.fujitsu.com>
 */

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#define TST_NO_DEFAULT_MAIN
#include "tst_test.h"
#include "tst_safe_sysv_ipc.h"

/*
 * The IPC_STAT, IPC_SET and IPC_RMID can return either 0 or -1.
 *
 * Linux specific cmds either returns -1 on failure or positive integer
 * either index into an kernel array or shared primitive indentifier.
 */
static int ret_check(int cmd, int ret)
{
	switch (cmd) {
	case IPC_STAT:
	case IPC_SET:
	case IPC_RMID:
		return ret != 0;
	default:
		return ret == -1;
	}
}

int safe_msgget(const char *file, const int lineno, key_t key, int msgflg)
{
	int rval;

	rval = msgget(key, msgflg);
	if (rval == -1) {
		tst_brk(TBROK | TERRNO, "%s:%d: msgget(%i, %x) failed",
			file, lineno, (int)key, msgflg);
	}

	return rval;
}

int safe_msgsnd(const char *file, const int lineno, int msqid, const void *msgp,
		size_t msgsz, int msgflg)
{
	int rval;

	rval = msgsnd(msqid, msgp, msgsz, msgflg);
	if (rval == -1) {
		tst_brk(TBROK | TERRNO,
			"%s:%d: msgsnd(%i, %p, %zu, %x) failed",
			file, lineno, msqid, msgp, msgsz, msgflg);
	}

	return rval;
}

ssize_t safe_msgrcv(const char *file, const int lineno, int msqid, void *msgp,
		size_t msgsz, long msgtyp, int msgflg)
{
	ssize_t rval;

	rval = msgrcv(msqid, msgp, msgsz, msgtyp, msgflg);
	if (rval == -1) {
		tst_brk(TBROK | TERRNO,
			"%s:%d: msgrcv(%i, %p, %zu, %li, %x) failed",
			file, lineno, msqid, msgp, msgsz, msgtyp, msgflg);
	}

	return rval;
}

int safe_msgctl(const char *file, const int lineno, int msqid, int cmd,
		struct msqid_ds *buf)
{
	int rval;

	rval = msgctl(msqid, cmd, buf);
	if (ret_check(cmd, rval)) {
		tst_brk(TBROK | TERRNO,
			"%s:%d: msgctl(%i, %i, %p) = %i failed",
			file, lineno, msqid, cmd, buf, rval);
	}


	return rval;
}

int safe_shmget(const char *file, const int lineno, key_t key, size_t size,
		int shmflg)
{
	int rval;

	rval = shmget(key, size, shmflg);
	if (rval == -1) {
		tst_brk(TBROK | TERRNO, "%s:%d: shmget(%i, %zu, %x) failed",
			file, lineno, (int)key, size, shmflg);
	}

	return rval;
}

void *safe_shmat(const char *file, const int lineno, int shmid,
		const void *shmaddr, int shmflg)
{
	void *rval;

	rval = shmat(shmid, shmaddr, shmflg);
	if (rval == (void *)-1) {
		tst_brk(TBROK | TERRNO, "%s:%d: shmat(%i, %p, %x) failed",
			file, lineno, shmid, shmaddr, shmflg);
	}

	return rval;
}

int safe_shmdt(const char *file, const int lineno, const void *shmaddr)
{
	int rval;

	rval = shmdt(shmaddr);
	if (rval == -1) {
		tst_brk(TBROK | TERRNO, "%s:%d: shmdt(%p) failed",
			file, lineno, shmaddr);
	}

	return rval;
}

int safe_shmctl(const char *file, const int lineno, int shmid, int cmd,
		struct shmid_ds *buf)
{
	int rval;

	rval = shmctl(shmid, cmd, buf);
	if (ret_check(cmd, rval)) {
		tst_brk(TBROK | TERRNO,
			"%s:%d: shmctl(%i, %i, %p) = %i failed",
			file, lineno, shmid, cmd, buf, rval);
	}

	return rval;
}