1 /*
2  * Copyright (c) 2003, Intel Corporation. All rights reserved.
3  * Created by:  julie.n.fleischer REMOVE-THIS AT intel DOT com
4  * This file is licensed under the GPL license.  For the full content
5  * of this license, see the COPYING file at the top level of this
6  * source tree.
7  */
8 
9 /*
10  * Test that mq_timedsend() inserts messages into the message queue according
11  * to the priority given.  Specifically, test that messages with equal
12  * priority values are placed after previously sent messages.
13  *
14  * Test similar to 3-1.c; however, messages 3 and 4 will have equal priority
15  * order, but 3 will be sent first.
16  *
17  * 3/13/03 - Added fix from Gregoire Pichon for specifying an attr
18  *           with a mq_maxmsg >= BUFFER.
19  *
20  */
21 
22 #include <stdio.h>
23 #include <mqueue.h>
24 #include <fcntl.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <time.h>
30 #include "posixtest.h"
31 
32 #define NAMESIZE 50
33 #define MSG1 "1234567890"
34 #define PRI1 10
35 #define MSG2 "234567890"
36 #define PRI2 8
37 #define MSG3 "34567890"
38 #define PRI3 2
39 #define MSG4 "4567890"
40 #define PRI4 2
41 #define MSG5 "567890"
42 #define PRI5 1
43 #define BUFFER 40
44 #define MAXMSG 10
45 
main()46 int main()
47 {
48         char qname[NAMESIZE], msgrcd[BUFFER];
49         const char *msgptr1 = MSG1;
50         const char *msgptr2 = MSG2;
51         const char *msgptr3 = MSG3;
52         const char *msgptr4 = MSG4;
53         const char *msgptr5 = MSG5;
54 	struct timespec ts;
55         mqd_t queue;
56 	struct mq_attr attr;
57 	int unresolved=0, failure=0;
58 	unsigned pri;
59 
60         sprintf(qname, "/mq_timedsend_3-2_%d", getpid());
61 
62 	attr.mq_msgsize = BUFFER;
63 	attr.mq_maxmsg = BUFFER;
64         queue = mq_open(qname, O_CREAT |O_RDWR, S_IRUSR | S_IWUSR, &attr);
65         if (queue == (mqd_t)-1) {
66                 perror("mq_open() did not return success");
67                 return PTS_UNRESOLVED;
68         }
69 
70 	ts.tv_sec=time(NULL)+1;
71 	ts.tv_nsec=0;
72         if (mq_timedsend(queue, msgptr3, strlen(msgptr3), PRI3, &ts) != 0) {
73                 perror("mq_timedsend() did not return success");
74 		printf("error sending %s\n", msgptr3);
75 		failure=1;
76         }
77 
78 	ts.tv_sec++;
79         if (mq_timedsend(queue, msgptr1, strlen(msgptr1), PRI1, &ts) != 0) {
80                 perror("mq_timedsend() did not return success");
81 		printf("error sending %s\n", msgptr1);
82 		failure=1;
83         }
84 
85 	ts.tv_sec++;
86         if (mq_timedsend(queue, msgptr4, strlen(msgptr4), PRI4, &ts) != 0) {
87                 perror("mq_timedsend() did not return success");
88 		printf("error sending %s\n", msgptr4);
89 		failure=1;
90         }
91 
92 	ts.tv_sec++;
93         if (mq_timedsend(queue, msgptr2, strlen(msgptr2), PRI2, &ts) != 0) {
94                 perror("mq_timedsend() did not return success");
95 		printf("error sending %s\n", msgptr2);
96 		failure=1;
97         }
98 
99 	ts.tv_sec++;
100         if (mq_timedsend(queue, msgptr5, strlen(msgptr5), PRI5, &ts) != 0) {
101                 perror("mq_timedsend() did not return success");
102 		printf("error sending %s\n", msgptr5);
103 		failure=1;
104         }
105 
106         if (mq_receive(queue, msgrcd, BUFFER, &pri) == -1) {
107 		perror("mq_receive() returned failure");
108 		unresolved=1;
109 	}
110 
111 	if (strncmp(msgptr1, msgrcd, strlen(msgptr1)) != 0) {
112 		printf("FAIL:  sent %s received %s\n", msgptr1, msgrcd);
113 		failure = 1;
114 	}
115 
116         if (mq_receive(queue, msgrcd, BUFFER, &pri) == -1) {
117 		perror("mq_receive() returned failure");
118 		unresolved=1;
119 	}
120 
121 	if (strncmp(msgptr2, msgrcd, strlen(msgptr2)) != 0) {
122 		printf("FAIL:  sent %s received %s\n", msgptr2, msgrcd);
123 		failure = 1;
124 	}
125 
126         if (mq_receive(queue, msgrcd, BUFFER, &pri) == -1) {
127 		perror("mq_receive() returned failure");
128 		unresolved=1;
129 	}
130 
131 	if (strncmp(msgptr3, msgrcd, strlen(msgptr3)) != 0) {
132 		printf("FAIL:  sent %s received %s\n", msgptr3, msgrcd);
133 		failure = 1;
134 	}
135 
136         if (mq_receive(queue, msgrcd, BUFFER, &pri) == -1) {
137 		perror("mq_receive() returned failure");
138 		unresolved=1;
139 	}
140 
141 	if (strncmp(msgptr4, msgrcd, strlen(msgptr4)) != 0) {
142 		printf("FAIL:  sent %s received %s\n", msgptr4, msgrcd);
143 		failure = 1;
144 	}
145 
146         if (mq_receive(queue, msgrcd, BUFFER, &pri) == -1) {
147 		perror("mq_receive() returned failure");
148 		unresolved=1;
149 	}
150 
151 	if (strncmp(msgptr5, msgrcd, strlen(msgptr5)) != 0) {
152 		printf("FAIL:  sent %s received %s\n", msgptr5, msgrcd);
153 		failure = 1;
154 	}
155 
156         if (mq_close(queue) != 0) {
157 		perror("mq_close() did not return success");
158 		unresolved=1;
159         }
160 
161         if (mq_unlink(qname) != 0) {
162 		perror("mq_unlink() did not return success");
163 		unresolved=1;
164         }
165 
166 	if (failure==1) {
167 		printf("Test FAILED\n");
168 		return PTS_FAIL;
169 	}
170 
171 	if (unresolved==1) {
172 		printf("Test UNRESOLVED\n");
173 		return PTS_UNRESOLVED;
174 	}
175 
176         printf("Test PASSED\n");
177         return PTS_PASS;
178 }
179 
180