about summary refs log tree commit diff
path: root/resolv/tst-resolv-threads.c
blob: c099981b866fae45af02d412fb8c3526cff3afb3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/* Test basic nss_dns functionality with multiple threads.
   Copyright (C) 2016-2024 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

/* Unlike tst-resolv-basic, this test does not overwrite the _res
   structure and relies on namespaces to achieve the redirection to
   the test servers with a custom /etc/resolv.conf file.  */

#include <dlfcn.h>
#include <errno.h>
#include <gnu/lib-names.h>
#include <netdb.h>
#include <resolv/resolv-internal.h>
#include <resolv/resolv_context.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <support/check.h>
#include <support/namespace.h>
#include <support/resolv_test.h>
#include <support/support.h>
#include <support/temp_file.h>
#include <support/test-driver.h>
#include <support/xthread.h>
#include <support/xunistd.h>

/* Each client thread sends this many queries.  */
enum { queries_per_thread = 500 };

/* Return a small positive number identifying this thread.  */
static int
get_thread_number (void)
{
  static int __thread local;
  if (local != 0)
    return local;
  static int global = 1;
  local = __atomic_fetch_add (&global, 1, __ATOMIC_RELAXED);
  return local;
}

static void
response (const struct resolv_response_context *ctx,
          struct resolv_response_builder *b,
          const char *qname, uint16_t qclass, uint16_t qtype)
{
  TEST_VERIFY_EXIT (qname != NULL);

  int counter = 0;
  int thread = 0;
  int dummy = 0;
  TEST_VERIFY (sscanf (qname, "counter%d.thread%d.example.com%n",
                       &counter, &thread, &dummy) == 2);
  TEST_VERIFY (dummy > 0);

  struct resolv_response_flags flags = { 0 };
  resolv_response_init (b, flags);
  resolv_response_add_question (b, qname, qclass, qtype);

  resolv_response_section (b, ns_s_an);
  resolv_response_open_record (b, qname, qclass, qtype, 0);
  switch (qtype)
    {
    case T_A:
      {
        char ipv4[4] = {10, 0, counter, thread};
        resolv_response_add_data (b, &ipv4, sizeof (ipv4));
      }
      break;
    case T_AAAA:
      {
        char ipv6[16]
          = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0,
             counter, 0, thread, 0, 0};
        resolv_response_add_data (b, &ipv6, sizeof (ipv6));
      }
      break;
    default:
      support_record_failure ();
      printf ("error: unexpected QTYPE: %s/%u/%u\n",
              qname, qclass, qtype);
    }
  resolv_response_close_record (b);
}

/* Check that the resolver configuration for this thread has an
   extended resolver configuration.  */
static void
check_have_conf (void)
{
  struct resolv_context *ctx = __resolv_context_get ();
  TEST_VERIFY_EXIT (ctx != NULL);
  TEST_VERIFY (ctx->conf != NULL);
  __resolv_context_put (ctx);
}

/* Verify that E matches the expected response for FAMILY and
   COUNTER.  */
static void
check_hostent (const char *caller, const char *function, const char *qname,
               int ret, struct hostent *e, int family, int counter)
{
  if (ret != 0)
    {
      errno = ret;
      support_record_failure ();
      printf ("error: %s: %s for %s failed: %m\n", caller, function, qname);
      return;
    }

  TEST_VERIFY_EXIT (e != NULL);
  TEST_VERIFY (strcmp (qname, e->h_name) == 0);
  TEST_VERIFY (e->h_addrtype == family);
  TEST_VERIFY_EXIT (e->h_addr_list[0] != NULL);
  TEST_VERIFY (e->h_addr_list[1] == NULL);
  switch (family)
    {
    case AF_INET:
      {
        char addr[4] = {10, 0, counter, get_thread_number ()};
        TEST_VERIFY (e->h_length == sizeof (addr));
        TEST_VERIFY (memcmp (e->h_addr_list[0], addr, sizeof (addr)) == 0);
      }
      break;
    case AF_INET6:
      {
      char addr[16]
        = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0,
           0, counter, 0, get_thread_number (), 0, 0};
      TEST_VERIFY (e->h_length == sizeof (addr));
      TEST_VERIFY (memcmp (e->h_addr_list[0], addr, sizeof (addr)) == 0);
      }
      break;
    default:
      FAIL_EXIT1 ("%s: invalid address family %d", caller, family);
    }
  check_have_conf ();
}

/* Check a getaddrinfo result.  */
static void
check_addrinfo (const char *caller, const char *qname,
                int ret, struct addrinfo *ai, int family, int counter)
{
  if (ret != 0)
    {
      support_record_failure ();
      printf ("error: %s: getaddrinfo for %s failed: %s\n",
              caller, qname, gai_strerror (ret));
      return;
    }

  TEST_VERIFY_EXIT (ai != NULL);

  /* Check that available data matches the requirements.  */
  bool have_ipv4 = false;
  bool have_ipv6 = false;
  for (struct addrinfo *p = ai; p != NULL; p = p->ai_next)
    {
      TEST_VERIFY (p->ai_socktype == SOCK_STREAM);
      TEST_VERIFY (p->ai_protocol == IPPROTO_TCP);
      TEST_VERIFY_EXIT (p->ai_addr != NULL);
      TEST_VERIFY (p->ai_addr->sa_family == p->ai_family);

      switch (p->ai_family)
        {
        case AF_INET:
          {
            TEST_VERIFY (!have_ipv4);
            have_ipv4 = true;
            struct sockaddr_in *sa = (struct sockaddr_in *) p->ai_addr;
            TEST_VERIFY (p->ai_addrlen == sizeof (*sa));
            char addr[4] = {10, 0, counter, get_thread_number ()};
            TEST_VERIFY (memcmp (&sa->sin_addr, addr, sizeof (addr)) == 0);
            TEST_VERIFY (ntohs (sa->sin_port) == 80);
          }
          break;
        case AF_INET6:
          {
            TEST_VERIFY (!have_ipv6);
            have_ipv6 = true;
            struct sockaddr_in6 *sa = (struct sockaddr_in6 *) p->ai_addr;
            TEST_VERIFY (p->ai_addrlen == sizeof (*sa));
            char addr[16]
              = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0,
                 0, counter, 0, get_thread_number (), 0, 0};
            TEST_VERIFY (memcmp (&sa->sin6_addr, addr, sizeof (addr)) == 0);
            TEST_VERIFY (ntohs (sa->sin6_port) == 80);
          }
          break;
        default:
          FAIL_EXIT1 ("%s: invalid address family %d", caller, family);
        }
    }

  switch (family)
    {
      case AF_INET:
        TEST_VERIFY (have_ipv4);
        TEST_VERIFY (!have_ipv6);
        break;
      case AF_INET6:
        TEST_VERIFY (!have_ipv4);
        TEST_VERIFY (have_ipv6);
        break;
      case AF_UNSPEC:
        TEST_VERIFY (have_ipv4);
        TEST_VERIFY (have_ipv6);
        break;
    default:
      FAIL_EXIT1 ("%s: invalid address family %d", caller, family);
    }

  check_have_conf ();
}

/* This barrier ensures that all test threads begin their work
   simultaneously.  */
static pthread_barrier_t barrier;

/* Test gethostbyname2_r (if do_2 is false) or gethostbyname2_r with
   AF_INET (if do_2 is true).  */
static void *
byname (bool do_2)
{
  int this_thread = get_thread_number ();
  xpthread_barrier_wait (&barrier);
  for (int i = 0; i < queries_per_thread; ++i)
    {
      char qname[100];
      snprintf (qname, sizeof (qname), "counter%d.thread%d.example.com",
                i, this_thread);
      struct hostent storage;
      char buf[1000];
      struct hostent *e = NULL;
      int herrno;
      int ret;
      if (do_2)
        ret = gethostbyname_r (qname, &storage, buf, sizeof (buf),
                               &e, &herrno);
      else
        ret = gethostbyname2_r (qname, AF_INET, &storage, buf, sizeof (buf),
                                &e, &herrno);
      check_hostent (__func__, do_2 ? "gethostbyname2_r" : "gethostbyname_r",
                     qname, ret, e, AF_INET, i);
    }
  check_have_conf ();
  return NULL;
}

/* Test gethostbyname_r.  */
static void *
thread_byname (void *closure)
{
  return byname (false);
}

/* Test gethostbyname2_r with AF_INET.  */
static void *
thread_byname2 (void *closure)
{
  return byname (true);
}

/* Test gethostbyname2_r with AF_INET6.  */
static void *
thread_byname2_af_inet6 (void *closure)
{
  int this_thread = get_thread_number ();
  xpthread_barrier_wait (&barrier);
  for (int i = 0; i < queries_per_thread; ++i)
    {
      char qname[100];
      snprintf (qname, sizeof (qname), "counter%d.thread%d.example.com",
                i, this_thread);
      struct hostent storage;
      char buf[1000];
      struct hostent *e = NULL;
      int herrno;
      int ret = gethostbyname2_r (qname, AF_INET6, &storage, buf, sizeof (buf),
                                  &e, &herrno);
      check_hostent (__func__, "gethostbyname2_r", qname, ret, e, AF_INET6, i);
    }
  return NULL;
}

/* Run getaddrinfo tests for FAMILY.  */
static void *
gai (int family)
{
  int this_thread = get_thread_number ();
  xpthread_barrier_wait (&barrier);
  for (int i = 0; i < queries_per_thread; ++i)
    {
      char qname[100];
      snprintf (qname, sizeof (qname), "counter%d.thread%d.example.com",
                i, this_thread);
      struct addrinfo hints =
        {
          .ai_family = family,
          .ai_socktype = SOCK_STREAM,
          .ai_protocol = IPPROTO_TCP,
        };
      struct addrinfo *ai;
      int ret = getaddrinfo (qname, "80", &hints, &ai);
      check_addrinfo (__func__, qname, ret, ai, family, i);
      if (ret == 0)
        freeaddrinfo (ai);
    }
  return NULL;
}

/* Test getaddrinfo with AF_INET.  */
static void *
thread_gai_inet (void *closure)
{
  return gai (AF_INET);
}

/* Test getaddrinfo with AF_INET6.  */
static void *
thread_gai_inet6 (void *closure)
{
  return gai (AF_INET6);
}

/* Test getaddrinfo with AF_UNSPEC.  */
static void *
thread_gai_unspec (void *closure)
{
  return gai (AF_UNSPEC);
}

/* Description of the chroot environment used to run the tests.  */
static struct support_chroot *chroot_env;

/* Set up the chroot environment.  */
static void
prepare (int argc, char **argv)
{
  chroot_env = support_chroot_create
    ((struct support_chroot_configuration)
     {
       .resolv_conf =
         "search example.com\n"
         "nameserver 127.0.0.1\n"
         "nameserver 127.0.0.2\n"
         "nameserver 127.0.0.3\n",
     });
}

static int
do_test (void)
{
  support_become_root ();
  if (!support_enter_network_namespace ())
    return EXIT_UNSUPPORTED;
  if (!support_can_chroot ())
    return EXIT_UNSUPPORTED;

  /* Load the shared object outside of the chroot.  */
  TEST_VERIFY (dlopen (LIBNSS_DNS_SO, RTLD_LAZY) != NULL);

  xchroot (chroot_env->path_chroot);
  TEST_VERIFY_EXIT (chdir ("/") == 0);

  struct sockaddr_in server_address =
    {
      .sin_family = AF_INET,
      .sin_addr = { .s_addr = htonl (INADDR_LOOPBACK) },
      .sin_port = htons (53)
    };
  const struct sockaddr *server_addresses[1] =
    { (const struct sockaddr *) &server_address };

  struct resolv_test *aux = resolv_test_start
    ((struct resolv_redirect_config)
     {
       .response_callback = response,
       .nscount = 1,
       .disable_redirect = true,
       .server_address_overrides = server_addresses,
     });

  enum { thread_count = 6 };
  xpthread_barrier_init (&barrier, NULL, thread_count + 1);
  pthread_t threads[thread_count];
  typedef void *(*thread_func) (void *);
  thread_func thread_funcs[thread_count] =
    {
      thread_byname,
      thread_byname2,
      thread_byname2_af_inet6,
      thread_gai_inet,
      thread_gai_inet6,
      thread_gai_unspec,
    };
  for (int i = 0; i < thread_count; ++i)
    threads[i] = xpthread_create (NULL, thread_funcs[i], NULL);
  xpthread_barrier_wait (&barrier); /* Start the test threads.  */
  for (int i = 0; i < thread_count; ++i)
    xpthread_join (threads[i]);

  resolv_test_end (aux);
  support_chroot_free (chroot_env);

  return 0;
}

#define PREPARE prepare
#include <support/test-driver.c>