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