From: Bert Hubert Date: Sun, 30 Apr 2006 11:34:16 +0000 (+0000) Subject: reinstate windows 'service' support - untested X-Git-Tag: rec-3.1.2~55 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f7c1d4e3511130a9e18934dfcd2071fd7ad77d29;p=pdns reinstate windows 'service' support - untested git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@809 d19b8d6e-7fed-0310-83ef-9ca221ded41b --- diff --git a/pdns/ntservice.hh b/pdns/ntservice.hh index 64fb9f897..1b21eefa9 100644 --- a/pdns/ntservice.hh +++ b/pdns/ntservice.hh @@ -24,9 +24,7 @@ #ifndef NTSERVICE_HH #define NTSERVICE_HH - -#define WINDOWS_LEAN_AND_MEAN -#include +#include "utility.hh" #include #include "singleton.hh" diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index c1d1e09d3..52ab05229 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -17,8 +17,11 @@ */ #ifndef WIN32 -#include -#include +# include +# include +#else + #include "ntservice.hh" + #include "recursorservice.hh" #endif // WIN32 #include "utility.hh" @@ -1269,17 +1272,209 @@ void parseAuthAndForwards() } } +int serviceMain(int argc, char**argv) +{ + L.setName("pdns_recursor"); + + L< ips; + stringtok(ips, ::arg()["allow-from"], ", "); + L<::const_iterator i = ips.begin(); i!= ips.end(); ++i) { + g_allowFrom->addMask(*i); + if(i!=ips.begin()) + L<(100000); + makeControlChannelSocket(); + PacketID pident; + primeHints(); + L<addReadFD(i->first, i->second); + + int newgid=0; + if(!::arg()["setgid"].empty()) + newgid=Utility::makeGidNumeric(::arg()["setgid"]); + int newuid=0; + if(!::arg()["setuid"].empty()) + newuid=Utility::makeUidNumeric(::arg()["setuid"]); + +#ifndef WIN32 + if (!::arg()["chroot"].empty()) { + if (chroot(::arg()["chroot"].c_str())<0) { + L<addReadFD(s_rcc.d_fd, handleRCC); // control channel +#endif + + counter=0; + unsigned int maxTcpClients=::arg().asNum("max-tcp-clients"); + g_tcpTimeout=::arg().asNum("client-tcp-timeout"); + + g_maxTCPPerClient=::arg().asNum("max-tcp-per-client"); + + + bool listenOnTCP(true); + + for(;;) { + while(MT->schedule()); // housekeeping, let threads do their thing + + if(!(counter%500)) { + MT->makeThread(houseKeeping,0); + } + + if(!(counter%11)) { + typedef vector > expired_t; + expired_t expired=g_fdm->getTimeouts(g_now); + + for(expired_t::iterator i=expired.begin() ; i != expired.end(); ++i) { + TCPConnection conn=any_cast(i->second); + if(conn.state != TCPConnection::DONE) { + if(g_logCommonErrors) + L<removeReadFD(i->first); + conn.closeAndCleanup(); + } + } + } + + counter++; + + if(statsWanted) { + doStats(); + } + + Utility::gettimeofday(&g_now, 0); + g_fdm->run(&g_now); + + if(listenOnTCP) { + if(TCPConnection::s_currentConnections > maxTcpClients) { // shutdown + for(g_tcpListenSockets_t::iterator i=g_tcpListenSockets.begin(); i != g_tcpListenSockets.end(); ++i) + g_fdm->removeReadFD(*i); + listenOnTCP=false; + } + } + else { + if(TCPConnection::s_currentConnections <= maxTcpClients) { // reenable + for(g_tcpListenSockets_t::iterator i=g_tcpListenSockets.begin(); i != g_tcpListenSockets.end(); ++i) + g_fdm->addReadFD(*i, handleNewTCPQuestion); + listenOnTCP=true; + } + } + } +} +#ifdef WIN32 +void doWindowsServiceArguments(RecursorService& recursor) +{ + if(::arg().mustDo( "register-service" )) { + if ( !recursor.registerService( "The PowerDNS Recursor.", true )) { + cerr << "Could not register service." << endl; + exit( 99 ); + } + + exit( 0 ); + } + + if ( ::arg().mustDo( "unregister-service" )) { + recursor.unregisterService(); + exit( 0 ); + } +} +#endif + int main(int argc, char **argv) { reportBasicTypes(); int ret = EXIT_SUCCESS; #ifdef WIN32 - WSADATA wsaData; - if(WSAStartup( MAKEWORD( 2, 2 ), &wsaData )) { - cerr<<"Unable to initialize winsock\n"; - exit(1); - } + RecursorService service; + WSADATA wsaData; + if(WSAStartup( MAKEWORD( 2, 2 ), &wsaData )) { + cerr<<"Unable to initialize winsock\n"; + exit(1); + } #endif // WIN32 try { @@ -1298,6 +1493,12 @@ int main(int argc, char **argv) ::arg().set("setuid","If set, change user id to this uid for more security")=""; #ifdef WIN32 ::arg().set("quiet","Suppress logging of questions and answers")="off"; + ::arg().setSwitch( "register-service", "Register the service" )= "no"; + ::arg().setSwitch( "unregister-service", "Unregister the service" )= "no"; + ::arg().setSwitch( "ntservice", "Run as service" )= "no"; + ::arg().setSwitch( "use-ntlog", "Use the NT logging facilities" )= "yes"; + ::arg().setSwitch( "use-logfile", "Use a log file" )= "no"; + ::arg().setSwitch( "logfile", "Filename of the log file" )= "recursor.log"; #else ::arg().set("quiet","Suppress logging of questions and answers")=""; #endif @@ -1306,7 +1507,6 @@ int main(int argc, char **argv) ::arg().set("delegation-only","Which domains we only accept delegations from")=""; ::arg().set("query-local-address","Source IP address for sending queries")="0.0.0.0"; ::arg().set("query-local-address6","Source IPv6 address for sending queries")=""; - // ::arg().set("query-local-port","Source port address for sending queries, defaults to random")=""; ::arg().set("client-tcp-timeout","Timeout in seconds when talking to TCP clients")="2"; ::arg().set("max-tcp-clients","Maximum number of simultaneous TCP clients")="128"; ::arg().set("hint-file", "If set, load root hints from this file")=""; @@ -1351,175 +1551,13 @@ int main(int argc, char **argv) exit(0); } - L.setName("pdns_recursor"); - - L< ips; - stringtok(ips, ::arg()["allow-from"], ", "); - L<::const_iterator i = ips.begin(); i!= ips.end(); ++i) { - g_allowFrom->addMask(*i); - if(i!=ips.begin()) - L<(100000); - makeControlChannelSocket(); - PacketID pident; - primeHints(); - L<start( argc, argv, ::arg().mustDo( "ntservice" )); #endif - g_fdm=getMultiplexer(); - - for(deferredAdd_t::const_iterator i=deferredAdd.begin(); i!=deferredAdd.end(); ++i) - g_fdm->addReadFD(i->first, i->second); - - int newgid=0; - if(!::arg()["setgid"].empty()) - newgid=Utility::makeGidNumeric(::arg()["setgid"]); - int newuid=0; - if(!::arg()["setuid"].empty()) - newuid=Utility::makeUidNumeric(::arg()["setuid"]); - -#ifndef WIN32 - if (!::arg()["chroot"].empty()) { - if (chroot(::arg()["chroot"].c_str())<0) { - L<addReadFD(s_rcc.d_fd, handleRCC); // control channel -#endif - - counter=0; - unsigned int maxTcpClients=::arg().asNum("max-tcp-clients"); - g_tcpTimeout=::arg().asNum("client-tcp-timeout"); - - g_maxTCPPerClient=::arg().asNum("max-tcp-per-client"); - - - bool listenOnTCP(true); - - for(;;) { - while(MT->schedule()); // housekeeping, let threads do their thing - - if(!(counter%500)) { - MT->makeThread(houseKeeping,0); - } - if(!(counter%11)) { - typedef vector > expired_t; - expired_t expired=g_fdm->getTimeouts(g_now); - - for(expired_t::iterator i=expired.begin() ; i != expired.end(); ++i) { - TCPConnection conn=any_cast(i->second); - if(conn.state != TCPConnection::DONE) { - if(g_logCommonErrors) - L<removeReadFD(i->first); - conn.closeAndCleanup(); - } - } - } - - counter++; - - if(statsWanted) { - doStats(); - } - - Utility::gettimeofday(&g_now, 0); - g_fdm->run(&g_now); - - if(listenOnTCP) { - if(TCPConnection::s_currentConnections > maxTcpClients) { // shutdown - for(g_tcpListenSockets_t::iterator i=g_tcpListenSockets.begin(); i != g_tcpListenSockets.end(); ++i) - g_fdm->removeReadFD(*i); - listenOnTCP=false; - } - } - else { - if(TCPConnection::s_currentConnections <= maxTcpClients) { // reenable - for(g_tcpListenSockets_t::iterator i=g_tcpListenSockets.begin(); i != g_tcpListenSockets.end(); ++i) - g_fdm->addReadFD(*i, handleNewTCPQuestion); - listenOnTCP=true; - } - } - } } catch(AhuException &ae) { L< diff --git a/pdns/win32_mtasker.cc b/pdns/win32_mtasker.cc index 326fc16b2..78c62872a 100644 --- a/pdns/win32_mtasker.cc +++ b/pdns/win32_mtasker.cc @@ -248,9 +248,9 @@ templatebool MTasker::schedule() time_t now=time(0); for(typename waiters_t::const_iterator i=d_waiters.begin();i!=d_waiters.end();) { if(i->ttd && i->ttdcontext); -d_waiters.erase(i++); // removes the waitpoint + d_waitstatus=TimeOut; + SwitchToFiber(i->context); + d_waiters.erase(i++); // removes the waitpoint } else ++i; }