log: add option fchown on log file

Add explicit call to set the log file uid/gid.  fchown it
immediately, and do the same if it is reopened.

Signed-off-by: Sage Weil <sage@redhat.com>
This commit is contained in:
Sage Weil 2016-02-08 11:28:27 -05:00
parent 63d7c34f5d
commit 32da9626a5
2 changed files with 27 additions and 0 deletions

View File

@ -50,6 +50,8 @@ Log::Log(SubsystemMap *s)
m_flush_mutex_holder(0),
m_new(), m_recent(),
m_fd(-1),
m_uid(0),
m_gid(0),
m_syslog_log(-2), m_syslog_crash(-2),
m_stderr_log(1), m_stderr_crash(-1),
m_graylog_log(-3), m_graylog_crash(-3),
@ -136,6 +138,14 @@ void Log::reopen_log_file()
VOID_TEMP_FAILURE_RETRY(::close(m_fd));
if (m_log_file.length()) {
m_fd = ::open(m_log_file.c_str(), O_CREAT|O_WRONLY|O_APPEND, 0644);
if (m_uid || m_gid) {
int r = ::fchown(m_fd, m_uid, m_gid);
if (r < 0) {
r = -errno;
cerr << "failed to chown " << m_log_file << ": " << cpp_strerror(r)
<< std::endl;
}
}
} else {
m_fd = -1;
}
@ -143,6 +153,20 @@ void Log::reopen_log_file()
pthread_mutex_unlock(&m_flush_mutex);
}
void Log::chown_log_file(uid_t uid, gid_t gid)
{
pthread_mutex_lock(&m_flush_mutex);
if (m_fd >= 0) {
int r = ::fchown(m_fd, uid, gid);
if (r < 0) {
r = -errno;
cerr << "failed to chown " << m_log_file << ": " << cpp_strerror(r)
<< std::endl;
}
}
pthread_mutex_unlock(&m_flush_mutex);
}
void Log::set_syslog_level(int log, int crash)
{
pthread_mutex_lock(&m_flush_mutex);

View File

@ -37,6 +37,8 @@ class Log : private Thread
std::string m_log_file;
int m_fd;
uid_t m_uid;
gid_t m_gid;
int m_syslog_log, m_syslog_crash;
int m_stderr_log, m_stderr_crash;
@ -66,6 +68,7 @@ public:
void set_max_recent(int n);
void set_log_file(std::string fn);
void reopen_log_file();
void chown_log_file(uid_t uid, gid_t gid);
void flush();