Sync usage with man page.
[netbsd-mini2440.git] / regress / sys / kern / mqueue / mq_prio_test.c
blob0381e0a5ff73f315bb7a02cc7433d7474efda6ff
1 /* $NetBSD: mq_prio_test.c,v 1.1 2009/07/13 02:55:42 rmind Exp $ */
3 /*
4 * Test for POSIX message queue priority handling.
6 * This file is in the Public Domain.
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <err.h>
13 #include <mqueue.h>
14 #include <assert.h>
16 #define MQUEUE "/mq_prio_test"
17 #define MQ_PRIO_BOUNDARY 31
19 static void
20 send_msgs(mqd_t mqfd)
22 char msg[2];
24 msg[1] = '\0';
26 msg[0] = 'a';
27 if (mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BOUNDARY) == -1)
28 err(EXIT_FAILURE, "mq_send 1");
30 msg[0] = 'b';
31 if (mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BOUNDARY + 1) == -1)
32 err(EXIT_FAILURE, "mq_send 2");
34 msg[0] = 'c';
35 if (mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BOUNDARY) == -1)
36 err(EXIT_FAILURE, "mq_send 3");
38 msg[0] = 'd';
39 if (mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BOUNDARY - 1) == -1)
40 err(EXIT_FAILURE, "mq_send 4");
42 msg[0] = 'e';
43 if (mq_send(mqfd, msg, sizeof(msg), 0) == -1)
44 err(EXIT_FAILURE, "mq_send 5");
46 msg[0] = 'f';
47 if (mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BOUNDARY + 1) == -1)
48 err(EXIT_FAILURE, "mq_send 6");
51 static void
52 receive_msgs(mqd_t mqfd)
54 struct mq_attr mqa;
55 char *m;
56 unsigned p;
57 int len;
59 if (mq_getattr(mqfd, &mqa) == -1) {
60 err(EXIT_FAILURE, "mq_getattr");
62 len = mqa.mq_msgsize;
63 m = calloc(1, len);
64 if (m == NULL) {
65 err(EXIT_FAILURE, "calloc");
68 if (mq_receive(mqfd, m, len, &p) == -1) {
69 err(EXIT_FAILURE, "mq_receive 1");
71 assert(p == (MQ_PRIO_BOUNDARY + 1) && m[0] == 'b');
73 if (mq_receive(mqfd, m, len, &p) == -1) {
74 err(EXIT_FAILURE, "mq_receive 2");
76 assert(p == (MQ_PRIO_BOUNDARY + 1) && m[0] == 'f');
78 if (mq_receive(mqfd, m, len, &p) == -1) {
79 err(EXIT_FAILURE, "mq_receive 3");
81 assert(p == MQ_PRIO_BOUNDARY && m[0] == 'a');
83 if (mq_receive(mqfd, m, len, &p) == -1) {
84 err(EXIT_FAILURE, "mq_receive 4");
86 assert(p == MQ_PRIO_BOUNDARY && m[0] == 'c');
88 if (mq_receive(mqfd, m, len, &p) == -1) {
89 err(EXIT_FAILURE, "mq_receive 5");
91 assert(p == (MQ_PRIO_BOUNDARY - 1) && m[0] == 'd');
93 if (mq_receive(mqfd, m, len, &p) == -1) {
94 err(EXIT_FAILURE, "mq_receive 6");
96 assert(p == 0 && m[0] == 'e');
98 free(m);
102 main(int argc, char **argv)
104 mqd_t mqfd;
106 (void)mq_unlink(MQUEUE);
108 mqfd = mq_open(MQUEUE, O_RDWR | O_CREAT,
109 S_IRUSR | S_IRWXG | S_IROTH, NULL);
110 if (mqfd == -1)
111 err(EXIT_FAILURE, "mq_open");
113 send_msgs(mqfd);
114 receive_msgs(mqfd);
116 mq_close(mqfd);
118 if (mq_unlink(MQUEUE) == -1)
119 err(EXIT_FAILURE, "mq_unlink");
121 return 0;