From f9ec80f9bd4e2017ebc063b9e17be005a43652e4 Mon Sep 17 00:00:00 2001
From: Felipe Shi Iu Wu <felipeshiwu@gmail.com>
Date: Fri, 14 Oct 2016 09:50:45 -0300
Subject: [PATCH] Issue #19: /var/log/wtmp creation

Signed-off-by: Felipe Shi Iu Wu <felipeshiwu@gmail.com>
---
 include/agent/linux/get_user_count.h |  5 ++++-
 src/linux/get_user_count.cpp         | 16 ++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/include/agent/linux/get_user_count.h b/include/agent/linux/get_user_count.h
index f80d58e3..05c82402 100644
--- a/include/agent/linux/get_user_count.h
+++ b/include/agent/linux/get_user_count.h
@@ -9,7 +9,9 @@
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <cstring>
+#include <string.h>
+#include <pwd.h>
+#include <grp.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/fcntl.h>
@@ -17,6 +19,7 @@
 #include <unistd.h>
 #include <utmp.h>
 #include <iostream>
+#include <fstream>
 #include <set>
 
 
diff --git a/src/linux/get_user_count.cpp b/src/linux/get_user_count.cpp
index 7ffe91ec..c942ee4a 100644
--- a/src/linux/get_user_count.cpp
+++ b/src/linux/get_user_count.cpp
@@ -473,6 +473,22 @@ static struct user_log * last_modified() {
 }
 
 int get_user_count() {
+
+    /* The agent execution depends on the file '/var/log/wtmp', which is not
+     * present on some distros, such as OpenSUSE docker image. When this happens,
+     * this file is created and it's permissions setted.
+     */
+
+    std::string wtmp_path = "/var/log/wtmp";
+    struct stat buffer;
+    if (stat(wtmp_path.c_str(), &buffer) != 0) { //file not exists
+        ofstream wtmp_file;
+        wtmp_file.open(wtmp_path.c_str());
+        wtmp_file.close();
+        chown(wtmp_path.c_str(), getpwnam("root")->pw_uid, getgrnam("tty")->gr_gid);
+        chmod(wtmp_path.c_str(), 664);
+    }
+
     time_t rawtime;
 
     time(&rawtime);
-- 
GitLab