1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_mqueue.c,v 1.4 2014/03/02 19:56:48 jmmv Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*
411be35a1SLionel Sambuc * Test for POSIX message queue priority handling.
511be35a1SLionel Sambuc *
611be35a1SLionel Sambuc * This file is in the Public Domain.
711be35a1SLionel Sambuc */
811be35a1SLionel Sambuc
911be35a1SLionel Sambuc #include <atf-c.h>
1011be35a1SLionel Sambuc
1111be35a1SLionel Sambuc #include <stdio.h>
1211be35a1SLionel Sambuc #include <stdlib.h>
1311be35a1SLionel Sambuc #include <string.h>
1411be35a1SLionel Sambuc #include <errno.h>
1511be35a1SLionel Sambuc #include <unistd.h>
1611be35a1SLionel Sambuc
1711be35a1SLionel Sambuc #include <mqueue.h>
1811be35a1SLionel Sambuc
1911be35a1SLionel Sambuc #define MQ_PRIO_BASE 24
2011be35a1SLionel Sambuc
2111be35a1SLionel Sambuc static void
send_msgs(mqd_t mqfd)2211be35a1SLionel Sambuc send_msgs(mqd_t mqfd)
2311be35a1SLionel Sambuc {
2411be35a1SLionel Sambuc char msg[2];
2511be35a1SLionel Sambuc
2611be35a1SLionel Sambuc msg[1] = '\0';
2711be35a1SLionel Sambuc
2811be35a1SLionel Sambuc msg[0] = 'a';
2911be35a1SLionel Sambuc ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE) != -1,
3011be35a1SLionel Sambuc "mq_send 1 failed: %d", errno);
3111be35a1SLionel Sambuc
3211be35a1SLionel Sambuc msg[0] = 'b';
3311be35a1SLionel Sambuc ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE + 1) != -1,
3411be35a1SLionel Sambuc "mq_send 2 failed: %d", errno);
3511be35a1SLionel Sambuc
3611be35a1SLionel Sambuc msg[0] = 'c';
3711be35a1SLionel Sambuc ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE) != -1,
3811be35a1SLionel Sambuc "mq_send 3 failed: %d", errno);
3911be35a1SLionel Sambuc
4011be35a1SLionel Sambuc msg[0] = 'd';
4111be35a1SLionel Sambuc ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE - 1) != -1,
4211be35a1SLionel Sambuc "mq_send 4 failed: %d", errno);
4311be35a1SLionel Sambuc
4411be35a1SLionel Sambuc msg[0] = 'e';
4511be35a1SLionel Sambuc ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), 0) != -1,
4611be35a1SLionel Sambuc "mq_send 5 failed: %d", errno);
4711be35a1SLionel Sambuc
4811be35a1SLionel Sambuc msg[0] = 'f';
4911be35a1SLionel Sambuc ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE + 1) != -1,
5011be35a1SLionel Sambuc "mq_send 6 failed: %d", errno);
5111be35a1SLionel Sambuc }
5211be35a1SLionel Sambuc
5311be35a1SLionel Sambuc static void
receive_msgs(mqd_t mqfd)5411be35a1SLionel Sambuc receive_msgs(mqd_t mqfd)
5511be35a1SLionel Sambuc {
5611be35a1SLionel Sambuc struct mq_attr mqa;
5711be35a1SLionel Sambuc char *m;
5811be35a1SLionel Sambuc unsigned p;
5911be35a1SLionel Sambuc int len;
6011be35a1SLionel Sambuc
6111be35a1SLionel Sambuc ATF_REQUIRE_MSG(mq_getattr(mqfd, &mqa) != -1, "mq_getattr failed %d",
6211be35a1SLionel Sambuc errno);
6311be35a1SLionel Sambuc
6411be35a1SLionel Sambuc len = mqa.mq_msgsize;
6511be35a1SLionel Sambuc m = calloc(1, len);
6611be35a1SLionel Sambuc ATF_REQUIRE_MSG(m != NULL, "calloc failed");
6711be35a1SLionel Sambuc
6811be35a1SLionel Sambuc ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
6911be35a1SLionel Sambuc "mq_receive 1 failed: %d", errno);
7011be35a1SLionel Sambuc ATF_REQUIRE_MSG(p == (MQ_PRIO_BASE + 1) && m[0] == 'b',
7111be35a1SLionel Sambuc "mq_receive 1 prio/data mismatch");
7211be35a1SLionel Sambuc
7311be35a1SLionel Sambuc ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
7411be35a1SLionel Sambuc "mq_receive 2 failed: %d", errno);
7511be35a1SLionel Sambuc ATF_REQUIRE_MSG(p == (MQ_PRIO_BASE + 1) && m[0] == 'f',
7611be35a1SLionel Sambuc "mq_receive 2 prio/data mismatch");
7711be35a1SLionel Sambuc
7811be35a1SLionel Sambuc ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
7911be35a1SLionel Sambuc "mq_receive 3 failed: %d", errno);
8011be35a1SLionel Sambuc ATF_REQUIRE_MSG(p == MQ_PRIO_BASE && m[0] == 'a',
8111be35a1SLionel Sambuc "mq_receive 3 prio/data mismatch");
8211be35a1SLionel Sambuc
8311be35a1SLionel Sambuc ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
8411be35a1SLionel Sambuc "mq_receive 4 failed: %d", errno);
8511be35a1SLionel Sambuc ATF_REQUIRE_MSG(p == MQ_PRIO_BASE && m[0] == 'c',
8611be35a1SLionel Sambuc "mq_receive 4 prio/data mismatch");
8711be35a1SLionel Sambuc
8811be35a1SLionel Sambuc ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
8911be35a1SLionel Sambuc "mq_receive 5 failed: %d", errno);
9011be35a1SLionel Sambuc ATF_REQUIRE_MSG(p == (MQ_PRIO_BASE - 1) && m[0] == 'd',
9111be35a1SLionel Sambuc "mq_receive 5 prio/data mismatch");
9211be35a1SLionel Sambuc
9311be35a1SLionel Sambuc ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
9411be35a1SLionel Sambuc "mq_receive 6 failed: %d", errno);
9511be35a1SLionel Sambuc ATF_REQUIRE_MSG(p == 0 && m[0] == 'e',
9611be35a1SLionel Sambuc "mq_receive 6 prio/data mismatch");
9711be35a1SLionel Sambuc }
9811be35a1SLionel Sambuc
99*0a6a1f1dSLionel Sambuc ATF_TC(mqueue);
ATF_TC_HEAD(mqueue,tc)10011be35a1SLionel Sambuc ATF_TC_HEAD(mqueue, tc)
10111be35a1SLionel Sambuc {
10211be35a1SLionel Sambuc
10311be35a1SLionel Sambuc atf_tc_set_md_var(tc, "timeout", "3");
10411be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks mqueue send/receive");
10511be35a1SLionel Sambuc }
10611be35a1SLionel Sambuc
ATF_TC_BODY(mqueue,tc)10711be35a1SLionel Sambuc ATF_TC_BODY(mqueue, tc)
10811be35a1SLionel Sambuc {
10911be35a1SLionel Sambuc int status;
110*0a6a1f1dSLionel Sambuc char *tmpdir;
11111be35a1SLionel Sambuc char template[32];
11211be35a1SLionel Sambuc char mq_name[64];
11311be35a1SLionel Sambuc
11411be35a1SLionel Sambuc strlcpy(template, "./t_mqueue.XXXXXX", sizeof(template));
11511be35a1SLionel Sambuc tmpdir = mkdtemp(template);
11611be35a1SLionel Sambuc ATF_REQUIRE_MSG(tmpdir != NULL, "mkdtemp failed: %d", errno);
11711be35a1SLionel Sambuc snprintf(mq_name, sizeof(mq_name), "%s/mq", tmpdir);
11811be35a1SLionel Sambuc
11911be35a1SLionel Sambuc mqd_t mqfd;
12011be35a1SLionel Sambuc
12111be35a1SLionel Sambuc mqfd = mq_open(mq_name, O_RDWR | O_CREAT,
12211be35a1SLionel Sambuc S_IRUSR | S_IRWXG | S_IROTH, NULL);
12311be35a1SLionel Sambuc ATF_REQUIRE_MSG(mqfd != -1, "mq_open failed: %d", errno);
12411be35a1SLionel Sambuc
12511be35a1SLionel Sambuc send_msgs(mqfd);
12611be35a1SLionel Sambuc receive_msgs(mqfd);
12711be35a1SLionel Sambuc
12811be35a1SLionel Sambuc status = mq_close(mqfd);
12911be35a1SLionel Sambuc ATF_REQUIRE_MSG(status == 0, "mq_close failed: %d", errno);
13011be35a1SLionel Sambuc }
13111be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)13211be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
13311be35a1SLionel Sambuc {
13411be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, mqueue);
13511be35a1SLionel Sambuc
13611be35a1SLionel Sambuc return atf_no_error();
13711be35a1SLionel Sambuc }
138