xref: /freebsd/contrib/netbsd-tests/kernel/t_mqueue.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1 /*	$NetBSD: t_mqueue.c,v 1.6 2017/01/14 20:57:24 christos Exp $ */
2 
3 /*
4  * Test for POSIX message queue priority handling.
5  *
6  * This file is in the Public Domain.
7  */
8 
9 #include <sys/stat.h>
10 
11 #include <atf-c.h>
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <mqueue.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19 
20 #ifdef __FreeBSD__
21 #include "freebsd_test_suite/macros.h"
22 #endif
23 
24 #define	MQ_PRIO_BASE	24
25 
26 static void
27 send_msgs(mqd_t mqfd)
28 {
29 	char msg[2];
30 
31 	msg[1] = '\0';
32 
33 	msg[0] = 'a';
34 	ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE) != -1,
35 	    "mq_send 1 failed: %d", errno);
36 
37 	msg[0] = 'b';
38 	ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE + 1) != -1,
39 	    "mq_send 2 failed: %d", errno);
40 
41 	msg[0] = 'c';
42 	ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE) != -1,
43 	    "mq_send 3 failed: %d", errno);
44 
45 	msg[0] = 'd';
46 	ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE - 1) != -1,
47 	    "mq_send 4 failed: %d", errno);
48 
49 	msg[0] = 'e';
50 	ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), 0) != -1,
51 	    "mq_send 5 failed: %d", errno);
52 
53 	msg[0] = 'f';
54 	ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE + 1) != -1,
55 	    "mq_send 6 failed: %d", errno);
56 }
57 
58 static void
59 receive_msgs(mqd_t mqfd)
60 {
61 	struct mq_attr mqa;
62 	char *m;
63 	unsigned p;
64 	int len;
65 
66 	ATF_REQUIRE_MSG(mq_getattr(mqfd, &mqa) != -1, "mq_getattr failed %d",
67 	    errno);
68 
69 	len = mqa.mq_msgsize;
70 	m = calloc(1, len);
71 	ATF_REQUIRE_MSG(m != NULL, "calloc failed");
72 
73 	ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
74 	    "mq_receive 1 failed: %d", errno);
75 	ATF_REQUIRE_MSG(p == (MQ_PRIO_BASE + 1) && m[0] == 'b',
76 	    "mq_receive 1 prio/data mismatch");
77 
78 	ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
79 	    "mq_receive 2 failed: %d", errno);
80 	ATF_REQUIRE_MSG(p == (MQ_PRIO_BASE + 1) && m[0] == 'f',
81 	    "mq_receive 2 prio/data mismatch");
82 
83 	ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
84 	    "mq_receive 3 failed: %d", errno);
85 	ATF_REQUIRE_MSG(p == MQ_PRIO_BASE && m[0] == 'a',
86 	    "mq_receive 3 prio/data mismatch");
87 
88 	ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
89 	    "mq_receive 4 failed: %d", errno);
90 	ATF_REQUIRE_MSG(p == MQ_PRIO_BASE && m[0] == 'c',
91 	    "mq_receive 4 prio/data mismatch");
92 
93 	ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
94 	    "mq_receive 5 failed: %d", errno);
95 	ATF_REQUIRE_MSG(p == (MQ_PRIO_BASE - 1) && m[0] == 'd',
96 	    "mq_receive 5 prio/data mismatch");
97 
98 	ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
99 	    "mq_receive 6 failed: %d", errno);
100 	ATF_REQUIRE_MSG(p == 0 && m[0] == 'e',
101 	    "mq_receive 6 prio/data mismatch");
102 }
103 
104 ATF_TC(mqueue);
105 ATF_TC_HEAD(mqueue, tc)
106 {
107 
108 	atf_tc_set_md_var(tc, "timeout", "3");
109 	atf_tc_set_md_var(tc, "descr", "Checks mqueue send/receive");
110 }
111 
112 ATF_TC_BODY(mqueue, tc)
113 {
114 	int status;
115 	char *tmpdir;
116 	char template[32];
117 	char mq_name[64];
118 
119 #ifdef __FreeBSD__
120 	ATF_REQUIRE_KERNEL_MODULE("mqueuefs");
121 #endif
122 
123 	strlcpy(template, "./t_mqueue.XXXXXX", sizeof(template));
124 	tmpdir = mkdtemp(template);
125 	ATF_REQUIRE_MSG(tmpdir != NULL, "mkdtemp failed: %d", errno);
126 #ifdef __FreeBSD__
127 	snprintf(mq_name, sizeof(mq_name), "/t_mqueue");
128 #else
129 	snprintf(mq_name, sizeof(mq_name), "%s/mq", tmpdir);
130 #endif
131 
132 	mqd_t mqfd;
133 
134 	mqfd = mq_open(mq_name, O_RDWR | O_CREAT,
135 	    S_IRUSR | S_IRWXG | S_IROTH, NULL);
136 #ifdef __FreeBSD__
137 	ATF_REQUIRE_MSG(mqfd != (mqd_t)-1, "mq_open failed: %d", errno);
138 #else
139 	ATF_REQUIRE_MSG(mqfd != -1, "mq_open failed: %d", errno);
140 #endif
141 
142 	send_msgs(mqfd);
143 	receive_msgs(mqfd);
144 
145 	status = mq_close(mqfd);
146 	ATF_REQUIRE_MSG(status == 0, "mq_close failed: %d", errno);
147 }
148 
149 ATF_TP_ADD_TCS(tp)
150 {
151 	ATF_TP_ADD_TC(tp, mqueue);
152 
153 	return atf_no_error();
154 }
155