1 /* BZ #17977 _res_hconf_reorder_addrs test.
3 Copyright (C) 2015-2024 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
28 #include <netinet/in.h>
29 #include <sys/socket.h>
31 static struct timespec ts
;
33 /* The first thread that gets a lock in _res_hconf_reorder_addrs()
34 should hold the lock long enough to make two other threads blocked.
35 This is achieved by slowing down realloc(3) that is called several times
36 by _res_hconf_reorder_addrs(). */
39 realloc (void *ptr
, size_t len
)
41 static void *(*fun
) (void *, size_t);
44 fun
= dlsym (RTLD_NEXT
, "realloc");
47 nanosleep (&ts
, NULL
);
49 return (*fun
) (ptr
, len
);
57 struct hostent
*result
;
61 addr
.s_addr
= htonl (INADDR_LOOPBACK
);
62 (void) gethostbyaddr_r ((void *) &addr
, sizeof (addr
), AF_INET
,
63 &ent
, buf
, sizeof (buf
), &result
, &err
);
75 /* turn on realloc slowdown */
76 ts
.tv_nsec
= 100000000;
78 for (i
= 0; i
< N
; ++i
)
80 int rc
= pthread_create (&thr
[i
], NULL
, resolve
, NULL
);
84 printf ("pthread_create: %s\n", strerror(rc
));
89 for (i
= 0; i
< N
; ++i
)
92 int rc
= pthread_join (thr
[i
], &retval
);
96 printf ("pthread_join: %s\n", strerror(rc
));
101 printf ("thread %u exit status %p\n", i
, retval
);
106 /* turn off realloc slowdown, no longer needed */
112 #define TEST_FUNCTION do_test ()
113 #include "../test-skeleton.c"