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

This commit is contained in:
Guenter Obiltschnig 2016-02-27 21:17:39 +01:00
parent a53e80f761
commit 8b4709155d
2 changed files with 26 additions and 2 deletions

View File

@ -173,6 +173,7 @@ private:
static Poco::Event _terminate;
#elif defined(POCO_OS_FAMILY_UNIX)
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);
bool isDaemon(int argc, char** argv);
void beDaemon();

View File

@ -18,6 +18,7 @@
#include "Poco/Util/Option.h"
#include "Poco/Util/OptionSet.h"
#include "Poco/Util/OptionException.h"
#include "Poco/FileStream.h"
#include "Poco/Exception.h"
#if !defined(POCO_VXWORKS)
#include "Poco/Process.h"
@ -668,7 +669,7 @@ void ServerApplication::beDaemon()
exit(0);
setsid();
umask(0);
umask(027);
// attach stdin, stdout, stderr to /dev/null
// instead of just closing them. This avoids
@ -696,6 +697,13 @@ void ServerApplication::defineOptions(OptionSet& options)
.repeatable(false)
.callback(OptionCallback<ServerApplication>(this, &ServerApplication::handleDaemon)));
options.addOption(
Option("umask", "", "Set the daemon's umask (octal, e.g. 027).")
.required(false)
.repeatable(false)
.argument("mask")
.callback(OptionCallback<ServerApplication>(this, &ServerApplication::handleUMask)));
options.addOption(
Option("pidfile", "", "Write the process ID of the application to given file.")
.required(false)
@ -711,9 +719,24 @@ void ServerApplication::handleDaemon(const std::string& name, const std::string&
}
void ServerApplication::handleUMask(const std::string& name, const std::string& value)
{
int mask = 0;
for (std::string::const_iterator it = value.begin(); it != value.end(); ++it)
{
mask *= 8;
if (*it >= '0' && *it <= '7')
mask += *it - '0';
else
throw Poco::InvalidArgumentException("umask contains non-octal characters", value);
}
umask(mask);
}
void ServerApplication::handlePidFile(const std::string& name, const std::string& value)
{
std::ofstream ostr(value.c_str());
Poco::FileOutputStream ostr(value);
if (ostr.good())
ostr << Poco::Process::id() << std::endl;
else