use _exit to exit a child
[metze/samba/wip.git] / source3 / nmbd / asyncdns.c
1 /*
2    Unix SMB/Netbios implementation.
3    a async DNS handler
4    Copyright (C) Andrew Tridgell 1997
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19    */
20
21 #include "includes.h"
22
23 extern int DEBUGLEVEL;
24
25 /***************************************************************************
26   Add a DNS result to the name cache.
27 ****************************************************************************/
28
29 static struct name_record *add_dns_result(struct nmb_name *question, struct in_addr addr)
30 {
31   int name_type = question->name_type;
32   char *qname = question->name;
33   
34   
35   if (!addr.s_addr) {
36     /* add the fail to WINS cache of names. give it 1 hour in the cache */
37     DEBUG(3,("add_dns_result: Negative DNS answer for %s\n", qname));
38     add_name_to_subnet(wins_server_subnet,qname,name_type,
39                        NB_ACTIVE, 60*60, DNSFAIL_NAME, 1, &addr);
40     return NULL;
41   }
42
43   /* add it to our WINS cache of names. give it 2 hours in the cache */
44   DEBUG(3,("add_dns_result: DNS gave answer for %s of %s\n", qname, inet_ntoa(addr)));
45
46   return add_name_to_subnet(wins_server_subnet,qname,name_type,
47                             NB_ACTIVE, 2*60*60, DNS_NAME, 1, &addr);
48 }
49
50
51
52 #ifndef SYNC_DNS
53
54 static int fd_in = -1, fd_out = -1;
55 static int child_pid = -1;
56 static int in_dns;
57
58 /* this is the structure that is passed between the parent and child */
59 struct query_record {
60         struct nmb_name name;
61         struct in_addr result;
62 };
63
64 /* a queue of pending requests waiting to be sent to the DNS child */
65 static struct packet_struct *dns_queue;
66
67 /* the packet currently being processed by the dns child */
68 static struct packet_struct *dns_current;
69
70
71 /***************************************************************************
72   return the fd used to gather async dns replies. This is added to the select
73   loop
74   ****************************************************************************/
75 int asyncdns_fd(void)
76 {
77         return fd_in;
78 }
79
80 /***************************************************************************
81   handle DNS queries arriving from the parent
82   ****************************************************************************/
83 static void asyncdns_process(void)
84 {
85         struct query_record r;
86         fstring qname;
87
88         DEBUGLEVEL = -1;
89
90         while (1) {
91                 if (read_data(fd_in, (char *)&r, sizeof(r)) != sizeof(r)) 
92                         break;
93
94                 fstrcpy(qname, r.name.name);
95
96                 r.result.s_addr = interpret_addr(qname);
97
98                 if (write_data(fd_out, (char *)&r, sizeof(r)) != sizeof(r))
99                         break;
100         }
101
102         _exit(0);
103 }
104
105 /**************************************************************************** **
106   catch a sigterm
107   We need a separate term handler here so we don't release any 
108   names that our parent is going to release, or overwrite a 
109   WINS db that our parent is going to write.
110  **************************************************************************** */
111
112 static int sig_term()
113 {
114   _exit(0);
115   /* Keep compiler happy.. */
116   return 0;
117 }
118
119 /***************************************************************************
120   create a child process to handle DNS lookups
121   ****************************************************************************/
122 void start_async_dns(void)
123 {
124         int fd1[2], fd2[2];
125
126         signal(SIGCLD, SIG_IGN);
127
128         if (pipe(fd1) || pipe(fd2)) {
129                 return;
130         }
131
132         child_pid = fork();
133
134         if (child_pid) {
135                 fd_in = fd1[0];
136                 fd_out = fd2[1];
137                 close(fd1[1]);
138                 close(fd2[0]);
139                 return;
140         }
141
142         fd_in = fd2[0];
143         fd_out = fd1[1];
144
145         signal(SIGUSR2, SIG_IGN);
146         signal(SIGUSR1, SIG_IGN);
147         signal(SIGHUP, SIG_IGN);
148         signal(SIGTERM, SIGNAL_CAST sig_term );
149
150         asyncdns_process();
151 }
152
153
154 /***************************************************************************
155 check if a particular name is already being queried
156   ****************************************************************************/
157 static BOOL query_current(struct query_record *r)
158 {
159         return dns_current &&
160                 nmb_name_equal(&r->name, 
161                            &dns_current->packet.nmb.question.question_name);
162 }
163
164
165 /***************************************************************************
166   write a query to the child process
167   ****************************************************************************/
168 static BOOL write_child(struct packet_struct *p)
169 {
170         struct query_record r;
171
172         r.name = p->packet.nmb.question.question_name;
173
174         return write_data(fd_out, (char *)&r, sizeof(r)) == sizeof(r);
175 }
176
177 /***************************************************************************
178   check the DNS queue
179   ****************************************************************************/
180 void run_dns_queue(void)
181 {
182         struct query_record r;
183         struct packet_struct *p, *p2;
184         struct name_record *namerec;
185         int size;
186
187         if (fd_in == -1)
188                 return;
189
190         if (!process_exists(child_pid)) {
191                 close(fd_in);
192                 start_async_dns();
193         }
194
195         if ((size=read_data(fd_in, (char *)&r, sizeof(r))) != sizeof(r)) {
196                 if (size) {
197                         DEBUG(0,("Incomplete DNS answer from child!\n"));
198                         fd_in = -1;
199                 }
200                 return;
201         }
202
203         namerec = add_dns_result(&r.name, r.result);
204
205         if (dns_current) {
206                 if (query_current(&r)) {
207                         DEBUG(3,("DNS calling send_wins_name_query_response\n"));
208                         in_dns = 1;
209                         if(namerec == NULL)
210                           send_wins_name_query_response(NAM_ERR, dns_current, NULL);
211                         else
212                           send_wins_name_query_response(0,dns_current,namerec);
213                         in_dns = 0;
214                 }
215
216                 dns_current->locked = False;
217                 free_packet(dns_current);
218                 dns_current = NULL;
219         }
220
221         /* loop over the whole dns queue looking for entries that
222            match the result we just got */
223         for (p = dns_queue; p;) {
224                 struct nmb_packet *nmb = &p->packet.nmb;
225                 struct nmb_name *question = &nmb->question.question_name;
226
227                 if (nmb_name_equal(question, &r.name)) {
228                         DEBUG(3,("DNS calling send_wins_name_query_response\n"));
229                         in_dns = 1;
230                         if(namerec == NULL)
231                           send_wins_name_query_response(NAM_ERR, p, NULL);
232                         else
233                           send_wins_name_query_response(0,p,namerec);
234                         in_dns = 0;
235                         p->locked = False;
236
237                         if (p->prev)
238                                 p->prev->next = p->next;
239                         else
240                                 dns_queue = p->next;
241                         if (p->next)
242                                 p->next->prev = p->prev;
243                         p2 = p->next;
244                         free_packet(p);
245                         p = p2;
246                 } else {
247                         p = p->next;
248                 }
249         }
250
251         if (dns_queue) {
252                 dns_current = dns_queue;
253                 dns_queue = dns_queue->next;
254                 if (dns_queue) dns_queue->prev = NULL;
255                 dns_current->next = NULL;
256
257                 if (!write_child(dns_current)) {
258                         DEBUG(3,("failed to send DNS query to child!\n"));
259                         return;
260                 }
261         }
262
263 }
264
265 /***************************************************************************
266 queue a DNS query
267   ****************************************************************************/
268 BOOL queue_dns_query(struct packet_struct *p,struct nmb_name *question,
269                      struct name_record **n)
270 {
271         if (in_dns || fd_in == -1)
272                 return False;
273
274         if (!dns_current) {
275                 if (!write_child(p)) {
276                         DEBUG(3,("failed to send DNS query to child!\n"));
277                         return False;
278                 }
279                 dns_current = p;
280                 p->locked = True;
281         } else {
282                 p->locked = True;
283                 p->next = dns_queue;
284                 p->prev = NULL;
285                 if (p->next)
286                         p->next->prev = p;
287                 dns_queue = p;
288         }
289
290         DEBUG(3,("added DNS query for %s\n", namestr(question)));
291         return True;
292 }
293
294 #else
295
296
297 /***************************************************************************
298   we use this then we can't do async DNS lookups
299   ****************************************************************************/
300 BOOL queue_dns_query(struct packet_struct *p,struct nmb_name *question,
301                      struct name_record **n)
302 {
303         char *qname = question->name;
304         struct in_addr dns_ip;
305
306         DEBUG(3,("DNS search for %s - ", namestr(question)));
307
308         dns_ip.s_addr = interpret_addr(qname);
309
310         *n = add_dns_result(question, dns_ip);
311         if(*n == NULL)
312           send_wins_name_query_response(NAM_ERR, p, NULL);
313         else
314           send_wins_name_query_response(0, p, *n);
315         return False;
316 }
317 #endif