1 /* Bug 22830: malloc_stats fails to re-enable cancellation on exit.
2 Copyright (C) 2018 Free Software Foundation.
3 Copying and distribution of this file, with or without modification,
4 are permitted in any medium without royalty provided the copyright
5 notice and this notice are preserved. This file is offered as-is,
6 without any warranty. */
14 #include <sys/types.h>
21 test_threadproc (void *gatep
)
23 /* When we are released from the barrier, there is a cancellation
24 request pending for this thread. N.B. pthread_barrier_wait is
25 not itself a cancellation point (oddly enough). */
26 pthread_barrier_wait ((pthread_barrier_t
*)gatep
);
28 fputs ("this call should trigger cancellation\n", stderr
);
32 /* We cannot replace stderr with a memstream because writes to memstreams
33 do not trigger cancellation. Instead, main swaps out fd 2 to point to
34 a pipe, and this thread reads from the pipe and writes to a memstream
35 until EOF, then returns the data accumulated in the memstream. main
36 can't do that itself because, when the test thread gets cancelled,
37 it doesn't close the pipe. */
46 buffer_threadproc (void *argp
)
48 struct buffer_tp_args
*args
= argp
;
50 char block
[BUFSIZ
], *p
;
56 FILE *ofp
= open_memstream (&obuf
, &obufsz
);
59 fprintf (args
->real_stderr
,
60 "buffer_threadproc: open_memstream: %s\n", strerror (errno
));
64 while ((nread
= read (ifd
, block
, BUFSIZ
)) > 0)
69 nwritten
= fwrite_unlocked (p
, 1, nread
, ofp
);
72 fprintf (args
->real_stderr
,
73 "buffer_threadproc: fwrite_unlocked: %s\n",
84 fprintf (args
->real_stderr
, "buffer_threadproc: read: %s\n",
97 int result
= 0, err
, real_stderr_fd
, bufpipe
[2];
98 pthread_t t_thr
, b_thr
;
99 pthread_barrier_t gate
;
104 struct buffer_tp_args b_args
;
106 real_stderr_fd
= dup (2);
107 if (real_stderr_fd
== -1)
112 real_stderr
= fdopen(real_stderr_fd
, "w");
118 if (setvbuf (real_stderr
, 0, _IOLBF
, 0))
120 perror ("setvbuf(real_stderr)");
130 /* Below this point, nobody other than the test_threadproc should use
131 the normal stderr. */
132 if (dup2 (bufpipe
[1], 2) == -1)
134 fprintf (real_stderr
, "dup2: %s\n", strerror (errno
));
139 b_args
.ifd
= bufpipe
[0];
140 b_args
.real_stderr
= real_stderr
;
141 err
= pthread_create (&b_thr
, 0, buffer_threadproc
, &b_args
);
144 fprintf (real_stderr
, "pthread_create(buffer_thr): %s\n",
149 err
= pthread_barrier_init (&gate
, 0, 2);
152 fprintf (real_stderr
, "pthread_barrier_init: %s\n", strerror (err
));
156 err
= pthread_create (&t_thr
, 0, test_threadproc
, &gate
);
159 fprintf (real_stderr
, "pthread_create(test_thr): %s\n", strerror (err
));
163 err
= pthread_cancel (t_thr
);
166 fprintf (real_stderr
, "pthread_cancel: %s\n", strerror (err
));
170 pthread_barrier_wait (&gate
); /* cannot fail */
172 err
= pthread_join (t_thr
, &rv
);
175 fprintf (real_stderr
, "pthread_join(test_thr): %s\n", strerror (err
));
179 /* Closing the normal stderr releases the buffer_threadproc from its
182 err
= pthread_join (b_thr
, &obuf_v
);
185 fprintf (real_stderr
, "pthread_join(buffer_thr): %s\n", strerror (err
));
190 return 2; /* error within buffer_threadproc, already reported */
192 if (rv
!= PTHREAD_CANCELED
)
194 fputs ("FAIL: thread was not cancelled\n", real_stderr
);
197 /* obuf should have received all of the text printed by malloc_stats,
198 but not the text printed by the final call to fputs. */
199 if (!strstr (obuf
, "max mmap bytes"))
201 fputs ("FAIL: malloc_stats output incomplete\n", real_stderr
);
204 if (strstr (obuf
, "this call should trigger cancellation"))
206 fputs ("FAIL: fputs produced output\n", real_stderr
);
212 fputs ("--- output from thread below ---\n", real_stderr
);
213 fputs (obuf
, real_stderr
);
218 #include <support/test-driver.c>