fixed GH #1114: World-write permissions on files created by daemon. Default umask is now 027; other mask can be set with --umask command-line parameter

Conflicts:
	Util/src/ServerApplication.cpp
This commit is contained in:
Guenter Obiltschnig
2016-02-27 21:17:39 +01:00
parent 861303267e
commit b7da5ae9b9
2 changed files with 30 additions and 6 deletions

View File

@@ -183,6 +183,7 @@ private:
static Poco::Event _terminate; static Poco::Event _terminate;
#elif defined(POCO_OS_FAMILY_UNIX) #elif defined(POCO_OS_FAMILY_UNIX)
void handleDaemon(const std::string& name, const std::string& value); void handleDaemon(const std::string& name, const std::string& value);
void handleUMask(const std::string& name, const std::string& value);
void handlePidFile(const std::string& name, const std::string& value); void handlePidFile(const std::string& name, const std::string& value);
bool isDaemon(int argc, char** argv); bool isDaemon(int argc, char** argv);
void beDaemon(); void beDaemon();

View File

@@ -18,6 +18,7 @@
#include "Poco/Util/Option.h" #include "Poco/Util/Option.h"
#include "Poco/Util/OptionSet.h" #include "Poco/Util/OptionSet.h"
#include "Poco/Util/OptionException.h" #include "Poco/Util/OptionException.h"
#include "Poco/FileStream.h"
#include "Poco/Exception.h" #include "Poco/Exception.h"
#if !defined(POCO_VXWORKS) #if !defined(POCO_VXWORKS)
#include "Poco/Process.h" #include "Poco/Process.h"
@@ -690,7 +691,7 @@ void ServerApplication::beDaemon()
exit(0); exit(0);
setsid(); setsid();
umask(0); umask(027);
// attach stdin, stdout, stderr to /dev/null // attach stdin, stdout, stderr to /dev/null
// instead of just closing them. This avoids // instead of just closing them. This avoids
@@ -718,6 +719,13 @@ void ServerApplication::defineOptions(OptionSet& rOptions)
.repeatable(false) .repeatable(false)
.callback(OptionCallback<ServerApplication>(this, &ServerApplication::handleDaemon))); .callback(OptionCallback<ServerApplication>(this, &ServerApplication::handleDaemon)));
rOptions.addOption(
Option("umask", "", "Set the daemon's umask (octal, e.g. 027).")
.required(false)
.repeatable(false)
.argument("mask")
.callback(OptionCallback<ServerApplication>(this, &ServerApplication::handleUMask)));
rOptions.addOption( rOptions.addOption(
Option("pidfile", "", "Write the process ID of the application to given file.") Option("pidfile", "", "Write the process ID of the application to given file.")
.required(false) .required(false)
@@ -727,20 +735,35 @@ void ServerApplication::defineOptions(OptionSet& rOptions)
} }
void ServerApplication::handleDaemon(const std::string& rName, const std::string& Value) void ServerApplication::handleDaemon(const std::string& rName, const std::string&)
{ {
config().setBool("application.runAsDaemon", true); config().setBool("application.runAsDaemon", true);
} }
void ServerApplication::handlePidFile(const std::string& rName, const std::string& value) void ServerApplication::handleUMask(const std::string& rName, const std::string& rValue)
{ {
std::ofstream ostr(value.c_str()); int mask = 0;
for (std::string::const_iterator it = rValue.begin(); it != rValue.end(); ++it)
{
mask *= 8;
if (*it >= '0' && *it <= '7')
mask += *it - '0';
else
throw Poco::InvalidArgumentException("umask contains non-octal characters", rValue);
}
umask(mask);
}
void ServerApplication::handlePidFile(const std::string& rName, const std::string& rValue)
{
Poco::FileOutputStream ostr(rValue);
if (ostr.good()) if (ostr.good())
ostr << Poco::Process::id() << std::endl; ostr << Poco::Process::id() << std::endl;
else else
throw Poco::CreateFileException("Cannot write PID to file", value); throw Poco::CreateFileException("Cannot write PID to file", rValue);
Poco::TemporaryFile::registerForDeletion(value); Poco::TemporaryFile::registerForDeletion(rValue);
} }