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