xref: /OK3568_Linux_fs/kernel/tools/testing/selftests/ipc/msgque.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #define _GNU_SOURCE
3*4882a593Smuzhiyun #include <stdlib.h>
4*4882a593Smuzhiyun #include <stdio.h>
5*4882a593Smuzhiyun #include <string.h>
6*4882a593Smuzhiyun #include <errno.h>
7*4882a593Smuzhiyun #include <sys/msg.h>
8*4882a593Smuzhiyun #include <fcntl.h>
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include "../kselftest.h"
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #define MAX_MSG_SIZE		32
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun struct msg1 {
15*4882a593Smuzhiyun 	int msize;
16*4882a593Smuzhiyun 	long mtype;
17*4882a593Smuzhiyun 	char mtext[MAX_MSG_SIZE];
18*4882a593Smuzhiyun };
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #define TEST_STRING "Test sysv5 msg"
21*4882a593Smuzhiyun #define MSG_TYPE 1
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #define ANOTHER_TEST_STRING "Yet another test sysv5 msg"
24*4882a593Smuzhiyun #define ANOTHER_MSG_TYPE 26538
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun struct msgque_data {
27*4882a593Smuzhiyun 	key_t key;
28*4882a593Smuzhiyun 	int msq_id;
29*4882a593Smuzhiyun 	int qbytes;
30*4882a593Smuzhiyun 	int qnum;
31*4882a593Smuzhiyun 	int mode;
32*4882a593Smuzhiyun 	struct msg1 *messages;
33*4882a593Smuzhiyun };
34*4882a593Smuzhiyun 
restore_queue(struct msgque_data * msgque)35*4882a593Smuzhiyun int restore_queue(struct msgque_data *msgque)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun 	int fd, ret, id, i;
38*4882a593Smuzhiyun 	char buf[32];
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	fd = open("/proc/sys/kernel/msg_next_id", O_WRONLY);
41*4882a593Smuzhiyun 	if (fd == -1) {
42*4882a593Smuzhiyun 		printf("Failed to open /proc/sys/kernel/msg_next_id\n");
43*4882a593Smuzhiyun 		return -errno;
44*4882a593Smuzhiyun 	}
45*4882a593Smuzhiyun 	sprintf(buf, "%d", msgque->msq_id);
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	ret = write(fd, buf, strlen(buf));
48*4882a593Smuzhiyun 	if (ret != strlen(buf)) {
49*4882a593Smuzhiyun 		printf("Failed to write to /proc/sys/kernel/msg_next_id\n");
50*4882a593Smuzhiyun 		return -errno;
51*4882a593Smuzhiyun 	}
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	id = msgget(msgque->key, msgque->mode | IPC_CREAT | IPC_EXCL);
54*4882a593Smuzhiyun 	if (id == -1) {
55*4882a593Smuzhiyun 		printf("Failed to create queue\n");
56*4882a593Smuzhiyun 		return -errno;
57*4882a593Smuzhiyun 	}
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	if (id != msgque->msq_id) {
60*4882a593Smuzhiyun 		printf("Restored queue has wrong id (%d instead of %d)\n",
61*4882a593Smuzhiyun 							id, msgque->msq_id);
62*4882a593Smuzhiyun 		ret = -EFAULT;
63*4882a593Smuzhiyun 		goto destroy;
64*4882a593Smuzhiyun 	}
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	for (i = 0; i < msgque->qnum; i++) {
67*4882a593Smuzhiyun 		if (msgsnd(msgque->msq_id, &msgque->messages[i].mtype,
68*4882a593Smuzhiyun 			   msgque->messages[i].msize, IPC_NOWAIT) != 0) {
69*4882a593Smuzhiyun 			printf("msgsnd failed (%m)\n");
70*4882a593Smuzhiyun 			ret = -errno;
71*4882a593Smuzhiyun 			goto destroy;
72*4882a593Smuzhiyun 		};
73*4882a593Smuzhiyun 	}
74*4882a593Smuzhiyun 	return 0;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun destroy:
77*4882a593Smuzhiyun 	if (msgctl(id, IPC_RMID, NULL))
78*4882a593Smuzhiyun 		printf("Failed to destroy queue: %d\n", -errno);
79*4882a593Smuzhiyun 	return ret;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun 
check_and_destroy_queue(struct msgque_data * msgque)82*4882a593Smuzhiyun int check_and_destroy_queue(struct msgque_data *msgque)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun 	struct msg1 message;
85*4882a593Smuzhiyun 	int cnt = 0, ret;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	while (1) {
88*4882a593Smuzhiyun 		ret = msgrcv(msgque->msq_id, &message.mtype, MAX_MSG_SIZE,
89*4882a593Smuzhiyun 				0, IPC_NOWAIT);
90*4882a593Smuzhiyun 		if (ret < 0) {
91*4882a593Smuzhiyun 			if (errno == ENOMSG)
92*4882a593Smuzhiyun 				break;
93*4882a593Smuzhiyun 			printf("Failed to read IPC message: %m\n");
94*4882a593Smuzhiyun 			ret = -errno;
95*4882a593Smuzhiyun 			goto err;
96*4882a593Smuzhiyun 		}
97*4882a593Smuzhiyun 		if (ret != msgque->messages[cnt].msize) {
98*4882a593Smuzhiyun 			printf("Wrong message size: %d (expected %d)\n", ret,
99*4882a593Smuzhiyun 						msgque->messages[cnt].msize);
100*4882a593Smuzhiyun 			ret = -EINVAL;
101*4882a593Smuzhiyun 			goto err;
102*4882a593Smuzhiyun 		}
103*4882a593Smuzhiyun 		if (message.mtype != msgque->messages[cnt].mtype) {
104*4882a593Smuzhiyun 			printf("Wrong message type\n");
105*4882a593Smuzhiyun 			ret = -EINVAL;
106*4882a593Smuzhiyun 			goto err;
107*4882a593Smuzhiyun 		}
108*4882a593Smuzhiyun 		if (memcmp(message.mtext, msgque->messages[cnt].mtext, ret)) {
109*4882a593Smuzhiyun 			printf("Wrong message content\n");
110*4882a593Smuzhiyun 			ret = -EINVAL;
111*4882a593Smuzhiyun 			goto err;
112*4882a593Smuzhiyun 		}
113*4882a593Smuzhiyun 		cnt++;
114*4882a593Smuzhiyun 	}
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	if (cnt != msgque->qnum) {
117*4882a593Smuzhiyun 		printf("Wrong message number\n");
118*4882a593Smuzhiyun 		ret = -EINVAL;
119*4882a593Smuzhiyun 		goto err;
120*4882a593Smuzhiyun 	}
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	ret = 0;
123*4882a593Smuzhiyun err:
124*4882a593Smuzhiyun 	if (msgctl(msgque->msq_id, IPC_RMID, NULL)) {
125*4882a593Smuzhiyun 		printf("Failed to destroy queue: %d\n", -errno);
126*4882a593Smuzhiyun 		return -errno;
127*4882a593Smuzhiyun 	}
128*4882a593Smuzhiyun 	return ret;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
dump_queue(struct msgque_data * msgque)131*4882a593Smuzhiyun int dump_queue(struct msgque_data *msgque)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	struct msqid_ds ds;
134*4882a593Smuzhiyun 	int kern_id;
135*4882a593Smuzhiyun 	int i, ret;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	for (kern_id = 0; kern_id < 256; kern_id++) {
138*4882a593Smuzhiyun 		ret = msgctl(kern_id, MSG_STAT, &ds);
139*4882a593Smuzhiyun 		if (ret < 0) {
140*4882a593Smuzhiyun 			if (errno == EINVAL)
141*4882a593Smuzhiyun 				continue;
142*4882a593Smuzhiyun 			printf("Failed to get stats for IPC queue with id %d\n",
143*4882a593Smuzhiyun 					kern_id);
144*4882a593Smuzhiyun 			return -errno;
145*4882a593Smuzhiyun 		}
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 		if (ret == msgque->msq_id)
148*4882a593Smuzhiyun 			break;
149*4882a593Smuzhiyun 	}
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	msgque->messages = malloc(sizeof(struct msg1) * ds.msg_qnum);
152*4882a593Smuzhiyun 	if (msgque->messages == NULL) {
153*4882a593Smuzhiyun 		printf("Failed to get stats for IPC queue\n");
154*4882a593Smuzhiyun 		return -ENOMEM;
155*4882a593Smuzhiyun 	}
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	msgque->qnum = ds.msg_qnum;
158*4882a593Smuzhiyun 	msgque->mode = ds.msg_perm.mode;
159*4882a593Smuzhiyun 	msgque->qbytes = ds.msg_qbytes;
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	for (i = 0; i < msgque->qnum; i++) {
162*4882a593Smuzhiyun 		ret = msgrcv(msgque->msq_id, &msgque->messages[i].mtype,
163*4882a593Smuzhiyun 				MAX_MSG_SIZE, i, IPC_NOWAIT | MSG_COPY);
164*4882a593Smuzhiyun 		if (ret < 0) {
165*4882a593Smuzhiyun 			printf("Failed to copy IPC message: %m (%d)\n", errno);
166*4882a593Smuzhiyun 			return -errno;
167*4882a593Smuzhiyun 		}
168*4882a593Smuzhiyun 		msgque->messages[i].msize = ret;
169*4882a593Smuzhiyun 	}
170*4882a593Smuzhiyun 	return 0;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun 
fill_msgque(struct msgque_data * msgque)173*4882a593Smuzhiyun int fill_msgque(struct msgque_data *msgque)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	struct msg1 msgbuf;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	msgbuf.mtype = MSG_TYPE;
178*4882a593Smuzhiyun 	memcpy(msgbuf.mtext, TEST_STRING, sizeof(TEST_STRING));
179*4882a593Smuzhiyun 	if (msgsnd(msgque->msq_id, &msgbuf.mtype, sizeof(TEST_STRING),
180*4882a593Smuzhiyun 				IPC_NOWAIT) != 0) {
181*4882a593Smuzhiyun 		printf("First message send failed (%m)\n");
182*4882a593Smuzhiyun 		return -errno;
183*4882a593Smuzhiyun 	};
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	msgbuf.mtype = ANOTHER_MSG_TYPE;
186*4882a593Smuzhiyun 	memcpy(msgbuf.mtext, ANOTHER_TEST_STRING, sizeof(ANOTHER_TEST_STRING));
187*4882a593Smuzhiyun 	if (msgsnd(msgque->msq_id, &msgbuf.mtype, sizeof(ANOTHER_TEST_STRING),
188*4882a593Smuzhiyun 				IPC_NOWAIT) != 0) {
189*4882a593Smuzhiyun 		printf("Second message send failed (%m)\n");
190*4882a593Smuzhiyun 		return -errno;
191*4882a593Smuzhiyun 	};
192*4882a593Smuzhiyun 	return 0;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun 
main(int argc,char ** argv)195*4882a593Smuzhiyun int main(int argc, char **argv)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun 	int msg, pid, err;
198*4882a593Smuzhiyun 	struct msgque_data msgque;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	if (getuid() != 0)
201*4882a593Smuzhiyun 		return ksft_exit_skip(
202*4882a593Smuzhiyun 				"Please run the test as root - Exiting.\n");
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	msgque.key = ftok(argv[0], 822155650);
205*4882a593Smuzhiyun 	if (msgque.key == -1) {
206*4882a593Smuzhiyun 		printf("Can't make key: %d\n", -errno);
207*4882a593Smuzhiyun 		return ksft_exit_fail();
208*4882a593Smuzhiyun 	}
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	msgque.msq_id = msgget(msgque.key, IPC_CREAT | IPC_EXCL | 0666);
211*4882a593Smuzhiyun 	if (msgque.msq_id == -1) {
212*4882a593Smuzhiyun 		err = -errno;
213*4882a593Smuzhiyun 		printf("Can't create queue: %d\n", err);
214*4882a593Smuzhiyun 		goto err_out;
215*4882a593Smuzhiyun 	}
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	err = fill_msgque(&msgque);
218*4882a593Smuzhiyun 	if (err) {
219*4882a593Smuzhiyun 		printf("Failed to fill queue: %d\n", err);
220*4882a593Smuzhiyun 		goto err_destroy;
221*4882a593Smuzhiyun 	}
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	err = dump_queue(&msgque);
224*4882a593Smuzhiyun 	if (err) {
225*4882a593Smuzhiyun 		printf("Failed to dump queue: %d\n", err);
226*4882a593Smuzhiyun 		goto err_destroy;
227*4882a593Smuzhiyun 	}
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	err = check_and_destroy_queue(&msgque);
230*4882a593Smuzhiyun 	if (err) {
231*4882a593Smuzhiyun 		printf("Failed to check and destroy queue: %d\n", err);
232*4882a593Smuzhiyun 		goto err_out;
233*4882a593Smuzhiyun 	}
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	err = restore_queue(&msgque);
236*4882a593Smuzhiyun 	if (err) {
237*4882a593Smuzhiyun 		printf("Failed to restore queue: %d\n", err);
238*4882a593Smuzhiyun 		goto err_destroy;
239*4882a593Smuzhiyun 	}
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	err = check_and_destroy_queue(&msgque);
242*4882a593Smuzhiyun 	if (err) {
243*4882a593Smuzhiyun 		printf("Failed to test queue: %d\n", err);
244*4882a593Smuzhiyun 		goto err_out;
245*4882a593Smuzhiyun 	}
246*4882a593Smuzhiyun 	return ksft_exit_pass();
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun err_destroy:
249*4882a593Smuzhiyun 	if (msgctl(msgque.msq_id, IPC_RMID, NULL)) {
250*4882a593Smuzhiyun 		printf("Failed to destroy queue: %d\n", -errno);
251*4882a593Smuzhiyun 		return ksft_exit_fail();
252*4882a593Smuzhiyun 	}
253*4882a593Smuzhiyun err_out:
254*4882a593Smuzhiyun 	return ksft_exit_fail();
255*4882a593Smuzhiyun }
256