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