2 Copyright (C) 2004-2021 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 <https://www.gnu.org/licenses/>. */
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
);
120 printf ("mq_open failed with: %m\n");
126 pthread_attr_t nattr
;
127 if (pthread_attr_init (&nattr
)
128 || pthread_attr_setguardsize (&nattr
, 0))
130 puts ("pthread_attr_t setup failed");
137 memset (&ev
, 0xaa, sizeof (ev
));
138 ev
.sigev_notify
= SIGEV_THREAD
;
139 ev
.sigev_notify_function
= fct
;
140 ev
.sigev_notify_attributes
= &nattr
;
141 ev
.sigev_value
.sival_ptr
= &q
;
142 if (mq_notify (q
, &ev
) != 0)
144 printf ("mq_notify (q, { SIGEV_THREAD }) failed with: %m\n");
148 size_t ps
= sysconf (_SC_PAGESIZE
);
149 if (pthread_attr_setguardsize (&nattr
, 32 * ps
))
151 puts ("pthread_attr_t setup failed");
155 if (mq_notify (q
, &ev
) == 0)
157 puts ("second mq_notify (q, { SIGEV_NONE }) unexpectedly succeeded");
160 else if (errno
!= EBUSY
)
162 printf ("second mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
168 printf ("fct called too early (%d on %d)\n", fct_cnt
, __LINE__
);
172 result
|= mqsend (q
);
174 result
|= mqrecv (q
);
175 result
|= mqrecv (q
);
179 printf ("fct not called (%d on %d)\n", fct_cnt
, __LINE__
);
182 else if (fct_guardsize
!= 0)
184 printf ("fct_guardsize %zd != 0\n", fct_guardsize
);
188 if (mq_notify (q
, &ev
) != 0)
190 printf ("third mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
194 if (mq_notify (q
, NULL
) != 0)
196 printf ("mq_notify (q, NULL) failed with: %m\n");
200 memset (&ev
, 0x11, sizeof (ev
));
201 ev
.sigev_notify
= SIGEV_THREAD
;
202 ev
.sigev_notify_function
= fct
;
203 ev
.sigev_notify_attributes
= &nattr
;
204 ev
.sigev_value
.sival_ptr
= &q
;
205 if (mq_notify (q
, &ev
) != 0)
207 printf ("mq_notify (q, { SIGEV_THREAD }) failed with: %m\n");
211 if (pthread_attr_setguardsize (&nattr
, 0))
213 puts ("pthread_attr_t setup failed");
217 if (mq_notify (q
, &ev
) == 0)
219 puts ("second mq_notify (q, { SIGEV_NONE }) unexpectedly succeeded");
222 else if (errno
!= EBUSY
)
224 printf ("second mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
230 printf ("fct called too early (%d on %d)\n", fct_cnt
, __LINE__
);
234 result
|= mqsend (q
);
236 result
|= mqrecv (q
);
237 result
|= mqrecv (q
);
241 printf ("fct not called (%d on %d)\n", fct_cnt
, __LINE__
);
244 else if (fct_guardsize
!= 32 * ps
)
246 printf ("fct_guardsize %zd != %zd\n", fct_guardsize
, 32 * ps
);
250 if (mq_notify (q
, &ev
) != 0)
252 printf ("third mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
256 if (mq_notify (q
, NULL
) != 0)
258 printf ("mq_notify (q, NULL) failed with: %m\n");
262 if (pthread_attr_destroy (&nattr
) != 0)
264 puts ("pthread_attr_destroy failed");
268 if (mq_unlink (name
) != 0)
270 printf ("mq_unlink failed: %m\n");
274 if (mq_close (q
) != 0)
276 printf ("mq_close failed: %m\n");
280 memset (&ev
, 0x55, sizeof (ev
));
281 ev
.sigev_notify
= SIGEV_THREAD
;
282 ev
.sigev_notify_function
= fct
;
283 ev
.sigev_notify_attributes
= NULL
;
284 ev
.sigev_value
.sival_int
= 0;
285 if (mq_notify (q
, &ev
) == 0)
287 puts ("mq_notify on closed mqd_t unexpectedly succeeded");
290 else if (errno
!= EBADF
)
292 printf ("mq_notify on closed mqd_t did not fail with EBADF: %m\n");
301 # define TEST_FUNCTION 0
304 #include "../test-skeleton.c"