• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 Petr Vorel <pvorel@suse.cz>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef MQ_H
19 #define MQ_H
20 
21 #include "tst_test.h"
22 #include "tst_sig_proc.h"
23 #include "tst_safe_posix_ipc.h"
24 
25 #define MAX_MSGSIZE	8192
26 #define MSG_LENGTH	10
27 #define QUEUE_NAME	"/test_mqueue"
28 #define QUEUE_NAME_NONBLOCK	"/test_mqueue_nonblock"
29 
30 static char smsg[MAX_MSGSIZE];
31 static struct sigaction act;
32 
cleanup_common(void)33 static void cleanup_common(void)
34 {
35 	if (fd_root > 0)
36 		SAFE_CLOSE(fd_root);
37 
38 	if (fd > 0)
39 		SAFE_CLOSE(fd);
40 
41 	if (fd_nonblock > 0)
42 		SAFE_CLOSE(fd_nonblock);
43 
44 	mq_unlink(QUEUE_NAME);
45 	mq_unlink(QUEUE_NAME_NONBLOCK);
46 }
47 
sighandler(int sig LTP_ATTRIBUTE_UNUSED)48 static void sighandler(int sig LTP_ATTRIBUTE_UNUSED) { }
49 
setup_common(void)50 static void setup_common(void)
51 {
52 	int i;
53 
54 	act.sa_handler = sighandler;
55 	sigaction(SIGINT, &act, NULL);
56 
57 	cleanup_common();
58 
59 	fd_root = SAFE_OPEN("/", O_RDONLY);
60 	fd = SAFE_MQ_OPEN(QUEUE_NAME, O_CREAT | O_EXCL | O_RDWR, 0700, NULL);
61 	fd_nonblock = SAFE_MQ_OPEN(QUEUE_NAME_NONBLOCK, O_CREAT | O_EXCL | O_RDWR |
62 		O_NONBLOCK, 0700, NULL);
63 
64 	for (i = 0; i < MAX_MSGSIZE; i++)
65 		smsg[i] = i;
66 }
67 
cleanup_queue(mqd_t fd)68 static void cleanup_queue(mqd_t fd)
69 {
70 	int i;
71 	struct mq_attr mqstat;
72 	unsigned int prio;
73 	char rmsg[MAX_MSGSIZE];
74 
75 	memset(&mqstat, 0, sizeof(mqstat));
76 	if (mq_getattr(fd, &mqstat) == -1) {
77 		tst_brk(TBROK|TERRNO, "mq_getattr() failed");
78 		return;
79 	}
80 
81 	for (i = 0; i < mqstat.mq_curmsgs; i++) {
82 		tst_res(TINFO, "receive %d/%ld message", i + 1, mqstat.mq_curmsgs);
83 		mq_receive(fd, rmsg, MAX_MSGSIZE, &prio);
84 	}
85 }
86 
87 #endif /* MQ_H */
88