]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/rec_control.cc
rec: Ensure control socket can be created when running in systemd
[thirdparty/pdns.git] / pdns / rec_control.cc
CommitLineData
aaacf7f2 1/*
6edbf68a
PL
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
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
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
870a0fe4
AT
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
1d5b3ce6
BH
25#include "rec_channel.hh"
26#include <iostream>
5c409fa2 27#include "pdnsexception.hh"
aaacf7f2 28#include "arguments.hh"
1d5b3ce6 29
10f4eea8 30#include "namespaces.hh"
1d5b3ce6 31
aaacf7f2
BH
32ArgvMap &arg()
33{
34 static ArgvMap arg;
35 return arg;
36}
37
38static void initArguments(int argc, char** argv)
39{
08b7c2e2 40 arg().set("config-dir","Location of configuration directory (recursor.conf)")=SYSCONFDIR;
41f7a068 41
0524add9 42 arg().set("socket-dir",string("Where the controlsocket will live, ")+LOCALSTATEDIR+"/pdns-recursor when unset and not chrooted" )="";
f0f3f0b0 43 arg().set("chroot","switch to chroot jail")="";
677e2a46 44 arg().set("process","When controlling multiple recursors, the target process number")="";
61203240 45 arg().set("timeout", "Number of seconds to wait for the recursor to respond")="5";
5124de27 46 arg().set("config-name","Name of this virtual configuration - will rename the binary image")="";
aaacf7f2 47 arg().setCmd("help","Provide this helpful message");
2494e120 48 arg().setCmd("version","Show the version of this program");
aaacf7f2
BH
49
50 arg().laxParse(argc,argv);
b710dd81 51 if(arg().mustDo("help") || arg().getCommands().empty()) {
ff5ba4f9
WA
52 cout<<"syntax: rec_control [options] command, options as below: "<<endl<<endl;
53 cout<<arg().helpstring(arg()["help"])<<endl;
b710dd81 54 cout<<"In addition, 'rec_control help' can be used to retrieve a list\nof available commands from PowerDNS"<<endl;
55 exit(arg().mustDo("help") ? 0 : 99);
aaacf7f2 56 }
5124de27 57
2494e120
PL
58 if(arg().mustDo("version")) {
59 cout<<"rec_control version "<<VERSION<<endl;
60 exit(0);
61 }
62
832ad64e 63 string configname=::arg()["config-dir"]+"/recursor.conf";
5124de27
RA
64 if (::arg()["config-name"] != "")
65 configname=::arg()["config-dir"]+"/recursor-"+::arg()["config-name"]+".conf";
66
832ad64e
PD
67 cleanSlashes(configname);
68
14cd9774 69 arg().laxFile(configname.c_str());
f0f3f0b0 70
effe43d0 71 arg().laxParse(argc,argv); // make sure the commandline wins
f0f3f0b0
PL
72
73 if (::arg()["socket-dir"].empty()) {
74 if (::arg()["chroot"].empty())
0524add9 75 ::arg().set("socket-dir") = std::string(LOCALSTATEDIR) + "/pdns-recursor";
f0f3f0b0
PL
76 else
77 ::arg().set("socket-dir") = ::arg()["chroot"] + "/";
78 } else if (!::arg()["chroot"].empty()) {
79 ::arg().set("socket-dir") = ::arg()["chroot"] + "/" + ::arg()["socket-dir"];
80 }
aaacf7f2
BH
81}
82
f6f016d1 83int main(int argc, char** argv)
1d5b3ce6 84try
f6f016d1 85{
aaacf7f2 86 initArguments(argc, argv);
1d5b3ce6 87 RecursorControlChannel rccS;
19e570d2 88 string sockname="pdns_recursor";
5124de27
RA
89
90 if (arg()["config-name"] != "")
91 sockname+="-"+arg()["config-name"];
92
677e2a46
BH
93 if(!arg()["process"].empty())
94 sockname+="."+arg()["process"];
95
96 sockname.append(".controlsocket");
41f7a068
BH
97
98 rccS.connect(arg()["socket-dir"], sockname);
1d5b3ce6 99
aaacf7f2 100 const vector<string>&commands=arg().getCommands();
1d5b3ce6 101 string command;
a2bfc3ff 102 for(unsigned int i=0; i< commands.size(); ++i) {
aaacf7f2 103 if(i>0)
1d5b3ce6 104 command+=" ";
aaacf7f2 105 command+=commands[i];
1d5b3ce6 106 }
11d641f2 107 rccS.send(command, nullptr, arg().asNum("timeout"));
61203240 108 string receive=rccS.recv(0, arg().asNum("timeout"));
be10c6b7
RK
109 if(receive.compare(0, 7, "Unknown") == 0) {
110 cerr<<receive<<endl;
111 return 1;
112 }
1d5b3ce6 113 cout<<receive;
dd9e83ed 114 return 0;
1d5b3ce6 115}
3f81d239 116catch(PDNSException& ae)
1d5b3ce6
BH
117{
118 cerr<<"Fatal: "<<ae.reason<<"\n";
dd9e83ed 119 return 1;
f6f016d1 120}