1 /* Test message queue passing.
2 Copyright (C) 2004-2018 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/>. */
33 #include "tst-mqueue.h"
36 #define TEST_FUNCTION do_test ()
42 char name
[sizeof "/tst-mqueue4-" + sizeof (pid_t
) * 3 + NAME_MAX
];
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
);
50 printf ("mq_open failed with: %m\n");
57 memset (p
+ 1, 'x', NAME_MAX
+ 1 - (p
- name
));
58 name
[NAME_MAX
+ 1] = '\0';
60 mqd_t q2
= mq_open (name
, O_CREAT
| O_EXCL
| O_RDWR
, 0600, &attr
);
63 printf ("mq_open with NAME_MAX long name compoment failed with: %m\n");
67 if (mq_unlink (name
) != 0)
69 printf ("mq_unlink failed: %m\n");
73 if (mq_close (q2
) != 0)
75 printf ("mq_close failed: %m\n");
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
);
84 puts ("mq_open with too long name component unexpectedly succeeded");
89 else if (errno
!= ENAMETOOLONG
)
91 printf ("mq_open with too long name component did not fail with "
92 "ENAMETOOLONG: %m\n");
96 if (mq_unlink (name
) == 0)
98 puts ("mq_unlink with too long name component unexpectedly succeeded");
101 else if (errno
!= ENAMETOOLONG
)
103 printf ("mq_unlink with too long name component did not fail with "
104 "ENAMETOOLONG: %m\n");
111 q2
= mq_open (name
, O_CREAT
| O_RDWR
, 0600, &attr
);
112 if (q2
== (mqd_t
) -1)
114 printf ("mq_open without O_EXCL failed with %m\n");
120 if (mq_send (q
, buf
, 2, 4) != 0)
122 printf ("mq_send failed: %m\n");
126 if (mq_send (q
, buf
+ 1, 1, 5) != 0)
128 printf ("mq_send failed: %m\n");
132 if (mq_getattr (q2
, &attr
) != 0)
134 printf ("mq_getattr failed: %m\n");
138 if ((attr
.mq_flags
& O_NONBLOCK
)
139 || attr
.mq_maxmsg
!= 2
140 || attr
.mq_msgsize
!= 2
141 || attr
.mq_curmsgs
!= 2)
143 printf ("mq_getattr returned unexpected { .mq_flags = %jd,\n"
144 ".mq_maxmsg = %jd, .mq_msgsize = %jd, .mq_curmsgs = %jd }\n",
145 (intmax_t) attr
.mq_flags
, (intmax_t) attr
.mq_maxmsg
,
146 (intmax_t) attr
.mq_msgsize
, (intmax_t) attr
.mq_curmsgs
);
151 if (clock_gettime (CLOCK_REALTIME
, &ts
) == 0)
155 ts
.tv_sec
= time (NULL
) + 1;
159 if (mq_timedsend (q2
, buf
, 1, 1, &ts
) == 0)
161 puts ("mq_timedsend unexpectedly succeeded");
164 else if (errno
!= ETIMEDOUT
)
166 printf ("mq_timedsend did not fail with ETIMEDOUT: %m\n");
170 if (mq_close (q2
) != 0)
172 printf ("mq_close failed: %m\n");
176 q2
= mq_open (name
, O_RDONLY
, 0600);
177 if (q2
== (mqd_t
) -1)
179 printf ("mq_open without O_CREAT failed with %m\n");
183 mqd_t q3
= mq_open (name
, O_RDONLY
, 0600);
184 if (q3
== (mqd_t
) -1)
186 printf ("mq_open without O_CREAT failed with %m\n");
190 memset (buf
, ' ', sizeof (buf
));
193 ssize_t rets
= mq_receive (q2
, buf
, 2, &prio
);
197 printf ("mq_receive failed with: %m\n");
199 printf ("mq_receive returned %zd != 1\n", rets
);
202 else if (prio
!= 5 || memcmp (buf
, "k ", 3) != 0)
204 printf ("mq_receive returned prio %u (2) buf \"%c%c%c\" (\"k \")\n",
205 prio
, buf
[0], buf
[1], buf
[2]);
209 if (mq_getattr (q3
, &attr
) != 0)
211 printf ("mq_getattr failed: %m\n");
215 if ((attr
.mq_flags
& O_NONBLOCK
)
216 || attr
.mq_maxmsg
!= 2
217 || attr
.mq_msgsize
!= 2
218 || attr
.mq_curmsgs
!= 1)
220 printf ("mq_getattr returned unexpected { .mq_flags = %jd,\n"
221 ".mq_maxmsg = %jd, .mq_msgsize = %jd, .mq_curmsgs = %jd }\n",
222 (intmax_t) attr
.mq_flags
, (intmax_t) attr
.mq_maxmsg
,
223 (intmax_t) attr
.mq_msgsize
, (intmax_t) attr
.mq_curmsgs
);
227 rets
= mq_receive (q3
, buf
, 2, NULL
);
231 printf ("mq_receive failed with: %m\n");
233 printf ("mq_receive returned %zd != 2\n", rets
);
236 else if (memcmp (buf
, "jk ", 3) != 0)
238 printf ("mq_receive returned buf \"%c%c%c\" != \"jk \"\n",
239 buf
[0], buf
[1], buf
[2]);
243 if (clock_gettime (CLOCK_REALTIME
, &ts
) == 0)
247 ts
.tv_sec
= time (NULL
) + 1;
251 if (mq_timedreceive (q2
, buf
, 2, NULL
, &ts
) != -1)
253 puts ("mq_timedreceive on empty queue unexpectedly succeeded");
256 else if (errno
!= ETIMEDOUT
)
258 printf ("mq_timedreceive on empty queue did not fail with "
263 if (mq_unlink (name
) != 0)
265 printf ("mq_unlink failed: %m\n");
269 if (mq_close (q
) != 0)
271 printf ("mq_close failed: %m\n");
275 if (mq_close (q2
) != 0)
277 printf ("mq_close failed: %m\n");
281 if (mq_close (q3
) != 0)
283 printf ("mq_close failed: %m\n");
290 #include "../test-skeleton.c"