xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/utils/mq.h (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2017 Petr Vorel <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker #ifndef MQ_H
7*49cdfc7eSAndroid Build Coastguard Worker #define MQ_H
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
10*49cdfc7eSAndroid Build Coastguard Worker #include "tst_sig_proc.h"
11*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_posix_ipc.h"
12*49cdfc7eSAndroid Build Coastguard Worker 
13*49cdfc7eSAndroid Build Coastguard Worker #define MAX_MSGSIZE	8192
14*49cdfc7eSAndroid Build Coastguard Worker #define MSG_LENGTH	10
15*49cdfc7eSAndroid Build Coastguard Worker #define QUEUE_NAME	"/test_mqueue"
16*49cdfc7eSAndroid Build Coastguard Worker #define QUEUE_NAME_NONBLOCK	"/test_mqueue_nonblock"
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker static char smsg[MAX_MSGSIZE];
19*49cdfc7eSAndroid Build Coastguard Worker static struct sigaction act;
20*49cdfc7eSAndroid Build Coastguard Worker 
cleanup_common(void)21*49cdfc7eSAndroid Build Coastguard Worker static void cleanup_common(void)
22*49cdfc7eSAndroid Build Coastguard Worker {
23*49cdfc7eSAndroid Build Coastguard Worker 	if (fd_root > 0)
24*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd_root);
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > 0)
27*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker 	if (fd_nonblock > 0)
30*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd_nonblock);
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	mq_unlink(QUEUE_NAME);
33*49cdfc7eSAndroid Build Coastguard Worker 	mq_unlink(QUEUE_NAME_NONBLOCK);
34*49cdfc7eSAndroid Build Coastguard Worker }
35*49cdfc7eSAndroid Build Coastguard Worker 
sighandler(int sig LTP_ATTRIBUTE_UNUSED)36*49cdfc7eSAndroid Build Coastguard Worker static void sighandler(int sig LTP_ATTRIBUTE_UNUSED) { }
37*49cdfc7eSAndroid Build Coastguard Worker 
setup_common(void)38*49cdfc7eSAndroid Build Coastguard Worker static void setup_common(void)
39*49cdfc7eSAndroid Build Coastguard Worker {
40*49cdfc7eSAndroid Build Coastguard Worker 	int i;
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 	act.sa_handler = sighandler;
43*49cdfc7eSAndroid Build Coastguard Worker 	sigaction(SIGINT, &act, NULL);
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	cleanup_common();
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	fd_root = SAFE_OPEN("/", O_RDONLY);
48*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_MQ_OPEN(QUEUE_NAME, O_CREAT | O_EXCL | O_RDWR, 0700, NULL);
49*49cdfc7eSAndroid Build Coastguard Worker 	fd_nonblock = SAFE_MQ_OPEN(QUEUE_NAME_NONBLOCK, O_CREAT | O_EXCL | O_RDWR |
50*49cdfc7eSAndroid Build Coastguard Worker 		O_NONBLOCK, 0700, NULL);
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < MAX_MSGSIZE; i++)
53*49cdfc7eSAndroid Build Coastguard Worker 		smsg[i] = i;
54*49cdfc7eSAndroid Build Coastguard Worker }
55*49cdfc7eSAndroid Build Coastguard Worker 
cleanup_queue(mqd_t fd)56*49cdfc7eSAndroid Build Coastguard Worker static void cleanup_queue(mqd_t fd)
57*49cdfc7eSAndroid Build Coastguard Worker {
58*49cdfc7eSAndroid Build Coastguard Worker 	int i;
59*49cdfc7eSAndroid Build Coastguard Worker 	struct mq_attr mqstat;
60*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int prio;
61*49cdfc7eSAndroid Build Coastguard Worker 	char rmsg[MAX_MSGSIZE];
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	memset(&mqstat, 0, sizeof(mqstat));
64*49cdfc7eSAndroid Build Coastguard Worker 	if (mq_getattr(fd, &mqstat) == -1) {
65*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK|TERRNO, "mq_getattr() failed");
66*49cdfc7eSAndroid Build Coastguard Worker 		return;
67*49cdfc7eSAndroid Build Coastguard Worker 	}
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < mqstat.mq_curmsgs; i++) {
70*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "receive %d/%ld message", i + 1, mqstat.mq_curmsgs);
71*49cdfc7eSAndroid Build Coastguard Worker 		mq_receive(fd, rmsg, MAX_MSGSIZE, &prio);
72*49cdfc7eSAndroid Build Coastguard Worker 	}
73*49cdfc7eSAndroid Build Coastguard Worker }
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker #endif /* MQ_H */
76