diff options
author | Andy Schwerin <schwerin@10gen.com> | 2013-05-23 13:43:28 -0400 |
---|---|---|
committer | Andy Schwerin <schwerin@10gen.com> | 2013-06-03 18:53:52 -0400 |
commit | 9b148b0419d79f7173d0e972e8427fea67f77616 (patch) | |
tree | bc9c96a4a82c4384a7d5220b69417eb1835b922c /src | |
parent | 06c6c3b6fb835e1bcc62e5f725aca6d815cfb42e (diff) | |
download | mongo-9b148b0419d79f7173d0e972e8427fea67f77616.tar.gz |
SERVER-9809 Replace getpid() with ProcessId::getCurrent() and pid_t with ProcessId.
Diffstat (limited to 'src')
-rw-r--r-- | src/SConscript.client | 1 | ||||
-rw-r--r-- | src/mongo/bson/oid.cpp | 13 | ||||
-rw-r--r-- | src/mongo/bson/oid.h | 1 | ||||
-rw-r--r-- | src/mongo/db/cmdline.h | 5 | ||||
-rw-r--r-- | src/mongo/db/commands/server_status.cpp | 3 | ||||
-rw-r--r-- | src/mongo/db/db.cpp | 9 | ||||
-rw-r--r-- | src/mongo/db/initialize_server_global_state.cpp | 9 | ||||
-rw-r--r-- | src/mongo/db/instance.cpp | 3 | ||||
-rw-r--r-- | src/mongo/platform/process_id.cpp | 9 | ||||
-rw-r--r-- | src/mongo/platform/process_id.h | 6 | ||||
-rw-r--r-- | src/mongo/platform/process_id_test.cpp | 2 | ||||
-rw-r--r-- | src/mongo/s/server.cpp | 13 | ||||
-rw-r--r-- | src/mongo/shell/shell_utils_launcher.cpp | 65 | ||||
-rw-r--r-- | src/mongo/shell/shell_utils_launcher.h | 31 | ||||
-rw-r--r-- | src/mongo/util/processinfo.cpp | 2 | ||||
-rw-r--r-- | src/mongo/util/processinfo.h | 16 | ||||
-rw-r--r-- | src/mongo/util/processinfo_darwin.cpp | 6 | ||||
-rw-r--r-- | src/mongo/util/processinfo_freebsd.cpp | 2 | ||||
-rw-r--r-- | src/mongo/util/processinfo_linux2.cpp | 6 | ||||
-rw-r--r-- | src/mongo/util/processinfo_none.cpp | 2 | ||||
-rw-r--r-- | src/mongo/util/processinfo_test.cpp | 2 | ||||
-rw-r--r-- | src/mongo/util/processinfo_win32.cpp | 6 |
22 files changed, 107 insertions, 105 deletions
diff --git a/src/SConscript.client b/src/SConscript.client index f278eddb56d..434213701db 100644 --- a/src/SConscript.client +++ b/src/SConscript.client @@ -47,6 +47,7 @@ clientSourceBasic = [ 'mongo/db/namespace.cpp', 'mongo/db/dbmessage.cpp', 'mongo/pch.cpp', + 'mongo/platform/process_id.cpp', 'mongo/platform/random.cpp', 'mongo/util/assert_util.cpp', 'mongo/util/background.cpp', diff --git a/src/mongo/bson/oid.cpp b/src/mongo/bson/oid.cpp index e96b076762a..8847e498159 100644 --- a/src/mongo/bson/oid.cpp +++ b/src/mongo/bson/oid.cpp @@ -20,6 +20,7 @@ #include <boost/functional/hash.hpp> #include "mongo/platform/atomic_word.h" +#include "mongo/platform/process_id.h" #include "mongo/platform/random.h" #include "mongo/bson/bsonobjbuilder.h" #include "mongo/bson/oid.h" @@ -46,17 +47,9 @@ namespace mongo { return s; } - unsigned OID::ourPid() { -#ifdef _WIN32 - return static_cast<unsigned>( GetCurrentProcessId() ); -#else - return static_cast<unsigned>( getpid() ); -#endif - } - void OID::foldInPid(OID::MachineAndPid& x) { - unsigned p = ourPid(); - x._pid ^= (unsigned short) p; + unsigned p = ProcessId::getCurrent().asUInt32(); + x._pid ^= static_cast<unsigned short>(p); // when the pid is greater than 16 bits, let the high bits modulate the machine id field. unsigned short& rest = (unsigned short &) x._machineNumber[1]; rest ^= p >> 16; diff --git a/src/mongo/bson/oid.h b/src/mongo/bson/oid.h index 8b5f3ba18c8..2c9ee5c1f18 100644 --- a/src/mongo/bson/oid.h +++ b/src/mongo/bson/oid.h @@ -137,7 +137,6 @@ namespace mongo { unsigned char data[kOIDSize]; }; - static unsigned ourPid(); static void foldInPid(MachineAndPid& x); static MachineAndPid genMachineAndPid(); }; diff --git a/src/mongo/db/cmdline.h b/src/mongo/db/cmdline.h index 22bd63f9eb4..e2796e84588 100644 --- a/src/mongo/db/cmdline.h +++ b/src/mongo/db/cmdline.h @@ -20,6 +20,7 @@ #include <vector> #include "mongo/db/jsobj.h" +#include "mongo/platform/process_id.h" #include "mongo/util/net/listen.h" namespace boost { @@ -127,8 +128,8 @@ namespace mongo { bool logWithSyslog; // True if logging to syslog; must not be set if logpath is set. #ifndef _WIN32 - pid_t parentProc; // --fork pid of initial process - pid_t leaderProc; // --fork pid of leader process + ProcessId parentProc; // --fork pid of initial process + ProcessId leaderProc; // --fork pid of leader process #endif #ifdef MONGO_SSL diff --git a/src/mongo/db/commands/server_status.cpp b/src/mongo/db/commands/server_status.cpp index c067b285540..689dac9aac2 100644 --- a/src/mongo/db/commands/server_status.cpp +++ b/src/mongo/db/commands/server_status.cpp @@ -28,6 +28,7 @@ #include "mongo/db/commands.h" #include "mongo/db/commands/server_status.h" #include "mongo/db/stats/counters.h" +#include "mongo/platform/process_id.h" #include "mongo/util/net/listen.h" #include "mongo/util/processinfo.h" #include "mongo/util/ramlog.h" @@ -91,7 +92,7 @@ namespace mongo { result.append("host", prettyHostName() ); result.append("version", versionString); result.append("process",cmdLine.binaryName); - result.append("pid", (int)getpid()); + result.append("pid", ProcessId::getCurrent().asLongLong()); result.append("uptime",(double) (time(0)-cmdLine.started)); result.append("uptimeMillis", (long long)(curTimeMillis64()-_started)); result.append("uptimeEstimate",(double) (start/1000)); diff --git a/src/mongo/db/db.cpp b/src/mongo/db/db.cpp index 36a579b9f4f..00a023b9e16 100644 --- a/src/mongo/db/db.cpp +++ b/src/mongo/db/db.cpp @@ -55,6 +55,7 @@ #include "mongo/db/stats/counters.h" #include "mongo/db/stats/snapshots.h" #include "mongo/db/ttl.h" +#include "mongo/platform/process_id.h" #include "mongo/s/d_writeback.h" #include "mongo/scripting/engine.h" #include "mongo/util/background.h" @@ -257,7 +258,7 @@ namespace mongo { toLog.append( "startTimeLocal", buf ); toLog.append( "cmdLine", CmdLine::getParsedOpts() ); - toLog.append( "pid", getpid() ); + toLog.append( "pid", ProcessId::getCurrent().asLongLong() ); BSONObjBuilder buildinfo( toLog.subobjStart("buildinfo")); @@ -581,11 +582,7 @@ namespace mongo { bool is32bit = sizeof(int*) == 4; { -#if !defined(_WIN32) - pid_t pid = getpid(); -#else - DWORD pid=GetCurrentProcessId(); -#endif + ProcessId pid = ProcessId::getCurrent(); Nullstream& l = log(); l << "MongoDB starting : pid=" << pid << " port=" << cmdLine.port << " dbpath=" << dbpath; if( replSettings.master ) l << " master=" << replSettings.master; diff --git a/src/mongo/db/initialize_server_global_state.cpp b/src/mongo/db/initialize_server_global_state.cpp index 432ac05fb8a..99a5242287c 100644 --- a/src/mongo/db/initialize_server_global_state.cpp +++ b/src/mongo/db/initialize_server_global_state.cpp @@ -28,6 +28,7 @@ #include "mongo/db/auth/authorization_manager.h" #include "mongo/db/auth/security_key.h" #include "mongo/db/cmdline.h" +#include "mongo/platform/process_id.h" #include "mongo/util/log.h" #include "mongo/util/net/listen.h" #include "mongo/util/processinfo.h" @@ -40,7 +41,7 @@ namespace mongo { // support for exit value propagation with fork void launchSignal( int sig ) { if ( sig == SIGUSR2 ) { - pid_t cur = getpid(); + ProcessId cur = ProcessId::getCurrent(); if ( cur == cmdLine.parentProc || cur == cmdLine.leaderProc ) { // signal indicates successful start allowing us to exit @@ -56,7 +57,7 @@ namespace mongo { void CmdLine::launchOk() { if ( cmdLine.doFork ) { // killing leader will propagate to parent - verify( kill( cmdLine.leaderProc, SIGUSR2 ) == 0 ); + verify( kill( cmdLine.leaderProc.toNative(), SIGUSR2 ) == 0 ); } } #endif @@ -77,7 +78,7 @@ namespace mongo { cout.flush(); cerr.flush(); - cmdLine.parentProc = getpid(); + cmdLine.parentProc = ProcessId::getCurrent(); // facilitate clean exit when child starts successfully setupLaunchSignals(); @@ -116,7 +117,7 @@ namespace mongo { } setsid(); - cmdLine.leaderProc = getpid(); + cmdLine.leaderProc = ProcessId::getCurrent(); pid_t child2 = fork(); if (child2 == -1) { diff --git a/src/mongo/db/instance.cpp b/src/mongo/db/instance.cpp index 1127ba52145..324a4e59589 100644 --- a/src/mongo/db/instance.cpp +++ b/src/mongo/db/instance.cpp @@ -55,6 +55,7 @@ #include "mongo/db/repl/is_master.h" #include "mongo/db/repl/oplog.h" #include "mongo/db/stats/counters.h" +#include "mongo/platform/process_id.h" #include "mongo/s/d_logic.h" #include "mongo/s/stale_exception.h" // for SendStaleConfigException #include "mongo/util/fail_point_service.h" @@ -1133,7 +1134,7 @@ namespace mongo { #if !defined(__sunos__) void writePid(int fd) { stringstream ss; - ss << getpid() << endl; + ss << ProcessId::getCurrent() << endl; string s = ss.str(); const char * data = s.c_str(); #ifdef _WIN32 diff --git a/src/mongo/platform/process_id.cpp b/src/mongo/platform/process_id.cpp index ef5a6ecc80b..424933ada0d 100644 --- a/src/mongo/platform/process_id.cpp +++ b/src/mongo/platform/process_id.cpp @@ -19,6 +19,7 @@ #include <boost/static_assert.hpp> #include <iostream> +#include <sstream> #include <limits> namespace mongo { @@ -49,8 +50,14 @@ namespace mongo { return static_cast<long long>(asInt64()); } + std::string ProcessId::toString() const { + std::ostringstream os; + os << *this; + return os.str(); + } + std::ostream& operator<<(std::ostream& os, ProcessId pid) { - return os << pid.asInt64(); + return os << pid.toNative(); } } // namespace mongo diff --git a/src/mongo/platform/process_id.h b/src/mongo/platform/process_id.h index 208b921a1c0..1f067307667 100644 --- a/src/mongo/platform/process_id.h +++ b/src/mongo/platform/process_id.h @@ -16,6 +16,7 @@ #pragma once #include <iosfwd> +#include <string> #ifndef _WIN32 #include <unistd.h> @@ -80,6 +81,11 @@ namespace mongo { */ uint32_t asUInt32() const { return static_cast<uint32_t>(_npid); } + /** + * Provides a string representation of the pid. + */ + std::string toString() const; + bool operator==(const ProcessId other) const { return _npid == other._npid; } bool operator!=(const ProcessId other) const { return _npid != other._npid; } bool operator<(const ProcessId other) const { return _npid < other._npid; } diff --git a/src/mongo/platform/process_id_test.cpp b/src/mongo/platform/process_id_test.cpp index 83053e3d886..60d2844c16e 100644 --- a/src/mongo/platform/process_id_test.cpp +++ b/src/mongo/platform/process_id_test.cpp @@ -13,6 +13,8 @@ * limitations under the License. */ +#include "mongo/platform/basic.h" + #include "mongo/platform/process_id.h" #include "mongo/unittest/unittest.h" diff --git a/src/mongo/s/server.cpp b/src/mongo/s/server.cpp index 2addd112407..59a3ef666b8 100644 --- a/src/mongo/s/server.cpp +++ b/src/mongo/s/server.cpp @@ -30,6 +30,7 @@ #include "mongo/db/dbwebserver.h" #include "mongo/db/initialize_server_global_state.h" #include "mongo/db/lasterror.h" +#include "mongo/platform/process_id.h" #include "mongo/s/balance.h" #include "mongo/s/chunk.h" #include "mongo/s/client_info.h" @@ -247,15 +248,19 @@ namespace mongo { void printShardingVersionInfo( bool out ) { if ( out ) { - cout << "MongoS version " << versionString << " starting: pid=" << getpid() << " port=" << cmdLine.port << - ( sizeof(int*) == 4 ? " 32" : " 64" ) << "-bit host=" << getHostNameCached() << " (--help for usage)" << endl; + cout << "MongoS version " << versionString << " starting: pid=" << + ProcessId::getCurrent() << " port=" << cmdLine.port << + ( sizeof(int*) == 4 ? " 32" : " 64" ) << "-bit host=" << getHostNameCached() << + " (--help for usage)" << endl; DEV cout << "_DEBUG build" << endl; cout << "git version: " << gitVersion() << endl; cout << "build sys info: " << sysInfo() << endl; } else { - log() << "MongoS version " << versionString << " starting: pid=" << getpid() << " port=" << cmdLine.port << - ( sizeof( int* ) == 4 ? " 32" : " 64" ) << "-bit host=" << getHostNameCached() << " (--help for usage)" << endl; + log() << "MongoS version " << versionString << " starting: pid=" << + ProcessId::getCurrent() << " port=" << cmdLine.port << + ( sizeof( int* ) == 4 ? " 32" : " 64" ) << "-bit host=" << getHostNameCached() << + " (--help for usage)" << endl; DEV log() << "_DEBUG build" << endl; printGitVersion(); printSysInfo(); diff --git a/src/mongo/shell/shell_utils_launcher.cpp b/src/mongo/shell/shell_utils_launcher.cpp index 625efa83e1c..4059007f593 100644 --- a/src/mongo/shell/shell_utils_launcher.cpp +++ b/src/mongo/shell/shell_utils_launcher.cpp @@ -66,15 +66,15 @@ namespace mongo { return _ports.count( port ) == 1; } - pid_t ProgramRegistry::pidForPort( int port ) const { + ProcessId ProgramRegistry::pidForPort( int port ) const { boost::recursive_mutex::scoped_lock lk( _mutex ); verify( isPortRegistered( port ) ); return _ports.find( port )->second.first; } - int ProgramRegistry::portForPid(pid_t pid) const { + int ProgramRegistry::portForPid(ProcessId pid) const { boost::recursive_mutex::scoped_lock lk(_mutex); - for (map<int, pair<pid_t, int> >::const_iterator it = _ports.begin(); + for (map<int, pair<ProcessId, int> >::const_iterator it = _ports.begin(); it != _ports.end(); ++it) { if (it->second.first == pid) return it->first; @@ -83,7 +83,7 @@ namespace mongo { return -1; } - void ProgramRegistry::registerPort( int port, pid_t pid, int output ) { + void ProgramRegistry::registerPort( int port, ProcessId pid, int output ) { boost::recursive_mutex::scoped_lock lk( _mutex ); verify( !isPortRegistered( port ) ); _ports.insert( make_pair( port, make_pair( pid, output ) ) ); @@ -100,24 +100,24 @@ namespace mongo { void ProgramRegistry::getRegisteredPorts( vector<int> &ports ) { boost::recursive_mutex::scoped_lock lk( _mutex ); - for( map<int,pair<pid_t,int> >::const_iterator i = _ports.begin(); i != _ports.end(); + for( map<int,pair<ProcessId,int> >::const_iterator i = _ports.begin(); i != _ports.end(); ++i ) { ports.push_back( i->first ); } } - bool ProgramRegistry::isPidRegistered( pid_t pid ) const { + bool ProgramRegistry::isPidRegistered( ProcessId pid ) const { boost::recursive_mutex::scoped_lock lk( _mutex ); return _pids.count( pid ) == 1; } - void ProgramRegistry::registerPid( pid_t pid, int output ) { + void ProgramRegistry::registerPid( ProcessId pid, int output ) { boost::recursive_mutex::scoped_lock lk( _mutex ); verify( !isPidRegistered( pid ) ); _pids.insert( make_pair( pid, output ) ); } - void ProgramRegistry::deletePid(pid_t pid) { + void ProgramRegistry::deletePid(ProcessId pid) { boost::recursive_mutex::scoped_lock lk(_mutex); if (!isPidRegistered(pid)) { int port = portForPid(pid); @@ -129,9 +129,9 @@ namespace mongo { _pids.erase(pid); } - void ProgramRegistry::getRegisteredPids( vector<pid_t> &pids ) { + void ProgramRegistry::getRegisteredPids( vector<ProcessId> &pids ) { boost::recursive_mutex::scoped_lock lk( _mutex ); - for( map<pid_t,int>::const_iterator i = _pids.begin(); i != _pids.end(); ++i ) { + for( map<ProcessId,int>::const_iterator i = _pids.begin(); i != _pids.end(); ++i ) { pids.push_back( i->first ); } } @@ -143,7 +143,7 @@ namespace mongo { mongo::dbexitCalled = true; } - void ProgramOutputMultiplexer::appendLine( int port, int pid, const char *line ) { + void ProgramOutputMultiplexer::appendLine( int port, ProcessId pid, const char *line ) { mongo::mutex::scoped_lock lk( mongoProgramOutputMutex ); if( mongo::dbexitCalled ) throw "program is terminating"; stringstream buf; @@ -399,7 +399,7 @@ namespace mongo { CloseHandle(pi.hThread); - _pid = pi.dwProcessId; + _pid = ProcessId::fromNative(pi.dwProcessId); registry._handles.insert( make_pair( _pid, pi.hProcess ) ); #else @@ -417,14 +417,13 @@ namespace mongo { env[1] = NULL; bool isMongos = ( _argv[0].find( "mongos" ) != string::npos ); - - _pid = fork(); + + pid_t nativePid = fork(); + _pid = ProcessId::fromNative(nativePid); // Async signal unsafe functions should not be called in the child process. - if ( _pid == -1 ) { - verify( _pid != -1 ); - } - else if ( _pid == 0 ) { + verify( nativePid != -1 ); + if ( nativePid == 0 ) { // DON'T ASSERT IN THIS BLOCK - very bad things will happen if ( dup2( child_stdout, STDOUT_FILENO ) == -1 || @@ -460,7 +459,7 @@ namespace mongo { } //returns true if process exited - bool wait_for_pid(pid_t pid, bool block=true, int* exit_code=NULL) { + bool wait_for_pid(ProcessId pid, bool block=true, int* exit_code=NULL) { #ifdef _WIN32 verify(registry._handles.count(pid)); HANDLE h = registry._handles[pid]; @@ -484,7 +483,7 @@ namespace mongo { } #else int tmp; - bool ret = (pid == waitpid(pid, &tmp, (block ? 0 : WNOHANG))); + bool ret = (pid.toNative() == waitpid(pid.toNative(), &tmp, (block ? 0 : WNOHANG))); if (exit_code) *exit_code = WEXITSTATUS(tmp); return ret; @@ -502,14 +501,14 @@ namespace mongo { } BSONObj CheckProgram(const BSONObj& args, void* data) { - int pid = singleArg(args).numberInt(); + ProcessId pid = ProcessId::fromNative(singleArg(args).numberInt()); bool isDead = wait_for_pid(pid, false); if (isDead) registry.deletePid(pid); return BSON( string( "" ) << (!isDead) ); } BSONObj WaitProgram( const BSONObj& a, void* data ) { - int pid = singleArg( a ).numberInt(); + ProcessId pid = ProcessId::fromNative(singleArg( a ).numberInt()); BSONObj x = BSON( "" << wait_for_pid( pid ) ); registry.deletePid( pid ); return x; @@ -520,7 +519,7 @@ namespace mongo { ProgramRunner r( a ); r.start(); boost::thread t( r ); - return BSON( string( "" ) << int( r.pid() ) ); + return BSON( string( "" ) << r.pid().asLongLong() ); } BSONObj RunMongoProgram( const BSONObj &a, void* data ) { @@ -600,7 +599,7 @@ namespace mongo { return undefinedReturn; } - inline void kill_wrapper( pid_t pid, int sig, int port, const BSONObj& opt ) { + inline void kill_wrapper( ProcessId pid, int sig, int port, const BSONObj& opt ) { #ifdef _WIN32 if (sig == SIGKILL || port == 0) { verify( registry._handles.count(pid) ); @@ -635,7 +634,7 @@ namespace mongo { } } #else - int x = kill( pid, sig ); + int x = kill( pid.toNative(), sig ); if ( x ) { if ( errno == ESRCH ) { } @@ -648,8 +647,8 @@ namespace mongo { #endif } - int killDb( int port, pid_t _pid, int signal, const BSONObj& opt ) { - pid_t pid; + int killDb( int port, ProcessId _pid, int signal, const BSONObj& opt ) { + ProcessId pid; int exitCode = 0; if ( port > 0 ) { if( !registry.isPortRegistered( port ) ) { @@ -700,7 +699,7 @@ namespace mongo { return exitCode; } - int killDb( int port, pid_t _pid, int signal ) { + int killDb( int port, ProcessId _pid, int signal ) { BSONObj dummyOpt; return killDb( port, _pid, signal, dummyOpt ); } @@ -738,7 +737,7 @@ namespace mongo { verify( nFields >= 1 && nFields <= 3 ); uassert( 15853 , "stopMongo needs a number" , a.firstElement().isNumber() ); int port = int( a.firstElement().number() ); - int code = killDb( port, 0, getSignal( a ), getStopMongodOpts( a )); + int code = killDb( port, ProcessId::fromNative(0), getSignal( a ), getStopMongodOpts( a )); log() << "shell: stopped mongo program on port " << port << endl; return BSON( "" << (double)code ); } @@ -746,7 +745,7 @@ namespace mongo { BSONObj StopMongoProgramByPid( const BSONObj &a, void* data ) { verify( a.nFields() == 1 || a.nFields() == 2 ); uassert( 15852 , "stopMongoByPid needs a number" , a.firstElement().isNumber() ); - int pid = int( a.firstElement().number() ); + ProcessId pid = ProcessId::fromNative(int( a.firstElement().number() )); int code = killDb( 0, pid, getSignal( a ) ); log() << "shell: stopped mongo program on pid " << pid << endl; return BSON( "" << (double)code ); @@ -756,10 +755,10 @@ namespace mongo { vector< int > ports; registry.getRegisteredPorts( ports ); for( vector< int >::iterator i = ports.begin(); i != ports.end(); ++i ) - killDb( *i, 0, SIGTERM ); - vector< pid_t > pids; + killDb( *i, ProcessId::fromNative(0), SIGTERM ); + vector< ProcessId > pids; registry.getRegisteredPids( pids ); - for( vector< pid_t >::iterator i = pids.begin(); i != pids.end(); ++i ) + for( vector< ProcessId >::iterator i = pids.begin(); i != pids.end(); ++i ) killDb( 0, *i, SIGTERM ); } diff --git a/src/mongo/shell/shell_utils_launcher.h b/src/mongo/shell/shell_utils_launcher.h index 37b531a815f..04fd48fa35a 100644 --- a/src/mongo/shell/shell_utils_launcher.h +++ b/src/mongo/shell/shell_utils_launcher.h @@ -27,10 +27,7 @@ #include <utility> #include "mongo/bson/bsonobj.h" - -#ifdef _WIN32 -typedef int pid_t; -#endif +#include "mongo/platform/process_id.h" namespace mongo { @@ -52,7 +49,7 @@ namespace mongo { /** Record log lines from concurrent programs. All public members are thread safe. */ class ProgramOutputMultiplexer { public: - void appendLine( int port, int pid, const char *line ); + void appendLine( int port, ProcessId pid, const char *line ); /** @return up to 100000 characters of the most recent log output. */ std::string str() const; void clear(); @@ -71,28 +68,28 @@ namespace mongo { bool isPortRegistered( int port ) const; /** @return pid for a registered port. */ - pid_t pidForPort( int port ) const; + ProcessId pidForPort( int port ) const; /** @return port (-1 if doesn't exist) for a registered pid. */ - int portForPid( pid_t pid ) const; + int portForPid( ProcessId pid ) const; /** Register an unregistered port. */ - void registerPort( int port, pid_t pid, int output ); + void registerPort( int port, ProcessId pid, int output ); void deletePort( int port ); void getRegisteredPorts( std::vector<int> &ports ); - bool isPidRegistered( pid_t pid ) const; + bool isPidRegistered( ProcessId pid ) const; /** Register an unregistered pid. */ - void registerPid( pid_t pid, int output ); - void deletePid( pid_t pid ); - void getRegisteredPids( vector<pid_t> &pids ); + void registerPid( ProcessId pid, int output ); + void deletePid( ProcessId pid ); + void getRegisteredPids( vector<ProcessId> &pids ); private: - std::map<int,std::pair<pid_t,int> > _ports; - std::map<pid_t,int> _pids; + std::map<int,std::pair<ProcessId,int> > _ports; + std::map<ProcessId,int> _pids; mutable boost::recursive_mutex _mutex; #ifdef _WIN32 public: - std::map<pid_t,HANDLE> _handles; + std::map<ProcessId,HANDLE> _handles; #endif }; @@ -105,7 +102,7 @@ namespace mongo { void start(); /** Continuously read the program's output, generally from a special purpose thread. */ void operator()(); - pid_t pid() const { return _pid; } + ProcessId pid() const { return _pid; } int port() const { return _port; } private: @@ -115,7 +112,7 @@ namespace mongo { std::vector<std::string> _argv; int _port; int _pipe; - pid_t _pid; + ProcessId _pid; }; } } diff --git a/src/mongo/util/processinfo.cpp b/src/mongo/util/processinfo.cpp index 3b8d5369cf3..21057de9863 100644 --- a/src/mongo/util/processinfo.cpp +++ b/src/mongo/util/processinfo.cpp @@ -37,7 +37,7 @@ namespace mongo { void write( const string& p ) { path = p; ofstream out( path.c_str() , ios_base::out ); - out << getpid() << endl; + out << ProcessId::getCurrent() << endl; out.close(); } diff --git a/src/mongo/util/processinfo.h b/src/mongo/util/processinfo.h index 8e9044d39e4..1de16692c70 100644 --- a/src/mongo/util/processinfo.h +++ b/src/mongo/util/processinfo.h @@ -17,23 +17,17 @@ #pragma once -#include <sys/types.h> #include <string> -#ifndef _WIN32 -#include <unistd.h> -#else -typedef int pid_t; -int getpid(); -#endif - -#include <db/jsobj.h> +#include "mongo/platform/cstdint.h" +#include "mongo/platform/process_id.h" +#include "mongo/db/jsobj.h" namespace mongo { class ProcessInfo { public: - ProcessInfo( pid_t pid = getpid() ); + ProcessInfo( ProcessId pid = ProcessId::getCurrent() ); ~ProcessInfo(); /** @@ -159,7 +153,7 @@ namespace mongo { void collectSystemInfo(); }; - pid_t _pid; + ProcessId _pid; static mongo::mutex _sysInfoLock; static bool checkNumaEnabled(); diff --git a/src/mongo/util/processinfo_darwin.cpp b/src/mongo/util/processinfo_darwin.cpp index 3f5658658e8..e2b99ce5a8f 100644 --- a/src/mongo/util/processinfo_darwin.cpp +++ b/src/mongo/util/processinfo_darwin.cpp @@ -38,7 +38,7 @@ using namespace std; namespace mongo { - ProcessInfo::ProcessInfo( pid_t pid ) : _pid( pid ) { + ProcessInfo::ProcessInfo( ProcessId pid ) : _pid( pid ) { } ProcessInfo::~ProcessInfo() { @@ -53,7 +53,7 @@ namespace mongo { mach_port_t task; - if ( ( result = task_for_pid( mach_task_self() , _pid , &task) ) != KERN_SUCCESS ) { + if ((result = task_for_pid(mach_task_self(), _pid.toNative(), &task)) != KERN_SUCCESS) { cout << "error getting task\n"; return 0; } @@ -76,7 +76,7 @@ namespace mongo { mach_port_t task; - if ( ( result = task_for_pid( mach_task_self() , _pid , &task) ) != KERN_SUCCESS ) { + if ((result = task_for_pid(mach_task_self(), _pid.toNative(), &task)) != KERN_SUCCESS) { cout << "error getting task\n"; return 0; } diff --git a/src/mongo/util/processinfo_freebsd.cpp b/src/mongo/util/processinfo_freebsd.cpp index ddfea94078b..c5018722c17 100644 --- a/src/mongo/util/processinfo_freebsd.cpp +++ b/src/mongo/util/processinfo_freebsd.cpp @@ -32,7 +32,7 @@ namespace mongo { - ProcessInfo::ProcessInfo(pid_t pid) : _pid( pid ) { + ProcessInfo::ProcessInfo(ProcessId pid) : _pid( pid ) { } ProcessInfo::~ProcessInfo() { diff --git a/src/mongo/util/processinfo_linux2.cpp b/src/mongo/util/processinfo_linux2.cpp index b2b5cf9fde8..6544fb7af83 100644 --- a/src/mongo/util/processinfo_linux2.cpp +++ b/src/mongo/util/processinfo_linux2.cpp @@ -36,9 +36,9 @@ namespace mongo { class LinuxProc { public: - LinuxProc( pid_t pid = getpid() ) { + LinuxProc( ProcessId pid ) { char name[128]; - sprintf( name , "/proc/%d/stat" , pid ); + sprintf( name , "/proc/%d/stat" , pid.asUInt32() ); FILE * f = fopen( name , "r"); if ( ! f ) { @@ -353,7 +353,7 @@ namespace mongo { }; - ProcessInfo::ProcessInfo( pid_t pid ) : _pid( pid ) { + ProcessInfo::ProcessInfo( ProcessId pid ) : _pid( pid ) { } ProcessInfo::~ProcessInfo() { diff --git a/src/mongo/util/processinfo_none.cpp b/src/mongo/util/processinfo_none.cpp index 089edc24eed..77d223f7e03 100644 --- a/src/mongo/util/processinfo_none.cpp +++ b/src/mongo/util/processinfo_none.cpp @@ -23,7 +23,7 @@ using namespace std; namespace mongo { - ProcessInfo::ProcessInfo( pid_t pid ) { + ProcessInfo::ProcessInfo( ProcessId pid ) { } ProcessInfo::~ProcessInfo() { diff --git a/src/mongo/util/processinfo_test.cpp b/src/mongo/util/processinfo_test.cpp index 263e7cc0026..5d63c0c7518 100644 --- a/src/mongo/util/processinfo_test.cpp +++ b/src/mongo/util/processinfo_test.cpp @@ -14,6 +14,8 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include "mongo/platform/basic.h" + #include <vector> #include "mongo/util/processinfo.h" diff --git a/src/mongo/util/processinfo_win32.cpp b/src/mongo/util/processinfo_win32.cpp index 3f42eb12698..54a731d5114 100644 --- a/src/mongo/util/processinfo_win32.cpp +++ b/src/mongo/util/processinfo_win32.cpp @@ -22,10 +22,6 @@ using namespace std; -int getpid() { - return GetCurrentProcessId(); -} - namespace mongo { // dynamically link to psapi.dll (in case this version of Windows @@ -57,7 +53,7 @@ namespace mongo { return (int)( s / ( 1024 * 1024 ) ); } - ProcessInfo::ProcessInfo( pid_t pid ) { + ProcessInfo::ProcessInfo( ProcessId pid ) { } ProcessInfo::~ProcessInfo() { |