#include "copyright.h"
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>
#include "externs.h"
#include "debug.h"
Include dependency graph for dnschild.c:
Go to the source code of this file.
Data Structures | |
struct | dns_query_state_t |
Functions | |
static void | dnschild_finish (int fd, short event, void *arg) |
int | dnschild_init () |
void * | dnschild_request (DESC *d) |
void | dnschild_destruct () |
void | dnschild_kill (void *arg) |
Variables | |
static struct dns_query_state_t * | running |
static int | running_queries = 0 |
static int | dnschild_state = 0 |
static struct timeval | query_timeout = { 60, 0 } |
void dnschild_destruct | ( | ) |
Definition at line 102 of file dnschild.c.
References descriptor_data::addr, dns_query_state_t::desc, dnschild_state, dprintk, dns_query_state_t::ev, dns_query_state_t::fd, dns_query_state_t::next, descriptor_data::outstanding_dnschild_query, and running.
Referenced by shutdown_services().
00103 { 00104 struct dns_query_state_t *dqst; 00105 dprintk("dnschild expiring queries and shutting down."); 00106 dnschild_state = 0; 00107 while (running) { 00108 dqst = running; 00109 dprintk("dnschild query for %s aborting early.", dqst->desc->addr); 00110 if(event_pending(&dqst->ev, EV_READ, NULL)) 00111 event_del(&dqst->ev); 00112 close(dqst->fd); 00113 dqst->desc->outstanding_dnschild_query = NULL; 00114 running = running->next; 00115 free(dqst); 00116 } 00117 }
static void dnschild_finish | ( | int | fd, | |
short | event, | |||
void * | arg | |||
) | [static] |
Definition at line 145 of file dnschild.c.
References descriptor_data::addr, dns_query_state_t::desc, dnschild_state, dprintk, log_perror(), dns_query_state_t::next, descriptor_data::outstanding_dnschild_query, dns_query_state_t::pid, and running.
Referenced by dnschild_request().
00146 { 00147 char buffer[2048]; 00148 struct dns_query_state_t *dqst = (struct dns_query_state_t *) arg, *iter; 00149 00150 iter = running; 00151 if(running == dqst) { 00152 running = dqst->next; 00153 } else { 00154 while (iter) { 00155 if(iter->next == dqst) { 00156 iter->next = dqst->next; 00157 break; 00158 } 00159 iter = iter->next; 00160 } 00161 } 00162 00163 dqst->desc->outstanding_dnschild_query = NULL; 00164 00165 if(event & EV_TIMEOUT || !dnschild_state) { 00166 kill(dqst->pid, SIGTERM); 00167 log_perror("DNS", "ERR", NULL, "dnschild failed to finish."); 00168 close(fd); 00169 free(dqst); 00170 return; 00171 } 00172 00173 read(fd, buffer, 2048); 00174 00175 if(buffer[0] == '0') { 00176 dprintk("dnschild failed with error: %s", buffer + 1); 00177 close(fd); 00178 free(dqst); 00179 return; 00180 } 00181 00182 strncpy(dqst->desc->addr, buffer + 1, sizeof(dqst->desc->addr) - 1); 00183 dprintk("dnschild resolved %s correctly.", buffer + 1); 00184 close(fd); 00185 free(dqst); 00186 return; 00187 }
int dnschild_init | ( | ) |
Definition at line 41 of file dnschild.c.
References dnschild_state, and dprintk.
Referenced by main().
00042 { 00043 dprintk("dnschild initialized."); 00044 dnschild_state = 1; 00045 return 1; 00046 }
void dnschild_kill | ( | void * | arg | ) |
Definition at line 119 of file dnschild.c.
References descriptor_data::addr, dns_query_state_t::desc, dprintk, dns_query_state_t::ev, dns_query_state_t::fd, dns_query_state_t::next, descriptor_data::outstanding_dnschild_query, and running.
Referenced by close_sockets(), and shutdownsock().
00120 { 00121 struct dns_query_state_t *dqst = (struct dns_query_state_t *) arg, *iter; 00122 00123 dprintk("dnschild query for %s aborting early.", dqst->desc->addr); 00124 00125 iter = running; 00126 if(running == dqst) { 00127 running = dqst->next; 00128 } else { 00129 while (iter) { 00130 if(iter->next == dqst) { 00131 iter->next = dqst->next; 00132 break; 00133 } 00134 iter = iter->next; 00135 } 00136 } 00137 00138 if(event_pending(&dqst->ev, EV_READ, NULL)) 00139 event_del(&dqst->ev); 00140 dqst->desc->outstanding_dnschild_query = NULL; 00141 close(dqst->fd); 00142 free(dqst); 00143 }
void* dnschild_request | ( | DESC * | d | ) |
Definition at line 48 of file dnschild.c.
References dns_query_state_t::desc, dnschild_finish(), dnschild_state, dprintk, dns_query_state_t::ev, dns_query_state_t::fd, log_perror(), dns_query_state_t::next, dns_query_state_t::pid, query_timeout, running, running_queries, descriptor_data::saddr, descriptor_data::saddr_len, and unbind_signals().
Referenced by initializesock(), load_restart_db(), and load_restart_db_xdr().
00049 { 00050 int fds[2]; 00051 struct dns_query_state_t *dqst; 00052 char address[1024]; 00053 char outbuffer[255]; 00054 int length; 00055 int result; 00056 00057 if(!dnschild_state) { 00058 dprintk("bailing query, dnschild is shut down."); 00059 return 0; 00060 } 00061 00062 dqst = malloc(sizeof(struct dns_query_state_t)); 00063 memset(dqst, 0, sizeof(struct dns_query_state_t)); 00064 00065 dqst->desc = d; 00066 00067 if(pipe(fds) < 0) { 00068 log_perror("DNS", "ERR", NULL, "pipe"); 00069 } 00070 00071 event_set(&dqst->ev, fds[0], EV_TIMEOUT | EV_READ, dnschild_finish, dqst); 00072 event_add(&dqst->ev, &query_timeout); 00073 00074 dqst->pid = fork(); 00075 if(dqst->pid == 0) { 00076 close(fds[0]); 00077 /* begin child section */ 00078 unbind_signals(); 00079 memset(address, 0, 1023); 00080 if((result=getnameinfo((struct sockaddr *) &d->saddr, d->saddr_len, 00081 address, 1023, NULL, 0, NI_NAMEREQD))) { 00082 length = snprintf(outbuffer, 255, "0%s/%s", gai_strerror(result), strerror(errno)); 00083 outbuffer[length++] = '\0'; 00084 write(fds[1], outbuffer, length); 00085 } else { 00086 length = snprintf(outbuffer, 255, "1%s", address); 00087 outbuffer[length++] = '\0'; 00088 write(fds[1], outbuffer, length); 00089 } 00090 close(fds[1]); 00091 exit(0); 00092 /* end child section */ 00093 } 00094 dqst->fd = fds[0]; 00095 close(fds[1]); 00096 dqst->next = running; 00097 running = dqst; 00098 running_queries++; 00099 return (void *) dqst; 00100 }
int dnschild_state = 0 [static] |
Definition at line 35 of file dnschild.c.
Referenced by dnschild_destruct(), dnschild_finish(), dnschild_init(), and dnschild_request().
struct timeval query_timeout = { 60, 0 } [static] |
struct dns_query_state_t * running [static] |
Referenced by dnschild_destruct(), dnschild_finish(), dnschild_kill(), dnschild_request(), and Dump_Mech().
int running_queries = 0 [static] |