]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
log: add option fchown on log file
authorSage Weil <sage@redhat.com>
Mon, 8 Feb 2016 16:28:27 +0000 (11:28 -0500)
committerSage Weil <sage@redhat.com>
Wed, 10 Feb 2016 18:17:27 +0000 (13:17 -0500)
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>
src/log/Log.cc
src/log/Log.h

index 46dbb71466f4619f0cf234ebad27d0bc916a6b6a..4b226a1ac0644754c8b47e7b958eaf4cfab206aa 100644 (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);
index ba2dc41d0572ea48b3e8345e2f306ed07d107c92..d270ae3beb0272fe55e3bb7cda3637a0b1b4cc90 100644 (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();