mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-27 11:06:50 +01:00
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:
@@ -173,6 +173,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();
|
||||||
|
|||||||
@@ -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"
|
||||||
@@ -668,7 +669,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
|
||||||
@@ -696,6 +697,13 @@ void ServerApplication::defineOptions(OptionSet& options)
|
|||||||
.repeatable(false)
|
.repeatable(false)
|
||||||
.callback(OptionCallback<ServerApplication>(this, &ServerApplication::handleDaemon)));
|
.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(
|
options.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)
|
||||||
@@ -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)
|
void ServerApplication::handlePidFile(const std::string& name, const std::string& value)
|
||||||
{
|
{
|
||||||
std::ofstream ostr(value.c_str());
|
Poco::FileOutputStream ostr(value);
|
||||||
if (ostr.good())
|
if (ostr.good())
|
||||||
ostr << Poco::Process::id() << std::endl;
|
ostr << Poco::Process::id() << std::endl;
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in New Issue
Block a user