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.
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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
35 #include "tst-mqueue.h"
40 # define mqsend(q) (mqsend) (q, __LINE__)
42 (mqsend
) (mqd_t q
, int line
)
45 if (mq_send (q
, &c
, 1, 1) != 0)
47 printf ("mq_send on line %d failed with: %m\n", line
);
53 # define mqrecv(q) (mqrecv) (q, __LINE__)
55 (mqrecv
) (mqd_t q
, int line
)
58 ssize_t rets
= TEMP_FAILURE_RETRY (mq_receive (q
, &c
, 1, NULL
));
62 printf ("mq_receive on line %d failed with: %m\n", line
);
64 printf ("mq_receive on line %d returned %zd != 1\n",
71 volatile int fct_cnt
, fct_err
;
77 mqd_t q
= *(mqd_t
*) s
.sival_ptr
;
80 int ret
= pthread_getattr_np (pthread_self (), &nattr
);
84 printf ("pthread_getattr_np failed: %m\n");
89 ret
= pthread_attr_getguardsize (&nattr
, &fct_guardsize
);
93 printf ("pthread_attr_getguardsize failed: %m\n");
96 if (pthread_attr_destroy (&nattr
) != 0)
98 puts ("pthread_attr_destroy failed");
104 fct_err
|= mqsend (q
);
107 # define TEST_FUNCTION do_test ()
113 char name
[sizeof "/tst-mqueue6-" + sizeof (pid_t
) * 3];
114 snprintf (name
, sizeof (name
), "/tst-mqueue6-%u", getpid ());
116 struct mq_attr attr
= { .mq_maxmsg
= 1, .mq_msgsize
= 1 };
117 mqd_t q
= mq_open (name
, O_CREAT
| O_EXCL
| O_RDWR
, 0600, &attr
);
121 printf ("mq_open failed with: %m\n");
127 pthread_attr_t nattr
;
128 if (pthread_attr_init (&nattr
)
129 || pthread_attr_setguardsize (&nattr
, 0))
131 puts ("pthread_attr_t setup failed");
138 memset (&ev
, 0xaa, sizeof (ev
));
139 ev
.sigev_notify
= SIGEV_THREAD
;
140 ev
.sigev_notify_function
= fct
;
141 ev
.sigev_notify_attributes
= &nattr
;
142 ev
.sigev_value
.sival_ptr
= &q
;
143 if (mq_notify (q
, &ev
) != 0)
145 printf ("mq_notify (q, { SIGEV_THREAD }) failed with: %m\n");
149 size_t ps
= sysconf (_SC_PAGESIZE
);
150 if (pthread_attr_setguardsize (&nattr
, 32 * ps
))
152 puts ("pthread_attr_t setup failed");
156 if (mq_notify (q
, &ev
) == 0)
158 puts ("second mq_notify (q, { SIGEV_NONE }) unexpectedly succeeded");
161 else if (errno
!= EBUSY
)
163 printf ("second mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
169 printf ("fct called too early (%d on %d)\n", fct_cnt
, __LINE__
);
173 result
|= mqsend (q
);
175 result
|= mqrecv (q
);
176 result
|= mqrecv (q
);
180 printf ("fct not called (%d on %d)\n", fct_cnt
, __LINE__
);
183 else if (fct_guardsize
!= 0)
185 printf ("fct_guardsize %zd != 0\n", fct_guardsize
);
189 if (mq_notify (q
, &ev
) != 0)
191 printf ("third mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
195 if (mq_notify (q
, NULL
) != 0)
197 printf ("mq_notify (q, NULL) failed with: %m\n");
201 memset (&ev
, 0x11, sizeof (ev
));
202 ev
.sigev_notify
= SIGEV_THREAD
;
203 ev
.sigev_notify_function
= fct
;
204 ev
.sigev_notify_attributes
= &nattr
;
205 ev
.sigev_value
.sival_ptr
= &q
;
206 if (mq_notify (q
, &ev
) != 0)
208 printf ("mq_notify (q, { SIGEV_THREAD }) failed with: %m\n");
212 if (pthread_attr_setguardsize (&nattr
, 0))
214 puts ("pthread_attr_t setup failed");
218 if (mq_notify (q
, &ev
) == 0)
220 puts ("second mq_notify (q, { SIGEV_NONE }) unexpectedly succeeded");
223 else if (errno
!= EBUSY
)
225 printf ("second mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
231 printf ("fct called too early (%d on %d)\n", fct_cnt
, __LINE__
);
235 result
|= mqsend (q
);
237 result
|= mqrecv (q
);
238 result
|= mqrecv (q
);
242 printf ("fct not called (%d on %d)\n", fct_cnt
, __LINE__
);
245 else if (fct_guardsize
!= 32 * ps
)
247 printf ("fct_guardsize %zd != %zd\n", fct_guardsize
, 32 * ps
);
251 if (mq_notify (q
, &ev
) != 0)
253 printf ("third mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
257 if (mq_notify (q
, NULL
) != 0)
259 printf ("mq_notify (q, NULL) failed with: %m\n");
263 if (pthread_attr_destroy (&nattr
) != 0)
265 puts ("pthread_attr_destroy failed");
269 if (mq_unlink (name
) != 0)
271 printf ("mq_unlink failed: %m\n");
275 if (mq_close (q
) != 0)
277 printf ("mq_close failed: %m\n");
281 memset (&ev
, 0x55, sizeof (ev
));
282 ev
.sigev_notify
= SIGEV_THREAD
;
283 ev
.sigev_notify_function
= fct
;
284 ev
.sigev_notify_attributes
= NULL
;
285 ev
.sigev_value
.sival_int
= 0;
286 if (mq_notify (q
, &ev
) == 0)
288 puts ("mq_notify on closed mqd_t unexpectedly succeeded");
291 else if (errno
!= EBADF
)
293 printf ("mq_notify on closed mqd_t did not fail with EBADF: %m\n");
302 # define TEST_FUNCTION 0
305 #include "../test-skeleton.c"