| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include <errno.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
#include <time.h> |
| 25 |
#include <dlfcn.h> |
| 26 |
#include <pthread.h> |
| 27 |
#include <netdb.h> |
| 28 |
#include <netinet/in.h> |
| 29 |
#include <sys/socket.h> |
| 30 |
|
| 31 |
static struct timespec ts; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
void * |
| 39 |
realloc (void *ptr, size_t len) |
| 40 |
{ |
| 41 |
static void *(*fun) (void *, size_t); |
| 42 |
|
| 43 |
if (!fun) |
| 44 |
fun = dlsym (RTLD_NEXT, "realloc"); |
| 45 |
|
| 46 |
if (ts.tv_nsec) |
| 47 |
nanosleep (&ts, NULL); |
| 48 |
|
| 49 |
return (*fun) (ptr, len); |
| 50 |
} |
| 51 |
|
| 52 |
static void * |
| 53 |
resolve (void *arg) |
| 54 |
{ |
| 55 |
struct in_addr addr; |
| 56 |
struct hostent ent; |
| 57 |
struct hostent *result; |
| 58 |
int err; |
| 59 |
char buf[1024]; |
| 60 |
|
| 61 |
addr.s_addr = htonl (INADDR_LOOPBACK); |
| 62 |
(void) gethostbyaddr_r ((void *) &addr, sizeof (addr), AF_INET, |
| 63 |
&ent, buf, sizeof (buf), &result, &err); |
| 64 |
return arg; |
| 65 |
} |
| 66 |
|
| 67 |
static int |
| 68 |
do_test (void) |
| 69 |
{ |
| 70 |
#define N 3 |
| 71 |
pthread_t thr[N]; |
| 72 |
unsigned int i; |
| 73 |
int result = 0; |
| 74 |
|
| 75 |
|
| 76 |
ts.tv_nsec = 100000000; |
| 77 |
|
| 78 |
for (i = 0; i < N; ++i) |
| 79 |
{ |
| 80 |
int rc = pthread_create (&thr[i], NULL, resolve, NULL); |
| 81 |
|
| 82 |
if (rc) |
| 83 |
{ |
| 84 |
printf ("pthread_create: %s\n", strerror(rc)); |
| 85 |
exit (1); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
for (i = 0; i < N; ++i) |
| 90 |
{ |
| 91 |
void *retval; |
| 92 |
int rc = pthread_join (thr[i], &retval); |
| 93 |
|
| 94 |
if (rc) |
| 95 |
{ |
| 96 |
printf ("pthread_join: %s\n", strerror(rc)); |
| 97 |
exit (1); |
| 98 |
} |
| 99 |
if (retval) |
| 100 |
{ |
| 101 |
printf ("thread %u exit status %p\n", i, retval); |
| 102 |
result = 1; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
ts.tv_nsec = 0; |
| 108 |
|
| 109 |
return result; |
| 110 |
} |
| 111 |
|
| 112 |
#define TEST_FUNCTION do_test () |
| 113 |
#include "../test-skeleton.c" |