summaryrefslogtreecommitdiff
path: root/src/mongo
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo')
-rw-r--r--src/mongo/db/initialize_server_global_state.cpp4
-rw-r--r--src/mongo/db/initialize_server_global_state.h14
-rw-r--r--src/mongo/db/storage/storage_engine_lock_file.cpp11
-rw-r--r--src/mongo/db/storage/storage_engine_lock_file.h6
-rw-r--r--src/mongo/db/storage/storage_engine_lock_file_posix.cpp21
-rw-r--r--src/mongo/db/storage/storage_engine_lock_file_windows.cpp19
6 files changed, 45 insertions, 30 deletions
diff --git a/src/mongo/db/initialize_server_global_state.cpp b/src/mongo/db/initialize_server_global_state.cpp
index 5fa77e32020..7f90cdeeaa8 100644
--- a/src/mongo/db/initialize_server_global_state.cpp
+++ b/src/mongo/db/initialize_server_global_state.cpp
@@ -360,7 +360,7 @@ MONGO_INITIALIZER_WITH_PREREQUISITES(MungeUmask, ("EndStartupOptionHandling"))
}
} // namespace
-bool initializeServerGlobalState(ServiceContext* service) {
+bool initializeServerGlobalState(ServiceContext* service, PidFileWrite pidWrite) {
#ifndef _WIN32
if (!serverGlobalParams.noUnixSocket && !fs::is_directory(serverGlobalParams.socket)) {
cout << serverGlobalParams.socket << " must be a directory" << endl;
@@ -368,7 +368,7 @@ bool initializeServerGlobalState(ServiceContext* service) {
}
#endif
- if (!serverGlobalParams.pidFile.empty()) {
+ if (!serverGlobalParams.pidFile.empty() && pidWrite == PidFileWrite::kWrite) {
if (!writePidFile(serverGlobalParams.pidFile)) {
// error message logged in writePidFile
return false;
diff --git a/src/mongo/db/initialize_server_global_state.h b/src/mongo/db/initialize_server_global_state.h
index e648bc9d42d..d9d6db3e17f 100644
--- a/src/mongo/db/initialize_server_global_state.h
+++ b/src/mongo/db/initialize_server_global_state.h
@@ -34,11 +34,23 @@ namespace mongo {
class ServiceContext;
/**
+ * Enum which controls whether the pid file is written at startup.
+ */
+enum class PidFileWrite {
+ // Open PID file and write PID to disk
+ kWrite,
+
+ // Do not open or write PID file
+ kNoWrite,
+};
+
+/**
* Perform initialization activity common across all mongo server types.
*
* Set up logging, daemonize the process, configure SSL, etc.
*/
-bool initializeServerGlobalState(ServiceContext* service);
+bool initializeServerGlobalState(ServiceContext* service,
+ PidFileWrite pidWrite = PidFileWrite::kWrite);
/**
* Forks and detaches the server, on platforms that support it, if serverGlobalParams.doFork is
diff --git a/src/mongo/db/storage/storage_engine_lock_file.cpp b/src/mongo/db/storage/storage_engine_lock_file.cpp
index 3d55ca0374e..e37e84fa2dd 100644
--- a/src/mongo/db/storage/storage_engine_lock_file.cpp
+++ b/src/mongo/db/storage/storage_engine_lock_file.cpp
@@ -33,6 +33,8 @@
#include "mongo/db/storage/storage_engine_lock_file.h"
+#include "mongo/platform/process_id.h"
+
namespace mongo {
namespace {
@@ -44,4 +46,13 @@ boost::optional<StorageEngineLockFile>& StorageEngineLockFile::get(ServiceContex
return getLockFile(service);
}
+Status StorageEngineLockFile::writePid() {
+ ProcessId pid = ProcessId::getCurrent();
+ std::stringstream ss;
+ ss << pid << std::endl;
+ std::string pidStr = ss.str();
+
+ return writeString(pidStr);
+}
+
} // namespace mongo
diff --git a/src/mongo/db/storage/storage_engine_lock_file.h b/src/mongo/db/storage/storage_engine_lock_file.h
index 14a576aab01..603a83adfaa 100644
--- a/src/mongo/db/storage/storage_engine_lock_file.h
+++ b/src/mongo/db/storage/storage_engine_lock_file.h
@@ -86,6 +86,12 @@ public:
Status writePid();
/**
+ * Writes the string to file.
+ * Fails if lock file has not been opened.
+ */
+ Status writeString(StringData str);
+
+ /**
* Truncates file contents and releases file locks.
*/
void clearPidAndUnlock();
diff --git a/src/mongo/db/storage/storage_engine_lock_file_posix.cpp b/src/mongo/db/storage/storage_engine_lock_file_posix.cpp
index 1d0d5868cf7..5399a0b2f7e 100644
--- a/src/mongo/db/storage/storage_engine_lock_file_posix.cpp
+++ b/src/mongo/db/storage/storage_engine_lock_file_posix.cpp
@@ -42,7 +42,6 @@
#include <sys/types.h>
#include <unistd.h>
-#include "mongo/platform/process_id.h"
#include "mongo/util/log.h"
#include "mongo/util/str.h"
@@ -187,40 +186,34 @@ void StorageEngineLockFile::close() {
_lockFileHandle->clear();
}
-Status StorageEngineLockFile::writePid() {
+Status StorageEngineLockFile::writeString(StringData str) {
if (!_lockFileHandle->isValid()) {
return Status(ErrorCodes::FileNotOpen,
- str::stream() << "Unable to write process ID to " << _filespec
+ str::stream() << "Unable to write string to " << _filespec
<< " because file has not been opened.");
}
if (::ftruncate(_lockFileHandle->_fd, 0)) {
int errorcode = errno;
return Status(ErrorCodes::FileStreamFailed,
- str::stream() << "Unable to write process id to file (ftruncate failed): "
+ str::stream() << "Unable to write string to file (ftruncate failed): "
<< _filespec
<< ' '
<< errnoWithDescription(errorcode));
}
- ProcessId pid = ProcessId::getCurrent();
- std::stringstream ss;
- ss << pid << std::endl;
- std::string pidStr = ss.str();
- int bytesWritten = ::write(_lockFileHandle->_fd, pidStr.c_str(), pidStr.size());
+ int bytesWritten = ::write(_lockFileHandle->_fd, str.rawData(), str.size());
if (bytesWritten < 0) {
int errorcode = errno;
return Status(ErrorCodes::FileStreamFailed,
- str::stream() << "Unable to write process id " << pid.toString()
- << " to file: "
+ str::stream() << "Unable to write string " << str << " to file: "
<< _filespec
<< ' '
<< errnoWithDescription(errorcode));
} else if (bytesWritten == 0) {
return Status(ErrorCodes::FileStreamFailed,
- str::stream() << "Unable to write process id " << pid.toString()
- << " to file: "
+ str::stream() << "Unable to write string " << str << " to file: "
<< _filespec
<< " no data written.");
}
@@ -228,7 +221,7 @@ Status StorageEngineLockFile::writePid() {
if (::fsync(_lockFileHandle->_fd)) {
int errorcode = errno;
return Status(ErrorCodes::FileStreamFailed,
- str::stream() << "Unable to write process id " << pid.toString()
+ str::stream() << "Unable to write process id " << str
<< " to file (fsync failed): "
<< _filespec
<< ' '
diff --git a/src/mongo/db/storage/storage_engine_lock_file_windows.cpp b/src/mongo/db/storage/storage_engine_lock_file_windows.cpp
index 64d79c00ce7..72abd6a68bf 100644
--- a/src/mongo/db/storage/storage_engine_lock_file_windows.cpp
+++ b/src/mongo/db/storage/storage_engine_lock_file_windows.cpp
@@ -38,7 +38,6 @@
#include <ostream>
#include <sstream>
-#include "mongo/platform/process_id.h"
#include "mongo/util/log.h"
#include "mongo/util/str.h"
#include "mongo/util/text.h"
@@ -151,10 +150,10 @@ void StorageEngineLockFile::close() {
_lockFileHandle->clear();
}
-Status StorageEngineLockFile::writePid() {
+Status StorageEngineLockFile::writeString(StringData str) {
if (!_lockFileHandle->isValid()) {
return Status(ErrorCodes::FileNotOpen,
- str::stream() << "Unable to write process ID to " << _filespec
+ str::stream() << "Unable to write string to " << _filespec
<< " because file has not been opened.");
}
@@ -163,27 +162,21 @@ Status StorageEngineLockFile::writePid() {
return status;
}
- ProcessId pid = ProcessId::getCurrent();
- std::stringstream ss;
- ss << pid << std::endl;
- std::string pidStr = ss.str();
DWORD bytesWritten = 0;
if (::WriteFile(_lockFileHandle->_handle,
- static_cast<LPCVOID>(pidStr.c_str()),
- static_cast<DWORD>(pidStr.size()),
+ static_cast<LPCVOID>(str.rawData()),
+ static_cast<DWORD>(str.size()),
&bytesWritten,
NULL) == FALSE) {
int errorcode = GetLastError();
return Status(ErrorCodes::FileStreamFailed,
- str::stream() << "Unable to write process id " << pid.toString()
- << " to file: "
+ str::stream() << "Unable to write string " << str << " to file: "
<< _filespec
<< ' '
<< errnoWithDescription(errorcode));
} else if (bytesWritten == 0) {
return Status(ErrorCodes::FileStreamFailed,
- str::stream() << "Unable to write process id " << pid.toString()
- << " to file: "
+ str::stream() << "Unable to write string " << str << " to file: "
<< _filespec
<< " no data written.");
}