]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/rec_control.cc
3f6425fb11e4bbb935fcc9bb599b905ba5406a03
[thirdparty/pdns.git] / pdns / rec_control.cc
1 /*
2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2006 - 2011 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 version 2 as
7 published by the Free Software Foundation
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 #include "rec_channel.hh"
19 #include <iostream>
20 #include "pdnsexception.hh"
21 #include "arguments.hh"
22 #include "config.h"
23
24 #include "namespaces.hh"
25
26 #ifndef RECURSOR
27 #include "statbag.hh"
28 StatBag S;
29 #endif
30
31 ArgvMap &arg()
32 {
33 static ArgvMap arg;
34 return arg;
35 }
36
37 static void initArguments(int argc, char** argv)
38 {
39 arg().set("config-dir","Location of configuration directory (recursor.conf)")=SYSCONFDIR;
40
41 arg().set("socket-dir","Where the controlsocket will live")=LOCALSTATEDIR;
42 arg().set("process","When controlling multiple recursors, the target process number")="";
43 arg().set("timeout", "Number of seconds to wait for the recursor to respond")="5";
44 arg().set("config-name","Name of this virtual configuration - will rename the binary image")="";
45 arg().setCmd("help","Provide this helpful message");
46
47 arg().laxParse(argc,argv);
48 if(arg().getCommands().empty() || arg().mustDo("help")) {
49 cerr<<"syntax: rec_control [options] command, options as below: "<<endl<<endl;
50 cerr<<arg().helpstring(arg()["help"])<<endl;
51 exit(99);
52 }
53
54 string configname=::arg()["config-dir"]+"/recursor.conf";
55 if (::arg()["config-name"] != "")
56 configname=::arg()["config-dir"]+"/recursor-"+::arg()["config-name"]+".conf";
57
58 cleanSlashes(configname);
59
60 if(!::arg().preParseFile(configname.c_str(), "socket-dir", LOCALSTATEDIR))
61 cerr<<"Warning: unable to parse configuration file '"<<configname<<"'"<<endl;
62 arg().laxParse(argc,argv); // make sure the commandline wins
63 }
64
65 int main(int argc, char** argv)
66 try
67 {
68 initArguments(argc, argv);
69 RecursorControlChannel rccS;
70 string sockname="pdns_recursor";
71
72 if (arg()["config-name"] != "")
73 sockname+="-"+arg()["config-name"];
74
75 if(!arg()["process"].empty())
76 sockname+="."+arg()["process"];
77
78 sockname.append(".controlsocket");
79
80 rccS.connect(arg()["socket-dir"], sockname);
81
82 const vector<string>&commands=arg().getCommands();
83 string command;
84 for(unsigned int i=0; i< commands.size(); ++i) {
85 if(i>0)
86 command+=" ";
87 command+=commands[i];
88 }
89 rccS.send(command);
90 string receive=rccS.recv(0, arg().asNum("timeout"));
91 cout<<receive;
92 return 0;
93 }
94 catch(PDNSException& ae)
95 {
96 cerr<<"Fatal: "<<ae.reason<<"\n";
97 return 1;
98 }