source: branches/samba-3.3.x/source/nmbd/asyncdns.c@ 1075

Last change on this file since 1075 was 699, checked in by Silvan Scherrer, 13 years ago

Samba Server 3.3: made it high-mem aware

File size: 10.0 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 a async DNS handler
4 Copyright (C) Andrew Tridgell 1997-1998
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program 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
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19#ifdef __OS2__
20#define pipe(A) os2_pipe(A)
21#endif
22
23#include "includes.h"
24
25/***************************************************************************
26 Add a DNS result to the name cache.
27****************************************************************************/
28
29static struct name_record *add_dns_result(struct nmb_name *question, struct in_addr addr)
30{
31 int name_type = question->name_type;
32 unstring qname;
33
34 pull_ascii_nstring(qname, sizeof(qname), question->name);
35
36 if (!addr.s_addr) {
37 /* add the fail to WINS cache of names. give it 1 hour in the cache */
38 DEBUG(3,("add_dns_result: Negative DNS answer for %s\n", qname));
39 add_name_to_subnet( wins_server_subnet, qname, name_type,
40 NB_ACTIVE, 60*60, DNSFAIL_NAME, 1, &addr );
41 return NULL;
42 }
43
44 /* add it to our WINS cache of names. give it 2 hours in the cache */
45 DEBUG(3,("add_dns_result: DNS gave answer for %s of %s\n", qname, inet_ntoa(addr)));
46
47 add_name_to_subnet( wins_server_subnet, qname, name_type,
48 NB_ACTIVE, 2*60*60, DNS_NAME, 1, &addr);
49
50 return find_name_on_subnet(wins_server_subnet, question, FIND_ANY_NAME);
51}
52
53#ifndef SYNC_DNS
54
55static int fd_in = -1, fd_out = -1;
56static pid_t child_pid = -1;
57static int in_dns;
58
59/* this is the structure that is passed between the parent and child */
60struct query_record {
61 struct nmb_name name;
62 struct in_addr result;
63};
64
65/* a queue of pending requests waiting to be sent to the DNS child */
66static struct packet_struct *dns_queue;
67
68/* the packet currently being processed by the dns child */
69static struct packet_struct *dns_current;
70
71
72/***************************************************************************
73 return the fd used to gather async dns replies. This is added to the select
74 loop
75 ****************************************************************************/
76
77int asyncdns_fd(void)
78{
79 return fd_in;
80}
81
82/***************************************************************************
83 handle DNS queries arriving from the parent
84 ****************************************************************************/
85static void asyncdns_process(void)
86{
87 struct query_record r;
88 unstring qname;
89
90 DEBUGLEVEL = -1;
91
92 while (1) {
93 NTSTATUS status;
94
95 status = read_data(fd_in, (char *)&r, sizeof(r));
96
97 if (!NT_STATUS_IS_OK(status)) {
98 break;
99 }
100
101 pull_ascii_nstring( qname, sizeof(qname), r.name.name);
102 r.result.s_addr = interpret_addr(qname);
103
104 if (write_data(fd_out, (char *)&r, sizeof(r)) != sizeof(r))
105 break;
106 }
107
108 _exit(0);
109}
110
111/**************************************************************************** **
112 catch a sigterm (in the child process - the parent has a different handler
113 see nmbd.c for details).
114 We need a separate term handler here so we don't release any
115 names that our parent is going to release, or overwrite a
116 WINS db that our parent is going to write.
117 **************************************************************************** */
118
119static void sig_term(int sig)
120{
121 _exit(0);
122}
123
124/***************************************************************************
125 Called by the parent process when it receives a SIGTERM - also kills the
126 child so we don't get child async dns processes lying around, causing trouble.
127 ****************************************************************************/
128
129void kill_async_dns_child(void)
130{
131 if (child_pid > 0) {
132 kill(child_pid, SIGTERM);
133 child_pid = -1;
134 }
135}
136
137/***************************************************************************
138 create a child process to handle DNS lookups
139 ****************************************************************************/
140void start_async_dns(void)
141{
142 int fd1[2], fd2[2];
143
144 CatchChild();
145
146 if (pipe(fd1) || pipe(fd2)) {
147 DEBUG(0,("can't create asyncdns pipes\n"));
148 return;
149 }
150 child_pid = sys_fork();
151
152 if (child_pid) {
153 fd_in = fd1[0];
154 fd_out = fd2[1];
155 close(fd1[1]);
156 close(fd2[0]);
157 DEBUG(0,("started asyncdns process %d\n", (int)child_pid));
158 return;
159 }
160
161 fd_in = fd2[0];
162 fd_out = fd1[1];
163
164 CatchSignal(SIGUSR2, SIG_IGN);
165 CatchSignal(SIGUSR1, SIG_IGN);
166 CatchSignal(SIGHUP, SIG_IGN);
167 CatchSignal(SIGTERM, SIGNAL_CAST sig_term );
168
169 if (!reinit_after_fork(nmbd_messaging_context(),
170 nmbd_event_context(), true)) {
171 DEBUG(0,("reinit_after_fork() failed\n"));
172 smb_panic("reinit_after_fork() failed");
173 }
174
175 asyncdns_process();
176}
177
178
179/***************************************************************************
180check if a particular name is already being queried
181 ****************************************************************************/
182static bool query_current(struct query_record *r)
183{
184 return dns_current &&
185 nmb_name_equal(&r->name,
186 &dns_current->packet.nmb.question.question_name);
187}
188
189
190/***************************************************************************
191 write a query to the child process
192 ****************************************************************************/
193static bool write_child(struct packet_struct *p)
194{
195 struct query_record r;
196
197 r.name = p->packet.nmb.question.question_name;
198
199 return write_data(fd_out, (char *)&r, sizeof(r)) == sizeof(r);
200}
201
202/***************************************************************************
203 check the DNS queue
204 ****************************************************************************/
205void run_dns_queue(void)
206{
207 struct query_record r;
208 struct packet_struct *p, *p2;
209 struct name_record *namerec;
210 NTSTATUS status;
211
212 if (fd_in == -1)
213 return;
214
215 /* Allow SIGTERM to kill us. */
216 BlockSignals(False, SIGTERM);
217
218 if (!process_exists_by_pid(child_pid)) {
219 close(fd_in);
220 close(fd_out);
221 start_async_dns();
222 }
223
224 status = read_data(fd_in, (char *)&r, sizeof(r));
225
226 if (!NT_STATUS_IS_OK(status)) {
227 DEBUG(0, ("read from child failed: %s\n", nt_errstr(status)));
228 fd_in = -1;
229 BlockSignals(True, SIGTERM);
230 return;
231 }
232
233 BlockSignals(True, SIGTERM);
234
235 namerec = add_dns_result(&r.name, r.result);
236
237 if (dns_current) {
238 if (query_current(&r)) {
239 DEBUG(3,("DNS calling send_wins_name_query_response\n"));
240 in_dns = 1;
241 if(namerec == NULL)
242 send_wins_name_query_response(NAM_ERR, dns_current, NULL);
243 else
244 send_wins_name_query_response(0,dns_current,namerec);
245 in_dns = 0;
246 }
247
248 dns_current->locked = False;
249 free_packet(dns_current);
250 dns_current = NULL;
251 }
252
253 /* loop over the whole dns queue looking for entries that
254 match the result we just got */
255 for (p = dns_queue; p;) {
256 struct nmb_packet *nmb = &p->packet.nmb;
257 struct nmb_name *question = &nmb->question.question_name;
258
259 if (nmb_name_equal(question, &r.name)) {
260 DEBUG(3,("DNS calling send_wins_name_query_response\n"));
261 in_dns = 1;
262 if(namerec == NULL)
263 send_wins_name_query_response(NAM_ERR, p, NULL);
264 else
265 send_wins_name_query_response(0,p,namerec);
266 in_dns = 0;
267 p->locked = False;
268
269 if (p->prev)
270 p->prev->next = p->next;
271 else
272 dns_queue = p->next;
273 if (p->next)
274 p->next->prev = p->prev;
275 p2 = p->next;
276 free_packet(p);
277 p = p2;
278 } else {
279 p = p->next;
280 }
281 }
282
283 if (dns_queue) {
284 dns_current = dns_queue;
285 dns_queue = dns_queue->next;
286 if (dns_queue)
287 dns_queue->prev = NULL;
288 dns_current->next = NULL;
289
290 if (!write_child(dns_current)) {
291 DEBUG(3,("failed to send DNS query to child!\n"));
292 return;
293 }
294 }
295}
296
297/***************************************************************************
298queue a DNS query
299 ****************************************************************************/
300
301bool queue_dns_query(struct packet_struct *p,struct nmb_name *question)
302{
303 if (in_dns || fd_in == -1)
304 return False;
305
306 if (!dns_current) {
307 if (!write_child(p)) {
308 DEBUG(3,("failed to send DNS query to child!\n"));
309 return False;
310 }
311 dns_current = p;
312 p->locked = True;
313 } else {
314 p->locked = True;
315 p->next = dns_queue;
316 p->prev = NULL;
317 if (p->next)
318 p->next->prev = p;
319 dns_queue = p;
320 }
321
322 DEBUG(3,("added DNS query for %s\n", nmb_namestr(question)));
323 return True;
324}
325
326#else
327
328
329/***************************************************************************
330 we use this when we can't do async DNS lookups
331 ****************************************************************************/
332
333bool queue_dns_query(struct packet_struct *p,struct nmb_name *question)
334{
335 struct name_record *namerec = NULL;
336 struct in_addr dns_ip;
337 unstring qname;
338
339 pull_ascii_nstring(qname, sizeof(qname), question->name);
340
341 DEBUG(3,("DNS search for %s - ", nmb_namestr(question)));
342
343 /* Unblock TERM signal so we can be killed in DNS lookup. */
344 BlockSignals(False, SIGTERM);
345
346 dns_ip.s_addr = interpret_addr(qname);
347
348 /* Re-block TERM signal. */
349 BlockSignals(True, SIGTERM);
350
351 namerec = add_dns_result(question, dns_ip);
352 if(namerec == NULL) {
353 send_wins_name_query_response(NAM_ERR, p, NULL);
354 } else {
355 send_wins_name_query_response(0, p, namerec);
356 }
357 return False;
358}
359
360/***************************************************************************
361 With sync dns there is no child to kill on SIGTERM.
362 ****************************************************************************/
363
364void kill_async_dns_child(void)
365{
366 return;
367}
368#endif
Note: See TracBrowser for help on using the repository browser.