xref: /aosp_15_r20/external/ltp/libs/libltpipc/libmsgctl.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2002
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or
6*49cdfc7eSAndroid Build Coastguard Worker  * modify it under the terms of the GNU General Public License as
7*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation; either version 2 of
8*49cdfc7eSAndroid Build Coastguard Worker  * the License, or (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful,
11*49cdfc7eSAndroid Build Coastguard Worker  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*49cdfc7eSAndroid Build Coastguard Worker  * GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker  * along with this program; if not, write the Free Software Foundation,
17*49cdfc7eSAndroid Build Coastguard Worker  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <sys/ipc.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <sys/msg.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include "libmsgctl.h"
29*49cdfc7eSAndroid Build Coastguard Worker 
doreader(long key,int tid,long type,int child,int nreps)30*49cdfc7eSAndroid Build Coastguard Worker int doreader(long key, int tid, long type, int child, int nreps)
31*49cdfc7eSAndroid Build Coastguard Worker {
32*49cdfc7eSAndroid Build Coastguard Worker 	int i, size;
33*49cdfc7eSAndroid Build Coastguard Worker 	int id;
34*49cdfc7eSAndroid Build Coastguard Worker 	struct mbuffer buffer;
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	id = msgget(key, 0);
37*49cdfc7eSAndroid Build Coastguard Worker 	if (id < 0) {
38*49cdfc7eSAndroid Build Coastguard Worker 		printf("msgget() error in the reader of child group %d: %s\n",
39*49cdfc7eSAndroid Build Coastguard Worker 			child, strerror(errno));
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 		return FAIL;
42*49cdfc7eSAndroid Build Coastguard Worker 	}
43*49cdfc7eSAndroid Build Coastguard Worker 	if (id != tid) {
44*49cdfc7eSAndroid Build Coastguard Worker 		printf("Message queue mismatch in the reader of child group %d for message queue id %d\n",
45*49cdfc7eSAndroid Build Coastguard Worker 			child, id);
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 		return FAIL;
48*49cdfc7eSAndroid Build Coastguard Worker 	}
49*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < nreps; i++) {
50*49cdfc7eSAndroid Build Coastguard Worker 		memset(&buffer, 0, sizeof(buffer));
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 		size = msgrcv(id, &buffer, 100, type, 0);
53*49cdfc7eSAndroid Build Coastguard Worker 		if (size < 0) {
54*49cdfc7eSAndroid Build Coastguard Worker 			printf("msgrcv() error in child %d, read # = %d: %s\n",
55*49cdfc7eSAndroid Build Coastguard Worker 				child, (i + 1), strerror(errno));
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 			return FAIL;
58*49cdfc7eSAndroid Build Coastguard Worker 		}
59*49cdfc7eSAndroid Build Coastguard Worker 		if (buffer.type != type) {
60*49cdfc7eSAndroid Build Coastguard Worker 			printf("Type mismatch in child %d, read #d = %d: ",
61*49cdfc7eSAndroid Build Coastguard Worker 				child, (i + 1));
62*49cdfc7eSAndroid Build Coastguard Worker 			printf("for message got %ld, expected - %ld\n",
63*49cdfc7eSAndroid Build Coastguard Worker 				buffer.type, type);
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 			return FAIL;
66*49cdfc7eSAndroid Build Coastguard Worker 		}
67*49cdfc7eSAndroid Build Coastguard Worker 		if (buffer.data.len + 1 != size) {
68*49cdfc7eSAndroid Build Coastguard Worker 			printf("Size mismatch in child %d, read # = %d: ",
69*49cdfc7eSAndroid Build Coastguard Worker 				child, (i + 1));
70*49cdfc7eSAndroid Build Coastguard Worker 			printf("for message got %d, expected - %d\n",
71*49cdfc7eSAndroid Build Coastguard Worker 				buffer.data.len + 1, size);
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 			return FAIL;
74*49cdfc7eSAndroid Build Coastguard Worker 		}
75*49cdfc7eSAndroid Build Coastguard Worker 		if (verify(buffer.data.pbytes, (key % 255), size - 1, child)) {
76*49cdfc7eSAndroid Build Coastguard Worker 			printf("Verify failed in child %d read # = %d, key = %lx\n",
77*49cdfc7eSAndroid Build Coastguard Worker 				child, (i + 1), key);
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker 			return FAIL;
80*49cdfc7eSAndroid Build Coastguard Worker 		}
81*49cdfc7eSAndroid Build Coastguard Worker 		key++;
82*49cdfc7eSAndroid Build Coastguard Worker 	}
83*49cdfc7eSAndroid Build Coastguard Worker 	return PASS;
84*49cdfc7eSAndroid Build Coastguard Worker }
85*49cdfc7eSAndroid Build Coastguard Worker 
dowriter(long key,int tid,long type,int child,int nreps)86*49cdfc7eSAndroid Build Coastguard Worker int dowriter(long key, int tid, long type, int child, int nreps)
87*49cdfc7eSAndroid Build Coastguard Worker {
88*49cdfc7eSAndroid Build Coastguard Worker 	int i, size;
89*49cdfc7eSAndroid Build Coastguard Worker 	int id;
90*49cdfc7eSAndroid Build Coastguard Worker 	struct mbuffer buffer;
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 	id = msgget(key, 0);
93*49cdfc7eSAndroid Build Coastguard Worker 	if (id < 0) {
94*49cdfc7eSAndroid Build Coastguard Worker 		printf("msgget() error in the writer of child group %d: %s\n",
95*49cdfc7eSAndroid Build Coastguard Worker 			child, strerror(errno));
96*49cdfc7eSAndroid Build Coastguard Worker 
97*49cdfc7eSAndroid Build Coastguard Worker 		return FAIL;
98*49cdfc7eSAndroid Build Coastguard Worker 	}
99*49cdfc7eSAndroid Build Coastguard Worker 	if (id != tid) {
100*49cdfc7eSAndroid Build Coastguard Worker 		printf("Message queue mismatch in the reader of child group %d for message queue id %d\n",
101*49cdfc7eSAndroid Build Coastguard Worker 			child, id);
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 		return FAIL;
104*49cdfc7eSAndroid Build Coastguard Worker 	}
105*49cdfc7eSAndroid Build Coastguard Worker 
106*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < nreps; i++) {
107*49cdfc7eSAndroid Build Coastguard Worker 		memset(&buffer, 0, sizeof(buffer));
108*49cdfc7eSAndroid Build Coastguard Worker 
109*49cdfc7eSAndroid Build Coastguard Worker 		do {
110*49cdfc7eSAndroid Build Coastguard Worker 			size = (lrand48() % 99);
111*49cdfc7eSAndroid Build Coastguard Worker 		} while (size == 0);
112*49cdfc7eSAndroid Build Coastguard Worker 		fill_buffer(buffer.data.pbytes, (key % 255), size);
113*49cdfc7eSAndroid Build Coastguard Worker 		buffer.data.len = size;
114*49cdfc7eSAndroid Build Coastguard Worker 		buffer.type = type;
115*49cdfc7eSAndroid Build Coastguard Worker 		if (msgsnd(id, &buffer, size + 1, 0) < 0) {
116*49cdfc7eSAndroid Build Coastguard Worker 			printf("msgsnd() error in child %d, write # = %d, key = %lx: %s\n",
117*49cdfc7eSAndroid Build Coastguard Worker 				child, nreps, key, strerror(errno));
118*49cdfc7eSAndroid Build Coastguard Worker 
119*49cdfc7eSAndroid Build Coastguard Worker 			return FAIL;
120*49cdfc7eSAndroid Build Coastguard Worker 		}
121*49cdfc7eSAndroid Build Coastguard Worker 		key++;
122*49cdfc7eSAndroid Build Coastguard Worker 	}
123*49cdfc7eSAndroid Build Coastguard Worker 	return PASS;
124*49cdfc7eSAndroid Build Coastguard Worker }
125*49cdfc7eSAndroid Build Coastguard Worker 
fill_buffer(char * buf,char val,int size)126*49cdfc7eSAndroid Build Coastguard Worker int fill_buffer(char *buf, char val, int size)
127*49cdfc7eSAndroid Build Coastguard Worker {
128*49cdfc7eSAndroid Build Coastguard Worker 	int i;
129*49cdfc7eSAndroid Build Coastguard Worker 
130*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < size; i++)
131*49cdfc7eSAndroid Build Coastguard Worker 		buf[i] = val;
132*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
133*49cdfc7eSAndroid Build Coastguard Worker }
134*49cdfc7eSAndroid Build Coastguard Worker 
135*49cdfc7eSAndroid Build Coastguard Worker /* Check a buffer for correct values */
verify(char * buf,char val,int size,int child)136*49cdfc7eSAndroid Build Coastguard Worker int verify(char *buf, char val, int size, int child)
137*49cdfc7eSAndroid Build Coastguard Worker {
138*49cdfc7eSAndroid Build Coastguard Worker 	while (size-- > 0) {
139*49cdfc7eSAndroid Build Coastguard Worker 		if (*buf++ != val) {
140*49cdfc7eSAndroid Build Coastguard Worker 			printf("Verify error in child %d, *buf = %x, val = %x, size = %d\n",
141*49cdfc7eSAndroid Build Coastguard Worker 				child, *buf, val, size);
142*49cdfc7eSAndroid Build Coastguard Worker 
143*49cdfc7eSAndroid Build Coastguard Worker 			return FAIL;
144*49cdfc7eSAndroid Build Coastguard Worker 		}
145*49cdfc7eSAndroid Build Coastguard Worker 	}
146*49cdfc7eSAndroid Build Coastguard Worker 	return PASS;
147*49cdfc7eSAndroid Build Coastguard Worker }
148