2 Copyright (C) 2004-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
33 #include <support/check.h>
34 #include "tst-mqueue.h"
39 # define mqsend(q) (mqsend) (q, __LINE__)
41 (mqsend) (mqd_t q, int line)
44 if (mq_send (q, &c, 1, 1) != 0)
46 printf ("mq_send on line %d failed with: %m\n", line);
52 # define mqrecv(q) (mqrecv) (q, __LINE__)
54 (mqrecv) (mqd_t q, int line)
57 ssize_t rets = TEMP_FAILURE_RETRY (mq_receive (q, &c, 1, NULL));
61 printf ("mq_receive on line %d failed with: %m\n", line);
63 printf ("mq_receive on line %d returned %zd != 1\n",
70 volatile int fct_cnt, fct_err;
76 mqd_t q = *(mqd_t *) s.sival_ptr;
79 int ret = pthread_getattr_np (pthread_self (), &nattr);
83 printf ("pthread_getattr_np failed: %m\n");
88 ret = pthread_attr_getguardsize (&nattr, &fct_guardsize);
92 printf ("pthread_attr_getguardsize failed: %m\n");
95 if (pthread_attr_destroy (&nattr) != 0)
97 puts ("pthread_attr_destroy failed");
103 fct_err |= mqsend (q);
106 # define TEST_FUNCTION do_test ()
112 char name[sizeof "/tst-mqueue6-" + sizeof (pid_t) * 3];
113 snprintf (name, sizeof (name), "/tst-mqueue6-%u", getpid ());
115 struct mq_attr attr = { .mq_maxmsg = 1, .mq_msgsize = 1 };
116 mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
121 FAIL_UNSUPPORTED ("mq_open not supported");
123 printf ("mq_open failed with: %m\n");
129 pthread_attr_t nattr;
130 if (pthread_attr_init (&nattr)
131 || pthread_attr_setguardsize (&nattr, 0))
133 puts ("pthread_attr_t setup failed");
140 memset (&ev, 0xaa, sizeof (ev));
141 ev.sigev_notify = SIGEV_THREAD;
142 ev.sigev_notify_function = fct;
143 ev.sigev_notify_attributes = &nattr;
144 ev.sigev_value.sival_ptr = &q;
145 if (mq_notify (q, &ev) != 0)
147 printf ("mq_notify (q, { SIGEV_THREAD }) failed with: %m\n");
151 size_t ps = sysconf (_SC_PAGESIZE);
152 if (pthread_attr_setguardsize (&nattr, 32 * ps))
154 puts ("pthread_attr_t setup failed");
158 if (mq_notify (q, &ev) == 0)
160 puts ("second mq_notify (q, { SIGEV_NONE }) unexpectedly succeeded");
163 else if (errno != EBUSY)
165 printf ("second mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
171 printf ("fct called too early (%d on %d)\n", fct_cnt, __LINE__);
175 result |= mqsend (q);
177 result |= mqrecv (q);
178 result |= mqrecv (q);
182 printf ("fct not called (%d on %d)\n", fct_cnt, __LINE__);
185 else if (fct_guardsize != 0)
187 printf ("fct_guardsize %zd != 0\n", fct_guardsize);
191 if (mq_notify (q, &ev) != 0)
193 printf ("third mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
197 if (mq_notify (q, NULL) != 0)
199 printf ("mq_notify (q, NULL) failed with: %m\n");
203 memset (&ev, 0x11, sizeof (ev));
204 ev.sigev_notify = SIGEV_THREAD;
205 ev.sigev_notify_function = fct;
206 ev.sigev_notify_attributes = &nattr;
207 ev.sigev_value.sival_ptr = &q;
208 if (mq_notify (q, &ev) != 0)
210 printf ("mq_notify (q, { SIGEV_THREAD }) failed with: %m\n");
214 if (pthread_attr_setguardsize (&nattr, 0))
216 puts ("pthread_attr_t setup failed");
220 if (mq_notify (q, &ev) == 0)
222 puts ("second mq_notify (q, { SIGEV_NONE }) unexpectedly succeeded");
225 else if (errno != EBUSY)
227 printf ("second mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
233 printf ("fct called too early (%d on %d)\n", fct_cnt, __LINE__);
237 result |= mqsend (q);
239 result |= mqrecv (q);
240 result |= mqrecv (q);
244 printf ("fct not called (%d on %d)\n", fct_cnt, __LINE__);
247 else if (fct_guardsize != 32 * ps)
249 printf ("fct_guardsize %zd != %zd\n", fct_guardsize, 32 * ps);
253 if (mq_notify (q, &ev) != 0)
255 printf ("third mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
259 if (mq_notify (q, NULL) != 0)
261 printf ("mq_notify (q, NULL) failed with: %m\n");
265 if (pthread_attr_destroy (&nattr) != 0)
267 puts ("pthread_attr_destroy failed");
271 if (mq_unlink (name) != 0)
273 printf ("mq_unlink failed: %m\n");
277 if (mq_close (q) != 0)
279 printf ("mq_close failed: %m\n");
283 memset (&ev, 0x55, sizeof (ev));
284 ev.sigev_notify = SIGEV_THREAD;
285 ev.sigev_notify_function = fct;
286 ev.sigev_notify_attributes = NULL;
287 ev.sigev_value.sival_int = 0;
288 if (mq_notify (q, &ev) == 0)
290 puts ("mq_notify on closed mqd_t unexpectedly succeeded");
293 else if (errno != EBADF)
295 printf ("mq_notify on closed mqd_t did not fail with EBADF: %m\n");
304 # define TEST_FUNCTION 0
307 #include "../test-skeleton.c"