diff options
Diffstat (limited to 'db/cmdline.cpp')
-rw-r--r-- | db/cmdline.cpp | 92 |
1 files changed, 82 insertions, 10 deletions
diff --git a/db/cmdline.cpp b/db/cmdline.cpp index 2d15279..d79cb87 100644 --- a/db/cmdline.cpp +++ b/db/cmdline.cpp @@ -16,9 +16,10 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include "stdafx.h" +#include "pch.h" #include "cmdline.h" #include "commands.h" +#include "../util/processinfo.h" namespace po = boost::program_options; @@ -41,8 +42,10 @@ namespace mongo { ("verbose,v", "be more verbose (include multiple times for more verbosity e.g. -vvvvv)") ("quiet", "quieter output") ("port", po::value<int>(&cmdLine.port), "specify port number") + ("bind_ip", po::value<string>(&cmdLine.bind_ip), "comma separated list of ip addresses to listen on - all local ips by default") ("logpath", po::value<string>() , "file to send all output to instead of stdout" ) ("logappend" , "append to logpath instead of over-writing" ) + ("pidfilepath", po::value<string>(), "directory for pidfile (if not set, no pidfile is created)") #ifndef _WIN32 ("fork" , "fork server process" ) #endif @@ -114,25 +117,83 @@ namespace mongo { cmdLine.quiet = true; } + string logpath; + #ifndef _WIN32 if (params.count("fork")) { if ( ! params.count( "logpath" ) ){ cout << "--fork has to be used with --logpath" << endl; ::exit(-1); } + + { // test logpath + logpath = params["logpath"].as<string>(); + assert( logpath.size() ); + if ( logpath[0] != '/' ){ + char temp[256]; + assert( getcwd( temp , 256 ) ); + logpath = (string)temp + "/" + logpath; + } + FILE * test = fopen( logpath.c_str() , "a" ); + if ( ! test ){ + cout << "can't open [" << logpath << "] for log file: " << errnoWithDescription() << endl; + ::exit(-1); + } + fclose( test ); + } + + cout.flush(); + cerr.flush(); + pid_t c = fork(); if ( c ){ - cout << "forked process: " << c << endl; - ::exit(0); + _exit(0); + } + + if ( chdir("/") < 0 ){ + cout << "Cant chdir() while forking server process: " << strerror(errno) << endl; + ::exit(-1); } setsid(); + + pid_t c2 = fork(); + if ( c2 ){ + cout << "forked process: " << c2 << endl; + _exit(0); + } + + // stdout handled in initLogging + //fclose(stdout); + //freopen("/dev/null", "w", stdout); + + fclose(stderr); + fclose(stdin); + + FILE* f = freopen("/dev/null", "w", stderr); + if ( f == NULL ){ + cout << "Cant reassign stderr while forking server process: " << strerror(errno) << endl; + ::exit(-1); + } + + f = freopen("/dev/null", "r", stdin); + if ( f == NULL ){ + cout << "Cant reassign stdin while forking server process: " << strerror(errno) << endl; + ::exit(-1); + } + + setupCoreSignals(); setupSignals(); } #endif if (params.count("logpath")) { - string lp = params["logpath"].as<string>(); - uassert( 10033 , "logpath has to be non-zero" , lp.size() ); - initLogging( lp , params.count( "logappend" ) ); + if ( logpath.size() == 0 ) + logpath = params["logpath"].as<string>(); + uassert( 10033 , "logpath has to be non-zero" , logpath.size() ); + initLogging( logpath , params.count( "logappend" ) ); + } + + if ( params.count("pidfilepath")) { + writePidFile( params["pidfilepath"].as<string>() ); } { @@ -144,15 +205,26 @@ namespace mongo { return true; } + + void ignoreSignal( int signal ){ + } + + void setupCoreSignals(){ +#if !defined(_WIN32) + assert( signal(SIGUSR1 , rotateLogs ) != SIG_ERR ); + assert( signal(SIGHUP , ignoreSignal ) != SIG_ERR ); +#endif + } class CmdGetCmdLineOpts : Command{ public: CmdGetCmdLineOpts(): Command("getCmdLineOpts") {} - virtual LockType locktype() { return NONE; } - virtual bool adminOnly() { return true; } - virtual bool slaveOk() { return true; } + void help(stringstream& h) const { h << "get argv"; } + virtual LockType locktype() const { return NONE; } + virtual bool adminOnly() const { return true; } + virtual bool slaveOk() const { return true; } - virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl){ + virtual bool run(const string&, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl){ result.append("argv", argvArray); return true; } |