1 | /* Copyright (C) 2004 Free Software Foundation, Inc.
|
---|
2 | This file is part of the GNU C Library.
|
---|
3 | Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
|
---|
4 |
|
---|
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.
|
---|
9 |
|
---|
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.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU Lesser General Public
|
---|
16 | License along with the GNU C Library; if not, write to the Free
|
---|
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
18 | 02111-1307 USA. */
|
---|
19 |
|
---|
20 | #include <errno.h>
|
---|
21 | #include <mqueue.h>
|
---|
22 | #include <stdio.h>
|
---|
23 | #include <stdlib.h>
|
---|
24 | #include <string.h>
|
---|
25 | #include <unistd.h>
|
---|
26 | #if _POSIX_THREADS
|
---|
27 | # include <pthread.h>
|
---|
28 |
|
---|
29 | static pthread_barrier_t b;
|
---|
30 |
|
---|
31 | /* Cleanup handling test. */
|
---|
32 | static int cl_called;
|
---|
33 |
|
---|
34 | static void
|
---|
35 | cl (void *arg)
|
---|
36 | {
|
---|
37 | ++cl_called;
|
---|
38 | }
|
---|
39 |
|
---|
40 | #define TF_MQ_RECEIVE 0L
|
---|
41 | #define TF_MQ_TIMEDRECEIVE 1L
|
---|
42 | #define TF_MQ_SEND 2L
|
---|
43 | #define TF_MQ_TIMEDSEND 3L
|
---|
44 |
|
---|
45 | static const char *names[]
|
---|
46 | = { "mq_receive", "mq_timedreceive", "mq_send", "mq_timedsend" };
|
---|
47 |
|
---|
48 | static mqd_t q;
|
---|
49 | static struct timespec never;
|
---|
50 |
|
---|
51 | static void *
|
---|
52 | tf (void *arg)
|
---|
53 | {
|
---|
54 | int r = pthread_barrier_wait (&b);
|
---|
55 | if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
|
---|
56 | {
|
---|
57 | puts ("tf: barrier_wait failed");
|
---|
58 | exit (1);
|
---|
59 | }
|
---|
60 |
|
---|
61 | pthread_cleanup_push (cl, NULL);
|
---|
62 |
|
---|
63 | char c = ' ';
|
---|
64 |
|
---|
65 | switch ((long) arg)
|
---|
66 | {
|
---|
67 | case TF_MQ_SEND:
|
---|
68 | TEMP_FAILURE_RETRY (mq_send (q, &c, 1, 1));
|
---|
69 | break;
|
---|
70 | case TF_MQ_TIMEDSEND:
|
---|
71 | TEMP_FAILURE_RETRY (mq_timedsend (q, &c, 1, 1, &never));
|
---|
72 | break;
|
---|
73 | case TF_MQ_RECEIVE:
|
---|
74 | TEMP_FAILURE_RETRY (mq_receive (q, &c, 1, NULL));
|
---|
75 | break;
|
---|
76 | case TF_MQ_TIMEDRECEIVE:
|
---|
77 | TEMP_FAILURE_RETRY (mq_timedreceive (q, &c, 1, NULL, &never));
|
---|
78 | break;
|
---|
79 | }
|
---|
80 |
|
---|
81 | pthread_cleanup_pop (0);
|
---|
82 |
|
---|
83 | printf ("tf: %s returned\n", names[(long) arg]);
|
---|
84 |
|
---|
85 | exit (1);
|
---|
86 | }
|
---|
87 |
|
---|
88 | #define TEST_FUNCTION do_test ()
|
---|
89 | static int
|
---|
90 | do_test (void)
|
---|
91 | {
|
---|
92 | char name[sizeof "/tst-mqueue8-" + sizeof (pid_t) * 3];
|
---|
93 | snprintf (name, sizeof (name), "/tst-mqueue8-%u", getpid ());
|
---|
94 |
|
---|
95 | struct mq_attr attr = { .mq_maxmsg = 1, .mq_msgsize = 1 };
|
---|
96 | q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
|
---|
97 |
|
---|
98 | if (q == (mqd_t) -1)
|
---|
99 | {
|
---|
100 | printf ("mq_open failed with: %m\n");
|
---|
101 | return 0;
|
---|
102 | }
|
---|
103 |
|
---|
104 | if (mq_unlink (name) != 0)
|
---|
105 | {
|
---|
106 | printf ("mq_unlink failed with: %m\n");
|
---|
107 | return 1;
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (pthread_barrier_init (&b, NULL, 2) != 0)
|
---|
111 | {
|
---|
112 | puts ("barrier_init failed");
|
---|
113 | return 1;
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (clock_gettime (CLOCK_REALTIME, &never) == 0)
|
---|
117 | never.tv_sec += 100;
|
---|
118 | else
|
---|
119 | {
|
---|
120 | never.tv_sec = time (NULL) + 100;
|
---|
121 | never.tv_nsec = 0;
|
---|
122 | }
|
---|
123 |
|
---|
124 | int result = 0;
|
---|
125 | for (long l = TF_MQ_RECEIVE; l <= TF_MQ_TIMEDSEND; ++l)
|
---|
126 | {
|
---|
127 | cl_called = 0;
|
---|
128 |
|
---|
129 | pthread_t th;
|
---|
130 | if (pthread_create (&th, NULL, tf, (void *) l) != 0)
|
---|
131 | {
|
---|
132 | printf ("1st %s create failed\n", names[l]);
|
---|
133 | result = 1;
|
---|
134 | continue;
|
---|
135 | }
|
---|
136 |
|
---|
137 | int r = pthread_barrier_wait (&b);
|
---|
138 | if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
|
---|
139 | {
|
---|
140 | puts ("barrier_wait failed");
|
---|
141 | result = 1;
|
---|
142 | continue;
|
---|
143 | }
|
---|
144 |
|
---|
145 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
|
---|
146 | while (nanosleep (&ts, &ts) != 0)
|
---|
147 | continue;
|
---|
148 |
|
---|
149 | printf ("going to cancel %s in-time\n", names[l]);
|
---|
150 | if (pthread_cancel (th) != 0)
|
---|
151 | {
|
---|
152 | printf ("1st cancel of %s failed\n", names[l]);
|
---|
153 | result = 1;
|
---|
154 | continue;
|
---|
155 | }
|
---|
156 |
|
---|
157 | void *status;
|
---|
158 | if (pthread_join (th, &status) != 0)
|
---|
159 | {
|
---|
160 | printf ("1st join of %s failed\n", names[l]);
|
---|
161 | result = 1;
|
---|
162 | continue;
|
---|
163 | }
|
---|
164 | if (status != PTHREAD_CANCELED)
|
---|
165 | {
|
---|
166 | printf ("1st %s thread not canceled\n", names[l]);
|
---|
167 | result = 1;
|
---|
168 | continue;
|
---|
169 | }
|
---|
170 |
|
---|
171 | if (cl_called == 0)
|
---|
172 | {
|
---|
173 | printf ("%s cleanup handler not called\n", names[l]);
|
---|
174 | result = 1;
|
---|
175 | continue;
|
---|
176 | }
|
---|
177 | if (cl_called > 1)
|
---|
178 | {
|
---|
179 | printf ("%s cleanup handler called more than once\n", names[l]);
|
---|
180 | result = 1;
|
---|
181 | continue;
|
---|
182 | }
|
---|
183 |
|
---|
184 | printf ("in-time %s cancellation succeeded\n", names[l]);
|
---|
185 |
|
---|
186 | cl_called = 0;
|
---|
187 |
|
---|
188 | if (pthread_create (&th, NULL, tf, (void *) l) != 0)
|
---|
189 | {
|
---|
190 | printf ("2nd %s create failed\n", names[l]);
|
---|
191 | result = 1;
|
---|
192 | continue;
|
---|
193 | }
|
---|
194 |
|
---|
195 | printf ("going to cancel %s early\n", names[l]);
|
---|
196 | if (pthread_cancel (th) != 0)
|
---|
197 | {
|
---|
198 | printf ("2nd cancel of %s failed\n", names[l]);
|
---|
199 | result = 1;
|
---|
200 | continue;
|
---|
201 | }
|
---|
202 |
|
---|
203 | r = pthread_barrier_wait (&b);
|
---|
204 | if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
|
---|
205 | {
|
---|
206 | puts ("barrier_wait failed");
|
---|
207 | result = 1;
|
---|
208 | continue;
|
---|
209 | }
|
---|
210 |
|
---|
211 | if (pthread_join (th, &status) != 0)
|
---|
212 | {
|
---|
213 | printf ("2nd join of %s failed\n", names[l]);
|
---|
214 | result = 1;
|
---|
215 | continue;
|
---|
216 | }
|
---|
217 | if (status != PTHREAD_CANCELED)
|
---|
218 | {
|
---|
219 | printf ("2nd %s thread not canceled\n", names[l]);
|
---|
220 | result = 1;
|
---|
221 | continue;
|
---|
222 | }
|
---|
223 |
|
---|
224 | if (cl_called == 0)
|
---|
225 | {
|
---|
226 | printf ("%s cleanup handler not called\n", names[l]);
|
---|
227 | result = 1;
|
---|
228 | continue;
|
---|
229 | }
|
---|
230 | if (cl_called > 1)
|
---|
231 | {
|
---|
232 | printf ("%s cleanup handler called more than once\n", names[l]);
|
---|
233 | result = 1;
|
---|
234 | continue;
|
---|
235 | }
|
---|
236 |
|
---|
237 | printf ("early %s cancellation succeeded\n", names[l]);
|
---|
238 |
|
---|
239 | if (l == TF_MQ_TIMEDRECEIVE)
|
---|
240 | {
|
---|
241 | /* mq_receive and mq_timedreceive are tested on empty mq.
|
---|
242 | For mq_send and mq_timedsend we need to make it full.
|
---|
243 | If this fails, there is no point in doing further testing. */
|
---|
244 | char c = ' ';
|
---|
245 | if (mq_send (q, &c, 1, 1) != 0)
|
---|
246 | {
|
---|
247 | printf ("mq_send failed: %m\n");
|
---|
248 | result = 1;
|
---|
249 | break;
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | if (mq_close (q) != 0)
|
---|
255 | {
|
---|
256 | printf ("mq_close failed: %m\n");
|
---|
257 | result = 1;
|
---|
258 | }
|
---|
259 |
|
---|
260 | return result;
|
---|
261 | }
|
---|
262 | #else
|
---|
263 | # define TEST_FUNCTION 0
|
---|
264 | #endif
|
---|
265 |
|
---|
266 | #include "../test-skeleton.c"
|
---|