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