Update.
[platform/upstream/glibc.git] / rt / tst-mqueue4.c
1 /* Test message queue passing.
2    Copyright (C) 2004 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <mqueue.h>
24 #include <limits.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/time.h>
30 #include <sys/wait.h>
31 #include <time.h>
32 #include <unistd.h>
33 #include "tst-mqueue.h"
34
35 #define TIMEOUT 4
36 #define TEST_FUNCTION do_test ()
37 static int
38 do_test (void)
39 {
40   int result = 0;
41
42   char name[sizeof "/tst-mqueue4-" + sizeof (pid_t) * 3 + NAME_MAX];
43   char *p;
44   p = name + snprintf (name, sizeof (name), "/tst-mqueue4-%u", getpid ());
45   struct mq_attr attr = { .mq_maxmsg = 2, .mq_msgsize = 2 };
46   mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
47
48   if (q == (mqd_t) -1)
49     {
50       printf ("mq_open failed with: %m\n");
51       return result;
52     }
53   else
54     add_temp_mq (name);
55
56   *p = '.';
57   memset (p + 1, 'x', NAME_MAX + 1 - (p - name));
58   name[NAME_MAX + 1] = '\0';
59
60   mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
61   if (q2 == (mqd_t) -1)
62     {
63       printf ("mq_open with NAME_MAX long name compoment failed with: %m\n");
64       result = 1;
65     }
66
67   if (mq_unlink (name) != 0)
68     {
69       printf ("mq_unlink failed: %m\n");
70       result = 1;
71     }
72
73   if (mq_close (q2) != 0)
74     {
75       printf ("mq_close failed: %m\n");
76       result = 1;
77     }
78
79   name[NAME_MAX + 1] = 'x';
80   name[NAME_MAX + 2] = '\0';
81   q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
82   if (q2 != (mqd_t) -1)
83     {
84       puts ("mq_open with too long name component unexpectedly succeeded");
85       mq_unlink (name);
86       mq_close (q2);
87       result = 1;
88     }
89   else if (errno != ENAMETOOLONG)
90     {
91       printf ("mq_open with too long name component did not fail with "
92               "ENAMETOOLONG: %m\n");
93       result = 1;
94     }
95
96   *p = '\0';
97   attr.mq_maxmsg = 1;
98   attr.mq_msgsize = 3;
99   q2 = mq_open (name, O_CREAT | O_RDWR, 0600, &attr);
100   if (q2 == (mqd_t) -1)
101     {
102       printf ("mq_open without O_EXCL failed with %m\n");
103       result = 1;
104     }
105
106   char buf[3];
107   strcpy (buf, "jk");
108   if (mq_send (q, buf, 2, 4) != 0)
109     {
110       printf ("mq_send failed: %m\n");
111       result = 1;
112     }
113
114   if (mq_send (q, buf + 1, 1, 5) != 0)
115     {
116       printf ("mq_send failed: %m\n");
117       result = 1;
118     }
119
120   if (mq_getattr (q2, &attr) != 0)
121     {
122       printf ("mq_getattr failed: %m\n");
123       result = 1;
124     }
125
126   if ((attr.mq_flags & O_NONBLOCK)
127       || attr.mq_maxmsg != 2
128       || attr.mq_msgsize != 2
129       || attr.mq_curmsgs != 2)
130     {
131       printf ("mq_getattr returned unexpected { .mq_flags = %ld,\n"
132               ".mq_maxmsg = %ld, .mq_msgsize = %ld, .mq_curmsgs = %ld }\n",
133               attr.mq_flags, attr.mq_maxmsg, attr.mq_msgsize, attr.mq_curmsgs);
134       result = 1;
135     }
136
137   struct timespec ts;
138   if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
139     ++ts.tv_sec;
140   else
141     {
142       ts.tv_sec = time (NULL) + 1;
143       ts.tv_nsec = 0;
144     }
145
146   if (mq_timedsend (q2, buf, 1, 1, &ts) == 0)
147     {
148       puts ("mq_timedsend unexpectedly succeeded");
149       result = 1;
150     }
151   else if (errno != ETIMEDOUT)
152     {
153       printf ("mq_timedsend did not fail with ETIMEDOUT: %m\n");
154       result = 1;
155     }
156
157   if (mq_close (q2) != 0)
158     {
159       printf ("mq_close failed: %m\n");
160       result = 1;
161     }
162
163   q2 = mq_open (name, O_RDONLY, 0600);
164   if (q2 == (mqd_t) -1)
165     {
166       printf ("mq_open without O_CREAT failed with %m\n");
167       result = 1;
168     }
169
170   mqd_t q3 = mq_open (name, O_RDONLY, 0600);
171   if (q3 == (mqd_t) -1)
172     {
173       printf ("mq_open without O_CREAT failed with %m\n");
174       result = 1;
175     }
176
177   memset (buf, ' ', sizeof (buf));
178
179   unsigned int prio;
180   ssize_t rets = mq_receive (q2, buf, 2, &prio);
181   if (rets != 1)
182     {
183       if (rets == -1)
184         printf ("mq_receive failed with: %m\n");
185       else
186         printf ("mq_receive returned %zd != 1\n", rets);
187       result = 1;
188     }
189   else if (prio != 5 || memcmp (buf, "k  ", 3) != 0)
190     {
191       printf ("mq_receive returned prio %u (2) buf \"%c%c%c\" (\"k  \")\n",
192               prio, buf[0], buf[1], buf[2]);
193       result = 1;
194     }
195
196   if (mq_getattr (q3, &attr) != 0)
197     {
198       printf ("mq_getattr failed: %m\n");
199       result = 1;
200     }
201
202   if ((attr.mq_flags & O_NONBLOCK)
203       || attr.mq_maxmsg != 2
204       || attr.mq_msgsize != 2
205       || attr.mq_curmsgs != 1)
206     {
207       printf ("mq_getattr returned unexpected { .mq_flags = %ld,\n"
208               ".mq_maxmsg = %ld, .mq_msgsize = %ld, .mq_curmsgs = %ld }\n",
209               attr.mq_flags, attr.mq_maxmsg, attr.mq_msgsize, attr.mq_curmsgs);
210       result = 1;
211     }
212
213   rets = mq_receive (q3, buf, 2, NULL);
214   if (rets != 2)
215     {
216       if (rets == -1)
217         printf ("mq_receive failed with: %m\n");
218       else
219         printf ("mq_receive returned %zd != 2\n", rets);
220       result = 1;
221     }
222   else if (memcmp (buf, "jk ", 3) != 0)
223     {
224       printf ("mq_receive returned buf \"%c%c%c\" != \"jk \"\n",
225               buf[0], buf[1], buf[2]);
226       result = 1;
227     }
228
229   if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
230     ++ts.tv_sec;
231   else
232     {
233       ts.tv_sec = time (NULL) + 1;
234       ts.tv_nsec = 0;
235     }
236
237   if (mq_timedreceive (q2, buf, 2, NULL, &ts) != -1)
238     {
239       puts ("mq_timedreceive on empty queue unexpectedly succeeded");
240       result = 1;
241     }
242   else if (errno != ETIMEDOUT)
243     {
244       printf ("mq_timedreceive on empty queue did not fail with "
245               "ETIMEDOUT: %m\n");
246       result = 1;
247     }
248
249   if (mq_unlink (name) != 0)
250     {
251       printf ("mq_unlink failed: %m\n");
252       result = 1;
253     }
254
255   if (mq_close (q) != 0)
256     {
257       printf ("mq_close failed: %m\n");
258       result = 1;
259     }
260
261   if (mq_close (q2) != 0)
262     {
263       printf ("mq_close failed: %m\n");
264       result = 1;
265     }
266
267   if (mq_close (q3) != 0)
268     {
269       printf ("mq_close failed: %m\n");
270       result = 1;
271     }
272
273   return result;
274 }
275
276 #include "../test-skeleton.c"