root/trunk/pdns/pdns/common_startup.cc @ 135

Revision 135, 11.0 KB (checked in by ahu, 10 years ago)

heap of works

  • Property svn:eol-style set to native
  • Property svn:keywords set to author date id revision
Line 
1/*
2    PowerDNS Versatile Database Driven Nameserver
3    Copyright (C) 2002  PowerDNS.COM BV
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18*/
19#include "common_startup.hh"
20
21typedef Distributor<DNSPacket,DNSPacket,PacketHandler> DNSDistributor;
22
23
24ArgvMap theArg;
25StatBag S;  //!< Statistics are gathered accross PDNS via the StatBag class S
26PacketCache PC; //!< This is the main PacketCache, shared accross all threads
27DNSProxy *DP;
28DynListener *dl;
29CommunicatorClass Communicator;
30UDPNameserver *N;
31int avg_latency;
32TCPNameserver *TN;
33
34ArgvMap &arg()
35{
36  return theArg;
37}
38
39
40void declareArguments()
41{
42  arg().set("local-port","The port on which we listen")="53";
43  arg().setSwitch("log-failed-updates","If PDNS should log failed update requests")="";
44  arg().setSwitch("log-dns-details","If PDNS should log DNS non-erroneous details")="";
45  arg().set("urlredirector","Where we send hosts to that need to be url redirected")="127.0.0.1";
46  arg().set("smtpredirector","Our smtpredir MX host")="a.misconfigured.powerdns.smtp.server";
47  arg().set("local-address","Local IP address to which we bind")="0.0.0.0";
48  arg().set("local-ipv6","Local IP address to which we bind")="";
49  arg().set("max-queue-length","Maximum queuelength before considering situation lost")="5000";
50  arg().set("soa-serial-offset","Make sure that no SOA serial is less than this number")="0";
51  arg().set("only-soa","Make sure that no SOA serial is less than this number")="org";
52  arg().setCmd("help","Provide a helpful message");
53  arg().setCmd("config","Provide a helpful message");
54  arg().setCmd("list-modules","Lists all modules available");
55  arg().setCmd("no-config","Don't parse configuration file");
56 
57  arg().set("control-console","Debugging switch - don't use")="no"; // but I know you will!
58  arg().set("fancy-records","Process URL and MBOXFW records")="no";
59  arg().set("wildcard-url","Process URL and MBOXFW records")="no";
60  arg().set("wildcards","Honor wildcards in the database")="";
61  arg().set("loglevel","Amount of logging. Higher is more. Do not set below 3")="4";
62  arg().set("default-soa-name","name to insert in the SOA record if none set in the backend")="a.misconfigured.powerdns.server";
63  arg().set("distributor-threads","Default number of Distributor (backend) threads to start")="3";
64  arg().set("queue-limit","Maximum number of milliseconds to queue a query")="1500"; 
65  arg().set("recursor","If recursion is desired, IP address of a recursing nameserver")="no"; 
66  arg().set("lazy-recursion","Only recurse if question cannot be answered locally")="yes";
67  arg().set("allow-recursion","List of netmasks that are allowed to recurse")=""; 
68 
69  arg().set("disable-tcp","Do not listen to TCP queries")="no";
70  arg().set("disable-axfr","Do not allow zone transfers")="no";
71 
72  arg().set("config-name","Name of this virtual configuration - will rename the binary image")="";
73
74  arg().set("load-modules","Load this module - supply absolute or relative path")="";
75  arg().set("launch","Which backends to launch and order to query them in")="";
76  arg().setSwitch("disable-axfr","Disable zonetransfers but do allow TCP queries")="no";
77  arg().set("allow-axfr-ips","If disabled, DO allow zonetransfers from these IP addresses")="";
78  arg().set("slave-cycle-interval","Reschedule failed SOA serial checks once every .. seconds")="60";
79 
80  arg().setSwitch("slave","Act as a slave")="no";
81  arg().setSwitch("master","Act as a master")="no";
82  arg().setSwitch("guardian","Run within a guardian process")="no";
83  arg().setSwitch("skip-cname","Do not perform CNAME indirection for each query")="no";
84  arg().setSwitch("strict-rfc-axfrs","Perform strictly rfc compliant axfrs (very slow)")="no";
85 
86  arg().setSwitch("webserver","Start a webserver for monitoring")="no"; 
87  arg().setSwitch("webserver-print-arguments","If the webserver should print arguments")="no"; 
88  arg().set("webserver-address","IP Address of webserver to listen on")="127.0.0.1";
89  arg().set("webserver-port","Port of webserver to listen on")="8081";
90  arg().set("webserver-password","Password required for accessing the webserver")="";
91
92  arg().set("receiver-threads","Number of receiver threads to launch")="1";
93 
94  arg().setSwitch("out-of-zone-additional-processing","Do out of zone additional processing")="no";
95  arg().setSwitch("query-logging","Hint backends that queries should be logged")="no";
96 
97  arg().set("cache-ttl","Seconds to store packets in the PacketCache")="20";
98  arg().set("recursive-cache-ttl","Seconds to store packets in the PacketCache")="10";
99  arg().set("negquery-cache-ttl","Seconds to store packets in the PacketCache")="60";
100  arg().set("query-cache-ttl","Seconds to store packets in the PacketCache")="20";
101  arg().set("soa-minimum-ttl","Default SOA mininum ttl")="3600";
102  arg().set("max-tcp-connections","Maximum number of TCP connections")="10";
103
104  arg().setSwitch( "use-logfile", "Use a log file" )= "no";
105  arg().set( "logfile", "Logfile to use" )= "pdns.log";
106  arg().set("setuid","If set, change user id to this uid for more security")="";
107  arg().set("setgid","If set, change group id to this gid for more security")="";
108}
109
110
111
112void declareStats(void)
113{
114  S.declare("udp-queries","Number of UDP queries received");
115  S.declare("udp-answers","Number of answers sent out over UDP");
116  S.declare("recursing-answers","Number of recursive answers sent out");
117  S.declare("recursing-questions","Number of questions sent to recursor");
118  S.declare("corrupt-packets","Number of corrupt packets received");
119
120  S.declare("tcp-queries","Number of TCP queries received");
121  S.declare("tcp-answers","Number of answers sent out over TCP");
122
123  S.declare("qsize-q","Number of questions waiting for database attention");
124
125  S.declare("deferred-cache-inserts","Amount of cache inserts that were deferred because of maintenance");
126  S.declare("deferred-cache-lookup","Amount of cache lookups that were deferred because of maintenance");
127
128  S.declare("query-cache-hit","Number of hits on the query cache");
129  S.declare("query-cache-miss","Number of misses on the query cache");
130
131
132  S.declare("servfail-packets","Number of times a server-failed packet was sent out");
133  S.declare("latency","Average number of microseconds needed to answer a question");
134  S.declare("timedout-packets","Number of packets which weren't answered within timeout set");
135
136  S.declareRing("queries","UDP Queries Received");
137  S.declareRing("nxdomain-queries","Queries for non-existent records within existent domains");
138  S.declareRing("noerror-queries","Queries for existing records, but for type we don't have");
139  S.declareRing("servfail-queries","Queries that could not be answered due to backend errors");
140  S.declareRing("unauth-queries","Queries for domains that we are not authoritative for");
141  S.declareRing("logmessages","Log Messages");
142  S.declareRing("remotes","Remote server IP addresses");
143  S.declareRing("remotes-unauth","Remote hosts querying domains for which we are not auth");
144  S.declareRing("remotes-corrupt","Remote hosts sending corrupt packets");
145
146}
147
148
149int isGuarded(char **argv)
150{
151  char *p=strstr(argv[0],"-instance");
152
153  return !!p;
154}
155
156
157void sendout(const DNSDistributor::AnswerData &AD)
158{
159  static int &numanswered=*S.getPointer("udp-answers");
160  if(!AD.A)
161    return;
162 
163  N->send(AD.A);
164  numanswered++;
165  int diff=AD.A->d_dt.udiff();
166  avg_latency=(int)(0.999*avg_latency+0.001*diff);
167
168  delete AD.A; 
169
170
171}
172
173
174//! The qthread receives questions over the internet via the Nameserver class, and hands them to the Distributor for futher processing
175void *qthread(void *p)
176{
177  DNSDistributor *D=static_cast<DNSDistributor *>(p);
178
179  DNSPacket *P;
180
181  DNSPacket question;
182  DNSPacket cached;
183
184  int &numreceived=*S.getPointer("udp-queries");
185  int &numanswered=*S.getPointer("udp-answers");
186  numreceived=-1;
187  int diff;
188
189  for(;;) {
190    if(!((numreceived++)%50)) { // maintenance tasks
191      S.set("latency",(int)avg_latency);
192      int qcount, acount;
193      D->getQueueSizes(qcount, acount);
194      S.set("qsize-q",qcount);
195    }
196   
197    if(!(P=N->receive(&question))) { // receive a packet         inline
198      continue;                    // packet was broken, try again
199    }
200
201
202    S.ringAccount("queries", P->qdomain+"/"+P->qtype.getName());
203    S.ringAccount("remotes",P->getRemote());
204
205    if(PC.get(P,&cached)) { // short circuit - does the PacketCache recognize this question?
206      cached.setRemote((struct sockaddr *)(P->remote),P->d_socklen);  // inlined
207      cached.setSocket(P->getSocket());                               // inlined
208      cached.spoofID(P->d.id);                                        // inlined
209      cached.d.rd=P->d.rd; // copy in recursion desired bit
210      cached.commitD(); // commit d to the packet                        inlined
211
212      N->send(&cached);   // answer it then                              inlined
213      diff=P->d_dt.udiff();                                                   
214      avg_latency=(int)(0.999*avg_latency+0.001*diff); // 'EWMA'
215     
216      numanswered++;
217      continue;
218    }
219
220    D->question(P, &sendout); // otherwise, give to the distributor
221  }
222  return 0;
223}
224
225
226void mainthread()
227{
228  Utility::srandom(time(0));
229
230   int newgid=0;     
231   if(!arg()["setgid"].empty()) 
232     newgid=Utility::makeGidNumeric(arg()["setgid"]);     
233   int newuid=0;     
234   if(!arg()["setuid"].empty())       
235     newuid=Utility::makeUidNumeric(arg()["setuid"]); 
236#ifndef WIN32
237   if(!arg()["chroot"].empty()) { 
238     if(chroot(arg()["chroot"].c_str())<0) {
239       L<<Logger::Error<<"Unable to chroot to '"+arg()["chroot"]+"': "<<strerror(errno)<<", exiting"<<endl; 
240       exit(1);
241     }   
242     else
243       L<<Logger::Error<<"Chrooted to '"<<arg()["chroot"]<<"'"<<endl;     
244   } 
245#endif
246
247   Utility::dropPrivs(newuid, newgid);
248
249  if(arg().mustDo("recursor")){
250    DP=new DNSProxy(arg()["recursor"]);
251    DP->onlyFrom(arg()["allow-recursion"]);
252    DP->go();
253  }
254  // NOW SAFE TO CREATE THREADS!
255  dl->go();
256
257  pthread_t qtid;
258  StatWebServer sws;
259
260  if(arg()["webserver"]!="no") 
261    sws.go();
262 
263  if(arg().mustDo("slave") || arg().mustDo("master"))
264    Communicator.go(); 
265
266  if(TN)
267    TN->go(); // tcp nameserver launch
268   
269  //  fork(); (this worked :-))
270  for(int n=0;n<arg().asNum("receiver-threads");++n) {
271    DNSDistributor *D= new DNSDistributor(arg().asNum("distributor-threads")); // the big dispatcher!
272    pthread_create(&qtid,0,qthread,static_cast<void *>(D)); // receives packets
273  }
274
275  void *p;
276  pthread_join(qtid, &p);
277 
278  L<<Logger::Error<<"Mainthread exiting - should never happen"<<endl;
279}
280
281
282
283
Note: See TracBrowser for help on using the browser.