2 chronyd/chronyc - Programs for keeping computer clocks accurate.
4 **********************************************************************
5 * Copyright (C) Miroslav Lichvar 2014
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 **********************************************************************
22 =======================================================================
24 Functions to asynchronously convert name to IP address
31 #include "nameserv_async.h"
38 #ifdef USE_PTHREAD_ASYNCDNS
41 /* ================================================== */
43 struct DNS_Async_Instance
{
46 IPAddr addresses
[DNS_MAX_ADDRESSES
];
47 DNS_NameResolveHandler handler
;
54 static pthread_mutex_t privops_lock
= PTHREAD_MUTEX_INITIALIZER
;
56 /* ================================================== */
59 start_resolving(void *anything
)
61 struct DNS_Async_Instance
*inst
= (struct DNS_Async_Instance
*)anything
;
63 pthread_mutex_lock(&privops_lock
);
64 inst
->status
= PRV_Name2IPAddress(inst
->name
, inst
->addresses
, DNS_MAX_ADDRESSES
);
65 pthread_mutex_unlock(&privops_lock
);
67 /* Notify the main thread that the result is ready */
68 if (write(inst
->pipe
[1], "", 1) < 0)
74 /* ================================================== */
77 end_resolving(int fd
, int event
, void *anything
)
79 struct DNS_Async_Instance
*inst
= (struct DNS_Async_Instance
*)anything
;
82 if (pthread_join(inst
->thread
, NULL
)) {
83 LOG_FATAL("pthread_join() failed");
86 SCH_RemoveFileHandler(inst
->pipe
[0]);
90 for (i
= 0; inst
->status
== DNS_Success
&& i
< DNS_MAX_ADDRESSES
&&
91 inst
->addresses
[i
].family
!= IPADDR_UNSPEC
; i
++)
94 (inst
->handler
)(inst
->status
, i
, inst
->addresses
, inst
->arg
);
99 /* ================================================== */
102 DNS_Name2IPAddressAsync(const char *name
, DNS_NameResolveHandler handler
, void *anything
)
104 struct DNS_Async_Instance
*inst
;
106 inst
= MallocNew(struct DNS_Async_Instance
);
108 inst
->handler
= handler
;
109 inst
->arg
= anything
;
110 inst
->status
= DNS_Failure
;
112 if (pipe(inst
->pipe
)) {
113 LOG_FATAL("pipe() failed");
116 UTI_FdSetCloexec(inst
->pipe
[0]);
117 UTI_FdSetCloexec(inst
->pipe
[1]);
119 if (pthread_create(&inst
->thread
, NULL
, start_resolving
, inst
)) {
120 LOG_FATAL("pthread_create() failed");
123 SCH_AddFileHandler(inst
->pipe
[0], SCH_FILE_INPUT
, end_resolving
, inst
);
126 /* ================================================== */