File: nameserv_async.c

package info (click to toggle)
chrony 3.0-4+deb9u2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,496 kB
  • sloc: ansic: 23,531; sh: 2,022; yacc: 866; makefile: 195
file content (133 lines) | stat: -rw-r--r-- 3,434 bytes parent folder | download
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
/*
  chronyd/chronyc - Programs for keeping computer clocks accurate.

 **********************************************************************
 * Copyright (C) Miroslav Lichvar  2014
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 * 
 * This program 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
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 **********************************************************************

  =======================================================================

  Functions to asynchronously convert name to IP address

  */

#include "config.h"
#include "sysincl.h"

#include "nameserv_async.h"
#include "logging.h"
#include "memory.h"
#include "privops.h"
#include "sched.h"
#include "util.h"

#ifdef USE_PTHREAD_ASYNCDNS
#include <pthread.h>

/* ================================================== */

struct DNS_Async_Instance {
  const char *name;
  DNS_Status status;
  IPAddr addresses[DNS_MAX_ADDRESSES];
  DNS_NameResolveHandler handler;
  void *arg;

  pthread_t thread;
  int pipe[2];
};

static int resolving_threads = 0;

/* ================================================== */

static void *
start_resolving(void *anything)
{
  struct DNS_Async_Instance *inst = (struct DNS_Async_Instance *)anything;

  inst->status = PRV_Name2IPAddress(inst->name, inst->addresses, DNS_MAX_ADDRESSES);

  /* Notify the main thread that the result is ready */
  if (write(inst->pipe[1], "", 1) < 0)
    ;

  return NULL;
}

/* ================================================== */

static void
end_resolving(int fd, int event, void *anything)
{
  struct DNS_Async_Instance *inst = (struct DNS_Async_Instance *)anything;
  int i;

  if (pthread_join(inst->thread, NULL)) {
    LOG_FATAL(LOGF_Nameserv, "pthread_join() failed");
  }

  resolving_threads--;

  SCH_RemoveFileHandler(inst->pipe[0]);
  close(inst->pipe[0]);
  close(inst->pipe[1]);

  for (i = 0; inst->status == DNS_Success && i < DNS_MAX_ADDRESSES &&
              inst->addresses[i].family != IPADDR_UNSPEC; i++)
    ;

  (inst->handler)(inst->status, i, inst->addresses, inst->arg);

  Free(inst);
}

/* ================================================== */

void
DNS_Name2IPAddressAsync(const char *name, DNS_NameResolveHandler handler, void *anything)
{
  struct DNS_Async_Instance *inst;

  inst = MallocNew(struct DNS_Async_Instance);
  inst->name = name;
  inst->handler = handler;
  inst->arg = anything;
  inst->status = DNS_Failure;

  if (pipe(inst->pipe)) {
    LOG_FATAL(LOGF_Nameserv, "pipe() failed");
  }

  UTI_FdSetCloexec(inst->pipe[0]);
  UTI_FdSetCloexec(inst->pipe[1]);

  resolving_threads++;
  assert(resolving_threads <= 1);

  if (pthread_create(&inst->thread, NULL, start_resolving, inst)) {
    LOG_FATAL(LOGF_Nameserv, "pthread_create() failed");
  }

  SCH_AddFileHandler(inst->pipe[0], SCH_FILE_INPUT, end_resolving, inst);
}

/* ================================================== */

#else
#error
#endif