1 /* Test message queue passing.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
32 #include "tst-mqueue.h"
35 #define TEST_FUNCTION do_test ()
41 char name
[sizeof "/tst-mqueue4-" + sizeof (pid_t
) * 3 + NAME_MAX
];
43 p
= name
+ snprintf (name
, sizeof (name
), "/tst-mqueue4-%u", getpid ());
44 struct mq_attr attr
= { .mq_maxmsg
= 2, .mq_msgsize
= 2 };
45 mqd_t q
= mq_open (name
, O_CREAT
| O_EXCL
| O_RDWR
, 0600, &attr
);
49 printf ("mq_open failed with: %m\n");
56 memset (p
+ 1, 'x', NAME_MAX
+ 1 - (p
- name
));
57 name
[NAME_MAX
+ 1] = '\0';
59 mqd_t q2
= mq_open (name
, O_CREAT
| O_EXCL
| O_RDWR
, 0600, &attr
);
62 printf ("mq_open with NAME_MAX long name compoment failed with: %m\n");
66 if (mq_unlink (name
) != 0)
68 printf ("mq_unlink failed: %m\n");
72 if (mq_close (q2
) != 0)
74 printf ("mq_close failed: %m\n");
78 name
[NAME_MAX
+ 1] = 'x';
79 name
[NAME_MAX
+ 2] = '\0';
80 q2
= mq_open (name
, O_CREAT
| O_EXCL
| O_RDWR
, 0600, &attr
);
83 puts ("mq_open with too long name component unexpectedly succeeded");
88 else if (errno
!= ENAMETOOLONG
)
90 printf ("mq_open with too long name component did not fail with "
91 "ENAMETOOLONG: %m\n");
95 if (mq_unlink (name
) == 0)
97 puts ("mq_unlink with too long name component unexpectedly succeeded");
100 else if (errno
!= ENAMETOOLONG
)
102 printf ("mq_unlink with too long name component did not fail with "
103 "ENAMETOOLONG: %m\n");
110 q2
= mq_open (name
, O_CREAT
| O_RDWR
, 0600, &attr
);
111 if (q2
== (mqd_t
) -1)
113 printf ("mq_open without O_EXCL failed with %m\n");
119 if (mq_send (q
, buf
, 2, 4) != 0)
121 printf ("mq_send failed: %m\n");
125 if (mq_send (q
, buf
+ 1, 1, 5) != 0)
127 printf ("mq_send failed: %m\n");
131 if (mq_getattr (q2
, &attr
) != 0)
133 printf ("mq_getattr failed: %m\n");
137 if ((attr
.mq_flags
& O_NONBLOCK
)
138 || attr
.mq_maxmsg
!= 2
139 || attr
.mq_msgsize
!= 2
140 || attr
.mq_curmsgs
!= 2)
142 printf ("mq_getattr returned unexpected { .mq_flags = %ld,\n"
143 ".mq_maxmsg = %ld, .mq_msgsize = %ld, .mq_curmsgs = %ld }\n",
144 attr
.mq_flags
, attr
.mq_maxmsg
, attr
.mq_msgsize
, attr
.mq_curmsgs
);
149 if (clock_gettime (CLOCK_REALTIME
, &ts
) == 0)
153 ts
.tv_sec
= time (NULL
) + 1;
157 if (mq_timedsend (q2
, buf
, 1, 1, &ts
) == 0)
159 puts ("mq_timedsend unexpectedly succeeded");
162 else if (errno
!= ETIMEDOUT
)
164 printf ("mq_timedsend did not fail with ETIMEDOUT: %m\n");
168 if (mq_close (q2
) != 0)
170 printf ("mq_close failed: %m\n");
174 q2
= mq_open (name
, O_RDONLY
, 0600);
175 if (q2
== (mqd_t
) -1)
177 printf ("mq_open without O_CREAT failed with %m\n");
181 mqd_t q3
= mq_open (name
, O_RDONLY
, 0600);
182 if (q3
== (mqd_t
) -1)
184 printf ("mq_open without O_CREAT failed with %m\n");
188 memset (buf
, ' ', sizeof (buf
));
191 ssize_t rets
= mq_receive (q2
, buf
, 2, &prio
);
195 printf ("mq_receive failed with: %m\n");
197 printf ("mq_receive returned %zd != 1\n", rets
);
200 else if (prio
!= 5 || memcmp (buf
, "k ", 3) != 0)
202 printf ("mq_receive returned prio %u (2) buf \"%c%c%c\" (\"k \")\n",
203 prio
, buf
[0], buf
[1], buf
[2]);
207 if (mq_getattr (q3
, &attr
) != 0)
209 printf ("mq_getattr failed: %m\n");
213 if ((attr
.mq_flags
& O_NONBLOCK
)
214 || attr
.mq_maxmsg
!= 2
215 || attr
.mq_msgsize
!= 2
216 || attr
.mq_curmsgs
!= 1)
218 printf ("mq_getattr returned unexpected { .mq_flags = %ld,\n"
219 ".mq_maxmsg = %ld, .mq_msgsize = %ld, .mq_curmsgs = %ld }\n",
220 attr
.mq_flags
, attr
.mq_maxmsg
, attr
.mq_msgsize
, attr
.mq_curmsgs
);
224 rets
= mq_receive (q3
, buf
, 2, NULL
);
228 printf ("mq_receive failed with: %m\n");
230 printf ("mq_receive returned %zd != 2\n", rets
);
233 else if (memcmp (buf
, "jk ", 3) != 0)
235 printf ("mq_receive returned buf \"%c%c%c\" != \"jk \"\n",
236 buf
[0], buf
[1], buf
[2]);
240 if (clock_gettime (CLOCK_REALTIME
, &ts
) == 0)
244 ts
.tv_sec
= time (NULL
) + 1;
248 if (mq_timedreceive (q2
, buf
, 2, NULL
, &ts
) != -1)
250 puts ("mq_timedreceive on empty queue unexpectedly succeeded");
253 else if (errno
!= ETIMEDOUT
)
255 printf ("mq_timedreceive on empty queue did not fail with "
260 if (mq_unlink (name
) != 0)
262 printf ("mq_unlink failed: %m\n");
266 if (mq_close (q
) != 0)
268 printf ("mq_close failed: %m\n");
272 if (mq_close (q2
) != 0)
274 printf ("mq_close failed: %m\n");
278 if (mq_close (q3
) != 0)
280 printf ("mq_close failed: %m\n");
287 #include "../test-skeleton.c"