]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/test/nptl/tst-cancel19.c
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / test / nptl / tst-cancel19.c
1 /* Copyright (C) 2003 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
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 <error.h>
22 #include <fcntl.h>
23 #include <pthread.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/select.h>
29 #include <sys/time.h>
30 #include <unistd.h>
31
32 static void *
33 tf (void *arg)
34 {
35   return NULL;
36 }
37
38 static void
39 handler (int sig)
40 {
41 }
42
43 static void __attribute__ ((noinline))
44 clobber_lots_of_regs (void)
45 {
46 #define X1(n) long r##n = 10##n; __asm__ __volatile__ ("" : "+r" (r##n));
47 #define X2(n) X1(n##0) X1(n##1) X1(n##2) X1(n##3) X1(n##4)
48 #define X3(n) X2(n##0) X2(n##1) X2(n##2) X2(n##3) X2(n##4)
49   X3(0) X3(1) X3(2) X3(3) X3(4)
50 #undef X1
51 #define X1(n) __asm__ __volatile__ ("" : : "r" (r##n));
52   X3(0) X3(1) X3(2) X3(3) X3(4)
53 #undef X1
54 #undef X2
55 #undef X3
56 }
57
58 static int
59 do_test (void)
60 {
61   pthread_t th;
62   int old, rc;
63   int ret = 0;
64   int fd[2];
65
66   rc = pipe (fd);
67   if (rc < 0)
68     error (EXIT_FAILURE, errno, "couldn't create pipe");
69
70   rc = pthread_create (&th, NULL, tf, NULL);
71   if (rc)
72     error (EXIT_FAILURE, rc, "couldn't create thread");
73
74   rc = pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &old);
75   if (rc)
76     {
77       error (0, rc, "1st pthread_setcanceltype failed");
78       ret = 1;
79     }
80   if (old != PTHREAD_CANCEL_DEFERRED && old != PTHREAD_CANCEL_ASYNCHRONOUS)
81     {
82       error (0, 0, "1st pthread_setcanceltype returned invalid value %d",
83              old);
84       ret = 1;
85     }
86
87   clobber_lots_of_regs ();
88   close (fd[0]);
89
90   rc = pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &old);
91   if (rc)
92     {
93       error (0, rc, "pthread_setcanceltype after close failed");
94       ret = 1;
95     }
96   if (old != PTHREAD_CANCEL_DEFERRED)
97     {
98       error (0, 0, "pthread_setcanceltype after close returned invalid value %d",
99              old);
100       ret = 1;
101     }
102
103   clobber_lots_of_regs ();
104   close (fd[1]);
105
106   rc = pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &old);
107   if (rc)
108     {
109       error (0, rc, "pthread_setcanceltype after 2nd close failed");
110       ret = 1;
111     }
112   if (old != PTHREAD_CANCEL_ASYNCHRONOUS)
113     {
114       error (0, 0, "pthread_setcanceltype after 2nd close returned invalid value %d",
115              old);
116       ret = 1;
117     }
118
119   struct sigaction sa = { .sa_handler = handler, .sa_flags = 0 };
120   sigemptyset (&sa.sa_mask);
121   sigaction (SIGALRM, &sa, NULL);
122
123   struct itimerval it;
124   it.it_value.tv_sec = 1;
125   it.it_value.tv_usec = 0;
126   it.it_interval = it.it_value;
127   setitimer (ITIMER_REAL, &it, NULL);
128
129   clobber_lots_of_regs ();
130   pause ();
131
132   memset (&it, 0, sizeof (it));
133   setitimer (ITIMER_REAL, &it, NULL);
134
135   rc = pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &old);
136   if (rc)
137     {
138       error (0, rc, "pthread_setcanceltype after pause failed");
139       ret = 1;
140     }
141   if (old != PTHREAD_CANCEL_DEFERRED)
142     {
143       error (0, 0, "pthread_setcanceltype after pause returned invalid value %d",
144              old);
145       ret = 1;
146     }
147
148   it.it_value.tv_sec = 1;
149   it.it_value.tv_usec = 0;
150   it.it_interval = it.it_value;
151   setitimer (ITIMER_REAL, &it, NULL);
152
153   clobber_lots_of_regs ();
154   pause ();
155
156   memset (&it, 0, sizeof (it));
157   setitimer (ITIMER_REAL, &it, NULL);
158
159   rc = pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &old);
160   if (rc)
161     {
162       error (0, rc, "pthread_setcanceltype after 2nd pause failed");
163       ret = 1;
164     }
165   if (old != PTHREAD_CANCEL_ASYNCHRONOUS)
166     {
167       error (0, 0, "pthread_setcanceltype after 2nd pause returned invalid value %d",
168              old);
169       ret = 1;
170     }
171
172   char fname[] = "/tmp/tst-cancel19-dir-XXXXXX\0foo/bar";
173   char *enddir = strchr (fname, '\0');
174   if (mkdtemp (fname) == NULL)
175     {
176       error (0, errno, "mkdtemp failed");
177       ret = 1;
178     }
179   *enddir = '/';
180
181   clobber_lots_of_regs ();
182   creat (fname, 0400);
183
184   rc = pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &old);
185   if (rc)
186     {
187       error (0, rc, "pthread_setcanceltype after creat failed");
188       ret = 1;
189     }
190   if (old != PTHREAD_CANCEL_DEFERRED)
191     {
192       error (0, 0, "pthread_setcanceltype after creat returned invalid value %d",
193              old);
194       ret = 1;
195     }
196
197   clobber_lots_of_regs ();
198   creat (fname, 0400);
199
200   rc = pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &old);
201   if (rc)
202     {
203       error (0, rc, "pthread_setcanceltype after 2nd creat failed");
204       ret = 1;
205     }
206   if (old != PTHREAD_CANCEL_ASYNCHRONOUS)
207     {
208       error (0, 0, "pthread_setcanceltype after 2nd creat returned invalid value %d",
209              old);
210       ret = 1;
211     }
212
213   clobber_lots_of_regs ();
214   open (fname, O_CREAT, 0400);
215
216   rc = pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &old);
217   if (rc)
218     {
219       error (0, rc, "pthread_setcanceltype after open failed");
220       ret = 1;
221     }
222   if (old != PTHREAD_CANCEL_DEFERRED)
223     {
224       error (0, 0, "pthread_setcanceltype after open returned invalid value %d",
225              old);
226       ret = 1;
227     }
228
229   clobber_lots_of_regs ();
230   open (fname, O_CREAT, 0400);
231
232   rc = pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &old);
233   if (rc)
234     {
235       error (0, rc, "pthread_setcanceltype after 2nd open failed");
236       ret = 1;
237     }
238   if (old != PTHREAD_CANCEL_ASYNCHRONOUS)
239     {
240       error (0, 0, "pthread_setcanceltype after 2nd open returned invalid value %d",
241              old);
242       ret = 1;
243     }
244
245   *enddir = '\0';
246   rmdir (fname);
247
248   clobber_lots_of_regs ();
249   select (-1, NULL, NULL, NULL, NULL);
250
251   rc = pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &old);
252   if (rc)
253     {
254       error (0, rc, "pthread_setcanceltype after select failed");
255       ret = 1;
256     }
257   if (old != PTHREAD_CANCEL_DEFERRED)
258     {
259       error (0, 0, "pthread_setcanceltype after select returned invalid value %d",
260              old);
261       ret = 1;
262     }
263
264   clobber_lots_of_regs ();
265   select (-1, NULL, NULL, NULL, NULL);
266
267   rc = pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &old);
268   if (rc)
269     {
270       error (0, rc, "pthread_setcanceltype after 2nd select failed");
271       ret = 1;
272     }
273   if (old != PTHREAD_CANCEL_ASYNCHRONOUS)
274     {
275       error (0, 0, "pthread_setcanceltype after 2nd select returned invalid value %d",
276              old);
277       ret = 1;
278     }
279
280   pthread_join (th, NULL);
281
282   return ret;
283 }
284
285 #define TIMEOUT 20
286 #define TEST_FUNCTION do_test ()
287 #include "../test-skeleton.c"