mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-29 17:06:18 +02:00
change user/group of config file
This commit is contained in:
parent
52b64e9f0c
commit
3e0e6f33d8
3 changed files with 44 additions and 26 deletions
|
@ -31,22 +31,40 @@ namespace utils
|
|||
namespace file
|
||||
{
|
||||
|
||||
|
||||
static bool exists(const std::string& filename)
|
||||
{
|
||||
std::ifstream infile(filename.c_str());
|
||||
return infile.good();
|
||||
}
|
||||
|
||||
|
||||
static void do_chown(const std::string& file_path, const std::string& user_name, const std::string& group_name)
|
||||
{
|
||||
uid_t uid;
|
||||
gid_t gid;
|
||||
struct passwd *pwd;
|
||||
struct group *grp;
|
||||
if (user_name.empty() && group_name.empty())
|
||||
return;
|
||||
|
||||
pwd = getpwnam(user_name.c_str());
|
||||
if (!exists(file_path))
|
||||
return;
|
||||
|
||||
uid_t uid = -1;
|
||||
gid_t gid = -1;
|
||||
|
||||
if (!user_name.empty())
|
||||
{
|
||||
struct passwd *pwd = getpwnam(user_name.c_str());
|
||||
if (pwd == NULL)
|
||||
throw std::runtime_error("Failed to get uid");
|
||||
uid = pwd->pw_uid;
|
||||
}
|
||||
|
||||
grp = getgrnam(group_name.c_str());
|
||||
if (!group_name.empty())
|
||||
{
|
||||
struct group *grp = getgrnam(group_name.c_str());
|
||||
if (grp == NULL)
|
||||
throw std::runtime_error("Failed to get gid");
|
||||
gid = grp->gr_gid;
|
||||
}
|
||||
|
||||
if (chown(file_path.c_str(), uid, gid) == -1)
|
||||
throw std::runtime_error("chown failed");
|
||||
|
|
|
@ -64,19 +64,6 @@ void Config::init(const std::string& root_directory, const std::string& user, co
|
|||
filename_ = dir + "server.json";
|
||||
SLOG(NOTICE) << "Settings file: \"" << filename_ << "\"\n";
|
||||
|
||||
if (!user.empty() && !group.empty())
|
||||
{
|
||||
try
|
||||
{
|
||||
utils::file::do_chown(dir, user, group);
|
||||
utils::file::do_chown(filename_, user, group);
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
LOG(ERROR) << "Exception in chown: " << e.what() << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
int fd;
|
||||
if ((fd = open(filename_.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
|
||||
{
|
||||
|
@ -87,6 +74,19 @@ void Config::init(const std::string& root_directory, const std::string& user, co
|
|||
}
|
||||
close(fd);
|
||||
|
||||
if (!user.empty() && !group.empty())
|
||||
{
|
||||
try
|
||||
{
|
||||
utils::file::do_chown(dir, user, group);
|
||||
utils::file::do_chown(filename_, user, group);
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
SLOG(ERROR) << "Exception in chown: " << e.what() << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
ifstream ifs(filename_, std::ifstream::in);
|
||||
|
|
|
@ -162,7 +162,7 @@ int main(int argc, char* argv[])
|
|||
group = user_group[1];
|
||||
}
|
||||
|
||||
Config::instance().init("/var/lib/snapserver");
|
||||
Config::instance().init("/var/lib/snapserver", user, group);
|
||||
daemon.reset(new Daemon(user, group, "/var/run/snapserver/pid"));
|
||||
daemon->daemonize();
|
||||
if (processPriority < -20)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue